[ARM] Add TLS relocations for FDPIC.
[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];
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         strncpy (data + 44, va_arg (ap, const char *), 80);
2178         va_end (ap);
2179
2180         return elfcore_write_note (abfd, buf, bufsiz,
2181                                    "CORE", note_type, data, sizeof (data));
2182       }
2183
2184     case NT_PRSTATUS:
2185       {
2186         char data[148];
2187         va_list ap;
2188         long pid;
2189         int cursig;
2190         const void *greg;
2191
2192         va_start (ap, note_type);
2193         memset (data, 0, sizeof (data));
2194         pid = va_arg (ap, long);
2195         bfd_put_32 (abfd, pid, data + 24);
2196         cursig = va_arg (ap, int);
2197         bfd_put_16 (abfd, cursig, data + 12);
2198         greg = va_arg (ap, const void *);
2199         memcpy (data + 72, greg, 72);
2200         va_end (ap);
2201
2202         return elfcore_write_note (abfd, buf, bufsiz,
2203                                    "CORE", note_type, data, sizeof (data));
2204       }
2205     }
2206 }
2207
2208 #define TARGET_LITTLE_SYM               arm_elf32_le_vec
2209 #define TARGET_LITTLE_NAME              "elf32-littlearm"
2210 #define TARGET_BIG_SYM                  arm_elf32_be_vec
2211 #define TARGET_BIG_NAME                 "elf32-bigarm"
2212
2213 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
2214 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
2215 #define elf_backend_write_core_note     elf32_arm_nabi_write_core_note
2216
2217 typedef unsigned long int insn32;
2218 typedef unsigned short int insn16;
2219
2220 /* In lieu of proper flags, assume all EABIv4 or later objects are
2221    interworkable.  */
2222 #define INTERWORK_FLAG(abfd)  \
2223   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
2224   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
2225   || ((abfd)->flags & BFD_LINKER_CREATED))
2226
2227 /* The linker script knows the section names for placement.
2228    The entry_names are used to do simple name mangling on the stubs.
2229    Given a function name, and its type, the stub can be found. The
2230    name can be changed. The only requirement is the %s be present.  */
2231 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
2232 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
2233
2234 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
2235 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
2236
2237 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
2238 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
2239
2240 #define STM32L4XX_ERRATUM_VENEER_SECTION_NAME ".text.stm32l4xx_veneer"
2241 #define STM32L4XX_ERRATUM_VENEER_ENTRY_NAME   "__stm32l4xx_veneer_%x"
2242
2243 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
2244 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
2245
2246 #define STUB_ENTRY_NAME   "__%s_veneer"
2247
2248 #define CMSE_PREFIX "__acle_se_"
2249
2250 /* The name of the dynamic interpreter.  This is put in the .interp
2251    section.  */
2252 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
2253
2254 static const unsigned long tls_trampoline [] =
2255 {
2256   0xe08e0000,           /* add r0, lr, r0 */
2257   0xe5901004,           /* ldr r1, [r0,#4] */
2258   0xe12fff11,           /* bx  r1 */
2259 };
2260
2261 static const unsigned long dl_tlsdesc_lazy_trampoline [] =
2262 {
2263   0xe52d2004, /*        push    {r2}                    */
2264   0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8]     */
2265   0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8]     */
2266   0xe79f2002, /* 1:   ldr     r2, [pc, r2]              */
2267   0xe081100f, /* 2:   add     r1, pc                    */
2268   0xe12fff12, /*      bx      r2                        */
2269   0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
2270                                 + dl_tlsdesc_lazy_resolver(GOT)   */
2271   0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
2272 };
2273
2274 /* ARM FDPIC PLT entry.  */
2275 /* The last 5 words contain PLT lazy fragment code and data.  */
2276 static const bfd_vma elf32_arm_fdpic_plt_entry [] =
2277   {
2278     0xe59fc008,    /* ldr     r12, .L1 */
2279     0xe08cc009,    /* add     r12, r12, r9 */
2280     0xe59c9004,    /* ldr     r9, [r12, #4] */
2281     0xe59cf000,    /* ldr     pc, [r12] */
2282     0x00000000,    /* L1.     .word   foo(GOTOFFFUNCDESC) */
2283     0x00000000,    /* L1.     .word   foo(funcdesc_value_reloc_offset) */
2284     0xe51fc00c,    /* ldr     r12, [pc, #-12] */
2285     0xe92d1000,    /* push    {r12} */
2286     0xe599c004,    /* ldr     r12, [r9, #4] */
2287     0xe599f000,    /* ldr     pc, [r9] */
2288   };
2289
2290 #ifdef FOUR_WORD_PLT
2291
2292 /* The first entry in a procedure linkage table looks like
2293    this.  It is set up so that any shared library function that is
2294    called before the relocation has been set up calls the dynamic
2295    linker first.  */
2296 static const bfd_vma elf32_arm_plt0_entry [] =
2297 {
2298   0xe52de004,           /* str   lr, [sp, #-4]! */
2299   0xe59fe010,           /* ldr   lr, [pc, #16]  */
2300   0xe08fe00e,           /* add   lr, pc, lr     */
2301   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2302 };
2303
2304 /* Subsequent entries in a procedure linkage table look like
2305    this.  */
2306 static const bfd_vma elf32_arm_plt_entry [] =
2307 {
2308   0xe28fc600,           /* add   ip, pc, #NN    */
2309   0xe28cca00,           /* add   ip, ip, #NN    */
2310   0xe5bcf000,           /* ldr   pc, [ip, #NN]! */
2311   0x00000000,           /* unused               */
2312 };
2313
2314 #else /* not FOUR_WORD_PLT */
2315
2316 /* The first entry in a procedure linkage table looks like
2317    this.  It is set up so that any shared library function that is
2318    called before the relocation has been set up calls the dynamic
2319    linker first.  */
2320 static const bfd_vma elf32_arm_plt0_entry [] =
2321 {
2322   0xe52de004,           /* str   lr, [sp, #-4]! */
2323   0xe59fe004,           /* ldr   lr, [pc, #4]   */
2324   0xe08fe00e,           /* add   lr, pc, lr     */
2325   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2326   0x00000000,           /* &GOT[0] - .          */
2327 };
2328
2329 /* By default subsequent entries in a procedure linkage table look like
2330    this. Offsets that don't fit into 28 bits will cause link error.  */
2331 static const bfd_vma elf32_arm_plt_entry_short [] =
2332 {
2333   0xe28fc600,           /* add   ip, pc, #0xNN00000 */
2334   0xe28cca00,           /* add   ip, ip, #0xNN000   */
2335   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!  */
2336 };
2337
2338 /* When explicitly asked, we'll use this "long" entry format
2339    which can cope with arbitrary displacements.  */
2340 static const bfd_vma elf32_arm_plt_entry_long [] =
2341 {
2342   0xe28fc200,           /* add   ip, pc, #0xN0000000 */
2343   0xe28cc600,           /* add   ip, ip, #0xNN00000  */
2344   0xe28cca00,           /* add   ip, ip, #0xNN000    */
2345   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!   */
2346 };
2347
2348 static bfd_boolean elf32_arm_use_long_plt_entry = FALSE;
2349
2350 #endif /* not FOUR_WORD_PLT */
2351
2352 /* The first entry in a procedure linkage table looks like this.
2353    It is set up so that any shared library function that is called before the
2354    relocation has been set up calls the dynamic linker first.  */
2355 static const bfd_vma elf32_thumb2_plt0_entry [] =
2356 {
2357   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2358      an instruction maybe encoded to one or two array elements.  */
2359   0xf8dfb500,           /* push    {lr}          */
2360   0x44fee008,           /* ldr.w   lr, [pc, #8]  */
2361                         /* add     lr, pc        */
2362   0xff08f85e,           /* ldr.w   pc, [lr, #8]! */
2363   0x00000000,           /* &GOT[0] - .           */
2364 };
2365
2366 /* Subsequent entries in a procedure linkage table for thumb only target
2367    look like this.  */
2368 static const bfd_vma elf32_thumb2_plt_entry [] =
2369 {
2370   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2371      an instruction maybe encoded to one or two array elements.  */
2372   0x0c00f240,           /* movw    ip, #0xNNNN    */
2373   0x0c00f2c0,           /* movt    ip, #0xNNNN    */
2374   0xf8dc44fc,           /* add     ip, pc         */
2375   0xbf00f000            /* ldr.w   pc, [ip]       */
2376                         /* nop                    */
2377 };
2378
2379 /* The format of the first entry in the procedure linkage table
2380    for a VxWorks executable.  */
2381 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
2382 {
2383   0xe52dc008,           /* str    ip,[sp,#-8]!                  */
2384   0xe59fc000,           /* ldr    ip,[pc]                       */
2385   0xe59cf008,           /* ldr    pc,[ip,#8]                    */
2386   0x00000000,           /* .long  _GLOBAL_OFFSET_TABLE_         */
2387 };
2388
2389 /* The format of subsequent entries in a VxWorks executable.  */
2390 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
2391 {
2392   0xe59fc000,         /* ldr    ip,[pc]                 */
2393   0xe59cf000,         /* ldr    pc,[ip]                 */
2394   0x00000000,         /* .long  @got                            */
2395   0xe59fc000,         /* ldr    ip,[pc]                 */
2396   0xea000000,         /* b      _PLT                            */
2397   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2398 };
2399
2400 /* The format of entries in a VxWorks shared library.  */
2401 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
2402 {
2403   0xe59fc000,         /* ldr    ip,[pc]                 */
2404   0xe79cf009,         /* ldr    pc,[ip,r9]                      */
2405   0x00000000,         /* .long  @got                            */
2406   0xe59fc000,         /* ldr    ip,[pc]                 */
2407   0xe599f008,         /* ldr    pc,[r9,#8]                      */
2408   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2409 };
2410
2411 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
2412 #define PLT_THUMB_STUB_SIZE 4
2413 static const bfd_vma elf32_arm_plt_thumb_stub [] =
2414 {
2415   0x4778,               /* bx pc */
2416   0x46c0                /* nop   */
2417 };
2418
2419 /* The entries in a PLT when using a DLL-based target with multiple
2420    address spaces.  */
2421 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2422 {
2423   0xe51ff004,         /* ldr   pc, [pc, #-4] */
2424   0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2425 };
2426
2427 /* The first entry in a procedure linkage table looks like
2428    this.  It is set up so that any shared library function that is
2429    called before the relocation has been set up calls the dynamic
2430    linker first.  */
2431 static const bfd_vma elf32_arm_nacl_plt0_entry [] =
2432 {
2433   /* First bundle: */
2434   0xe300c000,           /* movw ip, #:lower16:&GOT[2]-.+8       */
2435   0xe340c000,           /* movt ip, #:upper16:&GOT[2]-.+8       */
2436   0xe08cc00f,           /* add  ip, ip, pc                      */
2437   0xe52dc008,           /* str  ip, [sp, #-8]!                  */
2438   /* Second bundle: */
2439   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2440   0xe59cc000,           /* ldr  ip, [ip]                        */
2441   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2442   0xe12fff1c,           /* bx   ip                              */
2443   /* Third bundle: */
2444   0xe320f000,           /* nop                                  */
2445   0xe320f000,           /* nop                                  */
2446   0xe320f000,           /* nop                                  */
2447   /* .Lplt_tail: */
2448   0xe50dc004,           /* str  ip, [sp, #-4]                   */
2449   /* Fourth bundle: */
2450   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2451   0xe59cc000,           /* ldr  ip, [ip]                        */
2452   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2453   0xe12fff1c,           /* bx   ip                              */
2454 };
2455 #define ARM_NACL_PLT_TAIL_OFFSET        (11 * 4)
2456
2457 /* Subsequent entries in a procedure linkage table look like this.  */
2458 static const bfd_vma elf32_arm_nacl_plt_entry [] =
2459 {
2460   0xe300c000,           /* movw ip, #:lower16:&GOT[n]-.+8       */
2461   0xe340c000,           /* movt ip, #:upper16:&GOT[n]-.+8       */
2462   0xe08cc00f,           /* add  ip, ip, pc                      */
2463   0xea000000,           /* b    .Lplt_tail                      */
2464 };
2465
2466 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2467 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2468 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2469 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2470 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2471 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2472 #define THM2_MAX_FWD_COND_BRANCH_OFFSET (((1 << 20) -2) + 4)
2473 #define THM2_MAX_BWD_COND_BRANCH_OFFSET (-(1 << 20) + 4)
2474
2475 enum stub_insn_type
2476 {
2477   THUMB16_TYPE = 1,
2478   THUMB32_TYPE,
2479   ARM_TYPE,
2480   DATA_TYPE
2481 };
2482
2483 #define THUMB16_INSN(X)         {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2484 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2485    is inserted in arm_build_one_stub().  */
2486 #define THUMB16_BCOND_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2487 #define THUMB32_INSN(X)         {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2488 #define THUMB32_MOVT(X)         {(X), THUMB32_TYPE, R_ARM_THM_MOVT_ABS, 0}
2489 #define THUMB32_MOVW(X)         {(X), THUMB32_TYPE, R_ARM_THM_MOVW_ABS_NC, 0}
2490 #define THUMB32_B_INSN(X, Z)    {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2491 #define ARM_INSN(X)             {(X), ARM_TYPE, R_ARM_NONE, 0}
2492 #define ARM_REL_INSN(X, Z)      {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2493 #define DATA_WORD(X,Y,Z)        {(X), DATA_TYPE, (Y), (Z)}
2494
2495 typedef struct
2496 {
2497   bfd_vma              data;
2498   enum stub_insn_type  type;
2499   unsigned int         r_type;
2500   int                  reloc_addend;
2501 }  insn_sequence;
2502
2503 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2504    to reach the stub if necessary.  */
2505 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2506 {
2507   ARM_INSN (0xe51ff004),            /* ldr   pc, [pc, #-4] */
2508   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2509 };
2510
2511 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2512    available.  */
2513 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2514 {
2515   ARM_INSN (0xe59fc000),            /* ldr   ip, [pc, #0] */
2516   ARM_INSN (0xe12fff1c),            /* bx    ip */
2517   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2518 };
2519
2520 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2521 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2522 {
2523   THUMB16_INSN (0xb401),             /* push {r0} */
2524   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2525   THUMB16_INSN (0x4684),             /* mov  ip, r0 */
2526   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2527   THUMB16_INSN (0x4760),             /* bx   ip */
2528   THUMB16_INSN (0xbf00),             /* nop */
2529   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2530 };
2531
2532 /* Thumb -> Thumb long branch stub in thumb2 encoding.  Used on armv7.  */
2533 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only[] =
2534 {
2535   THUMB32_INSN (0xf85ff000),         /* ldr.w  pc, [pc, #-0] */
2536   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(x) */
2537 };
2538
2539 /* Thumb -> Thumb long branch stub. Used for PureCode sections on Thumb2
2540    M-profile architectures.  */
2541 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only_pure[] =
2542 {
2543   THUMB32_MOVW (0xf2400c00),         /* mov.w ip, R_ARM_MOVW_ABS_NC */
2544   THUMB32_MOVT (0xf2c00c00),         /* movt  ip, R_ARM_MOVT_ABS << 16 */
2545   THUMB16_INSN (0x4760),             /* bx   ip */
2546 };
2547
2548 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
2549    allowed.  */
2550 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2551 {
2552   THUMB16_INSN (0x4778),             /* bx   pc */
2553   THUMB16_INSN (0x46c0),             /* nop */
2554   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2555   ARM_INSN (0xe12fff1c),             /* bx   ip */
2556   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2557 };
2558
2559 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2560    available.  */
2561 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2562 {
2563   THUMB16_INSN (0x4778),             /* bx   pc */
2564   THUMB16_INSN (0x46c0),             /* nop   */
2565   ARM_INSN (0xe51ff004),             /* ldr   pc, [pc, #-4] */
2566   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2567 };
2568
2569 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2570    one, when the destination is close enough.  */
2571 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2572 {
2573   THUMB16_INSN (0x4778),             /* bx   pc */
2574   THUMB16_INSN (0x46c0),             /* nop   */
2575   ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
2576 };
2577
2578 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2579    blx to reach the stub if necessary.  */
2580 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2581 {
2582   ARM_INSN (0xe59fc000),             /* ldr   ip, [pc] */
2583   ARM_INSN (0xe08ff00c),             /* add   pc, pc, ip */
2584   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2585 };
2586
2587 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2588    blx to reach the stub if necessary.  We can not add into pc;
2589    it is not guaranteed to mode switch (different in ARMv6 and
2590    ARMv7).  */
2591 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2592 {
2593   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2594   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2595   ARM_INSN (0xe12fff1c),             /* bx    ip */
2596   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2597 };
2598
2599 /* V4T ARM -> ARM long branch stub, PIC.  */
2600 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2601 {
2602   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2603   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2604   ARM_INSN (0xe12fff1c),             /* bx    ip */
2605   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2606 };
2607
2608 /* V4T Thumb -> ARM long branch stub, PIC.  */
2609 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2610 {
2611   THUMB16_INSN (0x4778),             /* bx   pc */
2612   THUMB16_INSN (0x46c0),             /* nop  */
2613   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2614   ARM_INSN (0xe08cf00f),             /* add  pc, ip, pc */
2615   DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2616 };
2617
2618 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2619    architectures.  */
2620 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2621 {
2622   THUMB16_INSN (0xb401),             /* push {r0} */
2623   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2624   THUMB16_INSN (0x46fc),             /* mov  ip, pc */
2625   THUMB16_INSN (0x4484),             /* add  ip, r0 */
2626   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2627   THUMB16_INSN (0x4760),             /* bx   ip */
2628   DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2629 };
2630
2631 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2632    allowed.  */
2633 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2634 {
2635   THUMB16_INSN (0x4778),             /* bx   pc */
2636   THUMB16_INSN (0x46c0),             /* nop */
2637   ARM_INSN (0xe59fc004),             /* ldr  ip, [pc, #4] */
2638   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2639   ARM_INSN (0xe12fff1c),             /* bx   ip */
2640   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2641 };
2642
2643 /* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
2644    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2645 static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
2646 {
2647   ARM_INSN (0xe59f1000),             /* ldr   r1, [pc] */
2648   ARM_INSN (0xe08ff001),             /* add   pc, pc, r1 */
2649   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2650 };
2651
2652 /* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
2653    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2654 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
2655 {
2656   THUMB16_INSN (0x4778),             /* bx   pc */
2657   THUMB16_INSN (0x46c0),             /* nop */
2658   ARM_INSN (0xe59f1000),             /* ldr  r1, [pc, #0] */
2659   ARM_INSN (0xe081f00f),             /* add  pc, r1, pc */
2660   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
2661 };
2662
2663 /* NaCl ARM -> ARM long branch stub.  */
2664 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl[] =
2665 {
2666   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2667   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2668   ARM_INSN (0xe12fff1c),                /* bx   ip */
2669   ARM_INSN (0xe320f000),                /* nop */
2670   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2671   DATA_WORD (0, R_ARM_ABS32, 0),        /* dcd  R_ARM_ABS32(X) */
2672   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2673   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2674 };
2675
2676 /* NaCl ARM -> ARM long branch stub, PIC.  */
2677 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl_pic[] =
2678 {
2679   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2680   ARM_INSN (0xe08cc00f),                /* add  ip, ip, pc */
2681   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2682   ARM_INSN (0xe12fff1c),                /* bx   ip */
2683   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2684   DATA_WORD (0, R_ARM_REL32, 8),        /* dcd  R_ARM_REL32(X+8) */
2685   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2686   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2687 };
2688
2689 /* Stub used for transition to secure state (aka SG veneer).  */
2690 static const insn_sequence elf32_arm_stub_cmse_branch_thumb_only[] =
2691 {
2692   THUMB32_INSN (0xe97fe97f),            /* sg.  */
2693   THUMB32_B_INSN (0xf000b800, -4),      /* b.w original_branch_dest.  */
2694 };
2695
2696
2697 /* Cortex-A8 erratum-workaround stubs.  */
2698
2699 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2700    can't use a conditional branch to reach this stub).  */
2701
2702 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2703 {
2704   THUMB16_BCOND_INSN (0xd001),         /* b<cond>.n true.  */
2705   THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2706   THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2707 };
2708
2709 /* Stub used for b.w and bl.w instructions.  */
2710
2711 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2712 {
2713   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2714 };
2715
2716 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2717 {
2718   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2719 };
2720
2721 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2722    instruction (which switches to ARM mode) to point to this stub.  Jump to the
2723    real destination using an ARM-mode branch.  */
2724
2725 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2726 {
2727   ARM_REL_INSN (0xea000000, -8) /* b original_branch_dest.  */
2728 };
2729
2730 /* For each section group there can be a specially created linker section
2731    to hold the stubs for that group.  The name of the stub section is based
2732    upon the name of another section within that group with the suffix below
2733    applied.
2734
2735    PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
2736    create what appeared to be a linker stub section when it actually
2737    contained user code/data.  For example, consider this fragment:
2738
2739      const char * stubborn_problems[] = { "np" };
2740
2741    If this is compiled with "-fPIC -fdata-sections" then gcc produces a
2742    section called:
2743
2744      .data.rel.local.stubborn_problems
2745
2746    This then causes problems in arm32_arm_build_stubs() as it triggers:
2747
2748       // Ignore non-stub sections.
2749       if (!strstr (stub_sec->name, STUB_SUFFIX))
2750         continue;
2751
2752    And so the section would be ignored instead of being processed.  Hence
2753    the change in definition of STUB_SUFFIX to a name that cannot be a valid
2754    C identifier.  */
2755 #define STUB_SUFFIX ".__stub"
2756
2757 /* One entry per long/short branch stub defined above.  */
2758 #define DEF_STUBS \
2759   DEF_STUB(long_branch_any_any) \
2760   DEF_STUB(long_branch_v4t_arm_thumb) \
2761   DEF_STUB(long_branch_thumb_only) \
2762   DEF_STUB(long_branch_v4t_thumb_thumb) \
2763   DEF_STUB(long_branch_v4t_thumb_arm) \
2764   DEF_STUB(short_branch_v4t_thumb_arm) \
2765   DEF_STUB(long_branch_any_arm_pic) \
2766   DEF_STUB(long_branch_any_thumb_pic) \
2767   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
2768   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
2769   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
2770   DEF_STUB(long_branch_thumb_only_pic) \
2771   DEF_STUB(long_branch_any_tls_pic) \
2772   DEF_STUB(long_branch_v4t_thumb_tls_pic) \
2773   DEF_STUB(long_branch_arm_nacl) \
2774   DEF_STUB(long_branch_arm_nacl_pic) \
2775   DEF_STUB(cmse_branch_thumb_only) \
2776   DEF_STUB(a8_veneer_b_cond) \
2777   DEF_STUB(a8_veneer_b) \
2778   DEF_STUB(a8_veneer_bl) \
2779   DEF_STUB(a8_veneer_blx) \
2780   DEF_STUB(long_branch_thumb2_only) \
2781   DEF_STUB(long_branch_thumb2_only_pure)
2782
2783 #define DEF_STUB(x) arm_stub_##x,
2784 enum elf32_arm_stub_type
2785 {
2786   arm_stub_none,
2787   DEF_STUBS
2788   max_stub_type
2789 };
2790 #undef DEF_STUB
2791
2792 /* Note the first a8_veneer type.  */
2793 const unsigned arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond;
2794
2795 typedef struct
2796 {
2797   const insn_sequence* template_sequence;
2798   int template_size;
2799 } stub_def;
2800
2801 #define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
2802 static const stub_def stub_definitions[] =
2803 {
2804   {NULL, 0},
2805   DEF_STUBS
2806 };
2807
2808 struct elf32_arm_stub_hash_entry
2809 {
2810   /* Base hash table entry structure.  */
2811   struct bfd_hash_entry root;
2812
2813   /* The stub section.  */
2814   asection *stub_sec;
2815
2816   /* Offset within stub_sec of the beginning of this stub.  */
2817   bfd_vma stub_offset;
2818
2819   /* Given the symbol's value and its section we can determine its final
2820      value when building the stubs (so the stub knows where to jump).  */
2821   bfd_vma target_value;
2822   asection *target_section;
2823
2824   /* Same as above but for the source of the branch to the stub.  Used for
2825      Cortex-A8 erratum workaround to patch it to branch to the stub.  As
2826      such, source section does not need to be recorded since Cortex-A8 erratum
2827      workaround stubs are only generated when both source and target are in the
2828      same section.  */
2829   bfd_vma source_value;
2830
2831   /* The instruction which caused this stub to be generated (only valid for
2832      Cortex-A8 erratum workaround stubs at present).  */
2833   unsigned long orig_insn;
2834
2835   /* The stub type.  */
2836   enum elf32_arm_stub_type stub_type;
2837   /* Its encoding size in bytes.  */
2838   int stub_size;
2839   /* Its template.  */
2840   const insn_sequence *stub_template;
2841   /* The size of the template (number of entries).  */
2842   int stub_template_size;
2843
2844   /* The symbol table entry, if any, that this was derived from.  */
2845   struct elf32_arm_link_hash_entry *h;
2846
2847   /* Type of branch.  */
2848   enum arm_st_branch_type branch_type;
2849
2850   /* Where this stub is being called from, or, in the case of combined
2851      stub sections, the first input section in the group.  */
2852   asection *id_sec;
2853
2854   /* The name for the local symbol at the start of this stub.  The
2855      stub name in the hash table has to be unique; this does not, so
2856      it can be friendlier.  */
2857   char *output_name;
2858 };
2859
2860 /* Used to build a map of a section.  This is required for mixed-endian
2861    code/data.  */
2862
2863 typedef struct elf32_elf_section_map
2864 {
2865   bfd_vma vma;
2866   char type;
2867 }
2868 elf32_arm_section_map;
2869
2870 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2871
2872 typedef enum
2873 {
2874   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2875   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2876   VFP11_ERRATUM_ARM_VENEER,
2877   VFP11_ERRATUM_THUMB_VENEER
2878 }
2879 elf32_vfp11_erratum_type;
2880
2881 typedef struct elf32_vfp11_erratum_list
2882 {
2883   struct elf32_vfp11_erratum_list *next;
2884   bfd_vma vma;
2885   union
2886   {
2887     struct
2888     {
2889       struct elf32_vfp11_erratum_list *veneer;
2890       unsigned int vfp_insn;
2891     } b;
2892     struct
2893     {
2894       struct elf32_vfp11_erratum_list *branch;
2895       unsigned int id;
2896     } v;
2897   } u;
2898   elf32_vfp11_erratum_type type;
2899 }
2900 elf32_vfp11_erratum_list;
2901
2902 /* Information about a STM32L4XX erratum veneer, or a branch to such a
2903    veneer.  */
2904 typedef enum
2905 {
2906   STM32L4XX_ERRATUM_BRANCH_TO_VENEER,
2907   STM32L4XX_ERRATUM_VENEER
2908 }
2909 elf32_stm32l4xx_erratum_type;
2910
2911 typedef struct elf32_stm32l4xx_erratum_list
2912 {
2913   struct elf32_stm32l4xx_erratum_list *next;
2914   bfd_vma vma;
2915   union
2916   {
2917     struct
2918     {
2919       struct elf32_stm32l4xx_erratum_list *veneer;
2920       unsigned int insn;
2921     } b;
2922     struct
2923     {
2924       struct elf32_stm32l4xx_erratum_list *branch;
2925       unsigned int id;
2926     } v;
2927   } u;
2928   elf32_stm32l4xx_erratum_type type;
2929 }
2930 elf32_stm32l4xx_erratum_list;
2931
2932 typedef enum
2933 {
2934   DELETE_EXIDX_ENTRY,
2935   INSERT_EXIDX_CANTUNWIND_AT_END
2936 }
2937 arm_unwind_edit_type;
2938
2939 /* A (sorted) list of edits to apply to an unwind table.  */
2940 typedef struct arm_unwind_table_edit
2941 {
2942   arm_unwind_edit_type type;
2943   /* Note: we sometimes want to insert an unwind entry corresponding to a
2944      section different from the one we're currently writing out, so record the
2945      (text) section this edit relates to here.  */
2946   asection *linked_section;
2947   unsigned int index;
2948   struct arm_unwind_table_edit *next;
2949 }
2950 arm_unwind_table_edit;
2951
2952 typedef struct _arm_elf_section_data
2953 {
2954   /* Information about mapping symbols.  */
2955   struct bfd_elf_section_data elf;
2956   unsigned int mapcount;
2957   unsigned int mapsize;
2958   elf32_arm_section_map *map;
2959   /* Information about CPU errata.  */
2960   unsigned int erratumcount;
2961   elf32_vfp11_erratum_list *erratumlist;
2962   unsigned int stm32l4xx_erratumcount;
2963   elf32_stm32l4xx_erratum_list *stm32l4xx_erratumlist;
2964   unsigned int additional_reloc_count;
2965   /* Information about unwind tables.  */
2966   union
2967   {
2968     /* Unwind info attached to a text section.  */
2969     struct
2970     {
2971       asection *arm_exidx_sec;
2972     } text;
2973
2974     /* Unwind info attached to an .ARM.exidx section.  */
2975     struct
2976     {
2977       arm_unwind_table_edit *unwind_edit_list;
2978       arm_unwind_table_edit *unwind_edit_tail;
2979     } exidx;
2980   } u;
2981 }
2982 _arm_elf_section_data;
2983
2984 #define elf32_arm_section_data(sec) \
2985   ((_arm_elf_section_data *) elf_section_data (sec))
2986
2987 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
2988    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
2989    so may be created multiple times: we use an array of these entries whilst
2990    relaxing which we can refresh easily, then create stubs for each potentially
2991    erratum-triggering instruction once we've settled on a solution.  */
2992
2993 struct a8_erratum_fix
2994 {
2995   bfd *input_bfd;
2996   asection *section;
2997   bfd_vma offset;
2998   bfd_vma target_offset;
2999   unsigned long orig_insn;
3000   char *stub_name;
3001   enum elf32_arm_stub_type stub_type;
3002   enum arm_st_branch_type branch_type;
3003 };
3004
3005 /* A table of relocs applied to branches which might trigger Cortex-A8
3006    erratum.  */
3007
3008 struct a8_erratum_reloc
3009 {
3010   bfd_vma from;
3011   bfd_vma destination;
3012   struct elf32_arm_link_hash_entry *hash;
3013   const char *sym_name;
3014   unsigned int r_type;
3015   enum arm_st_branch_type branch_type;
3016   bfd_boolean non_a8_stub;
3017 };
3018
3019 /* The size of the thread control block.  */
3020 #define TCB_SIZE        8
3021
3022 /* ARM-specific information about a PLT entry, over and above the usual
3023    gotplt_union.  */
3024 struct arm_plt_info
3025 {
3026   /* We reference count Thumb references to a PLT entry separately,
3027      so that we can emit the Thumb trampoline only if needed.  */
3028   bfd_signed_vma thumb_refcount;
3029
3030   /* Some references from Thumb code may be eliminated by BL->BLX
3031      conversion, so record them separately.  */
3032   bfd_signed_vma maybe_thumb_refcount;
3033
3034   /* How many of the recorded PLT accesses were from non-call relocations.
3035      This information is useful when deciding whether anything takes the
3036      address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
3037      non-call references to the function should resolve directly to the
3038      real runtime target.  */
3039   unsigned int noncall_refcount;
3040
3041   /* Since PLT entries have variable size if the Thumb prologue is
3042      used, we need to record the index into .got.plt instead of
3043      recomputing it from the PLT offset.  */
3044   bfd_signed_vma got_offset;
3045 };
3046
3047 /* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
3048 struct arm_local_iplt_info
3049 {
3050   /* The information that is usually found in the generic ELF part of
3051      the hash table entry.  */
3052   union gotplt_union root;
3053
3054   /* The information that is usually found in the ARM-specific part of
3055      the hash table entry.  */
3056   struct arm_plt_info arm;
3057
3058   /* A list of all potential dynamic relocations against this symbol.  */
3059   struct elf_dyn_relocs *dyn_relocs;
3060 };
3061
3062 /* Structure to handle FDPIC support for local functions.  */
3063 struct fdpic_local {
3064   unsigned int funcdesc_cnt;
3065   unsigned int gotofffuncdesc_cnt;
3066   int funcdesc_offset;
3067 };
3068
3069 struct elf_arm_obj_tdata
3070 {
3071   struct elf_obj_tdata root;
3072
3073   /* tls_type for each local got entry.  */
3074   char *local_got_tls_type;
3075
3076   /* GOTPLT entries for TLS descriptors.  */
3077   bfd_vma *local_tlsdesc_gotent;
3078
3079   /* Information for local symbols that need entries in .iplt.  */
3080   struct arm_local_iplt_info **local_iplt;
3081
3082   /* Zero to warn when linking objects with incompatible enum sizes.  */
3083   int no_enum_size_warning;
3084
3085   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
3086   int no_wchar_size_warning;
3087
3088   /* Maintains FDPIC counters and funcdesc info.  */
3089   struct fdpic_local *local_fdpic_cnts;
3090 };
3091
3092 #define elf_arm_tdata(bfd) \
3093   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
3094
3095 #define elf32_arm_local_got_tls_type(bfd) \
3096   (elf_arm_tdata (bfd)->local_got_tls_type)
3097
3098 #define elf32_arm_local_tlsdesc_gotent(bfd) \
3099   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
3100
3101 #define elf32_arm_local_iplt(bfd) \
3102   (elf_arm_tdata (bfd)->local_iplt)
3103
3104 #define elf32_arm_local_fdpic_cnts(bfd) \
3105   (elf_arm_tdata (bfd)->local_fdpic_cnts)
3106
3107 #define is_arm_elf(bfd) \
3108   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
3109    && elf_tdata (bfd) != NULL \
3110    && elf_object_id (bfd) == ARM_ELF_DATA)
3111
3112 static bfd_boolean
3113 elf32_arm_mkobject (bfd *abfd)
3114 {
3115   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
3116                                   ARM_ELF_DATA);
3117 }
3118
3119 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
3120
3121 /* Structure to handle FDPIC support for extern functions.  */
3122 struct fdpic_global {
3123   unsigned int gotofffuncdesc_cnt;
3124   unsigned int gotfuncdesc_cnt;
3125   unsigned int funcdesc_cnt;
3126   int funcdesc_offset;
3127   int gotfuncdesc_offset;
3128 };
3129
3130 /* Arm ELF linker hash entry.  */
3131 struct elf32_arm_link_hash_entry
3132 {
3133   struct elf_link_hash_entry root;
3134
3135   /* Track dynamic relocs copied for this symbol.  */
3136   struct elf_dyn_relocs *dyn_relocs;
3137
3138   /* ARM-specific PLT information.  */
3139   struct arm_plt_info plt;
3140
3141 #define GOT_UNKNOWN     0
3142 #define GOT_NORMAL      1
3143 #define GOT_TLS_GD      2
3144 #define GOT_TLS_IE      4
3145 #define GOT_TLS_GDESC   8
3146 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
3147   unsigned int tls_type : 8;
3148
3149   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
3150   unsigned int is_iplt : 1;
3151
3152   unsigned int unused : 23;
3153
3154   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
3155      starting at the end of the jump table.  */
3156   bfd_vma tlsdesc_got;
3157
3158   /* The symbol marking the real symbol location for exported thumb
3159      symbols with Arm stubs.  */
3160   struct elf_link_hash_entry *export_glue;
3161
3162   /* A pointer to the most recently used stub hash entry against this
3163      symbol.  */
3164   struct elf32_arm_stub_hash_entry *stub_cache;
3165
3166   /* Counter for FDPIC relocations against this symbol.  */
3167   struct fdpic_global fdpic_cnts;
3168 };
3169
3170 /* Traverse an arm ELF linker hash table.  */
3171 #define elf32_arm_link_hash_traverse(table, func, info)                 \
3172   (elf_link_hash_traverse                                               \
3173    (&(table)->root,                                                     \
3174     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
3175     (info)))
3176
3177 /* Get the ARM elf linker hash table from a link_info structure.  */
3178 #define elf32_arm_hash_table(info) \
3179   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
3180   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
3181
3182 #define arm_stub_hash_lookup(table, string, create, copy) \
3183   ((struct elf32_arm_stub_hash_entry *) \
3184    bfd_hash_lookup ((table), (string), (create), (copy)))
3185
3186 /* Array to keep track of which stub sections have been created, and
3187    information on stub grouping.  */
3188 struct map_stub
3189 {
3190   /* This is the section to which stubs in the group will be
3191      attached.  */
3192   asection *link_sec;
3193   /* The stub section.  */
3194   asection *stub_sec;
3195 };
3196
3197 #define elf32_arm_compute_jump_table_size(htab) \
3198   ((htab)->next_tls_desc_index * 4)
3199
3200 /* ARM ELF linker hash table.  */
3201 struct elf32_arm_link_hash_table
3202 {
3203   /* The main hash table.  */
3204   struct elf_link_hash_table root;
3205
3206   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
3207   bfd_size_type thumb_glue_size;
3208
3209   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
3210   bfd_size_type arm_glue_size;
3211
3212   /* The size in bytes of section containing the ARMv4 BX veneers.  */
3213   bfd_size_type bx_glue_size;
3214
3215   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
3216      veneer has been populated.  */
3217   bfd_vma bx_glue_offset[15];
3218
3219   /* The size in bytes of the section containing glue for VFP11 erratum
3220      veneers.  */
3221   bfd_size_type vfp11_erratum_glue_size;
3222
3223  /* The size in bytes of the section containing glue for STM32L4XX erratum
3224      veneers.  */
3225   bfd_size_type stm32l4xx_erratum_glue_size;
3226
3227   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
3228      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
3229      elf32_arm_write_section().  */
3230   struct a8_erratum_fix *a8_erratum_fixes;
3231   unsigned int num_a8_erratum_fixes;
3232
3233   /* An arbitrary input BFD chosen to hold the glue sections.  */
3234   bfd * bfd_of_glue_owner;
3235
3236   /* Nonzero to output a BE8 image.  */
3237   int byteswap_code;
3238
3239   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
3240      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
3241   int target1_is_rel;
3242
3243   /* The relocation to use for R_ARM_TARGET2 relocations.  */
3244   int target2_reloc;
3245
3246   /* 0 = Ignore R_ARM_V4BX.
3247      1 = Convert BX to MOV PC.
3248      2 = Generate v4 interworing stubs.  */
3249   int fix_v4bx;
3250
3251   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
3252   int fix_cortex_a8;
3253
3254   /* Whether we should fix the ARM1176 BLX immediate issue.  */
3255   int fix_arm1176;
3256
3257   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
3258   int use_blx;
3259
3260   /* What sort of code sequences we should look for which may trigger the
3261      VFP11 denorm erratum.  */
3262   bfd_arm_vfp11_fix vfp11_fix;
3263
3264   /* Global counter for the number of fixes we have emitted.  */
3265   int num_vfp11_fixes;
3266
3267   /* What sort of code sequences we should look for which may trigger the
3268      STM32L4XX erratum.  */
3269   bfd_arm_stm32l4xx_fix stm32l4xx_fix;
3270
3271   /* Global counter for the number of fixes we have emitted.  */
3272   int num_stm32l4xx_fixes;
3273
3274   /* Nonzero to force PIC branch veneers.  */
3275   int pic_veneer;
3276
3277   /* The number of bytes in the initial entry in the PLT.  */
3278   bfd_size_type plt_header_size;
3279
3280   /* The number of bytes in the subsequent PLT etries.  */
3281   bfd_size_type plt_entry_size;
3282
3283   /* True if the target system is VxWorks.  */
3284   int vxworks_p;
3285
3286   /* True if the target system is Symbian OS.  */
3287   int symbian_p;
3288
3289   /* True if the target system is Native Client.  */
3290   int nacl_p;
3291
3292   /* True if the target uses REL relocations.  */
3293   bfd_boolean use_rel;
3294
3295   /* Nonzero if import library must be a secure gateway import library
3296      as per ARMv8-M Security Extensions.  */
3297   int cmse_implib;
3298
3299   /* The import library whose symbols' address must remain stable in
3300      the import library generated.  */
3301   bfd *in_implib_bfd;
3302
3303   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
3304   bfd_vma next_tls_desc_index;
3305
3306   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
3307   bfd_vma num_tls_desc;
3308
3309   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
3310   asection *srelplt2;
3311
3312   /* The offset into splt of the PLT entry for the TLS descriptor
3313      resolver.  Special values are 0, if not necessary (or not found
3314      to be necessary yet), and -1 if needed but not determined
3315      yet.  */
3316   bfd_vma dt_tlsdesc_plt;
3317
3318   /* The offset into sgot of the GOT entry used by the PLT entry
3319      above.  */
3320   bfd_vma dt_tlsdesc_got;
3321
3322   /* Offset in .plt section of tls_arm_trampoline.  */
3323   bfd_vma tls_trampoline;
3324
3325   /* Data for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
3326   union
3327   {
3328     bfd_signed_vma refcount;
3329     bfd_vma offset;
3330   } tls_ldm_got;
3331
3332   /* Small local sym cache.  */
3333   struct sym_cache sym_cache;
3334
3335   /* For convenience in allocate_dynrelocs.  */
3336   bfd * obfd;
3337
3338   /* The amount of space used by the reserved portion of the sgotplt
3339      section, plus whatever space is used by the jump slots.  */
3340   bfd_vma sgotplt_jump_table_size;
3341
3342   /* The stub hash table.  */
3343   struct bfd_hash_table stub_hash_table;
3344
3345   /* Linker stub bfd.  */
3346   bfd *stub_bfd;
3347
3348   /* Linker call-backs.  */
3349   asection * (*add_stub_section) (const char *, asection *, asection *,
3350                                   unsigned int);
3351   void (*layout_sections_again) (void);
3352
3353   /* Array to keep track of which stub sections have been created, and
3354      information on stub grouping.  */
3355   struct map_stub *stub_group;
3356
3357   /* Input stub section holding secure gateway veneers.  */
3358   asection *cmse_stub_sec;
3359
3360   /* Offset in cmse_stub_sec where new SG veneers (not in input import library)
3361      start to be allocated.  */
3362   bfd_vma new_cmse_stub_offset;
3363
3364   /* Number of elements in stub_group.  */
3365   unsigned int top_id;
3366
3367   /* Assorted information used by elf32_arm_size_stubs.  */
3368   unsigned int bfd_count;
3369   unsigned int top_index;
3370   asection **input_list;
3371
3372   /* True if the target system uses FDPIC. */
3373   int fdpic_p;
3374
3375   /* Fixup section. Used for FDPIC.  */
3376   asection *srofixup;
3377 };
3378
3379 /* Add an FDPIC read-only fixup.  */
3380 static void
3381 arm_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3382 {
3383   bfd_vma fixup_offset;
3384
3385   fixup_offset = srofixup->reloc_count++ * 4;
3386   BFD_ASSERT (fixup_offset < srofixup->size);
3387   bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3388 }
3389
3390 static inline int
3391 ctz (unsigned int mask)
3392 {
3393 #if GCC_VERSION >= 3004
3394   return __builtin_ctz (mask);
3395 #else
3396   unsigned int i;
3397
3398   for (i = 0; i < 8 * sizeof (mask); i++)
3399     {
3400       if (mask & 0x1)
3401         break;
3402       mask = (mask >> 1);
3403     }
3404   return i;
3405 #endif
3406 }
3407
3408 static inline int
3409 elf32_arm_popcount (unsigned int mask)
3410 {
3411 #if GCC_VERSION >= 3004
3412   return __builtin_popcount (mask);
3413 #else
3414   unsigned int i;
3415   int sum = 0;
3416
3417   for (i = 0; i < 8 * sizeof (mask); i++)
3418     {
3419       if (mask & 0x1)
3420         sum++;
3421       mask = (mask >> 1);
3422     }
3423   return sum;
3424 #endif
3425 }
3426
3427 static void elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
3428                                     asection *sreloc, Elf_Internal_Rela *rel);
3429
3430 static void
3431 arm_elf_fill_funcdesc(bfd *output_bfd,
3432                       struct bfd_link_info *info,
3433                       int *funcdesc_offset,
3434                       int dynindx,
3435                       int offset,
3436                       bfd_vma addr,
3437                       bfd_vma dynreloc_value,
3438                       bfd_vma seg)
3439 {
3440   if ((*funcdesc_offset & 1) == 0)
3441     {
3442       struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
3443       asection *sgot = globals->root.sgot;
3444
3445       if (bfd_link_pic(info))
3446         {
3447           asection *srelgot = globals->root.srelgot;
3448           Elf_Internal_Rela outrel;
3449
3450           outrel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
3451           outrel.r_offset = sgot->output_section->vma + sgot->output_offset + offset;
3452           outrel.r_addend = 0;
3453
3454           elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
3455           bfd_put_32 (output_bfd, addr, sgot->contents + offset);
3456           bfd_put_32 (output_bfd, seg, sgot->contents + offset + 4);
3457         }
3458       else
3459         {
3460           struct elf_link_hash_entry *hgot = globals->root.hgot;
3461           bfd_vma got_value = hgot->root.u.def.value
3462             + hgot->root.u.def.section->output_section->vma
3463             + hgot->root.u.def.section->output_offset;
3464
3465           arm_elf_add_rofixup(output_bfd, globals->srofixup,
3466                               sgot->output_section->vma + sgot->output_offset
3467                               + offset);
3468           arm_elf_add_rofixup(output_bfd, globals->srofixup,
3469                               sgot->output_section->vma + sgot->output_offset
3470                               + offset + 4);
3471           bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + offset);
3472           bfd_put_32 (output_bfd, got_value, sgot->contents + offset + 4);
3473         }
3474       *funcdesc_offset |= 1;
3475     }
3476 }
3477
3478 /* Create an entry in an ARM ELF linker hash table.  */
3479
3480 static struct bfd_hash_entry *
3481 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3482                              struct bfd_hash_table * table,
3483                              const char * string)
3484 {
3485   struct elf32_arm_link_hash_entry * ret =
3486     (struct elf32_arm_link_hash_entry *) entry;
3487
3488   /* Allocate the structure if it has not already been allocated by a
3489      subclass.  */
3490   if (ret == NULL)
3491     ret = (struct elf32_arm_link_hash_entry *)
3492         bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3493   if (ret == NULL)
3494     return (struct bfd_hash_entry *) ret;
3495
3496   /* Call the allocation method of the superclass.  */
3497   ret = ((struct elf32_arm_link_hash_entry *)
3498          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3499                                      table, string));
3500   if (ret != NULL)
3501     {
3502       ret->dyn_relocs = NULL;
3503       ret->tls_type = GOT_UNKNOWN;
3504       ret->tlsdesc_got = (bfd_vma) -1;
3505       ret->plt.thumb_refcount = 0;
3506       ret->plt.maybe_thumb_refcount = 0;
3507       ret->plt.noncall_refcount = 0;
3508       ret->plt.got_offset = -1;
3509       ret->is_iplt = FALSE;
3510       ret->export_glue = NULL;
3511
3512       ret->stub_cache = NULL;
3513
3514       ret->fdpic_cnts.gotofffuncdesc_cnt = 0;
3515       ret->fdpic_cnts.gotfuncdesc_cnt = 0;
3516       ret->fdpic_cnts.funcdesc_cnt = 0;
3517       ret->fdpic_cnts.funcdesc_offset = -1;
3518       ret->fdpic_cnts.gotfuncdesc_offset = -1;
3519     }
3520
3521   return (struct bfd_hash_entry *) ret;
3522 }
3523
3524 /* Ensure that we have allocated bookkeeping structures for ABFD's local
3525    symbols.  */
3526
3527 static bfd_boolean
3528 elf32_arm_allocate_local_sym_info (bfd *abfd)
3529 {
3530   if (elf_local_got_refcounts (abfd) == NULL)
3531     {
3532       bfd_size_type num_syms;
3533       bfd_size_type size;
3534       char *data;
3535
3536       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3537       size = num_syms * (sizeof (bfd_signed_vma)
3538                          + sizeof (struct arm_local_iplt_info *)
3539                          + sizeof (bfd_vma)
3540                          + sizeof (char)
3541                          + sizeof (struct fdpic_local));
3542       data = bfd_zalloc (abfd, size);
3543       if (data == NULL)
3544         return FALSE;
3545
3546       elf32_arm_local_fdpic_cnts (abfd) = (struct fdpic_local *) data;
3547       data += num_syms * sizeof (struct fdpic_local);
3548
3549       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
3550       data += num_syms * sizeof (bfd_signed_vma);
3551
3552       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
3553       data += num_syms * sizeof (struct arm_local_iplt_info *);
3554
3555       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
3556       data += num_syms * sizeof (bfd_vma);
3557
3558       elf32_arm_local_got_tls_type (abfd) = data;
3559     }
3560   return TRUE;
3561 }
3562
3563 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
3564    to input bfd ABFD.  Create the information if it doesn't already exist.
3565    Return null if an allocation fails.  */
3566
3567 static struct arm_local_iplt_info *
3568 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3569 {
3570   struct arm_local_iplt_info **ptr;
3571
3572   if (!elf32_arm_allocate_local_sym_info (abfd))
3573     return NULL;
3574
3575   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3576   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3577   if (*ptr == NULL)
3578     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3579   return *ptr;
3580 }
3581
3582 /* Try to obtain PLT information for the symbol with index R_SYMNDX
3583    in ABFD's symbol table.  If the symbol is global, H points to its
3584    hash table entry, otherwise H is null.
3585
3586    Return true if the symbol does have PLT information.  When returning
3587    true, point *ROOT_PLT at the target-independent reference count/offset
3588    union and *ARM_PLT at the ARM-specific information.  */
3589
3590 static bfd_boolean
3591 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_table *globals,
3592                         struct elf32_arm_link_hash_entry *h,
3593                         unsigned long r_symndx, union gotplt_union **root_plt,
3594                         struct arm_plt_info **arm_plt)
3595 {
3596   struct arm_local_iplt_info *local_iplt;
3597
3598   if (globals->root.splt == NULL && globals->root.iplt == NULL)
3599     return FALSE;
3600
3601   if (h != NULL)
3602     {
3603       *root_plt = &h->root.plt;
3604       *arm_plt = &h->plt;
3605       return TRUE;
3606     }
3607
3608   if (elf32_arm_local_iplt (abfd) == NULL)
3609     return FALSE;
3610
3611   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3612   if (local_iplt == NULL)
3613     return FALSE;
3614
3615   *root_plt = &local_iplt->root;
3616   *arm_plt = &local_iplt->arm;
3617   return TRUE;
3618 }
3619
3620 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
3621    before it.  */
3622
3623 static bfd_boolean
3624 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3625                                   struct arm_plt_info *arm_plt)
3626 {
3627   struct elf32_arm_link_hash_table *htab;
3628
3629   htab = elf32_arm_hash_table (info);
3630   return (arm_plt->thumb_refcount != 0
3631           || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0));
3632 }
3633
3634 /* Return a pointer to the head of the dynamic reloc list that should
3635    be used for local symbol ISYM, which is symbol number R_SYMNDX in
3636    ABFD's symbol table.  Return null if an error occurs.  */
3637
3638 static struct elf_dyn_relocs **
3639 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3640                                    Elf_Internal_Sym *isym)
3641 {
3642   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3643     {
3644       struct arm_local_iplt_info *local_iplt;
3645
3646       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3647       if (local_iplt == NULL)
3648         return NULL;
3649       return &local_iplt->dyn_relocs;
3650     }
3651   else
3652     {
3653       /* Track dynamic relocs needed for local syms too.
3654          We really need local syms available to do this
3655          easily.  Oh well.  */
3656       asection *s;
3657       void *vpp;
3658
3659       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3660       if (s == NULL)
3661         abort ();
3662
3663       vpp = &elf_section_data (s)->local_dynrel;
3664       return (struct elf_dyn_relocs **) vpp;
3665     }
3666 }
3667
3668 /* Initialize an entry in the stub hash table.  */
3669
3670 static struct bfd_hash_entry *
3671 stub_hash_newfunc (struct bfd_hash_entry *entry,
3672                    struct bfd_hash_table *table,
3673                    const char *string)
3674 {
3675   /* Allocate the structure if it has not already been allocated by a
3676      subclass.  */
3677   if (entry == NULL)
3678     {
3679       entry = (struct bfd_hash_entry *)
3680           bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3681       if (entry == NULL)
3682         return entry;
3683     }
3684
3685   /* Call the allocation method of the superclass.  */
3686   entry = bfd_hash_newfunc (entry, table, string);
3687   if (entry != NULL)
3688     {
3689       struct elf32_arm_stub_hash_entry *eh;
3690
3691       /* Initialize the local fields.  */
3692       eh = (struct elf32_arm_stub_hash_entry *) entry;
3693       eh->stub_sec = NULL;
3694       eh->stub_offset = (bfd_vma) -1;
3695       eh->source_value = 0;
3696       eh->target_value = 0;
3697       eh->target_section = NULL;
3698       eh->orig_insn = 0;
3699       eh->stub_type = arm_stub_none;
3700       eh->stub_size = 0;
3701       eh->stub_template = NULL;
3702       eh->stub_template_size = -1;
3703       eh->h = NULL;
3704       eh->id_sec = NULL;
3705       eh->output_name = NULL;
3706     }
3707
3708   return entry;
3709 }
3710
3711 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3712    shortcuts to them in our hash table.  */
3713
3714 static bfd_boolean
3715 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3716 {
3717   struct elf32_arm_link_hash_table *htab;
3718
3719   htab = elf32_arm_hash_table (info);
3720   if (htab == NULL)
3721     return FALSE;
3722
3723   /* BPABI objects never have a GOT, or associated sections.  */
3724   if (htab->symbian_p)
3725     return TRUE;
3726
3727   if (! _bfd_elf_create_got_section (dynobj, info))
3728     return FALSE;
3729
3730   /* Also create .rofixup.  */
3731   if (htab->fdpic_p)
3732     {
3733       htab->srofixup = bfd_make_section_with_flags (dynobj, ".rofixup",
3734                                                     (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3735                                                      | SEC_IN_MEMORY | SEC_LINKER_CREATED | SEC_READONLY));
3736       if (htab->srofixup == NULL || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
3737         return FALSE;
3738     }
3739
3740   return TRUE;
3741 }
3742
3743 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3744
3745 static bfd_boolean
3746 create_ifunc_sections (struct bfd_link_info *info)
3747 {
3748   struct elf32_arm_link_hash_table *htab;
3749   const struct elf_backend_data *bed;
3750   bfd *dynobj;
3751   asection *s;
3752   flagword flags;
3753
3754   htab = elf32_arm_hash_table (info);
3755   dynobj = htab->root.dynobj;
3756   bed = get_elf_backend_data (dynobj);
3757   flags = bed->dynamic_sec_flags;
3758
3759   if (htab->root.iplt == NULL)
3760     {
3761       s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3762                                               flags | SEC_READONLY | SEC_CODE);
3763       if (s == NULL
3764           || !bfd_set_section_alignment (dynobj, s, bed->plt_alignment))
3765         return FALSE;
3766       htab->root.iplt = s;
3767     }
3768
3769   if (htab->root.irelplt == NULL)
3770     {
3771       s = bfd_make_section_anyway_with_flags (dynobj,
3772                                               RELOC_SECTION (htab, ".iplt"),
3773                                               flags | SEC_READONLY);
3774       if (s == NULL
3775           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3776         return FALSE;
3777       htab->root.irelplt = s;
3778     }
3779
3780   if (htab->root.igotplt == NULL)
3781     {
3782       s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3783       if (s == NULL
3784           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3785         return FALSE;
3786       htab->root.igotplt = s;
3787     }
3788   return TRUE;
3789 }
3790
3791 /* Determine if we're dealing with a Thumb only architecture.  */
3792
3793 static bfd_boolean
3794 using_thumb_only (struct elf32_arm_link_hash_table *globals)
3795 {
3796   int arch;
3797   int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3798                                           Tag_CPU_arch_profile);
3799
3800   if (profile)
3801     return profile == 'M';
3802
3803   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3804
3805   /* Force return logic to be reviewed for each new architecture.  */
3806   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3807
3808   if (arch == TAG_CPU_ARCH_V6_M
3809       || arch == TAG_CPU_ARCH_V6S_M
3810       || arch == TAG_CPU_ARCH_V7E_M
3811       || arch == TAG_CPU_ARCH_V8M_BASE
3812       || arch == TAG_CPU_ARCH_V8M_MAIN)
3813     return TRUE;
3814
3815   return FALSE;
3816 }
3817
3818 /* Determine if we're dealing with a Thumb-2 object.  */
3819
3820 static bfd_boolean
3821 using_thumb2 (struct elf32_arm_link_hash_table *globals)
3822 {
3823   int arch;
3824   int thumb_isa = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3825                                             Tag_THUMB_ISA_use);
3826
3827   if (thumb_isa)
3828     return thumb_isa == 2;
3829
3830   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3831
3832   /* Force return logic to be reviewed for each new architecture.  */
3833   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3834
3835   return (arch == TAG_CPU_ARCH_V6T2
3836           || arch == TAG_CPU_ARCH_V7
3837           || arch == TAG_CPU_ARCH_V7E_M
3838           || arch == TAG_CPU_ARCH_V8
3839           || arch == TAG_CPU_ARCH_V8R
3840           || arch == TAG_CPU_ARCH_V8M_MAIN);
3841 }
3842
3843 /* Determine whether Thumb-2 BL instruction is available.  */
3844
3845 static bfd_boolean
3846 using_thumb2_bl (struct elf32_arm_link_hash_table *globals)
3847 {
3848   int arch =
3849     bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3850
3851   /* Force return logic to be reviewed for each new architecture.  */
3852   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3853
3854   /* Architecture was introduced after ARMv6T2 (eg. ARMv6-M).  */
3855   return (arch == TAG_CPU_ARCH_V6T2
3856           || arch >= TAG_CPU_ARCH_V7);
3857 }
3858
3859 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3860    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3861    hash table.  */
3862
3863 static bfd_boolean
3864 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3865 {
3866   struct elf32_arm_link_hash_table *htab;
3867
3868   htab = elf32_arm_hash_table (info);
3869   if (htab == NULL)
3870     return FALSE;
3871
3872   if (!htab->root.sgot && !create_got_section (dynobj, info))
3873     return FALSE;
3874
3875   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3876     return FALSE;
3877
3878   if (htab->vxworks_p)
3879     {
3880       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3881         return FALSE;
3882
3883       if (bfd_link_pic (info))
3884         {
3885           htab->plt_header_size = 0;
3886           htab->plt_entry_size
3887             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3888         }
3889       else
3890         {
3891           htab->plt_header_size
3892             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3893           htab->plt_entry_size
3894             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3895         }
3896
3897       if (elf_elfheader (dynobj))
3898         elf_elfheader (dynobj)->e_ident[EI_CLASS] = ELFCLASS32;
3899     }
3900   else
3901     {
3902       /* PR ld/16017
3903          Test for thumb only architectures.  Note - we cannot just call
3904          using_thumb_only() as the attributes in the output bfd have not been
3905          initialised at this point, so instead we use the input bfd.  */
3906       bfd * saved_obfd = htab->obfd;
3907
3908       htab->obfd = dynobj;
3909       if (using_thumb_only (htab))
3910         {
3911           htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
3912           htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
3913         }
3914       htab->obfd = saved_obfd;
3915     }
3916
3917   if (htab->fdpic_p) {
3918     htab->plt_header_size = 0;
3919     if (info->flags & DF_BIND_NOW)
3920       htab->plt_entry_size = 4 * (ARRAY_SIZE(elf32_arm_fdpic_plt_entry) - 5);
3921     else
3922       htab->plt_entry_size = 4 * ARRAY_SIZE(elf32_arm_fdpic_plt_entry);
3923   }
3924
3925   if (!htab->root.splt
3926       || !htab->root.srelplt
3927       || !htab->root.sdynbss
3928       || (!bfd_link_pic (info) && !htab->root.srelbss))
3929     abort ();
3930
3931   return TRUE;
3932 }
3933
3934 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3935
3936 static void
3937 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3938                                 struct elf_link_hash_entry *dir,
3939                                 struct elf_link_hash_entry *ind)
3940 {
3941   struct elf32_arm_link_hash_entry *edir, *eind;
3942
3943   edir = (struct elf32_arm_link_hash_entry *) dir;
3944   eind = (struct elf32_arm_link_hash_entry *) ind;
3945
3946   if (eind->dyn_relocs != NULL)
3947     {
3948       if (edir->dyn_relocs != NULL)
3949         {
3950           struct elf_dyn_relocs **pp;
3951           struct elf_dyn_relocs *p;
3952
3953           /* Add reloc counts against the indirect sym to the direct sym
3954              list.  Merge any entries against the same section.  */
3955           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3956             {
3957               struct elf_dyn_relocs *q;
3958
3959               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3960                 if (q->sec == p->sec)
3961                   {
3962                     q->pc_count += p->pc_count;
3963                     q->count += p->count;
3964                     *pp = p->next;
3965                     break;
3966                   }
3967               if (q == NULL)
3968                 pp = &p->next;
3969             }
3970           *pp = edir->dyn_relocs;
3971         }
3972
3973       edir->dyn_relocs = eind->dyn_relocs;
3974       eind->dyn_relocs = NULL;
3975     }
3976
3977   if (ind->root.type == bfd_link_hash_indirect)
3978     {
3979       /* Copy over PLT info.  */
3980       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3981       eind->plt.thumb_refcount = 0;
3982       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3983       eind->plt.maybe_thumb_refcount = 0;
3984       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3985       eind->plt.noncall_refcount = 0;
3986
3987       /* Copy FDPIC counters.  */
3988       edir->fdpic_cnts.gotofffuncdesc_cnt += eind->fdpic_cnts.gotofffuncdesc_cnt;
3989       edir->fdpic_cnts.gotfuncdesc_cnt += eind->fdpic_cnts.gotfuncdesc_cnt;
3990       edir->fdpic_cnts.funcdesc_cnt += eind->fdpic_cnts.funcdesc_cnt;
3991
3992       /* We should only allocate a function to .iplt once the final
3993          symbol information is known.  */
3994       BFD_ASSERT (!eind->is_iplt);
3995
3996       if (dir->got.refcount <= 0)
3997         {
3998           edir->tls_type = eind->tls_type;
3999           eind->tls_type = GOT_UNKNOWN;
4000         }
4001     }
4002
4003   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4004 }
4005
4006 /* Destroy an ARM elf linker hash table.  */
4007
4008 static void
4009 elf32_arm_link_hash_table_free (bfd *obfd)
4010 {
4011   struct elf32_arm_link_hash_table *ret
4012     = (struct elf32_arm_link_hash_table *) obfd->link.hash;
4013
4014   bfd_hash_table_free (&ret->stub_hash_table);
4015   _bfd_elf_link_hash_table_free (obfd);
4016 }
4017
4018 /* Create an ARM elf linker hash table.  */
4019
4020 static struct bfd_link_hash_table *
4021 elf32_arm_link_hash_table_create (bfd *abfd)
4022 {
4023   struct elf32_arm_link_hash_table *ret;
4024   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
4025
4026   ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
4027   if (ret == NULL)
4028     return NULL;
4029
4030   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
4031                                       elf32_arm_link_hash_newfunc,
4032                                       sizeof (struct elf32_arm_link_hash_entry),
4033                                       ARM_ELF_DATA))
4034     {
4035       free (ret);
4036       return NULL;
4037     }
4038
4039   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4040   ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
4041 #ifdef FOUR_WORD_PLT
4042   ret->plt_header_size = 16;
4043   ret->plt_entry_size = 16;
4044 #else
4045   ret->plt_header_size = 20;
4046   ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
4047 #endif
4048   ret->use_rel = TRUE;
4049   ret->obfd = abfd;
4050   ret->fdpic_p = 0;
4051
4052   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
4053                             sizeof (struct elf32_arm_stub_hash_entry)))
4054     {
4055       _bfd_elf_link_hash_table_free (abfd);
4056       return NULL;
4057     }
4058   ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
4059
4060   return &ret->root.root;
4061 }
4062
4063 /* Determine what kind of NOPs are available.  */
4064
4065 static bfd_boolean
4066 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
4067 {
4068   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
4069                                              Tag_CPU_arch);
4070
4071   /* Force return logic to be reviewed for each new architecture.  */
4072   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
4073
4074   return (arch == TAG_CPU_ARCH_V6T2
4075           || arch == TAG_CPU_ARCH_V6K
4076           || arch == TAG_CPU_ARCH_V7
4077           || arch == TAG_CPU_ARCH_V8
4078           || arch == TAG_CPU_ARCH_V8R);
4079 }
4080
4081 static bfd_boolean
4082 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
4083 {
4084   switch (stub_type)
4085     {
4086     case arm_stub_long_branch_thumb_only:
4087     case arm_stub_long_branch_thumb2_only:
4088     case arm_stub_long_branch_thumb2_only_pure:
4089     case arm_stub_long_branch_v4t_thumb_arm:
4090     case arm_stub_short_branch_v4t_thumb_arm:
4091     case arm_stub_long_branch_v4t_thumb_arm_pic:
4092     case arm_stub_long_branch_v4t_thumb_tls_pic:
4093     case arm_stub_long_branch_thumb_only_pic:
4094     case arm_stub_cmse_branch_thumb_only:
4095       return TRUE;
4096     case arm_stub_none:
4097       BFD_FAIL ();
4098       return FALSE;
4099       break;
4100     default:
4101       return FALSE;
4102     }
4103 }
4104
4105 /* Determine the type of stub needed, if any, for a call.  */
4106
4107 static enum elf32_arm_stub_type
4108 arm_type_of_stub (struct bfd_link_info *info,
4109                   asection *input_sec,
4110                   const Elf_Internal_Rela *rel,
4111                   unsigned char st_type,
4112                   enum arm_st_branch_type *actual_branch_type,
4113                   struct elf32_arm_link_hash_entry *hash,
4114                   bfd_vma destination,
4115                   asection *sym_sec,
4116                   bfd *input_bfd,
4117                   const char *name)
4118 {
4119   bfd_vma location;
4120   bfd_signed_vma branch_offset;
4121   unsigned int r_type;
4122   struct elf32_arm_link_hash_table * globals;
4123   bfd_boolean thumb2, thumb2_bl, thumb_only;
4124   enum elf32_arm_stub_type stub_type = arm_stub_none;
4125   int use_plt = 0;
4126   enum arm_st_branch_type branch_type = *actual_branch_type;
4127   union gotplt_union *root_plt;
4128   struct arm_plt_info *arm_plt;
4129   int arch;
4130   int thumb2_movw;
4131
4132   if (branch_type == ST_BRANCH_LONG)
4133     return stub_type;
4134
4135   globals = elf32_arm_hash_table (info);
4136   if (globals == NULL)
4137     return stub_type;
4138
4139   thumb_only = using_thumb_only (globals);
4140   thumb2 = using_thumb2 (globals);
4141   thumb2_bl = using_thumb2_bl (globals);
4142
4143   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
4144
4145   /* True for architectures that implement the thumb2 movw instruction.  */
4146   thumb2_movw = thumb2 || (arch  == TAG_CPU_ARCH_V8M_BASE);
4147
4148   /* Determine where the call point is.  */
4149   location = (input_sec->output_offset
4150               + input_sec->output_section->vma
4151               + rel->r_offset);
4152
4153   r_type = ELF32_R_TYPE (rel->r_info);
4154
4155   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
4156      are considering a function call relocation.  */
4157   if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4158                      || r_type == R_ARM_THM_JUMP19)
4159       && branch_type == ST_BRANCH_TO_ARM)
4160     branch_type = ST_BRANCH_TO_THUMB;
4161
4162   /* For TLS call relocs, it is the caller's responsibility to provide
4163      the address of the appropriate trampoline.  */
4164   if (r_type != R_ARM_TLS_CALL
4165       && r_type != R_ARM_THM_TLS_CALL
4166       && elf32_arm_get_plt_info (input_bfd, globals, hash,
4167                                  ELF32_R_SYM (rel->r_info), &root_plt,
4168                                  &arm_plt)
4169       && root_plt->offset != (bfd_vma) -1)
4170     {
4171       asection *splt;
4172
4173       if (hash == NULL || hash->is_iplt)
4174         splt = globals->root.iplt;
4175       else
4176         splt = globals->root.splt;
4177       if (splt != NULL)
4178         {
4179           use_plt = 1;
4180
4181           /* Note when dealing with PLT entries: the main PLT stub is in
4182              ARM mode, so if the branch is in Thumb mode, another
4183              Thumb->ARM stub will be inserted later just before the ARM
4184              PLT stub. If a long branch stub is needed, we'll add a
4185              Thumb->Arm one and branch directly to the ARM PLT entry.
4186              Here, we have to check if a pre-PLT Thumb->ARM stub
4187              is needed and if it will be close enough.  */
4188
4189           destination = (splt->output_section->vma
4190                          + splt->output_offset
4191                          + root_plt->offset);
4192           st_type = STT_FUNC;
4193
4194           /* Thumb branch/call to PLT: it can become a branch to ARM
4195              or to Thumb. We must perform the same checks and
4196              corrections as in elf32_arm_final_link_relocate.  */
4197           if ((r_type == R_ARM_THM_CALL)
4198               || (r_type == R_ARM_THM_JUMP24))
4199             {
4200               if (globals->use_blx
4201                   && r_type == R_ARM_THM_CALL
4202                   && !thumb_only)
4203                 {
4204                   /* If the Thumb BLX instruction is available, convert
4205                      the BL to a BLX instruction to call the ARM-mode
4206                      PLT entry.  */
4207                   branch_type = ST_BRANCH_TO_ARM;
4208                 }
4209               else
4210                 {
4211                   if (!thumb_only)
4212                     /* Target the Thumb stub before the ARM PLT entry.  */
4213                     destination -= PLT_THUMB_STUB_SIZE;
4214                   branch_type = ST_BRANCH_TO_THUMB;
4215                 }
4216             }
4217           else
4218             {
4219               branch_type = ST_BRANCH_TO_ARM;
4220             }
4221         }
4222     }
4223   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
4224   BFD_ASSERT (st_type != STT_GNU_IFUNC);
4225
4226   branch_offset = (bfd_signed_vma)(destination - location);
4227
4228   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4229       || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
4230     {
4231       /* Handle cases where:
4232          - this call goes too far (different Thumb/Thumb2 max
4233            distance)
4234          - it's a Thumb->Arm call and blx is not available, or it's a
4235            Thumb->Arm branch (not bl). A stub is needed in this case,
4236            but only if this call is not through a PLT entry. Indeed,
4237            PLT stubs handle mode switching already.  */
4238       if ((!thumb2_bl
4239             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4240                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4241           || (thumb2_bl
4242               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4243                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4244           || (thumb2
4245               && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
4246                   || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
4247               && (r_type == R_ARM_THM_JUMP19))
4248           || (branch_type == ST_BRANCH_TO_ARM
4249               && (((r_type == R_ARM_THM_CALL
4250                     || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
4251                   || (r_type == R_ARM_THM_JUMP24)
4252                   || (r_type == R_ARM_THM_JUMP19))
4253               && !use_plt))
4254         {
4255           /* If we need to insert a Thumb-Thumb long branch stub to a
4256              PLT, use one that branches directly to the ARM PLT
4257              stub. If we pretended we'd use the pre-PLT Thumb->ARM
4258              stub, undo this now.  */
4259           if ((branch_type == ST_BRANCH_TO_THUMB) && use_plt && !thumb_only)
4260             {
4261               branch_type = ST_BRANCH_TO_ARM;
4262               branch_offset += PLT_THUMB_STUB_SIZE;
4263             }
4264
4265           if (branch_type == ST_BRANCH_TO_THUMB)
4266             {
4267               /* Thumb to thumb.  */
4268               if (!thumb_only)
4269                 {
4270                   if (input_sec->flags & SEC_ELF_PURECODE)
4271                     _bfd_error_handler
4272                       (_("%pB(%pA): warning: long branch veneers used in"
4273                          " section with SHF_ARM_PURECODE section"
4274                          " attribute is only supported for M-profile"
4275                          " targets that implement the movw instruction"),
4276                        input_bfd, input_sec);
4277
4278                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4279                     /* PIC stubs.  */
4280                     ? ((globals->use_blx
4281                         && (r_type == R_ARM_THM_CALL))
4282                        /* V5T and above. Stub starts with ARM code, so
4283                           we must be able to switch mode before
4284                           reaching it, which is only possible for 'bl'
4285                           (ie R_ARM_THM_CALL relocation).  */
4286                        ? arm_stub_long_branch_any_thumb_pic
4287                        /* On V4T, use Thumb code only.  */
4288                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
4289
4290                     /* non-PIC stubs.  */
4291                     : ((globals->use_blx
4292                         && (r_type == R_ARM_THM_CALL))
4293                        /* V5T and above.  */
4294                        ? arm_stub_long_branch_any_any
4295                        /* V4T.  */
4296                        : arm_stub_long_branch_v4t_thumb_thumb);
4297                 }
4298               else
4299                 {
4300                   if (thumb2_movw && (input_sec->flags & SEC_ELF_PURECODE))
4301                       stub_type = arm_stub_long_branch_thumb2_only_pure;
4302                   else
4303                     {
4304                       if (input_sec->flags & SEC_ELF_PURECODE)
4305                         _bfd_error_handler
4306                           (_("%pB(%pA): warning: long branch veneers used in"
4307                              " section with SHF_ARM_PURECODE section"
4308                              " attribute is only supported for M-profile"
4309                              " targets that implement the movw instruction"),
4310                            input_bfd, input_sec);
4311
4312                       stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4313                         /* PIC stub.  */
4314                         ? arm_stub_long_branch_thumb_only_pic
4315                         /* non-PIC stub.  */
4316                         : (thumb2 ? arm_stub_long_branch_thumb2_only
4317                                   : arm_stub_long_branch_thumb_only);
4318                     }
4319                 }
4320             }
4321           else
4322             {
4323               if (input_sec->flags & SEC_ELF_PURECODE)
4324                 _bfd_error_handler
4325                   (_("%pB(%pA): warning: long branch veneers used in"
4326                      " section with SHF_ARM_PURECODE section"
4327                      " attribute is only supported" " for M-profile"
4328                      " targets that implement the movw instruction"),
4329                    input_bfd, input_sec);
4330
4331               /* Thumb to arm.  */
4332               if (sym_sec != NULL
4333                   && sym_sec->owner != NULL
4334                   && !INTERWORK_FLAG (sym_sec->owner))
4335                 {
4336                   _bfd_error_handler
4337                     (_("%pB(%s): warning: interworking not enabled;"
4338                        " first occurrence: %pB: %s call to %s"),
4339                      sym_sec->owner, name, input_bfd, "Thumb", "ARM");
4340                 }
4341
4342               stub_type =
4343                 (bfd_link_pic (info) | globals->pic_veneer)
4344                 /* PIC stubs.  */
4345                 ? (r_type == R_ARM_THM_TLS_CALL
4346                    /* TLS PIC stubs.  */
4347                    ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
4348                       : arm_stub_long_branch_v4t_thumb_tls_pic)
4349                    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4350                       /* V5T PIC and above.  */
4351                       ? arm_stub_long_branch_any_arm_pic
4352                       /* V4T PIC stub.  */
4353                       : arm_stub_long_branch_v4t_thumb_arm_pic))
4354
4355                 /* non-PIC stubs.  */
4356                 : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4357                    /* V5T and above.  */
4358                    ? arm_stub_long_branch_any_any
4359                    /* V4T.  */
4360                    : arm_stub_long_branch_v4t_thumb_arm);
4361
4362               /* Handle v4t short branches.  */
4363               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4364                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4365                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4366                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4367             }
4368         }
4369     }
4370   else if (r_type == R_ARM_CALL
4371            || r_type == R_ARM_JUMP24
4372            || r_type == R_ARM_PLT32
4373            || r_type == R_ARM_TLS_CALL)
4374     {
4375       if (input_sec->flags & SEC_ELF_PURECODE)
4376         _bfd_error_handler
4377           (_("%pB(%pA): warning: long branch veneers used in"
4378              " section with SHF_ARM_PURECODE section"
4379              " attribute is only supported for M-profile"
4380              " targets that implement the movw instruction"),
4381            input_bfd, input_sec);
4382       if (branch_type == ST_BRANCH_TO_THUMB)
4383         {
4384           /* Arm to thumb.  */
4385
4386           if (sym_sec != NULL
4387               && sym_sec->owner != NULL
4388               && !INTERWORK_FLAG (sym_sec->owner))
4389             {
4390               _bfd_error_handler
4391                 (_("%pB(%s): warning: interworking not enabled;"
4392                    " first occurrence: %pB: %s call to %s"),
4393                  sym_sec->owner, name, input_bfd, "ARM", "Thumb");
4394             }
4395
4396           /* We have an extra 2-bytes reach because of
4397              the mode change (bit 24 (H) of BLX encoding).  */
4398           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4399               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4400               || (r_type == R_ARM_CALL && !globals->use_blx)
4401               || (r_type == R_ARM_JUMP24)
4402               || (r_type == R_ARM_PLT32))
4403             {
4404               stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4405                 /* PIC stubs.  */
4406                 ? ((globals->use_blx)
4407                    /* V5T and above.  */
4408                    ? arm_stub_long_branch_any_thumb_pic
4409                    /* V4T stub.  */
4410                    : arm_stub_long_branch_v4t_arm_thumb_pic)
4411
4412                 /* non-PIC stubs.  */
4413                 : ((globals->use_blx)
4414                    /* V5T and above.  */
4415                    ? arm_stub_long_branch_any_any
4416                    /* V4T.  */
4417                    : arm_stub_long_branch_v4t_arm_thumb);
4418             }
4419         }
4420       else
4421         {
4422           /* Arm to arm.  */
4423           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4424               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4425             {
4426               stub_type =
4427                 (bfd_link_pic (info) | globals->pic_veneer)
4428                 /* PIC stubs.  */
4429                 ? (r_type == R_ARM_TLS_CALL
4430                    /* TLS PIC Stub.  */
4431                    ? arm_stub_long_branch_any_tls_pic
4432                    : (globals->nacl_p
4433                       ? arm_stub_long_branch_arm_nacl_pic
4434                       : arm_stub_long_branch_any_arm_pic))
4435                 /* non-PIC stubs.  */
4436                 : (globals->nacl_p
4437                    ? arm_stub_long_branch_arm_nacl
4438                    : arm_stub_long_branch_any_any);
4439             }
4440         }
4441     }
4442
4443   /* If a stub is needed, record the actual destination type.  */
4444   if (stub_type != arm_stub_none)
4445     *actual_branch_type = branch_type;
4446
4447   return stub_type;
4448 }
4449
4450 /* Build a name for an entry in the stub hash table.  */
4451
4452 static char *
4453 elf32_arm_stub_name (const asection *input_section,
4454                      const asection *sym_sec,
4455                      const struct elf32_arm_link_hash_entry *hash,
4456                      const Elf_Internal_Rela *rel,
4457                      enum elf32_arm_stub_type stub_type)
4458 {
4459   char *stub_name;
4460   bfd_size_type len;
4461
4462   if (hash)
4463     {
4464       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4465       stub_name = (char *) bfd_malloc (len);
4466       if (stub_name != NULL)
4467         sprintf (stub_name, "%08x_%s+%x_%d",
4468                  input_section->id & 0xffffffff,
4469                  hash->root.root.root.string,
4470                  (int) rel->r_addend & 0xffffffff,
4471                  (int) stub_type);
4472     }
4473   else
4474     {
4475       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4476       stub_name = (char *) bfd_malloc (len);
4477       if (stub_name != NULL)
4478         sprintf (stub_name, "%08x_%x:%x+%x_%d",
4479                  input_section->id & 0xffffffff,
4480                  sym_sec->id & 0xffffffff,
4481                  ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4482                  || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4483                  ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4484                  (int) rel->r_addend & 0xffffffff,
4485                  (int) stub_type);
4486     }
4487
4488   return stub_name;
4489 }
4490
4491 /* Look up an entry in the stub hash.  Stub entries are cached because
4492    creating the stub name takes a bit of time.  */
4493
4494 static struct elf32_arm_stub_hash_entry *
4495 elf32_arm_get_stub_entry (const asection *input_section,
4496                           const asection *sym_sec,
4497                           struct elf_link_hash_entry *hash,
4498                           const Elf_Internal_Rela *rel,
4499                           struct elf32_arm_link_hash_table *htab,
4500                           enum elf32_arm_stub_type stub_type)
4501 {
4502   struct elf32_arm_stub_hash_entry *stub_entry;
4503   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4504   const asection *id_sec;
4505
4506   if ((input_section->flags & SEC_CODE) == 0)
4507     return NULL;
4508
4509   /* If this input section is part of a group of sections sharing one
4510      stub section, then use the id of the first section in the group.
4511      Stub names need to include a section id, as there may well be
4512      more than one stub used to reach say, printf, and we need to
4513      distinguish between them.  */
4514   BFD_ASSERT (input_section->id <= htab->top_id);
4515   id_sec = htab->stub_group[input_section->id].link_sec;
4516
4517   if (h != NULL && h->stub_cache != NULL
4518       && h->stub_cache->h == h
4519       && h->stub_cache->id_sec == id_sec
4520       && h->stub_cache->stub_type == stub_type)
4521     {
4522       stub_entry = h->stub_cache;
4523     }
4524   else
4525     {
4526       char *stub_name;
4527
4528       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4529       if (stub_name == NULL)
4530         return NULL;
4531
4532       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4533                                         stub_name, FALSE, FALSE);
4534       if (h != NULL)
4535         h->stub_cache = stub_entry;
4536
4537       free (stub_name);
4538     }
4539
4540   return stub_entry;
4541 }
4542
4543 /* Whether veneers of type STUB_TYPE require to be in a dedicated output
4544    section.  */
4545
4546 static bfd_boolean
4547 arm_dedicated_stub_output_section_required (enum elf32_arm_stub_type stub_type)
4548 {
4549   if (stub_type >= max_stub_type)
4550     abort ();  /* Should be unreachable.  */
4551
4552   switch (stub_type)
4553     {
4554     case arm_stub_cmse_branch_thumb_only:
4555       return TRUE;
4556
4557     default:
4558       return FALSE;
4559     }
4560
4561   abort ();  /* Should be unreachable.  */
4562 }
4563
4564 /* Required alignment (as a power of 2) for the dedicated section holding
4565    veneers of type STUB_TYPE, or 0 if veneers of this type are interspersed
4566    with input sections.  */
4567
4568 static int
4569 arm_dedicated_stub_output_section_required_alignment
4570   (enum elf32_arm_stub_type stub_type)
4571 {
4572   if (stub_type >= max_stub_type)
4573     abort ();  /* Should be unreachable.  */
4574
4575   switch (stub_type)
4576     {
4577     /* Vectors of Secure Gateway veneers must be aligned on 32byte
4578        boundary.  */
4579     case arm_stub_cmse_branch_thumb_only:
4580       return 5;
4581
4582     default:
4583       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4584       return 0;
4585     }
4586
4587   abort ();  /* Should be unreachable.  */
4588 }
4589
4590 /* Name of the dedicated output section to put veneers of type STUB_TYPE, or
4591    NULL if veneers of this type are interspersed with input sections.  */
4592
4593 static const char *
4594 arm_dedicated_stub_output_section_name (enum elf32_arm_stub_type stub_type)
4595 {
4596   if (stub_type >= max_stub_type)
4597     abort ();  /* Should be unreachable.  */
4598
4599   switch (stub_type)
4600     {
4601     case arm_stub_cmse_branch_thumb_only:
4602       return ".gnu.sgstubs";
4603
4604     default:
4605       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4606       return NULL;
4607     }
4608
4609   abort ();  /* Should be unreachable.  */
4610 }
4611
4612 /* If veneers of type STUB_TYPE should go in a dedicated output section,
4613    returns the address of the hash table field in HTAB holding a pointer to the
4614    corresponding input section.  Otherwise, returns NULL.  */
4615
4616 static asection **
4617 arm_dedicated_stub_input_section_ptr (struct elf32_arm_link_hash_table *htab,
4618                                       enum elf32_arm_stub_type stub_type)
4619 {
4620   if (stub_type >= max_stub_type)
4621     abort ();  /* Should be unreachable.  */
4622
4623   switch (stub_type)
4624     {
4625     case arm_stub_cmse_branch_thumb_only:
4626       return &htab->cmse_stub_sec;
4627
4628     default:
4629       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4630       return NULL;
4631     }
4632
4633   abort ();  /* Should be unreachable.  */
4634 }
4635
4636 /* Find or create a stub section to contain a stub of type STUB_TYPE.  SECTION
4637    is the section that branch into veneer and can be NULL if stub should go in
4638    a dedicated output section.  Returns a pointer to the stub section, and the
4639    section to which the stub section will be attached (in *LINK_SEC_P).
4640    LINK_SEC_P may be NULL.  */
4641
4642 static asection *
4643 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4644                                    struct elf32_arm_link_hash_table *htab,
4645                                    enum elf32_arm_stub_type stub_type)
4646 {
4647   asection *link_sec, *out_sec, **stub_sec_p;
4648   const char *stub_sec_prefix;
4649   bfd_boolean dedicated_output_section =
4650     arm_dedicated_stub_output_section_required (stub_type);
4651   int align;
4652
4653   if (dedicated_output_section)
4654     {
4655       bfd *output_bfd = htab->obfd;
4656       const char *out_sec_name =
4657         arm_dedicated_stub_output_section_name (stub_type);
4658       link_sec = NULL;
4659       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
4660       stub_sec_prefix = out_sec_name;
4661       align = arm_dedicated_stub_output_section_required_alignment (stub_type);
4662       out_sec = bfd_get_section_by_name (output_bfd, out_sec_name);
4663       if (out_sec == NULL)
4664         {
4665           _bfd_error_handler (_("no address assigned to the veneers output "
4666                                 "section %s"), out_sec_name);
4667           return NULL;
4668         }
4669     }
4670   else
4671     {
4672       BFD_ASSERT (section->id <= htab->top_id);
4673       link_sec = htab->stub_group[section->id].link_sec;
4674       BFD_ASSERT (link_sec != NULL);
4675       stub_sec_p = &htab->stub_group[section->id].stub_sec;
4676       if (*stub_sec_p == NULL)
4677         stub_sec_p = &htab->stub_group[link_sec->id].stub_sec;
4678       stub_sec_prefix = link_sec->name;
4679       out_sec = link_sec->output_section;
4680       align = htab->nacl_p ? 4 : 3;
4681     }
4682
4683   if (*stub_sec_p == NULL)
4684     {
4685       size_t namelen;
4686       bfd_size_type len;
4687       char *s_name;
4688
4689       namelen = strlen (stub_sec_prefix);
4690       len = namelen + sizeof (STUB_SUFFIX);
4691       s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4692       if (s_name == NULL)
4693         return NULL;
4694
4695       memcpy (s_name, stub_sec_prefix, namelen);
4696       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4697       *stub_sec_p = (*htab->add_stub_section) (s_name, out_sec, link_sec,
4698                                                align);
4699       if (*stub_sec_p == NULL)
4700         return NULL;
4701
4702       out_sec->flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
4703                         | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY
4704                         | SEC_KEEP;
4705     }
4706
4707   if (!dedicated_output_section)
4708     htab->stub_group[section->id].stub_sec = *stub_sec_p;
4709
4710   if (link_sec_p)
4711     *link_sec_p = link_sec;
4712
4713   return *stub_sec_p;
4714 }
4715
4716 /* Add a new stub entry to the stub hash.  Not all fields of the new
4717    stub entry are initialised.  */
4718
4719 static struct elf32_arm_stub_hash_entry *
4720 elf32_arm_add_stub (const char *stub_name, asection *section,
4721                     struct elf32_arm_link_hash_table *htab,
4722                     enum elf32_arm_stub_type stub_type)
4723 {
4724   asection *link_sec;
4725   asection *stub_sec;
4726   struct elf32_arm_stub_hash_entry *stub_entry;
4727
4728   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab,
4729                                                 stub_type);
4730   if (stub_sec == NULL)
4731     return NULL;
4732
4733   /* Enter this entry into the linker stub hash table.  */
4734   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4735                                      TRUE, FALSE);
4736   if (stub_entry == NULL)
4737     {
4738       if (section == NULL)
4739         section = stub_sec;
4740       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4741                           section->owner, stub_name);
4742       return NULL;
4743     }
4744
4745   stub_entry->stub_sec = stub_sec;
4746   stub_entry->stub_offset = (bfd_vma) -1;
4747   stub_entry->id_sec = link_sec;
4748
4749   return stub_entry;
4750 }
4751
4752 /* Store an Arm insn into an output section not processed by
4753    elf32_arm_write_section.  */
4754
4755 static void
4756 put_arm_insn (struct elf32_arm_link_hash_table * htab,
4757               bfd * output_bfd, bfd_vma val, void * ptr)
4758 {
4759   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4760     bfd_putl32 (val, ptr);
4761   else
4762     bfd_putb32 (val, ptr);
4763 }
4764
4765 /* Store a 16-bit Thumb insn into an output section not processed by
4766    elf32_arm_write_section.  */
4767
4768 static void
4769 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4770                 bfd * output_bfd, bfd_vma val, void * ptr)
4771 {
4772   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4773     bfd_putl16 (val, ptr);
4774   else
4775     bfd_putb16 (val, ptr);
4776 }
4777
4778 /* Store a Thumb2 insn into an output section not processed by
4779    elf32_arm_write_section.  */
4780
4781 static void
4782 put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4783                  bfd * output_bfd, bfd_vma val, bfd_byte * ptr)
4784 {
4785   /* T2 instructions are 16-bit streamed.  */
4786   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4787     {
4788       bfd_putl16 ((val >> 16) & 0xffff, ptr);
4789       bfd_putl16 ((val & 0xffff), ptr + 2);
4790     }
4791   else
4792     {
4793       bfd_putb16 ((val >> 16) & 0xffff, ptr);
4794       bfd_putb16 ((val & 0xffff), ptr + 2);
4795     }
4796 }
4797
4798 /* If it's possible to change R_TYPE to a more efficient access
4799    model, return the new reloc type.  */
4800
4801 static unsigned
4802 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4803                           struct elf_link_hash_entry *h)
4804 {
4805   int is_local = (h == NULL);
4806
4807   if (bfd_link_pic (info)
4808       || (h && h->root.type == bfd_link_hash_undefweak))
4809     return r_type;
4810
4811   /* We do not support relaxations for Old TLS models.  */
4812   switch (r_type)
4813     {
4814     case R_ARM_TLS_GOTDESC:
4815     case R_ARM_TLS_CALL:
4816     case R_ARM_THM_TLS_CALL:
4817     case R_ARM_TLS_DESCSEQ:
4818     case R_ARM_THM_TLS_DESCSEQ:
4819       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4820     }
4821
4822   return r_type;
4823 }
4824
4825 static bfd_reloc_status_type elf32_arm_final_link_relocate
4826   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4827    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4828    const char *, unsigned char, enum arm_st_branch_type,
4829    struct elf_link_hash_entry *, bfd_boolean *, char **);
4830
4831 static unsigned int
4832 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4833 {
4834   switch (stub_type)
4835     {
4836     case arm_stub_a8_veneer_b_cond:
4837     case arm_stub_a8_veneer_b:
4838     case arm_stub_a8_veneer_bl:
4839       return 2;
4840
4841     case arm_stub_long_branch_any_any:
4842     case arm_stub_long_branch_v4t_arm_thumb:
4843     case arm_stub_long_branch_thumb_only:
4844     case arm_stub_long_branch_thumb2_only:
4845     case arm_stub_long_branch_thumb2_only_pure:
4846     case arm_stub_long_branch_v4t_thumb_thumb:
4847     case arm_stub_long_branch_v4t_thumb_arm:
4848     case arm_stub_short_branch_v4t_thumb_arm:
4849     case arm_stub_long_branch_any_arm_pic:
4850     case arm_stub_long_branch_any_thumb_pic:
4851     case arm_stub_long_branch_v4t_thumb_thumb_pic:
4852     case arm_stub_long_branch_v4t_arm_thumb_pic:
4853     case arm_stub_long_branch_v4t_thumb_arm_pic:
4854     case arm_stub_long_branch_thumb_only_pic:
4855     case arm_stub_long_branch_any_tls_pic:
4856     case arm_stub_long_branch_v4t_thumb_tls_pic:
4857     case arm_stub_cmse_branch_thumb_only:
4858     case arm_stub_a8_veneer_blx:
4859       return 4;
4860
4861     case arm_stub_long_branch_arm_nacl:
4862     case arm_stub_long_branch_arm_nacl_pic:
4863       return 16;
4864
4865     default:
4866       abort ();  /* Should be unreachable.  */
4867     }
4868 }
4869
4870 /* Returns whether stubs of type STUB_TYPE take over the symbol they are
4871    veneering (TRUE) or have their own symbol (FALSE).  */
4872
4873 static bfd_boolean
4874 arm_stub_sym_claimed (enum elf32_arm_stub_type stub_type)
4875 {
4876   if (stub_type >= max_stub_type)
4877     abort ();  /* Should be unreachable.  */
4878
4879   switch (stub_type)
4880     {
4881     case arm_stub_cmse_branch_thumb_only:
4882       return TRUE;
4883
4884     default:
4885       return FALSE;
4886     }
4887
4888   abort ();  /* Should be unreachable.  */
4889 }
4890
4891 /* Returns the padding needed for the dedicated section used stubs of type
4892    STUB_TYPE.  */
4893
4894 static int
4895 arm_dedicated_stub_section_padding (enum elf32_arm_stub_type stub_type)
4896 {
4897   if (stub_type >= max_stub_type)
4898     abort ();  /* Should be unreachable.  */
4899
4900   switch (stub_type)
4901     {
4902     case arm_stub_cmse_branch_thumb_only:
4903       return 32;
4904
4905     default:
4906       return 0;
4907     }
4908
4909   abort ();  /* Should be unreachable.  */
4910 }
4911
4912 /* If veneers of type STUB_TYPE should go in a dedicated output section,
4913    returns the address of the hash table field in HTAB holding the offset at
4914    which new veneers should be layed out in the stub section.  */
4915
4916 static bfd_vma*
4917 arm_new_stubs_start_offset_ptr (struct elf32_arm_link_hash_table *htab,
4918                                 enum elf32_arm_stub_type stub_type)
4919 {
4920   switch (stub_type)
4921     {
4922     case arm_stub_cmse_branch_thumb_only:
4923       return &htab->new_cmse_stub_offset;
4924
4925     default:
4926       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4927       return NULL;
4928     }
4929 }
4930
4931 static bfd_boolean
4932 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4933                     void * in_arg)
4934 {
4935 #define MAXRELOCS 3
4936   bfd_boolean removed_sg_veneer;
4937   struct elf32_arm_stub_hash_entry *stub_entry;
4938   struct elf32_arm_link_hash_table *globals;
4939   struct bfd_link_info *info;
4940   asection *stub_sec;
4941   bfd *stub_bfd;
4942   bfd_byte *loc;
4943   bfd_vma sym_value;
4944   int template_size;
4945   int size;
4946   const insn_sequence *template_sequence;
4947   int i;
4948   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4949   int stub_reloc_offset[MAXRELOCS] = {0, 0};
4950   int nrelocs = 0;
4951   int just_allocated = 0;
4952
4953   /* Massage our args to the form they really have.  */
4954   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4955   info = (struct bfd_link_info *) in_arg;
4956
4957   globals = elf32_arm_hash_table (info);
4958   if (globals == NULL)
4959     return FALSE;
4960
4961   stub_sec = stub_entry->stub_sec;
4962
4963   if ((globals->fix_cortex_a8 < 0)
4964       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4965     /* We have to do less-strictly-aligned fixes last.  */
4966     return TRUE;
4967
4968   /* Assign a slot at the end of section if none assigned yet.  */
4969   if (stub_entry->stub_offset == (bfd_vma) -1)
4970     {
4971       stub_entry->stub_offset = stub_sec->size;
4972       just_allocated = 1;
4973     }
4974   loc = stub_sec->contents + stub_entry->stub_offset;
4975
4976   stub_bfd = stub_sec->owner;
4977
4978   /* This is the address of the stub destination.  */
4979   sym_value = (stub_entry->target_value
4980                + stub_entry->target_section->output_offset
4981                + stub_entry->target_section->output_section->vma);
4982
4983   template_sequence = stub_entry->stub_template;
4984   template_size = stub_entry->stub_template_size;
4985
4986   size = 0;
4987   for (i = 0; i < template_size; i++)
4988     {
4989       switch (template_sequence[i].type)
4990         {
4991         case THUMB16_TYPE:
4992           {
4993             bfd_vma data = (bfd_vma) template_sequence[i].data;
4994             if (template_sequence[i].reloc_addend != 0)
4995               {
4996                 /* We've borrowed the reloc_addend field to mean we should
4997                    insert a condition code into this (Thumb-1 branch)
4998                    instruction.  See THUMB16_BCOND_INSN.  */
4999                 BFD_ASSERT ((data & 0xff00) == 0xd000);
5000                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
5001               }
5002             bfd_put_16 (stub_bfd, data, loc + size);
5003             size += 2;
5004           }
5005           break;
5006
5007         case THUMB32_TYPE:
5008           bfd_put_16 (stub_bfd,
5009                       (template_sequence[i].data >> 16) & 0xffff,
5010                       loc + size);
5011           bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
5012                       loc + size + 2);
5013           if (template_sequence[i].r_type != R_ARM_NONE)
5014             {
5015               stub_reloc_idx[nrelocs] = i;
5016               stub_reloc_offset[nrelocs++] = size;
5017             }
5018           size += 4;
5019           break;
5020
5021         case ARM_TYPE:
5022           bfd_put_32 (stub_bfd, template_sequence[i].data,
5023                       loc + size);
5024           /* Handle cases where the target is encoded within the
5025              instruction.  */
5026           if (template_sequence[i].r_type == R_ARM_JUMP24)
5027             {
5028               stub_reloc_idx[nrelocs] = i;
5029               stub_reloc_offset[nrelocs++] = size;
5030             }
5031           size += 4;
5032           break;
5033
5034         case DATA_TYPE:
5035           bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
5036           stub_reloc_idx[nrelocs] = i;
5037           stub_reloc_offset[nrelocs++] = size;
5038           size += 4;
5039           break;
5040
5041         default:
5042           BFD_FAIL ();
5043           return FALSE;
5044         }
5045     }
5046
5047   if (just_allocated)
5048     stub_sec->size += size;
5049
5050   /* Stub size has already been computed in arm_size_one_stub. Check
5051      consistency.  */
5052   BFD_ASSERT (size == stub_entry->stub_size);
5053
5054   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
5055   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
5056     sym_value |= 1;
5057
5058   /* Assume non empty slots have at least one and at most MAXRELOCS entries
5059      to relocate in each stub.  */
5060   removed_sg_veneer =
5061     (size == 0 && stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
5062   BFD_ASSERT (removed_sg_veneer || (nrelocs != 0 && nrelocs <= MAXRELOCS));
5063
5064   for (i = 0; i < nrelocs; i++)
5065     {
5066       Elf_Internal_Rela rel;
5067       bfd_boolean unresolved_reloc;
5068       char *error_message;
5069       bfd_vma points_to =
5070         sym_value + template_sequence[stub_reloc_idx[i]].reloc_addend;
5071
5072       rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
5073       rel.r_info = ELF32_R_INFO (0,
5074                                  template_sequence[stub_reloc_idx[i]].r_type);
5075       rel.r_addend = 0;
5076
5077       if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
5078         /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
5079            template should refer back to the instruction after the original
5080            branch.  We use target_section as Cortex-A8 erratum workaround stubs
5081            are only generated when both source and target are in the same
5082            section.  */
5083         points_to = stub_entry->target_section->output_section->vma
5084                     + stub_entry->target_section->output_offset
5085                     + stub_entry->source_value;
5086
5087       elf32_arm_final_link_relocate (elf32_arm_howto_from_type
5088           (template_sequence[stub_reloc_idx[i]].r_type),
5089            stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
5090            points_to, info, stub_entry->target_section, "", STT_FUNC,
5091            stub_entry->branch_type,
5092            (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
5093            &error_message);
5094     }
5095
5096   return TRUE;
5097 #undef MAXRELOCS
5098 }
5099
5100 /* Calculate the template, template size and instruction size for a stub.
5101    Return value is the instruction size.  */
5102
5103 static unsigned int
5104 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
5105                              const insn_sequence **stub_template,
5106                              int *stub_template_size)
5107 {
5108   const insn_sequence *template_sequence = NULL;
5109   int template_size = 0, i;
5110   unsigned int size;
5111
5112   template_sequence = stub_definitions[stub_type].template_sequence;
5113   if (stub_template)
5114     *stub_template = template_sequence;
5115
5116   template_size = stub_definitions[stub_type].template_size;
5117   if (stub_template_size)
5118     *stub_template_size = template_size;
5119
5120   size = 0;
5121   for (i = 0; i < template_size; i++)
5122     {
5123       switch (template_sequence[i].type)
5124         {
5125         case THUMB16_TYPE:
5126           size += 2;
5127           break;
5128
5129         case ARM_TYPE:
5130         case THUMB32_TYPE:
5131         case DATA_TYPE:
5132           size += 4;
5133           break;
5134
5135         default:
5136           BFD_FAIL ();
5137           return 0;
5138         }
5139     }
5140
5141   return size;
5142 }
5143
5144 /* As above, but don't actually build the stub.  Just bump offset so
5145    we know stub section sizes.  */
5146
5147 static bfd_boolean
5148 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
5149                    void *in_arg ATTRIBUTE_UNUSED)
5150 {
5151   struct elf32_arm_stub_hash_entry *stub_entry;
5152   const insn_sequence *template_sequence;
5153   int template_size, size;
5154
5155   /* Massage our args to the form they really have.  */
5156   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
5157
5158   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
5159              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
5160
5161   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
5162                                       &template_size);
5163
5164   /* Initialized to -1.  Null size indicates an empty slot full of zeros.  */
5165   if (stub_entry->stub_template_size)
5166     {
5167       stub_entry->stub_size = size;
5168       stub_entry->stub_template = template_sequence;
5169       stub_entry->stub_template_size = template_size;
5170     }
5171
5172   /* Already accounted for.  */
5173   if (stub_entry->stub_offset != (bfd_vma) -1)
5174     return TRUE;
5175
5176   size = (size + 7) & ~7;
5177   stub_entry->stub_sec->size += size;
5178
5179   return TRUE;
5180 }
5181
5182 /* External entry points for sizing and building linker stubs.  */
5183
5184 /* Set up various things so that we can make a list of input sections
5185    for each output section included in the link.  Returns -1 on error,
5186    0 when no stubs will be needed, and 1 on success.  */
5187
5188 int
5189 elf32_arm_setup_section_lists (bfd *output_bfd,
5190                                struct bfd_link_info *info)
5191 {
5192   bfd *input_bfd;
5193   unsigned int bfd_count;
5194   unsigned int top_id, top_index;
5195   asection *section;
5196   asection **input_list, **list;
5197   bfd_size_type amt;
5198   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5199
5200   if (htab == NULL)
5201     return 0;
5202   if (! is_elf_hash_table (htab))
5203     return 0;
5204
5205   /* Count the number of input BFDs and find the top input section id.  */
5206   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
5207        input_bfd != NULL;
5208        input_bfd = input_bfd->link.next)
5209     {
5210       bfd_count += 1;
5211       for (section = input_bfd->sections;
5212            section != NULL;
5213            section = section->next)
5214         {
5215           if (top_id < section->id)
5216             top_id = section->id;
5217         }
5218     }
5219   htab->bfd_count = bfd_count;
5220
5221   amt = sizeof (struct map_stub) * (top_id + 1);
5222   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
5223   if (htab->stub_group == NULL)
5224     return -1;
5225   htab->top_id = top_id;
5226
5227   /* We can't use output_bfd->section_count here to find the top output
5228      section index as some sections may have been removed, and
5229      _bfd_strip_section_from_output doesn't renumber the indices.  */
5230   for (section = output_bfd->sections, top_index = 0;
5231        section != NULL;
5232        section = section->next)
5233     {
5234       if (top_index < section->index)
5235         top_index = section->index;
5236     }
5237
5238   htab->top_index = top_index;
5239   amt = sizeof (asection *) * (top_index + 1);
5240   input_list = (asection **) bfd_malloc (amt);
5241   htab->input_list = input_list;
5242   if (input_list == NULL)
5243     return -1;
5244
5245   /* For sections we aren't interested in, mark their entries with a
5246      value we can check later.  */
5247   list = input_list + top_index;
5248   do
5249     *list = bfd_abs_section_ptr;
5250   while (list-- != input_list);
5251
5252   for (section = output_bfd->sections;
5253        section != NULL;
5254        section = section->next)
5255     {
5256       if ((section->flags & SEC_CODE) != 0)
5257         input_list[section->index] = NULL;
5258     }
5259
5260   return 1;
5261 }
5262
5263 /* The linker repeatedly calls this function for each input section,
5264    in the order that input sections are linked into output sections.
5265    Build lists of input sections to determine groupings between which
5266    we may insert linker stubs.  */
5267
5268 void
5269 elf32_arm_next_input_section (struct bfd_link_info *info,
5270                               asection *isec)
5271 {
5272   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5273
5274   if (htab == NULL)
5275     return;
5276
5277   if (isec->output_section->index <= htab->top_index)
5278     {
5279       asection **list = htab->input_list + isec->output_section->index;
5280
5281       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
5282         {
5283           /* Steal the link_sec pointer for our list.  */
5284 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
5285           /* This happens to make the list in reverse order,
5286              which we reverse later.  */
5287           PREV_SEC (isec) = *list;
5288           *list = isec;
5289         }
5290     }
5291 }
5292
5293 /* See whether we can group stub sections together.  Grouping stub
5294    sections may result in fewer stubs.  More importantly, we need to
5295    put all .init* and .fini* stubs at the end of the .init or
5296    .fini output sections respectively, because glibc splits the
5297    _init and _fini functions into multiple parts.  Putting a stub in
5298    the middle of a function is not a good idea.  */
5299
5300 static void
5301 group_sections (struct elf32_arm_link_hash_table *htab,
5302                 bfd_size_type stub_group_size,
5303                 bfd_boolean stubs_always_after_branch)
5304 {
5305   asection **list = htab->input_list;
5306
5307   do
5308     {
5309       asection *tail = *list;
5310       asection *head;
5311
5312       if (tail == bfd_abs_section_ptr)
5313         continue;
5314
5315       /* Reverse the list: we must avoid placing stubs at the
5316          beginning of the section because the beginning of the text
5317          section may be required for an interrupt vector in bare metal
5318          code.  */
5319 #define NEXT_SEC PREV_SEC
5320       head = NULL;
5321       while (tail != NULL)
5322         {
5323           /* Pop from tail.  */
5324           asection *item = tail;
5325           tail = PREV_SEC (item);
5326
5327           /* Push on head.  */
5328           NEXT_SEC (item) = head;
5329           head = item;
5330         }
5331
5332       while (head != NULL)
5333         {
5334           asection *curr;
5335           asection *next;
5336           bfd_vma stub_group_start = head->output_offset;
5337           bfd_vma end_of_next;
5338
5339           curr = head;
5340           while (NEXT_SEC (curr) != NULL)
5341             {
5342               next = NEXT_SEC (curr);
5343               end_of_next = next->output_offset + next->size;
5344               if (end_of_next - stub_group_start >= stub_group_size)
5345                 /* End of NEXT is too far from start, so stop.  */
5346                 break;
5347               /* Add NEXT to the group.  */
5348               curr = next;
5349             }
5350
5351           /* OK, the size from the start to the start of CURR is less
5352              than stub_group_size and thus can be handled by one stub
5353              section.  (Or the head section is itself larger than
5354              stub_group_size, in which case we may be toast.)
5355              We should really be keeping track of the total size of
5356              stubs added here, as stubs contribute to the final output
5357              section size.  */
5358           do
5359             {
5360               next = NEXT_SEC (head);
5361               /* Set up this stub group.  */
5362               htab->stub_group[head->id].link_sec = curr;
5363             }
5364           while (head != curr && (head = next) != NULL);
5365
5366           /* But wait, there's more!  Input sections up to stub_group_size
5367              bytes after the stub section can be handled by it too.  */
5368           if (!stubs_always_after_branch)
5369             {
5370               stub_group_start = curr->output_offset + curr->size;
5371
5372               while (next != NULL)
5373                 {
5374                   end_of_next = next->output_offset + next->size;
5375                   if (end_of_next - stub_group_start >= stub_group_size)
5376                     /* End of NEXT is too far from stubs, so stop.  */
5377                     break;
5378                   /* Add NEXT to the stub group.  */
5379                   head = next;
5380                   next = NEXT_SEC (head);
5381                   htab->stub_group[head->id].link_sec = curr;
5382                 }
5383             }
5384           head = next;
5385         }
5386     }
5387   while (list++ != htab->input_list + htab->top_index);
5388
5389   free (htab->input_list);
5390 #undef PREV_SEC
5391 #undef NEXT_SEC
5392 }
5393
5394 /* Comparison function for sorting/searching relocations relating to Cortex-A8
5395    erratum fix.  */
5396
5397 static int
5398 a8_reloc_compare (const void *a, const void *b)
5399 {
5400   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
5401   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
5402
5403   if (ra->from < rb->from)
5404     return -1;
5405   else if (ra->from > rb->from)
5406     return 1;
5407   else
5408     return 0;
5409 }
5410
5411 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
5412                                                     const char *, char **);
5413
5414 /* Helper function to scan code for sequences which might trigger the Cortex-A8
5415    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
5416    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
5417    otherwise.  */
5418
5419 static bfd_boolean
5420 cortex_a8_erratum_scan (bfd *input_bfd,
5421                         struct bfd_link_info *info,
5422                         struct a8_erratum_fix **a8_fixes_p,
5423                         unsigned int *num_a8_fixes_p,
5424                         unsigned int *a8_fix_table_size_p,
5425                         struct a8_erratum_reloc *a8_relocs,
5426                         unsigned int num_a8_relocs,
5427                         unsigned prev_num_a8_fixes,
5428                         bfd_boolean *stub_changed_p)
5429 {
5430   asection *section;
5431   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5432   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
5433   unsigned int num_a8_fixes = *num_a8_fixes_p;
5434   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
5435
5436   if (htab == NULL)
5437     return FALSE;
5438
5439   for (section = input_bfd->sections;
5440        section != NULL;
5441        section = section->next)
5442     {
5443       bfd_byte *contents = NULL;
5444       struct _arm_elf_section_data *sec_data;
5445       unsigned int span;
5446       bfd_vma base_vma;
5447
5448       if (elf_section_type (section) != SHT_PROGBITS
5449           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
5450           || (section->flags & SEC_EXCLUDE) != 0
5451           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5452           || (section->output_section == bfd_abs_section_ptr))
5453         continue;
5454
5455       base_vma = section->output_section->vma + section->output_offset;
5456
5457       if (elf_section_data (section)->this_hdr.contents != NULL)
5458         contents = elf_section_data (section)->this_hdr.contents;
5459       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
5460         return TRUE;
5461
5462       sec_data = elf32_arm_section_data (section);
5463
5464       for (span = 0; span < sec_data->mapcount; span++)
5465         {
5466           unsigned int span_start = sec_data->map[span].vma;
5467           unsigned int span_end = (span == sec_data->mapcount - 1)
5468             ? section->size : sec_data->map[span + 1].vma;
5469           unsigned int i;
5470           char span_type = sec_data->map[span].type;
5471           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
5472
5473           if (span_type != 't')
5474             continue;
5475
5476           /* Span is entirely within a single 4KB region: skip scanning.  */
5477           if (((base_vma + span_start) & ~0xfff)
5478               == ((base_vma + span_end) & ~0xfff))
5479             continue;
5480
5481           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
5482
5483                * The opcode is BLX.W, BL.W, B.W, Bcc.W
5484                * The branch target is in the same 4KB region as the
5485                  first half of the branch.
5486                * The instruction before the branch is a 32-bit
5487                  length non-branch instruction.  */
5488           for (i = span_start; i < span_end;)
5489             {
5490               unsigned int insn = bfd_getl16 (&contents[i]);
5491               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
5492               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
5493
5494               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
5495                 insn_32bit = TRUE;
5496
5497               if (insn_32bit)
5498                 {
5499                   /* Load the rest of the insn (in manual-friendly order).  */
5500                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
5501
5502                   /* Encoding T4: B<c>.W.  */
5503                   is_b = (insn & 0xf800d000) == 0xf0009000;
5504                   /* Encoding T1: BL<c>.W.  */
5505                   is_bl = (insn & 0xf800d000) == 0xf000d000;
5506                   /* Encoding T2: BLX<c>.W.  */
5507                   is_blx = (insn & 0xf800d000) == 0xf000c000;
5508                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
5509                   is_bcc = (insn & 0xf800d000) == 0xf0008000
5510                            && (insn & 0x07f00000) != 0x03800000;
5511                 }
5512
5513               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
5514
5515               if (((base_vma + i) & 0xfff) == 0xffe
5516                   && insn_32bit
5517                   && is_32bit_branch
5518                   && last_was_32bit
5519                   && ! last_was_branch)
5520                 {
5521                   bfd_signed_vma offset = 0;
5522                   bfd_boolean force_target_arm = FALSE;
5523                   bfd_boolean force_target_thumb = FALSE;
5524                   bfd_vma target;
5525                   enum elf32_arm_stub_type stub_type = arm_stub_none;
5526                   struct a8_erratum_reloc key, *found;
5527                   bfd_boolean use_plt = FALSE;
5528
5529                   key.from = base_vma + i;
5530                   found = (struct a8_erratum_reloc *)
5531                       bsearch (&key, a8_relocs, num_a8_relocs,
5532                                sizeof (struct a8_erratum_reloc),
5533                                &a8_reloc_compare);
5534
5535                   if (found)
5536                     {
5537                       char *error_message = NULL;
5538                       struct elf_link_hash_entry *entry;
5539
5540                       /* We don't care about the error returned from this
5541                          function, only if there is glue or not.  */
5542                       entry = find_thumb_glue (info, found->sym_name,
5543                                                &error_message);
5544
5545                       if (entry)
5546                         found->non_a8_stub = TRUE;
5547
5548                       /* Keep a simpler condition, for the sake of clarity.  */
5549                       if (htab->root.splt != NULL && found->hash != NULL
5550                           && found->hash->root.plt.offset != (bfd_vma) -1)
5551                         use_plt = TRUE;
5552
5553                       if (found->r_type == R_ARM_THM_CALL)
5554                         {
5555                           if (found->branch_type == ST_BRANCH_TO_ARM
5556                               || use_plt)
5557                             force_target_arm = TRUE;
5558                           else
5559                             force_target_thumb = TRUE;
5560                         }
5561                     }
5562
5563                   /* Check if we have an offending branch instruction.  */
5564
5565                   if (found && found->non_a8_stub)
5566                     /* We've already made a stub for this instruction, e.g.
5567                        it's a long branch or a Thumb->ARM stub.  Assume that
5568                        stub will suffice to work around the A8 erratum (see
5569                        setting of always_after_branch above).  */
5570                     ;
5571                   else if (is_bcc)
5572                     {
5573                       offset = (insn & 0x7ff) << 1;
5574                       offset |= (insn & 0x3f0000) >> 4;
5575                       offset |= (insn & 0x2000) ? 0x40000 : 0;
5576                       offset |= (insn & 0x800) ? 0x80000 : 0;
5577                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
5578                       if (offset & 0x100000)
5579                         offset |= ~ ((bfd_signed_vma) 0xfffff);
5580                       stub_type = arm_stub_a8_veneer_b_cond;
5581                     }
5582                   else if (is_b || is_bl || is_blx)
5583                     {
5584                       int s = (insn & 0x4000000) != 0;
5585                       int j1 = (insn & 0x2000) != 0;
5586                       int j2 = (insn & 0x800) != 0;
5587                       int i1 = !(j1 ^ s);
5588                       int i2 = !(j2 ^ s);
5589
5590                       offset = (insn & 0x7ff) << 1;
5591                       offset |= (insn & 0x3ff0000) >> 4;
5592                       offset |= i2 << 22;
5593                       offset |= i1 << 23;
5594                       offset |= s << 24;
5595                       if (offset & 0x1000000)
5596                         offset |= ~ ((bfd_signed_vma) 0xffffff);
5597
5598                       if (is_blx)
5599                         offset &= ~ ((bfd_signed_vma) 3);
5600
5601                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
5602                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5603                     }
5604
5605                   if (stub_type != arm_stub_none)
5606                     {
5607                       bfd_vma pc_for_insn = base_vma + i + 4;
5608
5609                       /* The original instruction is a BL, but the target is
5610                          an ARM instruction.  If we were not making a stub,
5611                          the BL would have been converted to a BLX.  Use the
5612                          BLX stub instead in that case.  */
5613                       if (htab->use_blx && force_target_arm
5614                           && stub_type == arm_stub_a8_veneer_bl)
5615                         {
5616                           stub_type = arm_stub_a8_veneer_blx;
5617                           is_blx = TRUE;
5618                           is_bl = FALSE;
5619                         }
5620                       /* Conversely, if the original instruction was
5621                          BLX but the target is Thumb mode, use the BL
5622                          stub.  */
5623                       else if (force_target_thumb
5624                                && stub_type == arm_stub_a8_veneer_blx)
5625                         {
5626                           stub_type = arm_stub_a8_veneer_bl;
5627                           is_blx = FALSE;
5628                           is_bl = TRUE;
5629                         }
5630
5631                       if (is_blx)
5632                         pc_for_insn &= ~ ((bfd_vma) 3);
5633
5634                       /* If we found a relocation, use the proper destination,
5635                          not the offset in the (unrelocated) instruction.
5636                          Note this is always done if we switched the stub type
5637                          above.  */
5638                       if (found)
5639                         offset =
5640                           (bfd_signed_vma) (found->destination - pc_for_insn);
5641
5642                       /* If the stub will use a Thumb-mode branch to a
5643                          PLT target, redirect it to the preceding Thumb
5644                          entry point.  */
5645                       if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5646                         offset -= PLT_THUMB_STUB_SIZE;
5647
5648                       target = pc_for_insn + offset;
5649
5650                       /* The BLX stub is ARM-mode code.  Adjust the offset to
5651                          take the different PC value (+8 instead of +4) into
5652                          account.  */
5653                       if (stub_type == arm_stub_a8_veneer_blx)
5654                         offset += 4;
5655
5656                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5657                         {
5658                           char *stub_name = NULL;
5659
5660                           if (num_a8_fixes == a8_fix_table_size)
5661                             {
5662                               a8_fix_table_size *= 2;
5663                               a8_fixes = (struct a8_erratum_fix *)
5664                                   bfd_realloc (a8_fixes,
5665                                                sizeof (struct a8_erratum_fix)
5666                                                * a8_fix_table_size);
5667                             }
5668
5669                           if (num_a8_fixes < prev_num_a8_fixes)
5670                             {
5671                               /* If we're doing a subsequent scan,
5672                                  check if we've found the same fix as
5673                                  before, and try and reuse the stub
5674                                  name.  */
5675                               stub_name = a8_fixes[num_a8_fixes].stub_name;
5676                               if ((a8_fixes[num_a8_fixes].section != section)
5677                                   || (a8_fixes[num_a8_fixes].offset != i))
5678                                 {
5679                                   free (stub_name);
5680                                   stub_name = NULL;
5681                                   *stub_changed_p = TRUE;
5682                                 }
5683                             }
5684
5685                           if (!stub_name)
5686                             {
5687                               stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5688                               if (stub_name != NULL)
5689                                 sprintf (stub_name, "%x:%x", section->id, i);
5690                             }
5691
5692                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5693                           a8_fixes[num_a8_fixes].section = section;
5694                           a8_fixes[num_a8_fixes].offset = i;
5695                           a8_fixes[num_a8_fixes].target_offset =
5696                             target - base_vma;
5697                           a8_fixes[num_a8_fixes].orig_insn = insn;
5698                           a8_fixes[num_a8_fixes].stub_name = stub_name;
5699                           a8_fixes[num_a8_fixes].stub_type = stub_type;
5700                           a8_fixes[num_a8_fixes].branch_type =
5701                             is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5702
5703                           num_a8_fixes++;
5704                         }
5705                     }
5706                 }
5707
5708               i += insn_32bit ? 4 : 2;
5709               last_was_32bit = insn_32bit;
5710               last_was_branch = is_32bit_branch;
5711             }
5712         }
5713
5714       if (elf_section_data (section)->this_hdr.contents == NULL)
5715         free (contents);
5716     }
5717
5718   *a8_fixes_p = a8_fixes;
5719   *num_a8_fixes_p = num_a8_fixes;
5720   *a8_fix_table_size_p = a8_fix_table_size;
5721
5722   return FALSE;
5723 }
5724
5725 /* Create or update a stub entry depending on whether the stub can already be
5726    found in HTAB.  The stub is identified by:
5727    - its type STUB_TYPE
5728    - its source branch (note that several can share the same stub) whose
5729      section and relocation (if any) are given by SECTION and IRELA
5730      respectively
5731    - its target symbol whose input section, hash, name, value and branch type
5732      are given in SYM_SEC, HASH, SYM_NAME, SYM_VALUE and BRANCH_TYPE
5733      respectively
5734
5735    If found, the value of the stub's target symbol is updated from SYM_VALUE
5736    and *NEW_STUB is set to FALSE.  Otherwise, *NEW_STUB is set to
5737    TRUE and the stub entry is initialized.
5738
5739    Returns the stub that was created or updated, or NULL if an error
5740    occurred.  */
5741
5742 static struct elf32_arm_stub_hash_entry *
5743 elf32_arm_create_stub (struct elf32_arm_link_hash_table *htab,
5744                        enum elf32_arm_stub_type stub_type, asection *section,
5745                        Elf_Internal_Rela *irela, asection *sym_sec,
5746                        struct elf32_arm_link_hash_entry *hash, char *sym_name,
5747                        bfd_vma sym_value, enum arm_st_branch_type branch_type,
5748                        bfd_boolean *new_stub)
5749 {
5750   const asection *id_sec;
5751   char *stub_name;
5752   struct elf32_arm_stub_hash_entry *stub_entry;
5753   unsigned int r_type;
5754   bfd_boolean sym_claimed = arm_stub_sym_claimed (stub_type);
5755
5756   BFD_ASSERT (stub_type != arm_stub_none);
5757   *new_stub = FALSE;
5758
5759   if (sym_claimed)
5760     stub_name = sym_name;
5761   else
5762     {
5763       BFD_ASSERT (irela);
5764       BFD_ASSERT (section);
5765       BFD_ASSERT (section->id <= htab->top_id);
5766
5767       /* Support for grouping stub sections.  */
5768       id_sec = htab->stub_group[section->id].link_sec;
5769
5770       /* Get the name of this stub.  */
5771       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela,
5772                                        stub_type);
5773       if (!stub_name)
5774         return NULL;
5775     }
5776
5777   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name, FALSE,
5778                                      FALSE);
5779   /* The proper stub has already been created, just update its value.  */
5780   if (stub_entry != NULL)
5781     {
5782       if (!sym_claimed)
5783         free (stub_name);
5784       stub_entry->target_value = sym_value;
5785       return stub_entry;
5786     }
5787
5788   stub_entry = elf32_arm_add_stub (stub_name, section, htab, stub_type);
5789   if (stub_entry == NULL)
5790     {
5791       if (!sym_claimed)
5792         free (stub_name);
5793       return NULL;
5794     }
5795
5796   stub_entry->target_value = sym_value;
5797   stub_entry->target_section = sym_sec;
5798   stub_entry->stub_type = stub_type;
5799   stub_entry->h = hash;
5800   stub_entry->branch_type = branch_type;
5801
5802   if (sym_claimed)
5803     stub_entry->output_name = sym_name;
5804   else
5805     {
5806       if (sym_name == NULL)
5807         sym_name = "unnamed";
5808       stub_entry->output_name = (char *)
5809         bfd_alloc (htab->stub_bfd, sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5810                                    + strlen (sym_name));
5811       if (stub_entry->output_name == NULL)
5812         {
5813           free (stub_name);
5814           return NULL;
5815         }
5816
5817       /* For historical reasons, use the existing names for ARM-to-Thumb and
5818          Thumb-to-ARM stubs.  */
5819       r_type = ELF32_R_TYPE (irela->r_info);
5820       if ((r_type == (unsigned int) R_ARM_THM_CALL
5821            || r_type == (unsigned int) R_ARM_THM_JUMP24
5822            || r_type == (unsigned int) R_ARM_THM_JUMP19)
5823           && branch_type == ST_BRANCH_TO_ARM)
5824         sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5825       else if ((r_type == (unsigned int) R_ARM_CALL
5826                 || r_type == (unsigned int) R_ARM_JUMP24)
5827                && branch_type == ST_BRANCH_TO_THUMB)
5828         sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5829       else
5830         sprintf (stub_entry->output_name, STUB_ENTRY_NAME, sym_name);
5831     }
5832
5833   *new_stub = TRUE;
5834   return stub_entry;
5835 }
5836
5837 /* Scan symbols in INPUT_BFD to identify secure entry functions needing a
5838    gateway veneer to transition from non secure to secure state and create them
5839    accordingly.
5840
5841    "ARMv8-M Security Extensions: Requirements on Development Tools" document
5842    defines the conditions that govern Secure Gateway veneer creation for a
5843    given symbol <SYM> as follows:
5844    - it has function type
5845    - it has non local binding
5846    - a symbol named __acle_se_<SYM> (called special symbol) exists with the
5847      same type, binding and value as <SYM> (called normal symbol).
5848    An entry function can handle secure state transition itself in which case
5849    its special symbol would have a different value from the normal symbol.
5850
5851    OUT_ATTR gives the output attributes, SYM_HASHES the symbol index to hash
5852    entry mapping while HTAB gives the name to hash entry mapping.
5853    *CMSE_STUB_CREATED is increased by the number of secure gateway veneer
5854    created.
5855
5856    The return value gives whether a stub failed to be allocated.  */
5857
5858 static bfd_boolean
5859 cmse_scan (bfd *input_bfd, struct elf32_arm_link_hash_table *htab,
5860            obj_attribute *out_attr, struct elf_link_hash_entry **sym_hashes,
5861            int *cmse_stub_created)
5862 {
5863   const struct elf_backend_data *bed;
5864   Elf_Internal_Shdr *symtab_hdr;
5865   unsigned i, j, sym_count, ext_start;
5866   Elf_Internal_Sym *cmse_sym, *local_syms;
5867   struct elf32_arm_link_hash_entry *hash, *cmse_hash = NULL;
5868   enum arm_st_branch_type branch_type;
5869   char *sym_name, *lsym_name;
5870   bfd_vma sym_value;
5871   asection *section;
5872   struct elf32_arm_stub_hash_entry *stub_entry;
5873   bfd_boolean is_v8m, new_stub, cmse_invalid, ret = TRUE;
5874
5875   bed = get_elf_backend_data (input_bfd);
5876   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5877   sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
5878   ext_start = symtab_hdr->sh_info;
5879   is_v8m = (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
5880             && out_attr[Tag_CPU_arch_profile].i == 'M');
5881
5882   local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5883   if (local_syms == NULL)
5884     local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5885                                        symtab_hdr->sh_info, 0, NULL, NULL,
5886                                        NULL);
5887   if (symtab_hdr->sh_info && local_syms == NULL)
5888     return FALSE;
5889
5890   /* Scan symbols.  */
5891   for (i = 0; i < sym_count; i++)
5892     {
5893       cmse_invalid = FALSE;
5894
5895       if (i < ext_start)
5896         {
5897           cmse_sym = &local_syms[i];
5898           /* Not a special symbol.  */
5899           if (!ARM_GET_SYM_CMSE_SPCL (cmse_sym->st_target_internal))
5900             continue;
5901           sym_name = bfd_elf_string_from_elf_section (input_bfd,
5902                                                       symtab_hdr->sh_link,
5903                                                       cmse_sym->st_name);
5904           /* Special symbol with local binding.  */
5905           cmse_invalid = TRUE;
5906         }
5907       else
5908         {
5909           cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
5910           sym_name = (char *) cmse_hash->root.root.root.string;
5911
5912           /* Not a special symbol.  */
5913           if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
5914             continue;
5915
5916           /* Special symbol has incorrect binding or type.  */
5917           if ((cmse_hash->root.root.type != bfd_link_hash_defined
5918                && cmse_hash->root.root.type != bfd_link_hash_defweak)
5919               || cmse_hash->root.type != STT_FUNC)
5920             cmse_invalid = TRUE;
5921         }
5922
5923       if (!is_v8m)
5924         {
5925           _bfd_error_handler (_("%pB: special symbol `%s' only allowed for "
5926                                 "ARMv8-M architecture or later"),
5927                               input_bfd, sym_name);
5928           is_v8m = TRUE; /* Avoid multiple warning.  */
5929           ret = FALSE;
5930         }
5931
5932       if (cmse_invalid)
5933         {
5934           _bfd_error_handler (_("%pB: invalid special symbol `%s'; it must be"
5935                                 " a global or weak function symbol"),
5936                               input_bfd, sym_name);
5937           ret = FALSE;
5938           if (i < ext_start)
5939             continue;
5940         }
5941
5942       sym_name += strlen (CMSE_PREFIX);
5943       hash = (struct elf32_arm_link_hash_entry *)
5944         elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
5945
5946       /* No associated normal symbol or it is neither global nor weak.  */
5947       if (!hash
5948           || (hash->root.root.type != bfd_link_hash_defined
5949               && hash->root.root.type != bfd_link_hash_defweak)
5950           || hash->root.type != STT_FUNC)
5951         {
5952           /* Initialize here to avoid warning about use of possibly
5953              uninitialized variable.  */
5954           j = 0;
5955
5956           if (!hash)
5957             {
5958               /* Searching for a normal symbol with local binding.  */
5959               for (; j < ext_start; j++)
5960                 {
5961                   lsym_name =
5962                     bfd_elf_string_from_elf_section (input_bfd,
5963                                                      symtab_hdr->sh_link,
5964                                                      local_syms[j].st_name);
5965                   if (!strcmp (sym_name, lsym_name))
5966                     break;
5967                 }
5968             }
5969
5970           if (hash || j < ext_start)
5971             {
5972               _bfd_error_handler
5973                 (_("%pB: invalid standard symbol `%s'; it must be "
5974                    "a global or weak function symbol"),
5975                  input_bfd, sym_name);
5976             }
5977           else
5978             _bfd_error_handler
5979               (_("%pB: absent standard symbol `%s'"), input_bfd, sym_name);
5980           ret = FALSE;
5981           if (!hash)
5982             continue;
5983         }
5984
5985       sym_value = hash->root.root.u.def.value;
5986       section = hash->root.root.u.def.section;
5987
5988       if (cmse_hash->root.root.u.def.section != section)
5989         {
5990           _bfd_error_handler
5991             (_("%pB: `%s' and its special symbol are in different sections"),
5992              input_bfd, sym_name);
5993           ret = FALSE;
5994         }
5995       if (cmse_hash->root.root.u.def.value != sym_value)
5996         continue; /* Ignore: could be an entry function starting with SG.  */
5997
5998         /* If this section is a link-once section that will be discarded, then
5999            don't create any stubs.  */
6000       if (section->output_section == NULL)
6001         {
6002           _bfd_error_handler
6003             (_("%pB: entry function `%s' not output"), input_bfd, sym_name);
6004           continue;
6005         }
6006
6007       if (hash->root.size == 0)
6008         {
6009           _bfd_error_handler
6010             (_("%pB: entry function `%s' is empty"), input_bfd, sym_name);
6011           ret = FALSE;
6012         }
6013
6014       if (!ret)
6015         continue;
6016       branch_type = ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6017       stub_entry
6018         = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6019                                  NULL, NULL, section, hash, sym_name,
6020                                  sym_value, branch_type, &new_stub);
6021
6022       if (stub_entry == NULL)
6023          ret = FALSE;
6024       else
6025         {
6026           BFD_ASSERT (new_stub);
6027           (*cmse_stub_created)++;
6028         }
6029     }
6030
6031   if (!symtab_hdr->contents)
6032     free (local_syms);
6033   return ret;
6034 }
6035
6036 /* Return TRUE iff a symbol identified by its linker HASH entry is a secure
6037    code entry function, ie can be called from non secure code without using a
6038    veneer.  */
6039
6040 static bfd_boolean
6041 cmse_entry_fct_p (struct elf32_arm_link_hash_entry *hash)
6042 {
6043   bfd_byte contents[4];
6044   uint32_t first_insn;
6045   asection *section;
6046   file_ptr offset;
6047   bfd *abfd;
6048
6049   /* Defined symbol of function type.  */
6050   if (hash->root.root.type != bfd_link_hash_defined
6051       && hash->root.root.type != bfd_link_hash_defweak)
6052     return FALSE;
6053   if (hash->root.type != STT_FUNC)
6054     return FALSE;
6055
6056   /* Read first instruction.  */
6057   section = hash->root.root.u.def.section;
6058   abfd = section->owner;
6059   offset = hash->root.root.u.def.value - section->vma;
6060   if (!bfd_get_section_contents (abfd, section, contents, offset,
6061                                  sizeof (contents)))
6062     return FALSE;
6063
6064   first_insn = bfd_get_32 (abfd, contents);
6065
6066   /* Starts by SG instruction.  */
6067   return first_insn == 0xe97fe97f;
6068 }
6069
6070 /* Output the name (in symbol table) of the veneer GEN_ENTRY if it is a new
6071    secure gateway veneers (ie. the veneers was not in the input import library)
6072    and there is no output import library (GEN_INFO->out_implib_bfd is NULL.  */
6073
6074 static bfd_boolean
6075 arm_list_new_cmse_stub (struct bfd_hash_entry *gen_entry, void *gen_info)
6076 {
6077   struct elf32_arm_stub_hash_entry *stub_entry;
6078   struct bfd_link_info *info;
6079
6080   /* Massage our args to the form they really have.  */
6081   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
6082   info = (struct bfd_link_info *) gen_info;
6083
6084   if (info->out_implib_bfd)
6085     return TRUE;
6086
6087   if (stub_entry->stub_type != arm_stub_cmse_branch_thumb_only)
6088     return TRUE;
6089
6090   if (stub_entry->stub_offset == (bfd_vma) -1)
6091     _bfd_error_handler ("  %s", stub_entry->output_name);
6092
6093   return TRUE;
6094 }
6095
6096 /* Set offset of each secure gateway veneers so that its address remain
6097    identical to the one in the input import library referred by
6098    HTAB->in_implib_bfd.  A warning is issued for veneers that disappeared
6099    (present in input import library but absent from the executable being
6100    linked) or if new veneers appeared and there is no output import library
6101    (INFO->out_implib_bfd is NULL and *CMSE_STUB_CREATED is bigger than the
6102    number of secure gateway veneers found in the input import library.
6103
6104    The function returns whether an error occurred.  If no error occurred,
6105    *CMSE_STUB_CREATED gives the number of SG veneers created by both cmse_scan
6106    and this function and HTAB->new_cmse_stub_offset is set to the biggest
6107    veneer observed set for new veneers to be layed out after.  */
6108
6109 static bfd_boolean
6110 set_cmse_veneer_addr_from_implib (struct bfd_link_info *info,
6111                                   struct elf32_arm_link_hash_table *htab,
6112                                   int *cmse_stub_created)
6113 {
6114   long symsize;
6115   char *sym_name;
6116   flagword flags;
6117   long i, symcount;
6118   bfd *in_implib_bfd;
6119   asection *stub_out_sec;
6120   bfd_boolean ret = TRUE;
6121   Elf_Internal_Sym *intsym;
6122   const char *out_sec_name;
6123   bfd_size_type cmse_stub_size;
6124   asymbol **sympp = NULL, *sym;
6125   struct elf32_arm_link_hash_entry *hash;
6126   const insn_sequence *cmse_stub_template;
6127   struct elf32_arm_stub_hash_entry *stub_entry;
6128   int cmse_stub_template_size, new_cmse_stubs_created = *cmse_stub_created;
6129   bfd_vma veneer_value, stub_offset, next_cmse_stub_offset;
6130   bfd_vma cmse_stub_array_start = (bfd_vma) -1, cmse_stub_sec_vma = 0;
6131
6132   /* No input secure gateway import library.  */
6133   if (!htab->in_implib_bfd)
6134     return TRUE;
6135
6136   in_implib_bfd = htab->in_implib_bfd;
6137   if (!htab->cmse_implib)
6138     {
6139       _bfd_error_handler (_("%pB: --in-implib only supported for Secure "
6140                             "Gateway import libraries"), in_implib_bfd);
6141       return FALSE;
6142     }
6143
6144   /* Get symbol table size.  */
6145   symsize = bfd_get_symtab_upper_bound (in_implib_bfd);
6146   if (symsize < 0)
6147     return FALSE;
6148
6149   /* Read in the input secure gateway import library's symbol table.  */
6150   sympp = (asymbol **) xmalloc (symsize);
6151   symcount = bfd_canonicalize_symtab (in_implib_bfd, sympp);
6152   if (symcount < 0)
6153     {
6154       ret = FALSE;
6155       goto free_sym_buf;
6156     }
6157
6158   htab->new_cmse_stub_offset = 0;
6159   cmse_stub_size =
6160     find_stub_size_and_template (arm_stub_cmse_branch_thumb_only,
6161                                  &cmse_stub_template,
6162                                  &cmse_stub_template_size);
6163   out_sec_name =
6164     arm_dedicated_stub_output_section_name (arm_stub_cmse_branch_thumb_only);
6165   stub_out_sec =
6166     bfd_get_section_by_name (htab->obfd, out_sec_name);
6167   if (stub_out_sec != NULL)
6168     cmse_stub_sec_vma = stub_out_sec->vma;
6169
6170   /* Set addresses of veneers mentionned in input secure gateway import
6171      library's symbol table.  */
6172   for (i = 0; i < symcount; i++)
6173     {
6174       sym = sympp[i];
6175       flags = sym->flags;
6176       sym_name = (char *) bfd_asymbol_name (sym);
6177       intsym = &((elf_symbol_type *) sym)->internal_elf_sym;
6178
6179       if (sym->section != bfd_abs_section_ptr
6180           || !(flags & (BSF_GLOBAL | BSF_WEAK))
6181           || (flags & BSF_FUNCTION) != BSF_FUNCTION
6182           || (ARM_GET_SYM_BRANCH_TYPE (intsym->st_target_internal)
6183               != ST_BRANCH_TO_THUMB))
6184         {
6185           _bfd_error_handler (_("%pB: invalid import library entry: `%s'; "
6186                                 "symbol should be absolute, global and "
6187                                 "refer to Thumb functions"),
6188                               in_implib_bfd, sym_name);
6189           ret = FALSE;
6190           continue;
6191         }
6192
6193       veneer_value = bfd_asymbol_value (sym);
6194       stub_offset = veneer_value - cmse_stub_sec_vma;
6195       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, sym_name,
6196                                          FALSE, FALSE);
6197       hash = (struct elf32_arm_link_hash_entry *)
6198         elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
6199
6200       /* Stub entry should have been created by cmse_scan or the symbol be of
6201          a secure function callable from non secure code.  */
6202       if (!stub_entry && !hash)
6203         {
6204           bfd_boolean new_stub;
6205
6206           _bfd_error_handler
6207             (_("entry function `%s' disappeared from secure code"), sym_name);
6208           hash = (struct elf32_arm_link_hash_entry *)
6209             elf_link_hash_lookup (&(htab)->root, sym_name, TRUE, TRUE, TRUE);
6210           stub_entry
6211             = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6212                                      NULL, NULL, bfd_abs_section_ptr, hash,
6213                                      sym_name, veneer_value,
6214                                      ST_BRANCH_TO_THUMB, &new_stub);
6215           if (stub_entry == NULL)
6216             ret = FALSE;
6217           else
6218           {
6219             BFD_ASSERT (new_stub);
6220             new_cmse_stubs_created++;
6221             (*cmse_stub_created)++;
6222           }
6223           stub_entry->stub_template_size = stub_entry->stub_size = 0;
6224           stub_entry->stub_offset = stub_offset;
6225         }
6226       /* Symbol found is not callable from non secure code.  */
6227       else if (!stub_entry)
6228         {
6229           if (!cmse_entry_fct_p (hash))
6230             {
6231               _bfd_error_handler (_("`%s' refers to a non entry function"),
6232                                   sym_name);
6233               ret = FALSE;
6234             }
6235           continue;
6236         }
6237       else
6238         {
6239           /* Only stubs for SG veneers should have been created.  */
6240           BFD_ASSERT (stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
6241
6242           /* Check visibility hasn't changed.  */
6243           if (!!(flags & BSF_GLOBAL)
6244               != (hash->root.root.type == bfd_link_hash_defined))
6245             _bfd_error_handler
6246               (_("%pB: visibility of symbol `%s' has changed"), in_implib_bfd,
6247                sym_name);
6248
6249           stub_entry->stub_offset = stub_offset;
6250         }
6251
6252       /* Size should match that of a SG veneer.  */
6253       if (intsym->st_size != cmse_stub_size)
6254         {
6255           _bfd_error_handler (_("%pB: incorrect size for symbol `%s'"),
6256                               in_implib_bfd, sym_name);
6257           ret = FALSE;
6258         }
6259
6260       /* Previous veneer address is before current SG veneer section.  */
6261       if (veneer_value < cmse_stub_sec_vma)
6262         {
6263           /* Avoid offset underflow.  */
6264           if (stub_entry)
6265             stub_entry->stub_offset = 0;
6266           stub_offset = 0;
6267           ret = FALSE;
6268         }
6269
6270       /* Complain if stub offset not a multiple of stub size.  */
6271       if (stub_offset % cmse_stub_size)
6272         {
6273           _bfd_error_handler
6274             (_("offset of veneer for entry function `%s' not a multiple of "
6275                "its size"), sym_name);
6276           ret = FALSE;
6277         }
6278
6279       if (!ret)
6280         continue;
6281
6282       new_cmse_stubs_created--;
6283       if (veneer_value < cmse_stub_array_start)
6284         cmse_stub_array_start = veneer_value;
6285       next_cmse_stub_offset = stub_offset + ((cmse_stub_size + 7) & ~7);
6286       if (next_cmse_stub_offset > htab->new_cmse_stub_offset)
6287         htab->new_cmse_stub_offset = next_cmse_stub_offset;
6288     }
6289
6290   if (!info->out_implib_bfd && new_cmse_stubs_created != 0)
6291     {
6292       BFD_ASSERT (new_cmse_stubs_created > 0);
6293       _bfd_error_handler
6294         (_("new entry function(s) introduced but no output import library "
6295            "specified:"));
6296       bfd_hash_traverse (&htab->stub_hash_table, arm_list_new_cmse_stub, info);
6297     }
6298
6299   if (cmse_stub_array_start != cmse_stub_sec_vma)
6300     {
6301       _bfd_error_handler
6302         (_("start address of `%s' is different from previous link"),
6303          out_sec_name);
6304       ret = FALSE;
6305     }
6306
6307 free_sym_buf:
6308   free (sympp);
6309   return ret;
6310 }
6311
6312 /* Determine and set the size of the stub section for a final link.
6313
6314    The basic idea here is to examine all the relocations looking for
6315    PC-relative calls to a target that is unreachable with a "bl"
6316    instruction.  */
6317
6318 bfd_boolean
6319 elf32_arm_size_stubs (bfd *output_bfd,
6320                       bfd *stub_bfd,
6321                       struct bfd_link_info *info,
6322                       bfd_signed_vma group_size,
6323                       asection * (*add_stub_section) (const char *, asection *,
6324                                                       asection *,
6325                                                       unsigned int),
6326                       void (*layout_sections_again) (void))
6327 {
6328   bfd_boolean ret = TRUE;
6329   obj_attribute *out_attr;
6330   int cmse_stub_created = 0;
6331   bfd_size_type stub_group_size;
6332   bfd_boolean m_profile, stubs_always_after_branch, first_veneer_scan = TRUE;
6333   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
6334   struct a8_erratum_fix *a8_fixes = NULL;
6335   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
6336   struct a8_erratum_reloc *a8_relocs = NULL;
6337   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
6338
6339   if (htab == NULL)
6340     return FALSE;
6341
6342   if (htab->fix_cortex_a8)
6343     {
6344       a8_fixes = (struct a8_erratum_fix *)
6345           bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
6346       a8_relocs = (struct a8_erratum_reloc *)
6347           bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
6348     }
6349
6350   /* Propagate mach to stub bfd, because it may not have been
6351      finalized when we created stub_bfd.  */
6352   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
6353                      bfd_get_mach (output_bfd));
6354
6355   /* Stash our params away.  */
6356   htab->stub_bfd = stub_bfd;
6357   htab->add_stub_section = add_stub_section;
6358   htab->layout_sections_again = layout_sections_again;
6359   stubs_always_after_branch = group_size < 0;
6360
6361   out_attr = elf_known_obj_attributes_proc (output_bfd);
6362   m_profile = out_attr[Tag_CPU_arch_profile].i == 'M';
6363
6364   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
6365      as the first half of a 32-bit branch straddling two 4K pages.  This is a
6366      crude way of enforcing that.  */
6367   if (htab->fix_cortex_a8)
6368     stubs_always_after_branch = 1;
6369
6370   if (group_size < 0)
6371     stub_group_size = -group_size;
6372   else
6373     stub_group_size = group_size;
6374
6375   if (stub_group_size == 1)
6376     {
6377       /* Default values.  */
6378       /* Thumb branch range is +-4MB has to be used as the default
6379          maximum size (a given section can contain both ARM and Thumb
6380          code, so the worst case has to be taken into account).
6381
6382          This value is 24K less than that, which allows for 2025
6383          12-byte stubs.  If we exceed that, then we will fail to link.
6384          The user will have to relink with an explicit group size
6385          option.  */
6386       stub_group_size = 4170000;
6387     }
6388
6389   group_sections (htab, stub_group_size, stubs_always_after_branch);
6390
6391   /* If we're applying the cortex A8 fix, we need to determine the
6392      program header size now, because we cannot change it later --
6393      that could alter section placements.  Notice the A8 erratum fix
6394      ends up requiring the section addresses to remain unchanged
6395      modulo the page size.  That's something we cannot represent
6396      inside BFD, and we don't want to force the section alignment to
6397      be the page size.  */
6398   if (htab->fix_cortex_a8)
6399     (*htab->layout_sections_again) ();
6400
6401   while (1)
6402     {
6403       bfd *input_bfd;
6404       unsigned int bfd_indx;
6405       asection *stub_sec;
6406       enum elf32_arm_stub_type stub_type;
6407       bfd_boolean stub_changed = FALSE;
6408       unsigned prev_num_a8_fixes = num_a8_fixes;
6409
6410       num_a8_fixes = 0;
6411       for (input_bfd = info->input_bfds, bfd_indx = 0;
6412            input_bfd != NULL;
6413            input_bfd = input_bfd->link.next, bfd_indx++)
6414         {
6415           Elf_Internal_Shdr *symtab_hdr;
6416           asection *section;
6417           Elf_Internal_Sym *local_syms = NULL;
6418
6419           if (!is_arm_elf (input_bfd))
6420             continue;
6421
6422           num_a8_relocs = 0;
6423
6424           /* We'll need the symbol table in a second.  */
6425           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6426           if (symtab_hdr->sh_info == 0)
6427             continue;
6428
6429           /* Limit scan of symbols to object file whose profile is
6430              Microcontroller to not hinder performance in the general case.  */
6431           if (m_profile && first_veneer_scan)
6432             {
6433               struct elf_link_hash_entry **sym_hashes;
6434
6435               sym_hashes = elf_sym_hashes (input_bfd);
6436               if (!cmse_scan (input_bfd, htab, out_attr, sym_hashes,
6437                               &cmse_stub_created))
6438                 goto error_ret_free_local;
6439
6440               if (cmse_stub_created != 0)
6441                 stub_changed = TRUE;
6442             }
6443
6444           /* Walk over each section attached to the input bfd.  */
6445           for (section = input_bfd->sections;
6446                section != NULL;
6447                section = section->next)
6448             {
6449               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6450
6451               /* If there aren't any relocs, then there's nothing more
6452                  to do.  */
6453               if ((section->flags & SEC_RELOC) == 0
6454                   || section->reloc_count == 0
6455                   || (section->flags & SEC_CODE) == 0)
6456                 continue;
6457
6458               /* If this section is a link-once section that will be
6459                  discarded, then don't create any stubs.  */
6460               if (section->output_section == NULL
6461                   || section->output_section->owner != output_bfd)
6462                 continue;
6463
6464               /* Get the relocs.  */
6465               internal_relocs
6466                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
6467                                              NULL, info->keep_memory);
6468               if (internal_relocs == NULL)
6469                 goto error_ret_free_local;
6470
6471               /* Now examine each relocation.  */
6472               irela = internal_relocs;
6473               irelaend = irela + section->reloc_count;
6474               for (; irela < irelaend; irela++)
6475                 {
6476                   unsigned int r_type, r_indx;
6477                   asection *sym_sec;
6478                   bfd_vma sym_value;
6479                   bfd_vma destination;
6480                   struct elf32_arm_link_hash_entry *hash;
6481                   const char *sym_name;
6482                   unsigned char st_type;
6483                   enum arm_st_branch_type branch_type;
6484                   bfd_boolean created_stub = FALSE;
6485
6486                   r_type = ELF32_R_TYPE (irela->r_info);
6487                   r_indx = ELF32_R_SYM (irela->r_info);
6488
6489                   if (r_type >= (unsigned int) R_ARM_max)
6490                     {
6491                       bfd_set_error (bfd_error_bad_value);
6492                     error_ret_free_internal:
6493                       if (elf_section_data (section)->relocs == NULL)
6494                         free (internal_relocs);
6495                     /* Fall through.  */
6496                     error_ret_free_local:
6497                       if (local_syms != NULL
6498                           && (symtab_hdr->contents
6499                               != (unsigned char *) local_syms))
6500                         free (local_syms);
6501                       return FALSE;
6502                     }
6503
6504                   hash = NULL;
6505                   if (r_indx >= symtab_hdr->sh_info)
6506                     hash = elf32_arm_hash_entry
6507                       (elf_sym_hashes (input_bfd)
6508                        [r_indx - symtab_hdr->sh_info]);
6509
6510                   /* Only look for stubs on branch instructions, or
6511                      non-relaxed TLSCALL  */
6512                   if ((r_type != (unsigned int) R_ARM_CALL)
6513                       && (r_type != (unsigned int) R_ARM_THM_CALL)
6514                       && (r_type != (unsigned int) R_ARM_JUMP24)
6515                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
6516                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
6517                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
6518                       && (r_type != (unsigned int) R_ARM_PLT32)
6519                       && !((r_type == (unsigned int) R_ARM_TLS_CALL
6520                             || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6521                            && r_type == elf32_arm_tls_transition
6522                                (info, r_type, &hash->root)
6523                            && ((hash ? hash->tls_type
6524                                 : (elf32_arm_local_got_tls_type
6525                                    (input_bfd)[r_indx]))
6526                                & GOT_TLS_GDESC) != 0))
6527                     continue;
6528
6529                   /* Now determine the call target, its name, value,
6530                      section.  */
6531                   sym_sec = NULL;
6532                   sym_value = 0;
6533                   destination = 0;
6534                   sym_name = NULL;
6535
6536                   if (r_type == (unsigned int) R_ARM_TLS_CALL
6537                       || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6538                     {
6539                       /* A non-relaxed TLS call.  The target is the
6540                          plt-resident trampoline and nothing to do
6541                          with the symbol.  */
6542                       BFD_ASSERT (htab->tls_trampoline > 0);
6543                       sym_sec = htab->root.splt;
6544                       sym_value = htab->tls_trampoline;
6545                       hash = 0;
6546                       st_type = STT_FUNC;
6547                       branch_type = ST_BRANCH_TO_ARM;
6548                     }
6549                   else if (!hash)
6550                     {
6551                       /* It's a local symbol.  */
6552                       Elf_Internal_Sym *sym;
6553
6554                       if (local_syms == NULL)
6555                         {
6556                           local_syms
6557                             = (Elf_Internal_Sym *) symtab_hdr->contents;
6558                           if (local_syms == NULL)
6559                             local_syms
6560                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
6561                                                       symtab_hdr->sh_info, 0,
6562                                                       NULL, NULL, NULL);
6563                           if (local_syms == NULL)
6564                             goto error_ret_free_internal;
6565                         }
6566
6567                       sym = local_syms + r_indx;
6568                       if (sym->st_shndx == SHN_UNDEF)
6569                         sym_sec = bfd_und_section_ptr;
6570                       else if (sym->st_shndx == SHN_ABS)
6571                         sym_sec = bfd_abs_section_ptr;
6572                       else if (sym->st_shndx == SHN_COMMON)
6573                         sym_sec = bfd_com_section_ptr;
6574                       else
6575                         sym_sec =
6576                           bfd_section_from_elf_index (input_bfd, sym->st_shndx);
6577
6578                       if (!sym_sec)
6579                         /* This is an undefined symbol.  It can never
6580                            be resolved.  */
6581                         continue;
6582
6583                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6584                         sym_value = sym->st_value;
6585                       destination = (sym_value + irela->r_addend
6586                                      + sym_sec->output_offset
6587                                      + sym_sec->output_section->vma);
6588                       st_type = ELF_ST_TYPE (sym->st_info);
6589                       branch_type =
6590                         ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
6591                       sym_name
6592                         = bfd_elf_string_from_elf_section (input_bfd,
6593                                                            symtab_hdr->sh_link,
6594                                                            sym->st_name);
6595                     }
6596                   else
6597                     {
6598                       /* It's an external symbol.  */
6599                       while (hash->root.root.type == bfd_link_hash_indirect
6600                              || hash->root.root.type == bfd_link_hash_warning)
6601                         hash = ((struct elf32_arm_link_hash_entry *)
6602                                 hash->root.root.u.i.link);
6603
6604                       if (hash->root.root.type == bfd_link_hash_defined
6605                           || hash->root.root.type == bfd_link_hash_defweak)
6606                         {
6607                           sym_sec = hash->root.root.u.def.section;
6608                           sym_value = hash->root.root.u.def.value;
6609
6610                           struct elf32_arm_link_hash_table *globals =
6611                                                   elf32_arm_hash_table (info);
6612
6613                           /* For a destination in a shared library,
6614                              use the PLT stub as target address to
6615                              decide whether a branch stub is
6616                              needed.  */
6617                           if (globals != NULL
6618                               && globals->root.splt != NULL
6619                               && hash != NULL
6620                               && hash->root.plt.offset != (bfd_vma) -1)
6621                             {
6622                               sym_sec = globals->root.splt;
6623                               sym_value = hash->root.plt.offset;
6624                               if (sym_sec->output_section != NULL)
6625                                 destination = (sym_value
6626                                                + sym_sec->output_offset
6627                                                + sym_sec->output_section->vma);
6628                             }
6629                           else if (sym_sec->output_section != NULL)
6630                             destination = (sym_value + irela->r_addend
6631                                            + sym_sec->output_offset
6632                                            + sym_sec->output_section->vma);
6633                         }
6634                       else if ((hash->root.root.type == bfd_link_hash_undefined)
6635                                || (hash->root.root.type == bfd_link_hash_undefweak))
6636                         {
6637                           /* For a shared library, use the PLT stub as
6638                              target address to decide whether a long
6639                              branch stub is needed.
6640                              For absolute code, they cannot be handled.  */
6641                           struct elf32_arm_link_hash_table *globals =
6642                             elf32_arm_hash_table (info);
6643
6644                           if (globals != NULL
6645                               && globals->root.splt != NULL
6646                               && hash != NULL
6647                               && hash->root.plt.offset != (bfd_vma) -1)
6648                             {
6649                               sym_sec = globals->root.splt;
6650                               sym_value = hash->root.plt.offset;
6651                               if (sym_sec->output_section != NULL)
6652                                 destination = (sym_value
6653                                                + sym_sec->output_offset
6654                                                + sym_sec->output_section->vma);
6655                             }
6656                           else
6657                             continue;
6658                         }
6659                       else
6660                         {
6661                           bfd_set_error (bfd_error_bad_value);
6662                           goto error_ret_free_internal;
6663                         }
6664                       st_type = hash->root.type;
6665                       branch_type =
6666                         ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6667                       sym_name = hash->root.root.root.string;
6668                     }
6669
6670                   do
6671                     {
6672                       bfd_boolean new_stub;
6673                       struct elf32_arm_stub_hash_entry *stub_entry;
6674
6675                       /* Determine what (if any) linker stub is needed.  */
6676                       stub_type = arm_type_of_stub (info, section, irela,
6677                                                     st_type, &branch_type,
6678                                                     hash, destination, sym_sec,
6679                                                     input_bfd, sym_name);
6680                       if (stub_type == arm_stub_none)
6681                         break;
6682
6683                       /* We've either created a stub for this reloc already,
6684                          or we are about to.  */
6685                       stub_entry =
6686                         elf32_arm_create_stub (htab, stub_type, section, irela,
6687                                                sym_sec, hash,
6688                                                (char *) sym_name, sym_value,
6689                                                branch_type, &new_stub);
6690
6691                       created_stub = stub_entry != NULL;
6692                       if (!created_stub)
6693                         goto error_ret_free_internal;
6694                       else if (!new_stub)
6695                         break;
6696                       else
6697                         stub_changed = TRUE;
6698                     }
6699                   while (0);
6700
6701                   /* Look for relocations which might trigger Cortex-A8
6702                      erratum.  */
6703                   if (htab->fix_cortex_a8
6704                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
6705                           || r_type == (unsigned int) R_ARM_THM_JUMP19
6706                           || r_type == (unsigned int) R_ARM_THM_CALL
6707                           || r_type == (unsigned int) R_ARM_THM_XPC22))
6708                     {
6709                       bfd_vma from = section->output_section->vma
6710                                      + section->output_offset
6711                                      + irela->r_offset;
6712
6713                       if ((from & 0xfff) == 0xffe)
6714                         {
6715                           /* Found a candidate.  Note we haven't checked the
6716                              destination is within 4K here: if we do so (and
6717                              don't create an entry in a8_relocs) we can't tell
6718                              that a branch should have been relocated when
6719                              scanning later.  */
6720                           if (num_a8_relocs == a8_reloc_table_size)
6721                             {
6722                               a8_reloc_table_size *= 2;
6723                               a8_relocs = (struct a8_erratum_reloc *)
6724                                   bfd_realloc (a8_relocs,
6725                                                sizeof (struct a8_erratum_reloc)
6726                                                * a8_reloc_table_size);
6727                             }
6728
6729                           a8_relocs[num_a8_relocs].from = from;
6730                           a8_relocs[num_a8_relocs].destination = destination;
6731                           a8_relocs[num_a8_relocs].r_type = r_type;
6732                           a8_relocs[num_a8_relocs].branch_type = branch_type;
6733                           a8_relocs[num_a8_relocs].sym_name = sym_name;
6734                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
6735                           a8_relocs[num_a8_relocs].hash = hash;
6736
6737                           num_a8_relocs++;
6738                         }
6739                     }
6740                 }
6741
6742               /* We're done with the internal relocs, free them.  */
6743               if (elf_section_data (section)->relocs == NULL)
6744                 free (internal_relocs);
6745             }
6746
6747           if (htab->fix_cortex_a8)
6748             {
6749               /* Sort relocs which might apply to Cortex-A8 erratum.  */
6750               qsort (a8_relocs, num_a8_relocs,
6751                      sizeof (struct a8_erratum_reloc),
6752                      &a8_reloc_compare);
6753
6754               /* Scan for branches which might trigger Cortex-A8 erratum.  */
6755               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
6756                                           &num_a8_fixes, &a8_fix_table_size,
6757                                           a8_relocs, num_a8_relocs,
6758                                           prev_num_a8_fixes, &stub_changed)
6759                   != 0)
6760                 goto error_ret_free_local;
6761             }
6762
6763           if (local_syms != NULL
6764               && symtab_hdr->contents != (unsigned char *) local_syms)
6765             {
6766               if (!info->keep_memory)
6767                 free (local_syms);
6768               else
6769                 symtab_hdr->contents = (unsigned char *) local_syms;
6770             }
6771         }
6772
6773       if (first_veneer_scan
6774           && !set_cmse_veneer_addr_from_implib (info, htab,
6775                                                 &cmse_stub_created))
6776         ret = FALSE;
6777
6778       if (prev_num_a8_fixes != num_a8_fixes)
6779         stub_changed = TRUE;
6780
6781       if (!stub_changed)
6782         break;
6783
6784       /* OK, we've added some stubs.  Find out the new size of the
6785          stub sections.  */
6786       for (stub_sec = htab->stub_bfd->sections;
6787            stub_sec != NULL;
6788            stub_sec = stub_sec->next)
6789         {
6790           /* Ignore non-stub sections.  */
6791           if (!strstr (stub_sec->name, STUB_SUFFIX))
6792             continue;
6793
6794           stub_sec->size = 0;
6795         }
6796
6797       /* Add new SG veneers after those already in the input import
6798          library.  */
6799       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6800            stub_type++)
6801         {
6802           bfd_vma *start_offset_p;
6803           asection **stub_sec_p;
6804
6805           start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6806           stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6807           if (start_offset_p == NULL)
6808             continue;
6809
6810           BFD_ASSERT (stub_sec_p != NULL);
6811           if (*stub_sec_p != NULL)
6812             (*stub_sec_p)->size = *start_offset_p;
6813         }
6814
6815       /* Compute stub section size, considering padding.  */
6816       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
6817       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6818            stub_type++)
6819         {
6820           int size, padding;
6821           asection **stub_sec_p;
6822
6823           padding = arm_dedicated_stub_section_padding (stub_type);
6824           stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6825           /* Skip if no stub input section or no stub section padding
6826              required.  */
6827           if ((stub_sec_p != NULL && *stub_sec_p == NULL) || padding == 0)
6828             continue;
6829           /* Stub section padding required but no dedicated section.  */
6830           BFD_ASSERT (stub_sec_p);
6831
6832           size = (*stub_sec_p)->size;
6833           size = (size + padding - 1) & ~(padding - 1);
6834           (*stub_sec_p)->size = size;
6835         }
6836
6837       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
6838       if (htab->fix_cortex_a8)
6839         for (i = 0; i < num_a8_fixes; i++)
6840           {
6841             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
6842                          a8_fixes[i].section, htab, a8_fixes[i].stub_type);
6843
6844             if (stub_sec == NULL)
6845               return FALSE;
6846
6847             stub_sec->size
6848               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
6849                                               NULL);
6850           }
6851
6852
6853       /* Ask the linker to do its stuff.  */
6854       (*htab->layout_sections_again) ();
6855       first_veneer_scan = FALSE;
6856     }
6857
6858   /* Add stubs for Cortex-A8 erratum fixes now.  */
6859   if (htab->fix_cortex_a8)
6860     {
6861       for (i = 0; i < num_a8_fixes; i++)
6862         {
6863           struct elf32_arm_stub_hash_entry *stub_entry;
6864           char *stub_name = a8_fixes[i].stub_name;
6865           asection *section = a8_fixes[i].section;
6866           unsigned int section_id = a8_fixes[i].section->id;
6867           asection *link_sec = htab->stub_group[section_id].link_sec;
6868           asection *stub_sec = htab->stub_group[section_id].stub_sec;
6869           const insn_sequence *template_sequence;
6870           int template_size, size = 0;
6871
6872           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
6873                                              TRUE, FALSE);
6874           if (stub_entry == NULL)
6875             {
6876               _bfd_error_handler (_("%pB: cannot create stub entry %s"),
6877                                   section->owner, stub_name);
6878               return FALSE;
6879             }
6880
6881           stub_entry->stub_sec = stub_sec;
6882           stub_entry->stub_offset = (bfd_vma) -1;
6883           stub_entry->id_sec = link_sec;
6884           stub_entry->stub_type = a8_fixes[i].stub_type;
6885           stub_entry->source_value = a8_fixes[i].offset;
6886           stub_entry->target_section = a8_fixes[i].section;
6887           stub_entry->target_value = a8_fixes[i].target_offset;
6888           stub_entry->orig_insn = a8_fixes[i].orig_insn;
6889           stub_entry->branch_type = a8_fixes[i].branch_type;
6890
6891           size = find_stub_size_and_template (a8_fixes[i].stub_type,
6892                                               &template_sequence,
6893                                               &template_size);
6894
6895           stub_entry->stub_size = size;
6896           stub_entry->stub_template = template_sequence;
6897           stub_entry->stub_template_size = template_size;
6898         }
6899
6900       /* Stash the Cortex-A8 erratum fix array for use later in
6901          elf32_arm_write_section().  */
6902       htab->a8_erratum_fixes = a8_fixes;
6903       htab->num_a8_erratum_fixes = num_a8_fixes;
6904     }
6905   else
6906     {
6907       htab->a8_erratum_fixes = NULL;
6908       htab->num_a8_erratum_fixes = 0;
6909     }
6910   return ret;
6911 }
6912
6913 /* Build all the stubs associated with the current output file.  The
6914    stubs are kept in a hash table attached to the main linker hash
6915    table.  We also set up the .plt entries for statically linked PIC
6916    functions here.  This function is called via arm_elf_finish in the
6917    linker.  */
6918
6919 bfd_boolean
6920 elf32_arm_build_stubs (struct bfd_link_info *info)
6921 {
6922   asection *stub_sec;
6923   struct bfd_hash_table *table;
6924   enum elf32_arm_stub_type stub_type;
6925   struct elf32_arm_link_hash_table *htab;
6926
6927   htab = elf32_arm_hash_table (info);
6928   if (htab == NULL)
6929     return FALSE;
6930
6931   for (stub_sec = htab->stub_bfd->sections;
6932        stub_sec != NULL;
6933        stub_sec = stub_sec->next)
6934     {
6935       bfd_size_type size;
6936
6937       /* Ignore non-stub sections.  */
6938       if (!strstr (stub_sec->name, STUB_SUFFIX))
6939         continue;
6940
6941       /* Allocate memory to hold the linker stubs.  Zeroing the stub sections
6942          must at least be done for stub section requiring padding and for SG
6943          veneers to ensure that a non secure code branching to a removed SG
6944          veneer causes an error.  */
6945       size = stub_sec->size;
6946       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
6947       if (stub_sec->contents == NULL && size != 0)
6948         return FALSE;
6949
6950       stub_sec->size = 0;
6951     }
6952
6953   /* Add new SG veneers after those already in the input import library.  */
6954   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
6955     {
6956       bfd_vma *start_offset_p;
6957       asection **stub_sec_p;
6958
6959       start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6960       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6961       if (start_offset_p == NULL)
6962         continue;
6963
6964       BFD_ASSERT (stub_sec_p != NULL);
6965       if (*stub_sec_p != NULL)
6966         (*stub_sec_p)->size = *start_offset_p;
6967     }
6968
6969   /* Build the stubs as directed by the stub hash table.  */
6970   table = &htab->stub_hash_table;
6971   bfd_hash_traverse (table, arm_build_one_stub, info);
6972   if (htab->fix_cortex_a8)
6973     {
6974       /* Place the cortex a8 stubs last.  */
6975       htab->fix_cortex_a8 = -1;
6976       bfd_hash_traverse (table, arm_build_one_stub, info);
6977     }
6978
6979   return TRUE;
6980 }
6981
6982 /* Locate the Thumb encoded calling stub for NAME.  */
6983
6984 static struct elf_link_hash_entry *
6985 find_thumb_glue (struct bfd_link_info *link_info,
6986                  const char *name,
6987                  char **error_message)
6988 {
6989   char *tmp_name;
6990   struct elf_link_hash_entry *hash;
6991   struct elf32_arm_link_hash_table *hash_table;
6992
6993   /* We need a pointer to the armelf specific hash table.  */
6994   hash_table = elf32_arm_hash_table (link_info);
6995   if (hash_table == NULL)
6996     return NULL;
6997
6998   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
6999                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
7000
7001   BFD_ASSERT (tmp_name);
7002
7003   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
7004
7005   hash = elf_link_hash_lookup
7006     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
7007
7008   if (hash == NULL
7009       && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
7010                    "Thumb", tmp_name, name) == -1)
7011     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7012
7013   free (tmp_name);
7014
7015   return hash;
7016 }
7017
7018 /* Locate the ARM encoded calling stub for NAME.  */
7019
7020 static struct elf_link_hash_entry *
7021 find_arm_glue (struct bfd_link_info *link_info,
7022                const char *name,
7023                char **error_message)
7024 {
7025   char *tmp_name;
7026   struct elf_link_hash_entry *myh;
7027   struct elf32_arm_link_hash_table *hash_table;
7028
7029   /* We need a pointer to the elfarm specific hash table.  */
7030   hash_table = elf32_arm_hash_table (link_info);
7031   if (hash_table == NULL)
7032     return NULL;
7033
7034   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7035                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7036
7037   BFD_ASSERT (tmp_name);
7038
7039   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7040
7041   myh = elf_link_hash_lookup
7042     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
7043
7044   if (myh == NULL
7045       && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
7046                    "ARM", tmp_name, name) == -1)
7047     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7048
7049   free (tmp_name);
7050
7051   return myh;
7052 }
7053
7054 /* ARM->Thumb glue (static images):
7055
7056    .arm
7057    __func_from_arm:
7058    ldr r12, __func_addr
7059    bx  r12
7060    __func_addr:
7061    .word func    @ behave as if you saw a ARM_32 reloc.
7062
7063    (v5t static images)
7064    .arm
7065    __func_from_arm:
7066    ldr pc, __func_addr
7067    __func_addr:
7068    .word func    @ behave as if you saw a ARM_32 reloc.
7069
7070    (relocatable images)
7071    .arm
7072    __func_from_arm:
7073    ldr r12, __func_offset
7074    add r12, r12, pc
7075    bx  r12
7076    __func_offset:
7077    .word func - .   */
7078
7079 #define ARM2THUMB_STATIC_GLUE_SIZE 12
7080 static const insn32 a2t1_ldr_insn = 0xe59fc000;
7081 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
7082 static const insn32 a2t3_func_addr_insn = 0x00000001;
7083
7084 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
7085 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
7086 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
7087
7088 #define ARM2THUMB_PIC_GLUE_SIZE 16
7089 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
7090 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
7091 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
7092
7093 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
7094
7095      .thumb                             .thumb
7096      .align 2                           .align 2
7097  __func_from_thumb:                 __func_from_thumb:
7098      bx pc                              push {r6, lr}
7099      nop                                ldr  r6, __func_addr
7100      .arm                               mov  lr, pc
7101      b func                             bx   r6
7102                                         .arm
7103                                     ;; back_to_thumb
7104                                         ldmia r13! {r6, lr}
7105                                         bx    lr
7106                                     __func_addr:
7107                                         .word        func  */
7108
7109 #define THUMB2ARM_GLUE_SIZE 8
7110 static const insn16 t2a1_bx_pc_insn = 0x4778;
7111 static const insn16 t2a2_noop_insn = 0x46c0;
7112 static const insn32 t2a3_b_insn = 0xea000000;
7113
7114 #define VFP11_ERRATUM_VENEER_SIZE 8
7115 #define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
7116 #define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
7117
7118 #define ARM_BX_VENEER_SIZE 12
7119 static const insn32 armbx1_tst_insn = 0xe3100001;
7120 static const insn32 armbx2_moveq_insn = 0x01a0f000;
7121 static const insn32 armbx3_bx_insn = 0xe12fff10;
7122
7123 #ifndef ELFARM_NABI_C_INCLUDED
7124 static void
7125 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
7126 {
7127   asection * s;
7128   bfd_byte * contents;
7129
7130   if (size == 0)
7131     {
7132       /* Do not include empty glue sections in the output.  */
7133       if (abfd != NULL)
7134         {
7135           s = bfd_get_linker_section (abfd, name);
7136           if (s != NULL)
7137             s->flags |= SEC_EXCLUDE;
7138         }
7139       return;
7140     }
7141
7142   BFD_ASSERT (abfd != NULL);
7143
7144   s = bfd_get_linker_section (abfd, name);
7145   BFD_ASSERT (s != NULL);
7146
7147   contents = (bfd_byte *) bfd_alloc (abfd, size);
7148
7149   BFD_ASSERT (s->size == size);
7150   s->contents = contents;
7151 }
7152
7153 bfd_boolean
7154 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
7155 {
7156   struct elf32_arm_link_hash_table * globals;
7157
7158   globals = elf32_arm_hash_table (info);
7159   BFD_ASSERT (globals != NULL);
7160
7161   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7162                                    globals->arm_glue_size,
7163                                    ARM2THUMB_GLUE_SECTION_NAME);
7164
7165   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7166                                    globals->thumb_glue_size,
7167                                    THUMB2ARM_GLUE_SECTION_NAME);
7168
7169   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7170                                    globals->vfp11_erratum_glue_size,
7171                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
7172
7173   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7174                                    globals->stm32l4xx_erratum_glue_size,
7175                                    STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7176
7177   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7178                                    globals->bx_glue_size,
7179                                    ARM_BX_GLUE_SECTION_NAME);
7180
7181   return TRUE;
7182 }
7183
7184 /* Allocate space and symbols for calling a Thumb function from Arm mode.
7185    returns the symbol identifying the stub.  */
7186
7187 static struct elf_link_hash_entry *
7188 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
7189                           struct elf_link_hash_entry * h)
7190 {
7191   const char * name = h->root.root.string;
7192   asection * s;
7193   char * tmp_name;
7194   struct elf_link_hash_entry * myh;
7195   struct bfd_link_hash_entry * bh;
7196   struct elf32_arm_link_hash_table * globals;
7197   bfd_vma val;
7198   bfd_size_type size;
7199
7200   globals = elf32_arm_hash_table (link_info);
7201   BFD_ASSERT (globals != NULL);
7202   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7203
7204   s = bfd_get_linker_section
7205     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
7206
7207   BFD_ASSERT (s != NULL);
7208
7209   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7210                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7211
7212   BFD_ASSERT (tmp_name);
7213
7214   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7215
7216   myh = elf_link_hash_lookup
7217     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7218
7219   if (myh != NULL)
7220     {
7221       /* We've already seen this guy.  */
7222       free (tmp_name);
7223       return myh;
7224     }
7225
7226   /* The only trick here is using hash_table->arm_glue_size as the value.
7227      Even though the section isn't allocated yet, this is where we will be
7228      putting it.  The +1 on the value marks that the stub has not been
7229      output yet - not that it is a Thumb function.  */
7230   bh = NULL;
7231   val = globals->arm_glue_size + 1;
7232   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7233                                     tmp_name, BSF_GLOBAL, s, val,
7234                                     NULL, TRUE, FALSE, &bh);
7235
7236   myh = (struct elf_link_hash_entry *) bh;
7237   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7238   myh->forced_local = 1;
7239
7240   free (tmp_name);
7241
7242   if (bfd_link_pic (link_info)
7243       || globals->root.is_relocatable_executable
7244       || globals->pic_veneer)
7245     size = ARM2THUMB_PIC_GLUE_SIZE;
7246   else if (globals->use_blx)
7247     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
7248   else
7249     size = ARM2THUMB_STATIC_GLUE_SIZE;
7250
7251   s->size += size;
7252   globals->arm_glue_size += size;
7253
7254   return myh;
7255 }
7256
7257 /* Allocate space for ARMv4 BX veneers.  */
7258
7259 static void
7260 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
7261 {
7262   asection * s;
7263   struct elf32_arm_link_hash_table *globals;
7264   char *tmp_name;
7265   struct elf_link_hash_entry *myh;
7266   struct bfd_link_hash_entry *bh;
7267   bfd_vma val;
7268
7269   /* BX PC does not need a veneer.  */
7270   if (reg == 15)
7271     return;
7272
7273   globals = elf32_arm_hash_table (link_info);
7274   BFD_ASSERT (globals != NULL);
7275   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7276
7277   /* Check if this veneer has already been allocated.  */
7278   if (globals->bx_glue_offset[reg])
7279     return;
7280
7281   s = bfd_get_linker_section
7282     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
7283
7284   BFD_ASSERT (s != NULL);
7285
7286   /* Add symbol for veneer.  */
7287   tmp_name = (char *)
7288       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
7289
7290   BFD_ASSERT (tmp_name);
7291
7292   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
7293
7294   myh = elf_link_hash_lookup
7295     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
7296
7297   BFD_ASSERT (myh == NULL);
7298
7299   bh = NULL;
7300   val = globals->bx_glue_size;
7301   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7302                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7303                                     NULL, TRUE, FALSE, &bh);
7304
7305   myh = (struct elf_link_hash_entry *) bh;
7306   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7307   myh->forced_local = 1;
7308
7309   s->size += ARM_BX_VENEER_SIZE;
7310   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
7311   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
7312 }
7313
7314
7315 /* Add an entry to the code/data map for section SEC.  */
7316
7317 static void
7318 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
7319 {
7320   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7321   unsigned int newidx;
7322
7323   if (sec_data->map == NULL)
7324     {
7325       sec_data->map = (elf32_arm_section_map *)
7326           bfd_malloc (sizeof (elf32_arm_section_map));
7327       sec_data->mapcount = 0;
7328       sec_data->mapsize = 1;
7329     }
7330
7331   newidx = sec_data->mapcount++;
7332
7333   if (sec_data->mapcount > sec_data->mapsize)
7334     {
7335       sec_data->mapsize *= 2;
7336       sec_data->map = (elf32_arm_section_map *)
7337           bfd_realloc_or_free (sec_data->map, sec_data->mapsize
7338                                * sizeof (elf32_arm_section_map));
7339     }
7340
7341   if (sec_data->map)
7342     {
7343       sec_data->map[newidx].vma = vma;
7344       sec_data->map[newidx].type = type;
7345     }
7346 }
7347
7348
7349 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
7350    veneers are handled for now.  */
7351
7352 static bfd_vma
7353 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
7354                              elf32_vfp11_erratum_list *branch,
7355                              bfd *branch_bfd,
7356                              asection *branch_sec,
7357                              unsigned int offset)
7358 {
7359   asection *s;
7360   struct elf32_arm_link_hash_table *hash_table;
7361   char *tmp_name;
7362   struct elf_link_hash_entry *myh;
7363   struct bfd_link_hash_entry *bh;
7364   bfd_vma val;
7365   struct _arm_elf_section_data *sec_data;
7366   elf32_vfp11_erratum_list *newerr;
7367
7368   hash_table = elf32_arm_hash_table (link_info);
7369   BFD_ASSERT (hash_table != NULL);
7370   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7371
7372   s = bfd_get_linker_section
7373     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
7374
7375   sec_data = elf32_arm_section_data (s);
7376
7377   BFD_ASSERT (s != NULL);
7378
7379   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7380                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7381
7382   BFD_ASSERT (tmp_name);
7383
7384   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7385            hash_table->num_vfp11_fixes);
7386
7387   myh = elf_link_hash_lookup
7388     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7389
7390   BFD_ASSERT (myh == NULL);
7391
7392   bh = NULL;
7393   val = hash_table->vfp11_erratum_glue_size;
7394   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7395                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7396                                     NULL, TRUE, FALSE, &bh);
7397
7398   myh = (struct elf_link_hash_entry *) bh;
7399   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7400   myh->forced_local = 1;
7401
7402   /* Link veneer back to calling location.  */
7403   sec_data->erratumcount += 1;
7404   newerr = (elf32_vfp11_erratum_list *)
7405       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7406
7407   newerr->type = VFP11_ERRATUM_ARM_VENEER;
7408   newerr->vma = -1;
7409   newerr->u.v.branch = branch;
7410   newerr->u.v.id = hash_table->num_vfp11_fixes;
7411   branch->u.b.veneer = newerr;
7412
7413   newerr->next = sec_data->erratumlist;
7414   sec_data->erratumlist = newerr;
7415
7416   /* A symbol for the return from the veneer.  */
7417   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7418            hash_table->num_vfp11_fixes);
7419
7420   myh = elf_link_hash_lookup
7421     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7422
7423   if (myh != NULL)
7424     abort ();
7425
7426   bh = NULL;
7427   val = offset + 4;
7428   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7429                                     branch_sec, val, 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   free (tmp_name);
7436
7437   /* Generate a mapping symbol for the veneer section, and explicitly add an
7438      entry for that symbol to the code/data map for the section.  */
7439   if (hash_table->vfp11_erratum_glue_size == 0)
7440     {
7441       bh = NULL;
7442       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
7443          ever requires this erratum fix.  */
7444       _bfd_generic_link_add_one_symbol (link_info,
7445                                         hash_table->bfd_of_glue_owner, "$a",
7446                                         BSF_LOCAL, s, 0, NULL,
7447                                         TRUE, FALSE, &bh);
7448
7449       myh = (struct elf_link_hash_entry *) bh;
7450       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7451       myh->forced_local = 1;
7452
7453       /* The elf32_arm_init_maps function only cares about symbols from input
7454          BFDs.  We must make a note of this generated mapping symbol
7455          ourselves so that code byteswapping works properly in
7456          elf32_arm_write_section.  */
7457       elf32_arm_section_map_add (s, 'a', 0);
7458     }
7459
7460   s->size += VFP11_ERRATUM_VENEER_SIZE;
7461   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
7462   hash_table->num_vfp11_fixes++;
7463
7464   /* The offset of the veneer.  */
7465   return val;
7466 }
7467
7468 /* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
7469    veneers need to be handled because used only in Cortex-M.  */
7470
7471 static bfd_vma
7472 record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
7473                                  elf32_stm32l4xx_erratum_list *branch,
7474                                  bfd *branch_bfd,
7475                                  asection *branch_sec,
7476                                  unsigned int offset,
7477                                  bfd_size_type veneer_size)
7478 {
7479   asection *s;
7480   struct elf32_arm_link_hash_table *hash_table;
7481   char *tmp_name;
7482   struct elf_link_hash_entry *myh;
7483   struct bfd_link_hash_entry *bh;
7484   bfd_vma val;
7485   struct _arm_elf_section_data *sec_data;
7486   elf32_stm32l4xx_erratum_list *newerr;
7487
7488   hash_table = elf32_arm_hash_table (link_info);
7489   BFD_ASSERT (hash_table != NULL);
7490   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7491
7492   s = bfd_get_linker_section
7493     (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7494
7495   BFD_ASSERT (s != NULL);
7496
7497   sec_data = elf32_arm_section_data (s);
7498
7499   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7500                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7501
7502   BFD_ASSERT (tmp_name);
7503
7504   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7505            hash_table->num_stm32l4xx_fixes);
7506
7507   myh = elf_link_hash_lookup
7508     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7509
7510   BFD_ASSERT (myh == NULL);
7511
7512   bh = NULL;
7513   val = hash_table->stm32l4xx_erratum_glue_size;
7514   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7515                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7516                                     NULL, TRUE, FALSE, &bh);
7517
7518   myh = (struct elf_link_hash_entry *) bh;
7519   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7520   myh->forced_local = 1;
7521
7522   /* Link veneer back to calling location.  */
7523   sec_data->stm32l4xx_erratumcount += 1;
7524   newerr = (elf32_stm32l4xx_erratum_list *)
7525       bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
7526
7527   newerr->type = STM32L4XX_ERRATUM_VENEER;
7528   newerr->vma = -1;
7529   newerr->u.v.branch = branch;
7530   newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
7531   branch->u.b.veneer = newerr;
7532
7533   newerr->next = sec_data->stm32l4xx_erratumlist;
7534   sec_data->stm32l4xx_erratumlist = newerr;
7535
7536   /* A symbol for the return from the veneer.  */
7537   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7538            hash_table->num_stm32l4xx_fixes);
7539
7540   myh = elf_link_hash_lookup
7541     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7542
7543   if (myh != NULL)
7544     abort ();
7545
7546   bh = NULL;
7547   val = offset + 4;
7548   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7549                                     branch_sec, val, 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   free (tmp_name);
7556
7557   /* Generate a mapping symbol for the veneer section, and explicitly add an
7558      entry for that symbol to the code/data map for the section.  */
7559   if (hash_table->stm32l4xx_erratum_glue_size == 0)
7560     {
7561       bh = NULL;
7562       /* Creates a THUMB symbol since there is no other choice.  */
7563       _bfd_generic_link_add_one_symbol (link_info,
7564                                         hash_table->bfd_of_glue_owner, "$t",
7565                                         BSF_LOCAL, s, 0, NULL,
7566                                         TRUE, FALSE, &bh);
7567
7568       myh = (struct elf_link_hash_entry *) bh;
7569       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7570       myh->forced_local = 1;
7571
7572       /* The elf32_arm_init_maps function only cares about symbols from input
7573          BFDs.  We must make a note of this generated mapping symbol
7574          ourselves so that code byteswapping works properly in
7575          elf32_arm_write_section.  */
7576       elf32_arm_section_map_add (s, 't', 0);
7577     }
7578
7579   s->size += veneer_size;
7580   hash_table->stm32l4xx_erratum_glue_size += veneer_size;
7581   hash_table->num_stm32l4xx_fixes++;
7582
7583   /* The offset of the veneer.  */
7584   return val;
7585 }
7586
7587 #define ARM_GLUE_SECTION_FLAGS \
7588   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
7589    | SEC_READONLY | SEC_LINKER_CREATED)
7590
7591 /* Create a fake section for use by the ARM backend of the linker.  */
7592
7593 static bfd_boolean
7594 arm_make_glue_section (bfd * abfd, const char * name)
7595 {
7596   asection * sec;
7597
7598   sec = bfd_get_linker_section (abfd, name);
7599   if (sec != NULL)
7600     /* Already made.  */
7601     return TRUE;
7602
7603   sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
7604
7605   if (sec == NULL
7606       || !bfd_set_section_alignment (abfd, sec, 2))
7607     return FALSE;
7608
7609   /* Set the gc mark to prevent the section from being removed by garbage
7610      collection, despite the fact that no relocs refer to this section.  */
7611   sec->gc_mark = 1;
7612
7613   return TRUE;
7614 }
7615
7616 /* Set size of .plt entries.  This function is called from the
7617    linker scripts in ld/emultempl/{armelf}.em.  */
7618
7619 void
7620 bfd_elf32_arm_use_long_plt (void)
7621 {
7622   elf32_arm_use_long_plt_entry = TRUE;
7623 }
7624
7625 /* Add the glue sections to ABFD.  This function is called from the
7626    linker scripts in ld/emultempl/{armelf}.em.  */
7627
7628 bfd_boolean
7629 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
7630                                         struct bfd_link_info *info)
7631 {
7632   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
7633   bfd_boolean dostm32l4xx = globals
7634     && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
7635   bfd_boolean addglue;
7636
7637   /* If we are only performing a partial
7638      link do not bother adding the glue.  */
7639   if (bfd_link_relocatable (info))
7640     return TRUE;
7641
7642   addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
7643     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
7644     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
7645     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
7646
7647   if (!dostm32l4xx)
7648     return addglue;
7649
7650   return addglue
7651     && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7652 }
7653
7654 /* Mark output sections of veneers needing a dedicated one with SEC_KEEP.  This
7655    ensures they are not marked for deletion by
7656    strip_excluded_output_sections () when veneers are going to be created
7657    later.  Not doing so would trigger assert on empty section size in
7658    lang_size_sections_1 ().  */
7659
7660 void
7661 bfd_elf32_arm_keep_private_stub_output_sections (struct bfd_link_info *info)
7662 {
7663   enum elf32_arm_stub_type stub_type;
7664
7665   /* If we are only performing a partial
7666      link do not bother adding the glue.  */
7667   if (bfd_link_relocatable (info))
7668     return;
7669
7670   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
7671     {
7672       asection *out_sec;
7673       const char *out_sec_name;
7674
7675       if (!arm_dedicated_stub_output_section_required (stub_type))
7676         continue;
7677
7678      out_sec_name = arm_dedicated_stub_output_section_name (stub_type);
7679      out_sec = bfd_get_section_by_name (info->output_bfd, out_sec_name);
7680      if (out_sec != NULL)
7681         out_sec->flags |= SEC_KEEP;
7682     }
7683 }
7684
7685 /* Select a BFD to be used to hold the sections used by the glue code.
7686    This function is called from the linker scripts in ld/emultempl/
7687    {armelf/pe}.em.  */
7688
7689 bfd_boolean
7690 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
7691 {
7692   struct elf32_arm_link_hash_table *globals;
7693
7694   /* If we are only performing a partial link
7695      do not bother getting a bfd to hold the glue.  */
7696   if (bfd_link_relocatable (info))
7697     return TRUE;
7698
7699   /* Make sure we don't attach the glue sections to a dynamic object.  */
7700   BFD_ASSERT (!(abfd->flags & DYNAMIC));
7701
7702   globals = elf32_arm_hash_table (info);
7703   BFD_ASSERT (globals != NULL);
7704
7705   if (globals->bfd_of_glue_owner != NULL)
7706     return TRUE;
7707
7708   /* Save the bfd for later use.  */
7709   globals->bfd_of_glue_owner = abfd;
7710
7711   return TRUE;
7712 }
7713
7714 static void
7715 check_use_blx (struct elf32_arm_link_hash_table *globals)
7716 {
7717   int cpu_arch;
7718
7719   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
7720                                        Tag_CPU_arch);
7721
7722   if (globals->fix_arm1176)
7723     {
7724       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
7725         globals->use_blx = 1;
7726     }
7727   else
7728     {
7729       if (cpu_arch > TAG_CPU_ARCH_V4T)
7730         globals->use_blx = 1;
7731     }
7732 }
7733
7734 bfd_boolean
7735 bfd_elf32_arm_process_before_allocation (bfd *abfd,
7736                                          struct bfd_link_info *link_info)
7737 {
7738   Elf_Internal_Shdr *symtab_hdr;
7739   Elf_Internal_Rela *internal_relocs = NULL;
7740   Elf_Internal_Rela *irel, *irelend;
7741   bfd_byte *contents = NULL;
7742
7743   asection *sec;
7744   struct elf32_arm_link_hash_table *globals;
7745
7746   /* If we are only performing a partial link do not bother
7747      to construct any glue.  */
7748   if (bfd_link_relocatable (link_info))
7749     return TRUE;
7750
7751   /* Here we have a bfd that is to be included on the link.  We have a
7752      hook to do reloc rummaging, before section sizes are nailed down.  */
7753   globals = elf32_arm_hash_table (link_info);
7754   BFD_ASSERT (globals != NULL);
7755
7756   check_use_blx (globals);
7757
7758   if (globals->byteswap_code && !bfd_big_endian (abfd))
7759     {
7760       _bfd_error_handler (_("%pB: BE8 images only valid in big-endian mode"),
7761                           abfd);
7762       return FALSE;
7763     }
7764
7765   /* PR 5398: If we have not decided to include any loadable sections in
7766      the output then we will not have a glue owner bfd.  This is OK, it
7767      just means that there is nothing else for us to do here.  */
7768   if (globals->bfd_of_glue_owner == NULL)
7769     return TRUE;
7770
7771   /* Rummage around all the relocs and map the glue vectors.  */
7772   sec = abfd->sections;
7773
7774   if (sec == NULL)
7775     return TRUE;
7776
7777   for (; sec != NULL; sec = sec->next)
7778     {
7779       if (sec->reloc_count == 0)
7780         continue;
7781
7782       if ((sec->flags & SEC_EXCLUDE) != 0)
7783         continue;
7784
7785       symtab_hdr = & elf_symtab_hdr (abfd);
7786
7787       /* Load the relocs.  */
7788       internal_relocs
7789         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
7790
7791       if (internal_relocs == NULL)
7792         goto error_return;
7793
7794       irelend = internal_relocs + sec->reloc_count;
7795       for (irel = internal_relocs; irel < irelend; irel++)
7796         {
7797           long r_type;
7798           unsigned long r_index;
7799
7800           struct elf_link_hash_entry *h;
7801
7802           r_type = ELF32_R_TYPE (irel->r_info);
7803           r_index = ELF32_R_SYM (irel->r_info);
7804
7805           /* These are the only relocation types we care about.  */
7806           if (   r_type != R_ARM_PC24
7807               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
7808             continue;
7809
7810           /* Get the section contents if we haven't done so already.  */
7811           if (contents == NULL)
7812             {
7813               /* Get cached copy if it exists.  */
7814               if (elf_section_data (sec)->this_hdr.contents != NULL)
7815                 contents = elf_section_data (sec)->this_hdr.contents;
7816               else
7817                 {
7818                   /* Go get them off disk.  */
7819                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7820                     goto error_return;
7821                 }
7822             }
7823
7824           if (r_type == R_ARM_V4BX)
7825             {
7826               int reg;
7827
7828               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
7829               record_arm_bx_glue (link_info, reg);
7830               continue;
7831             }
7832
7833           /* If the relocation is not against a symbol it cannot concern us.  */
7834           h = NULL;
7835
7836           /* We don't care about local symbols.  */
7837           if (r_index < symtab_hdr->sh_info)
7838             continue;
7839
7840           /* This is an external symbol.  */
7841           r_index -= symtab_hdr->sh_info;
7842           h = (struct elf_link_hash_entry *)
7843             elf_sym_hashes (abfd)[r_index];
7844
7845           /* If the relocation is against a static symbol it must be within
7846              the current section and so cannot be a cross ARM/Thumb relocation.  */
7847           if (h == NULL)
7848             continue;
7849
7850           /* If the call will go through a PLT entry then we do not need
7851              glue.  */
7852           if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
7853             continue;
7854
7855           switch (r_type)
7856             {
7857             case R_ARM_PC24:
7858               /* This one is a call from arm code.  We need to look up
7859                  the target of the call.  If it is a thumb target, we
7860                  insert glue.  */
7861               if (ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
7862                   == ST_BRANCH_TO_THUMB)
7863                 record_arm_to_thumb_glue (link_info, h);
7864               break;
7865
7866             default:
7867               abort ();
7868             }
7869         }
7870
7871       if (contents != NULL
7872           && elf_section_data (sec)->this_hdr.contents != contents)
7873         free (contents);
7874       contents = NULL;
7875
7876       if (internal_relocs != NULL
7877           && elf_section_data (sec)->relocs != internal_relocs)
7878         free (internal_relocs);
7879       internal_relocs = NULL;
7880     }
7881
7882   return TRUE;
7883
7884 error_return:
7885   if (contents != NULL
7886       && elf_section_data (sec)->this_hdr.contents != contents)
7887     free (contents);
7888   if (internal_relocs != NULL
7889       && elf_section_data (sec)->relocs != internal_relocs)
7890     free (internal_relocs);
7891
7892   return FALSE;
7893 }
7894 #endif
7895
7896
7897 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
7898
7899 void
7900 bfd_elf32_arm_init_maps (bfd *abfd)
7901 {
7902   Elf_Internal_Sym *isymbuf;
7903   Elf_Internal_Shdr *hdr;
7904   unsigned int i, localsyms;
7905
7906   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
7907   if (! is_arm_elf (abfd))
7908     return;
7909
7910   if ((abfd->flags & DYNAMIC) != 0)
7911     return;
7912
7913   hdr = & elf_symtab_hdr (abfd);
7914   localsyms = hdr->sh_info;
7915
7916   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
7917      should contain the number of local symbols, which should come before any
7918      global symbols.  Mapping symbols are always local.  */
7919   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
7920                                   NULL);
7921
7922   /* No internal symbols read?  Skip this BFD.  */
7923   if (isymbuf == NULL)
7924     return;
7925
7926   for (i = 0; i < localsyms; i++)
7927     {
7928       Elf_Internal_Sym *isym = &isymbuf[i];
7929       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7930       const char *name;
7931
7932       if (sec != NULL
7933           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
7934         {
7935           name = bfd_elf_string_from_elf_section (abfd,
7936             hdr->sh_link, isym->st_name);
7937
7938           if (bfd_is_arm_special_symbol_name (name,
7939                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
7940             elf32_arm_section_map_add (sec, name[1], isym->st_value);
7941         }
7942     }
7943 }
7944
7945
7946 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
7947    say what they wanted.  */
7948
7949 void
7950 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
7951 {
7952   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7953   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
7954
7955   if (globals == NULL)
7956     return;
7957
7958   if (globals->fix_cortex_a8 == -1)
7959     {
7960       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
7961       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
7962           && (out_attr[Tag_CPU_arch_profile].i == 'A'
7963               || out_attr[Tag_CPU_arch_profile].i == 0))
7964         globals->fix_cortex_a8 = 1;
7965       else
7966         globals->fix_cortex_a8 = 0;
7967     }
7968 }
7969
7970
7971 void
7972 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
7973 {
7974   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7975   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
7976
7977   if (globals == NULL)
7978     return;
7979   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
7980   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
7981     {
7982       switch (globals->vfp11_fix)
7983         {
7984         case BFD_ARM_VFP11_FIX_DEFAULT:
7985         case BFD_ARM_VFP11_FIX_NONE:
7986           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
7987           break;
7988
7989         default:
7990           /* Give a warning, but do as the user requests anyway.  */
7991           _bfd_error_handler (_("%pB: warning: selected VFP11 erratum "
7992             "workaround is not necessary for target architecture"), obfd);
7993         }
7994     }
7995   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
7996     /* For earlier architectures, we might need the workaround, but do not
7997        enable it by default.  If users is running with broken hardware, they
7998        must enable the erratum fix explicitly.  */
7999     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8000 }
8001
8002 void
8003 bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
8004 {
8005   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8006   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8007
8008   if (globals == NULL)
8009     return;
8010
8011   /* We assume only Cortex-M4 may require the fix.  */
8012   if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
8013       || out_attr[Tag_CPU_arch_profile].i != 'M')
8014     {
8015       if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
8016         /* Give a warning, but do as the user requests anyway.  */
8017         _bfd_error_handler
8018           (_("%pB: warning: selected STM32L4XX erratum "
8019              "workaround is not necessary for target architecture"), obfd);
8020     }
8021 }
8022
8023 enum bfd_arm_vfp11_pipe
8024 {
8025   VFP11_FMAC,
8026   VFP11_LS,
8027   VFP11_DS,
8028   VFP11_BAD
8029 };
8030
8031 /* Return a VFP register number.  This is encoded as RX:X for single-precision
8032    registers, or X:RX for double-precision registers, where RX is the group of
8033    four bits in the instruction encoding and X is the single extension bit.
8034    RX and X fields are specified using their lowest (starting) bit.  The return
8035    value is:
8036
8037      0...31: single-precision registers s0...s31
8038      32...63: double-precision registers d0...d31.
8039
8040    Although X should be zero for VFP11 (encoding d0...d15 only), we might
8041    encounter VFP3 instructions, so we allow the full range for DP registers.  */
8042
8043 static unsigned int
8044 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
8045                      unsigned int x)
8046 {
8047   if (is_double)
8048     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
8049   else
8050     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
8051 }
8052
8053 /* Set bits in *WMASK according to a register number REG as encoded by
8054    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
8055
8056 static void
8057 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
8058 {
8059   if (reg < 32)
8060     *wmask |= 1 << reg;
8061   else if (reg < 48)
8062     *wmask |= 3 << ((reg - 32) * 2);
8063 }
8064
8065 /* Return TRUE if WMASK overwrites anything in REGS.  */
8066
8067 static bfd_boolean
8068 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
8069 {
8070   int i;
8071
8072   for (i = 0; i < numregs; i++)
8073     {
8074       unsigned int reg = regs[i];
8075
8076       if (reg < 32 && (wmask & (1 << reg)) != 0)
8077         return TRUE;
8078
8079       reg -= 32;
8080
8081       if (reg >= 16)
8082         continue;
8083
8084       if ((wmask & (3 << (reg * 2))) != 0)
8085         return TRUE;
8086     }
8087
8088   return FALSE;
8089 }
8090
8091 /* In this function, we're interested in two things: finding input registers
8092    for VFP data-processing instructions, and finding the set of registers which
8093    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
8094    hold the written set, so FLDM etc. are easy to deal with (we're only
8095    interested in 32 SP registers or 16 dp registers, due to the VFP version
8096    implemented by the chip in question).  DP registers are marked by setting
8097    both SP registers in the write mask).  */
8098
8099 static enum bfd_arm_vfp11_pipe
8100 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
8101                            int *numregs)
8102 {
8103   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
8104   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
8105
8106   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
8107     {
8108       unsigned int pqrs;
8109       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8110       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8111
8112       pqrs = ((insn & 0x00800000) >> 20)
8113            | ((insn & 0x00300000) >> 19)
8114            | ((insn & 0x00000040) >> 6);
8115
8116       switch (pqrs)
8117         {
8118         case 0: /* fmac[sd].  */
8119         case 1: /* fnmac[sd].  */
8120         case 2: /* fmsc[sd].  */
8121         case 3: /* fnmsc[sd].  */
8122           vpipe = VFP11_FMAC;
8123           bfd_arm_vfp11_write_mask (destmask, fd);
8124           regs[0] = fd;
8125           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
8126           regs[2] = fm;
8127           *numregs = 3;
8128           break;
8129
8130         case 4: /* fmul[sd].  */
8131         case 5: /* fnmul[sd].  */
8132         case 6: /* fadd[sd].  */
8133         case 7: /* fsub[sd].  */
8134           vpipe = VFP11_FMAC;
8135           goto vfp_binop;
8136
8137         case 8: /* fdiv[sd].  */
8138           vpipe = VFP11_DS;
8139           vfp_binop:
8140           bfd_arm_vfp11_write_mask (destmask, fd);
8141           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
8142           regs[1] = fm;
8143           *numregs = 2;
8144           break;
8145
8146         case 15: /* extended opcode.  */
8147           {
8148             unsigned int extn = ((insn >> 15) & 0x1e)
8149                               | ((insn >> 7) & 1);
8150
8151             switch (extn)
8152               {
8153               case 0: /* fcpy[sd].  */
8154               case 1: /* fabs[sd].  */
8155               case 2: /* fneg[sd].  */
8156               case 8: /* fcmp[sd].  */
8157               case 9: /* fcmpe[sd].  */
8158               case 10: /* fcmpz[sd].  */
8159               case 11: /* fcmpez[sd].  */
8160               case 16: /* fuito[sd].  */
8161               case 17: /* fsito[sd].  */
8162               case 24: /* ftoui[sd].  */
8163               case 25: /* ftouiz[sd].  */
8164               case 26: /* ftosi[sd].  */
8165               case 27: /* ftosiz[sd].  */
8166                 /* These instructions will not bounce due to underflow.  */
8167                 *numregs = 0;
8168                 vpipe = VFP11_FMAC;
8169                 break;
8170
8171               case 3: /* fsqrt[sd].  */
8172                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
8173                    registers to cause the erratum in previous instructions.  */
8174                 bfd_arm_vfp11_write_mask (destmask, fd);
8175                 vpipe = VFP11_DS;
8176                 break;
8177
8178               case 15: /* fcvt{ds,sd}.  */
8179                 {
8180                   int rnum = 0;
8181
8182                   bfd_arm_vfp11_write_mask (destmask, fd);
8183
8184                   /* Only FCVTSD can underflow.  */
8185                   if ((insn & 0x100) != 0)
8186                     regs[rnum++] = fm;
8187
8188                   *numregs = rnum;
8189
8190                   vpipe = VFP11_FMAC;
8191                 }
8192                 break;
8193
8194               default:
8195                 return VFP11_BAD;
8196               }
8197           }
8198           break;
8199
8200         default:
8201           return VFP11_BAD;
8202         }
8203     }
8204   /* Two-register transfer.  */
8205   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
8206     {
8207       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8208
8209       if ((insn & 0x100000) == 0)
8210         {
8211           if (is_double)
8212             bfd_arm_vfp11_write_mask (destmask, fm);
8213           else
8214             {
8215               bfd_arm_vfp11_write_mask (destmask, fm);
8216               bfd_arm_vfp11_write_mask (destmask, fm + 1);
8217             }
8218         }
8219
8220       vpipe = VFP11_LS;
8221     }
8222   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
8223     {
8224       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8225       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
8226
8227       switch (puw)
8228         {
8229         case 0: /* Two-reg transfer.  We should catch these above.  */
8230           abort ();
8231
8232         case 2: /* fldm[sdx].  */
8233         case 3:
8234         case 5:
8235           {
8236             unsigned int i, offset = insn & 0xff;
8237
8238             if (is_double)
8239               offset >>= 1;
8240
8241             for (i = fd; i < fd + offset; i++)
8242               bfd_arm_vfp11_write_mask (destmask, i);
8243           }
8244           break;
8245
8246         case 4: /* fld[sd].  */
8247         case 6:
8248           bfd_arm_vfp11_write_mask (destmask, fd);
8249           break;
8250
8251         default:
8252           return VFP11_BAD;
8253         }
8254
8255       vpipe = VFP11_LS;
8256     }
8257   /* Single-register transfer. Note L==0.  */
8258   else if ((insn & 0x0f100e10) == 0x0e000a10)
8259     {
8260       unsigned int opcode = (insn >> 21) & 7;
8261       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
8262
8263       switch (opcode)
8264         {
8265         case 0: /* fmsr/fmdlr.  */
8266         case 1: /* fmdhr.  */
8267           /* Mark fmdhr and fmdlr as writing to the whole of the DP
8268              destination register.  I don't know if this is exactly right,
8269              but it is the conservative choice.  */
8270           bfd_arm_vfp11_write_mask (destmask, fn);
8271           break;
8272
8273         case 7: /* fmxr.  */
8274           break;
8275         }
8276
8277       vpipe = VFP11_LS;
8278     }
8279
8280   return vpipe;
8281 }
8282
8283
8284 static int elf32_arm_compare_mapping (const void * a, const void * b);
8285
8286
8287 /* Look for potentially-troublesome code sequences which might trigger the
8288    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
8289    (available from ARM) for details of the erratum.  A short version is
8290    described in ld.texinfo.  */
8291
8292 bfd_boolean
8293 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
8294 {
8295   asection *sec;
8296   bfd_byte *contents = NULL;
8297   int state = 0;
8298   int regs[3], numregs = 0;
8299   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8300   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
8301
8302   if (globals == NULL)
8303     return FALSE;
8304
8305   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
8306      The states transition as follows:
8307
8308        0 -> 1 (vector) or 0 -> 2 (scalar)
8309            A VFP FMAC-pipeline instruction has been seen. Fill
8310            regs[0]..regs[numregs-1] with its input operands. Remember this
8311            instruction in 'first_fmac'.
8312
8313        1 -> 2
8314            Any instruction, except for a VFP instruction which overwrites
8315            regs[*].
8316
8317        1 -> 3 [ -> 0 ]  or
8318        2 -> 3 [ -> 0 ]
8319            A VFP instruction has been seen which overwrites any of regs[*].
8320            We must make a veneer!  Reset state to 0 before examining next
8321            instruction.
8322
8323        2 -> 0
8324            If we fail to match anything in state 2, reset to state 0 and reset
8325            the instruction pointer to the instruction after 'first_fmac'.
8326
8327      If the VFP11 vector mode is in use, there must be at least two unrelated
8328      instructions between anti-dependent VFP11 instructions to properly avoid
8329      triggering the erratum, hence the use of the extra state 1.  */
8330
8331   /* If we are only performing a partial link do not bother
8332      to construct any glue.  */
8333   if (bfd_link_relocatable (link_info))
8334     return TRUE;
8335
8336   /* Skip if this bfd does not correspond to an ELF image.  */
8337   if (! is_arm_elf (abfd))
8338     return TRUE;
8339
8340   /* We should have chosen a fix type by the time we get here.  */
8341   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
8342
8343   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
8344     return TRUE;
8345
8346   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8347   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8348     return TRUE;
8349
8350   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8351     {
8352       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
8353       struct _arm_elf_section_data *sec_data;
8354
8355       /* If we don't have executable progbits, we're not interested in this
8356          section.  Also skip if section is to be excluded.  */
8357       if (elf_section_type (sec) != SHT_PROGBITS
8358           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8359           || (sec->flags & SEC_EXCLUDE) != 0
8360           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8361           || sec->output_section == bfd_abs_section_ptr
8362           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
8363         continue;
8364
8365       sec_data = elf32_arm_section_data (sec);
8366
8367       if (sec_data->mapcount == 0)
8368         continue;
8369
8370       if (elf_section_data (sec)->this_hdr.contents != NULL)
8371         contents = elf_section_data (sec)->this_hdr.contents;
8372       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8373         goto error_return;
8374
8375       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8376              elf32_arm_compare_mapping);
8377
8378       for (span = 0; span < sec_data->mapcount; span++)
8379         {
8380           unsigned int span_start = sec_data->map[span].vma;
8381           unsigned int span_end = (span == sec_data->mapcount - 1)
8382                                   ? sec->size : sec_data->map[span + 1].vma;
8383           char span_type = sec_data->map[span].type;
8384
8385           /* FIXME: Only ARM mode is supported at present.  We may need to
8386              support Thumb-2 mode also at some point.  */
8387           if (span_type != 'a')
8388             continue;
8389
8390           for (i = span_start; i < span_end;)
8391             {
8392               unsigned int next_i = i + 4;
8393               unsigned int insn = bfd_big_endian (abfd)
8394                 ? (contents[i] << 24)
8395                   | (contents[i + 1] << 16)
8396                   | (contents[i + 2] << 8)
8397                   | contents[i + 3]
8398                 : (contents[i + 3] << 24)
8399                   | (contents[i + 2] << 16)
8400                   | (contents[i + 1] << 8)
8401                   | contents[i];
8402               unsigned int writemask = 0;
8403               enum bfd_arm_vfp11_pipe vpipe;
8404
8405               switch (state)
8406                 {
8407                 case 0:
8408                   vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
8409                                                     &numregs);
8410                   /* I'm assuming the VFP11 erratum can trigger with denorm
8411                      operands on either the FMAC or the DS pipeline. This might
8412                      lead to slightly overenthusiastic veneer insertion.  */
8413                   if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
8414                     {
8415                       state = use_vector ? 1 : 2;
8416                       first_fmac = i;
8417                       veneer_of_insn = insn;
8418                     }
8419                   break;
8420
8421                 case 1:
8422                   {
8423                     int other_regs[3], other_numregs;
8424                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8425                                                       other_regs,
8426                                                       &other_numregs);
8427                     if (vpipe != VFP11_BAD
8428                         && bfd_arm_vfp11_antidependency (writemask, regs,
8429                                                          numregs))
8430                       state = 3;
8431                     else
8432                       state = 2;
8433                   }
8434                   break;
8435
8436                 case 2:
8437                   {
8438                     int other_regs[3], other_numregs;
8439                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8440                                                       other_regs,
8441                                                       &other_numregs);
8442                     if (vpipe != VFP11_BAD
8443                         && bfd_arm_vfp11_antidependency (writemask, regs,
8444                                                          numregs))
8445                       state = 3;
8446                     else
8447                       {
8448                         state = 0;
8449                         next_i = first_fmac + 4;
8450                       }
8451                   }
8452                   break;
8453
8454                 case 3:
8455                   abort ();  /* Should be unreachable.  */
8456                 }
8457
8458               if (state == 3)
8459                 {
8460                   elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
8461                       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
8462
8463                   elf32_arm_section_data (sec)->erratumcount += 1;
8464
8465                   newerr->u.b.vfp_insn = veneer_of_insn;
8466
8467                   switch (span_type)
8468                     {
8469                     case 'a':
8470                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
8471                       break;
8472
8473                     default:
8474                       abort ();
8475                     }
8476
8477                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
8478                                                first_fmac);
8479
8480                   newerr->vma = -1;
8481
8482                   newerr->next = sec_data->erratumlist;
8483                   sec_data->erratumlist = newerr;
8484
8485                   state = 0;
8486                 }
8487
8488               i = next_i;
8489             }
8490         }
8491
8492       if (contents != NULL
8493           && elf_section_data (sec)->this_hdr.contents != contents)
8494         free (contents);
8495       contents = NULL;
8496     }
8497
8498   return TRUE;
8499
8500 error_return:
8501   if (contents != NULL
8502       && elf_section_data (sec)->this_hdr.contents != contents)
8503     free (contents);
8504
8505   return FALSE;
8506 }
8507
8508 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
8509    after sections have been laid out, using specially-named symbols.  */
8510
8511 void
8512 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
8513                                           struct bfd_link_info *link_info)
8514 {
8515   asection *sec;
8516   struct elf32_arm_link_hash_table *globals;
8517   char *tmp_name;
8518
8519   if (bfd_link_relocatable (link_info))
8520     return;
8521
8522   /* Skip if this bfd does not correspond to an ELF image.  */
8523   if (! is_arm_elf (abfd))
8524     return;
8525
8526   globals = elf32_arm_hash_table (link_info);
8527   if (globals == NULL)
8528     return;
8529
8530   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8531                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
8532
8533   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8534     {
8535       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8536       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
8537
8538       for (; errnode != NULL; errnode = errnode->next)
8539         {
8540           struct elf_link_hash_entry *myh;
8541           bfd_vma vma;
8542
8543           switch (errnode->type)
8544             {
8545             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
8546             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
8547               /* Find veneer symbol.  */
8548               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
8549                        errnode->u.b.veneer->u.v.id);
8550
8551               myh = elf_link_hash_lookup
8552                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8553
8554               if (myh == NULL)
8555                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8556                                     abfd, "VFP11", tmp_name);
8557
8558               vma = myh->root.u.def.section->output_section->vma
8559                     + myh->root.u.def.section->output_offset
8560                     + myh->root.u.def.value;
8561
8562               errnode->u.b.veneer->vma = vma;
8563               break;
8564
8565             case VFP11_ERRATUM_ARM_VENEER:
8566             case VFP11_ERRATUM_THUMB_VENEER:
8567               /* Find return location.  */
8568               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
8569                        errnode->u.v.id);
8570
8571               myh = elf_link_hash_lookup
8572                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8573
8574               if (myh == NULL)
8575                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8576                                     abfd, "VFP11", tmp_name);
8577
8578               vma = myh->root.u.def.section->output_section->vma
8579                     + myh->root.u.def.section->output_offset
8580                     + myh->root.u.def.value;
8581
8582               errnode->u.v.branch->vma = vma;
8583               break;
8584
8585             default:
8586               abort ();
8587             }
8588         }
8589     }
8590
8591   free (tmp_name);
8592 }
8593
8594 /* Find virtual-memory addresses for STM32L4XX erratum veneers and
8595    return locations after sections have been laid out, using
8596    specially-named symbols.  */
8597
8598 void
8599 bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
8600                                               struct bfd_link_info *link_info)
8601 {
8602   asection *sec;
8603   struct elf32_arm_link_hash_table *globals;
8604   char *tmp_name;
8605
8606   if (bfd_link_relocatable (link_info))
8607     return;
8608
8609   /* Skip if this bfd does not correspond to an ELF image.  */
8610   if (! is_arm_elf (abfd))
8611     return;
8612
8613   globals = elf32_arm_hash_table (link_info);
8614   if (globals == NULL)
8615     return;
8616
8617   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8618                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
8619
8620   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8621     {
8622       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8623       elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
8624
8625       for (; errnode != NULL; errnode = errnode->next)
8626         {
8627           struct elf_link_hash_entry *myh;
8628           bfd_vma vma;
8629
8630           switch (errnode->type)
8631             {
8632             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
8633               /* Find veneer symbol.  */
8634               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
8635                        errnode->u.b.veneer->u.v.id);
8636
8637               myh = elf_link_hash_lookup
8638                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8639
8640               if (myh == NULL)
8641                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8642                                     abfd, "STM32L4XX", tmp_name);
8643
8644               vma = myh->root.u.def.section->output_section->vma
8645                 + myh->root.u.def.section->output_offset
8646                 + myh->root.u.def.value;
8647
8648               errnode->u.b.veneer->vma = vma;
8649               break;
8650
8651             case STM32L4XX_ERRATUM_VENEER:
8652               /* Find return location.  */
8653               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
8654                        errnode->u.v.id);
8655
8656               myh = elf_link_hash_lookup
8657                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8658
8659               if (myh == NULL)
8660                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8661                                     abfd, "STM32L4XX", tmp_name);
8662
8663               vma = myh->root.u.def.section->output_section->vma
8664                 + myh->root.u.def.section->output_offset
8665                 + myh->root.u.def.value;
8666
8667               errnode->u.v.branch->vma = vma;
8668               break;
8669
8670             default:
8671               abort ();
8672             }
8673         }
8674     }
8675
8676   free (tmp_name);
8677 }
8678
8679 static inline bfd_boolean
8680 is_thumb2_ldmia (const insn32 insn)
8681 {
8682   /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
8683      1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
8684   return (insn & 0xffd02000) == 0xe8900000;
8685 }
8686
8687 static inline bfd_boolean
8688 is_thumb2_ldmdb (const insn32 insn)
8689 {
8690   /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
8691      1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
8692   return (insn & 0xffd02000) == 0xe9100000;
8693 }
8694
8695 static inline bfd_boolean
8696 is_thumb2_vldm (const insn32 insn)
8697 {
8698   /* A6.5 Extension register load or store instruction
8699      A7.7.229
8700      We look for SP 32-bit and DP 64-bit registers.
8701      Encoding T1 VLDM{mode}<c> <Rn>{!}, <list>
8702      <list> is consecutive 64-bit registers
8703      1110 - 110P - UDW1 - rrrr - vvvv - 1011 - iiii - iiii
8704      Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
8705      <list> is consecutive 32-bit registers
8706      1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
8707      if P==0 && U==1 && W==1 && Rn=1101 VPOP
8708      if PUW=010 || PUW=011 || PUW=101 VLDM.  */
8709   return
8710     (((insn & 0xfe100f00) == 0xec100b00) ||
8711      ((insn & 0xfe100f00) == 0xec100a00))
8712     && /* (IA without !).  */
8713     (((((insn << 7) >> 28) & 0xd) == 0x4)
8714      /* (IA with !), includes VPOP (when reg number is SP).  */
8715      || ((((insn << 7) >> 28) & 0xd) == 0x5)
8716      /* (DB with !).  */
8717      || ((((insn << 7) >> 28) & 0xd) == 0x9));
8718 }
8719
8720 /* STM STM32L4XX erratum : This function assumes that it receives an LDM or
8721    VLDM opcode and:
8722  - computes the number and the mode of memory accesses
8723  - decides if the replacement should be done:
8724    . replaces only if > 8-word accesses
8725    . or (testing purposes only) replaces all accesses.  */
8726
8727 static bfd_boolean
8728 stm32l4xx_need_create_replacing_stub (const insn32 insn,
8729                                       bfd_arm_stm32l4xx_fix stm32l4xx_fix)
8730 {
8731   int nb_words = 0;
8732
8733   /* The field encoding the register list is the same for both LDMIA
8734      and LDMDB encodings.  */
8735   if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
8736     nb_words = elf32_arm_popcount (insn & 0x0000ffff);
8737   else if (is_thumb2_vldm (insn))
8738    nb_words = (insn & 0xff);
8739
8740   /* DEFAULT mode accounts for the real bug condition situation,
8741      ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
8742   return
8743     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT) ? nb_words > 8 :
8744     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL) ? TRUE : FALSE;
8745 }
8746
8747 /* Look for potentially-troublesome code sequences which might trigger
8748    the STM STM32L4XX erratum.  */
8749
8750 bfd_boolean
8751 bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
8752                                       struct bfd_link_info *link_info)
8753 {
8754   asection *sec;
8755   bfd_byte *contents = NULL;
8756   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8757
8758   if (globals == NULL)
8759     return FALSE;
8760
8761   /* If we are only performing a partial link do not bother
8762      to construct any glue.  */
8763   if (bfd_link_relocatable (link_info))
8764     return TRUE;
8765
8766   /* Skip if this bfd does not correspond to an ELF image.  */
8767   if (! is_arm_elf (abfd))
8768     return TRUE;
8769
8770   if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
8771     return TRUE;
8772
8773   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8774   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8775     return TRUE;
8776
8777   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8778     {
8779       unsigned int i, span;
8780       struct _arm_elf_section_data *sec_data;
8781
8782       /* If we don't have executable progbits, we're not interested in this
8783          section.  Also skip if section is to be excluded.  */
8784       if (elf_section_type (sec) != SHT_PROGBITS
8785           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8786           || (sec->flags & SEC_EXCLUDE) != 0
8787           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8788           || sec->output_section == bfd_abs_section_ptr
8789           || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
8790         continue;
8791
8792       sec_data = elf32_arm_section_data (sec);
8793
8794       if (sec_data->mapcount == 0)
8795         continue;
8796
8797       if (elf_section_data (sec)->this_hdr.contents != NULL)
8798         contents = elf_section_data (sec)->this_hdr.contents;
8799       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8800         goto error_return;
8801
8802       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8803              elf32_arm_compare_mapping);
8804
8805       for (span = 0; span < sec_data->mapcount; span++)
8806         {
8807           unsigned int span_start = sec_data->map[span].vma;
8808           unsigned int span_end = (span == sec_data->mapcount - 1)
8809             ? sec->size : sec_data->map[span + 1].vma;
8810           char span_type = sec_data->map[span].type;
8811           int itblock_current_pos = 0;
8812
8813           /* Only Thumb2 mode need be supported with this CM4 specific
8814              code, we should not encounter any arm mode eg span_type
8815              != 'a'.  */
8816           if (span_type != 't')
8817             continue;
8818
8819           for (i = span_start; i < span_end;)
8820             {
8821               unsigned int insn = bfd_get_16 (abfd, &contents[i]);
8822               bfd_boolean insn_32bit = FALSE;
8823               bfd_boolean is_ldm = FALSE;
8824               bfd_boolean is_vldm = FALSE;
8825               bfd_boolean is_not_last_in_it_block = FALSE;
8826
8827               /* The first 16-bits of all 32-bit thumb2 instructions start
8828                  with opcode[15..13]=0b111 and the encoded op1 can be anything
8829                  except opcode[12..11]!=0b00.
8830                  See 32-bit Thumb instruction encoding.  */
8831               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
8832                 insn_32bit = TRUE;
8833
8834               /* Compute the predicate that tells if the instruction
8835                  is concerned by the IT block
8836                  - Creates an error if there is a ldm that is not
8837                    last in the IT block thus cannot be replaced
8838                  - Otherwise we can create a branch at the end of the
8839                    IT block, it will be controlled naturally by IT
8840                    with the proper pseudo-predicate
8841                  - So the only interesting predicate is the one that
8842                    tells that we are not on the last item of an IT
8843                    block.  */
8844               if (itblock_current_pos != 0)
8845                   is_not_last_in_it_block = !!--itblock_current_pos;
8846
8847               if (insn_32bit)
8848                 {
8849                   /* Load the rest of the insn (in manual-friendly order).  */
8850                   insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
8851                   is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
8852                   is_vldm = is_thumb2_vldm (insn);
8853
8854                   /* Veneers are created for (v)ldm depending on
8855                      option flags and memory accesses conditions; but
8856                      if the instruction is not the last instruction of
8857                      an IT block, we cannot create a jump there, so we
8858                      bail out.  */
8859                     if ((is_ldm || is_vldm)
8860                         && stm32l4xx_need_create_replacing_stub
8861                         (insn, globals->stm32l4xx_fix))
8862                       {
8863                         if (is_not_last_in_it_block)
8864                           {
8865                             _bfd_error_handler
8866                               /* xgettext:c-format */
8867                               (_("%pB(%pA+%#x): error: multiple load detected"
8868                                  " in non-last IT block instruction:"
8869                                  " STM32L4XX veneer cannot be generated; "
8870                                  "use gcc option -mrestrict-it to generate"
8871                                  " only one instruction per IT block"),
8872                                abfd, sec, i);
8873                           }
8874                         else
8875                           {
8876                             elf32_stm32l4xx_erratum_list *newerr =
8877                               (elf32_stm32l4xx_erratum_list *)
8878                               bfd_zmalloc
8879                               (sizeof (elf32_stm32l4xx_erratum_list));
8880
8881                             elf32_arm_section_data (sec)
8882                               ->stm32l4xx_erratumcount += 1;
8883                             newerr->u.b.insn = insn;
8884                             /* We create only thumb branches.  */
8885                             newerr->type =
8886                               STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
8887                             record_stm32l4xx_erratum_veneer
8888                               (link_info, newerr, abfd, sec,
8889                                i,
8890                                is_ldm ?
8891                                STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
8892                                STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
8893                             newerr->vma = -1;
8894                             newerr->next = sec_data->stm32l4xx_erratumlist;
8895                             sec_data->stm32l4xx_erratumlist = newerr;
8896                           }
8897                       }
8898                 }
8899               else
8900                 {
8901                   /* A7.7.37 IT p208
8902                      IT blocks are only encoded in T1
8903                      Encoding T1: IT{x{y{z}}} <firstcond>
8904                      1 0 1 1 - 1 1 1 1 - firstcond - mask
8905                      if mask = '0000' then see 'related encodings'
8906                      We don't deal with UNPREDICTABLE, just ignore these.
8907                      There can be no nested IT blocks so an IT block
8908                      is naturally a new one for which it is worth
8909                      computing its size.  */
8910                   bfd_boolean is_newitblock = ((insn & 0xff00) == 0xbf00)
8911                     && ((insn & 0x000f) != 0x0000);
8912                   /* If we have a new IT block we compute its size.  */
8913                   if (is_newitblock)
8914                     {
8915                       /* Compute the number of instructions controlled
8916                          by the IT block, it will be used to decide
8917                          whether we are inside an IT block or not.  */
8918                       unsigned int mask = insn & 0x000f;
8919                       itblock_current_pos = 4 - ctz (mask);
8920                     }
8921                 }
8922
8923               i += insn_32bit ? 4 : 2;
8924             }
8925         }
8926
8927       if (contents != NULL
8928           && elf_section_data (sec)->this_hdr.contents != contents)
8929         free (contents);
8930       contents = NULL;
8931     }
8932
8933   return TRUE;
8934
8935 error_return:
8936   if (contents != NULL
8937       && elf_section_data (sec)->this_hdr.contents != contents)
8938     free (contents);
8939
8940   return FALSE;
8941 }
8942
8943 /* Set target relocation values needed during linking.  */
8944
8945 void
8946 bfd_elf32_arm_set_target_params (struct bfd *output_bfd,
8947                                  struct bfd_link_info *link_info,
8948                                  struct elf32_arm_params *params)
8949 {
8950   struct elf32_arm_link_hash_table *globals;
8951
8952   globals = elf32_arm_hash_table (link_info);
8953   if (globals == NULL)
8954     return;
8955
8956   globals->target1_is_rel = params->target1_is_rel;
8957   if (strcmp (params->target2_type, "rel") == 0)
8958     globals->target2_reloc = R_ARM_REL32;
8959   else if (strcmp (params->target2_type, "abs") == 0)
8960     globals->target2_reloc = R_ARM_ABS32;
8961   else if (strcmp (params->target2_type, "got-rel") == 0)
8962     globals->target2_reloc = R_ARM_GOT_PREL;
8963   else
8964     {
8965       _bfd_error_handler (_("invalid TARGET2 relocation type '%s'"),
8966                           params->target2_type);
8967     }
8968   globals->fix_v4bx = params->fix_v4bx;
8969   globals->use_blx |= params->use_blx;
8970   globals->vfp11_fix = params->vfp11_denorm_fix;
8971   globals->stm32l4xx_fix = params->stm32l4xx_fix;
8972   if (globals->fdpic_p)
8973     globals->pic_veneer = 1;
8974   else
8975     globals->pic_veneer = params->pic_veneer;
8976   globals->fix_cortex_a8 = params->fix_cortex_a8;
8977   globals->fix_arm1176 = params->fix_arm1176;
8978   globals->cmse_implib = params->cmse_implib;
8979   globals->in_implib_bfd = params->in_implib_bfd;
8980
8981   BFD_ASSERT (is_arm_elf (output_bfd));
8982   elf_arm_tdata (output_bfd)->no_enum_size_warning
8983     = params->no_enum_size_warning;
8984   elf_arm_tdata (output_bfd)->no_wchar_size_warning
8985     = params->no_wchar_size_warning;
8986 }
8987
8988 /* Replace the target offset of a Thumb bl or b.w instruction.  */
8989
8990 static void
8991 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
8992 {
8993   bfd_vma upper;
8994   bfd_vma lower;
8995   int reloc_sign;
8996
8997   BFD_ASSERT ((offset & 1) == 0);
8998
8999   upper = bfd_get_16 (abfd, insn);
9000   lower = bfd_get_16 (abfd, insn + 2);
9001   reloc_sign = (offset < 0) ? 1 : 0;
9002   upper = (upper & ~(bfd_vma) 0x7ff)
9003           | ((offset >> 12) & 0x3ff)
9004           | (reloc_sign << 10);
9005   lower = (lower & ~(bfd_vma) 0x2fff)
9006           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
9007           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
9008           | ((offset >> 1) & 0x7ff);
9009   bfd_put_16 (abfd, upper, insn);
9010   bfd_put_16 (abfd, lower, insn + 2);
9011 }
9012
9013 /* Thumb code calling an ARM function.  */
9014
9015 static int
9016 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
9017                          const char *           name,
9018                          bfd *                  input_bfd,
9019                          bfd *                  output_bfd,
9020                          asection *             input_section,
9021                          bfd_byte *             hit_data,
9022                          asection *             sym_sec,
9023                          bfd_vma                offset,
9024                          bfd_signed_vma         addend,
9025                          bfd_vma                val,
9026                          char **error_message)
9027 {
9028   asection * s = 0;
9029   bfd_vma my_offset;
9030   long int ret_offset;
9031   struct elf_link_hash_entry * myh;
9032   struct elf32_arm_link_hash_table * globals;
9033
9034   myh = find_thumb_glue (info, name, error_message);
9035   if (myh == NULL)
9036     return FALSE;
9037
9038   globals = elf32_arm_hash_table (info);
9039   BFD_ASSERT (globals != NULL);
9040   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9041
9042   my_offset = myh->root.u.def.value;
9043
9044   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9045                               THUMB2ARM_GLUE_SECTION_NAME);
9046
9047   BFD_ASSERT (s != NULL);
9048   BFD_ASSERT (s->contents != NULL);
9049   BFD_ASSERT (s->output_section != NULL);
9050
9051   if ((my_offset & 0x01) == 0x01)
9052     {
9053       if (sym_sec != NULL
9054           && sym_sec->owner != NULL
9055           && !INTERWORK_FLAG (sym_sec->owner))
9056         {
9057           _bfd_error_handler
9058             (_("%pB(%s): warning: interworking not enabled;"
9059                " first occurrence: %pB: %s call to %s"),
9060              sym_sec->owner, name, input_bfd, "Thumb", "ARM");
9061
9062           return FALSE;
9063         }
9064
9065       --my_offset;
9066       myh->root.u.def.value = my_offset;
9067
9068       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
9069                       s->contents + my_offset);
9070
9071       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
9072                       s->contents + my_offset + 2);
9073
9074       ret_offset =
9075         /* Address of destination of the stub.  */
9076         ((bfd_signed_vma) val)
9077         - ((bfd_signed_vma)
9078            /* Offset from the start of the current section
9079               to the start of the stubs.  */
9080            (s->output_offset
9081             /* Offset of the start of this stub from the start of the stubs.  */
9082             + my_offset
9083             /* Address of the start of the current section.  */
9084             + s->output_section->vma)
9085            /* The branch instruction is 4 bytes into the stub.  */
9086            + 4
9087            /* ARM branches work from the pc of the instruction + 8.  */
9088            + 8);
9089
9090       put_arm_insn (globals, output_bfd,
9091                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
9092                     s->contents + my_offset + 4);
9093     }
9094
9095   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
9096
9097   /* Now go back and fix up the original BL insn to point to here.  */
9098   ret_offset =
9099     /* Address of where the stub is located.  */
9100     (s->output_section->vma + s->output_offset + my_offset)
9101      /* Address of where the BL is located.  */
9102     - (input_section->output_section->vma + input_section->output_offset
9103        + offset)
9104     /* Addend in the relocation.  */
9105     - addend
9106     /* Biassing for PC-relative addressing.  */
9107     - 8;
9108
9109   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
9110
9111   return TRUE;
9112 }
9113
9114 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
9115
9116 static struct elf_link_hash_entry *
9117 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
9118                              const char *           name,
9119                              bfd *                  input_bfd,
9120                              bfd *                  output_bfd,
9121                              asection *             sym_sec,
9122                              bfd_vma                val,
9123                              asection *             s,
9124                              char **                error_message)
9125 {
9126   bfd_vma my_offset;
9127   long int ret_offset;
9128   struct elf_link_hash_entry * myh;
9129   struct elf32_arm_link_hash_table * globals;
9130
9131   myh = find_arm_glue (info, name, error_message);
9132   if (myh == NULL)
9133     return NULL;
9134
9135   globals = elf32_arm_hash_table (info);
9136   BFD_ASSERT (globals != NULL);
9137   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9138
9139   my_offset = myh->root.u.def.value;
9140
9141   if ((my_offset & 0x01) == 0x01)
9142     {
9143       if (sym_sec != NULL
9144           && sym_sec->owner != NULL
9145           && !INTERWORK_FLAG (sym_sec->owner))
9146         {
9147           _bfd_error_handler
9148             (_("%pB(%s): warning: interworking not enabled;"
9149                " first occurrence: %pB: %s call to %s"),
9150              sym_sec->owner, name, input_bfd, "ARM", "Thumb");
9151         }
9152
9153       --my_offset;
9154       myh->root.u.def.value = my_offset;
9155
9156       if (bfd_link_pic (info)
9157           || globals->root.is_relocatable_executable
9158           || globals->pic_veneer)
9159         {
9160           /* For relocatable objects we can't use absolute addresses,
9161              so construct the address from a relative offset.  */
9162           /* TODO: If the offset is small it's probably worth
9163              constructing the address with adds.  */
9164           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
9165                         s->contents + my_offset);
9166           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
9167                         s->contents + my_offset + 4);
9168           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
9169                         s->contents + my_offset + 8);
9170           /* Adjust the offset by 4 for the position of the add,
9171              and 8 for the pipeline offset.  */
9172           ret_offset = (val - (s->output_offset
9173                                + s->output_section->vma
9174                                + my_offset + 12))
9175                        | 1;
9176           bfd_put_32 (output_bfd, ret_offset,
9177                       s->contents + my_offset + 12);
9178         }
9179       else if (globals->use_blx)
9180         {
9181           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
9182                         s->contents + my_offset);
9183
9184           /* It's a thumb address.  Add the low order bit.  */
9185           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
9186                       s->contents + my_offset + 4);
9187         }
9188       else
9189         {
9190           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
9191                         s->contents + my_offset);
9192
9193           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
9194                         s->contents + my_offset + 4);
9195
9196           /* It's a thumb address.  Add the low order bit.  */
9197           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
9198                       s->contents + my_offset + 8);
9199
9200           my_offset += 12;
9201         }
9202     }
9203
9204   BFD_ASSERT (my_offset <= globals->arm_glue_size);
9205
9206   return myh;
9207 }
9208
9209 /* Arm code calling a Thumb function.  */
9210
9211 static int
9212 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
9213                          const char *           name,
9214                          bfd *                  input_bfd,
9215                          bfd *                  output_bfd,
9216                          asection *             input_section,
9217                          bfd_byte *             hit_data,
9218                          asection *             sym_sec,
9219                          bfd_vma                offset,
9220                          bfd_signed_vma         addend,
9221                          bfd_vma                val,
9222                          char **error_message)
9223 {
9224   unsigned long int tmp;
9225   bfd_vma my_offset;
9226   asection * s;
9227   long int ret_offset;
9228   struct elf_link_hash_entry * myh;
9229   struct elf32_arm_link_hash_table * globals;
9230
9231   globals = elf32_arm_hash_table (info);
9232   BFD_ASSERT (globals != NULL);
9233   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9234
9235   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9236                               ARM2THUMB_GLUE_SECTION_NAME);
9237   BFD_ASSERT (s != NULL);
9238   BFD_ASSERT (s->contents != NULL);
9239   BFD_ASSERT (s->output_section != NULL);
9240
9241   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
9242                                      sym_sec, val, s, error_message);
9243   if (!myh)
9244     return FALSE;
9245
9246   my_offset = myh->root.u.def.value;
9247   tmp = bfd_get_32 (input_bfd, hit_data);
9248   tmp = tmp & 0xFF000000;
9249
9250   /* Somehow these are both 4 too far, so subtract 8.  */
9251   ret_offset = (s->output_offset
9252                 + my_offset
9253                 + s->output_section->vma
9254                 - (input_section->output_offset
9255                    + input_section->output_section->vma
9256                    + offset + addend)
9257                 - 8);
9258
9259   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
9260
9261   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
9262
9263   return TRUE;
9264 }
9265
9266 /* Populate Arm stub for an exported Thumb function.  */
9267
9268 static bfd_boolean
9269 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
9270 {
9271   struct bfd_link_info * info = (struct bfd_link_info *) inf;
9272   asection * s;
9273   struct elf_link_hash_entry * myh;
9274   struct elf32_arm_link_hash_entry *eh;
9275   struct elf32_arm_link_hash_table * globals;
9276   asection *sec;
9277   bfd_vma val;
9278   char *error_message;
9279
9280   eh = elf32_arm_hash_entry (h);
9281   /* Allocate stubs for exported Thumb functions on v4t.  */
9282   if (eh->export_glue == NULL)
9283     return TRUE;
9284
9285   globals = elf32_arm_hash_table (info);
9286   BFD_ASSERT (globals != NULL);
9287   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9288
9289   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9290                               ARM2THUMB_GLUE_SECTION_NAME);
9291   BFD_ASSERT (s != NULL);
9292   BFD_ASSERT (s->contents != NULL);
9293   BFD_ASSERT (s->output_section != NULL);
9294
9295   sec = eh->export_glue->root.u.def.section;
9296
9297   BFD_ASSERT (sec->output_section != NULL);
9298
9299   val = eh->export_glue->root.u.def.value + sec->output_offset
9300         + sec->output_section->vma;
9301
9302   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
9303                                      h->root.u.def.section->owner,
9304                                      globals->obfd, sec, val, s,
9305                                      &error_message);
9306   BFD_ASSERT (myh);
9307   return TRUE;
9308 }
9309
9310 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
9311
9312 static bfd_vma
9313 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
9314 {
9315   bfd_byte *p;
9316   bfd_vma glue_addr;
9317   asection *s;
9318   struct elf32_arm_link_hash_table *globals;
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                               ARM_BX_GLUE_SECTION_NAME);
9326   BFD_ASSERT (s != NULL);
9327   BFD_ASSERT (s->contents != NULL);
9328   BFD_ASSERT (s->output_section != NULL);
9329
9330   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
9331
9332   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
9333
9334   if ((globals->bx_glue_offset[reg] & 1) == 0)
9335     {
9336       p = s->contents + glue_addr;
9337       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
9338       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
9339       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
9340       globals->bx_glue_offset[reg] |= 1;
9341     }
9342
9343   return glue_addr + s->output_section->vma + s->output_offset;
9344 }
9345
9346 /* Generate Arm stubs for exported Thumb symbols.  */
9347 static void
9348 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
9349                                   struct bfd_link_info *link_info)
9350 {
9351   struct elf32_arm_link_hash_table * globals;
9352
9353   if (link_info == NULL)
9354     /* Ignore this if we are not called by the ELF backend linker.  */
9355     return;
9356
9357   globals = elf32_arm_hash_table (link_info);
9358   if (globals == NULL)
9359     return;
9360
9361   /* If blx is available then exported Thumb symbols are OK and there is
9362      nothing to do.  */
9363   if (globals->use_blx)
9364     return;
9365
9366   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
9367                           link_info);
9368 }
9369
9370 /* Reserve space for COUNT dynamic relocations in relocation selection
9371    SRELOC.  */
9372
9373 static void
9374 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
9375                               bfd_size_type count)
9376 {
9377   struct elf32_arm_link_hash_table *htab;
9378
9379   htab = elf32_arm_hash_table (info);
9380   BFD_ASSERT (htab->root.dynamic_sections_created);
9381   if (sreloc == NULL)
9382     abort ();
9383   sreloc->size += RELOC_SIZE (htab) * count;
9384 }
9385
9386 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
9387    dynamic, the relocations should go in SRELOC, otherwise they should
9388    go in the special .rel.iplt section.  */
9389
9390 static void
9391 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
9392                             bfd_size_type count)
9393 {
9394   struct elf32_arm_link_hash_table *htab;
9395
9396   htab = elf32_arm_hash_table (info);
9397   if (!htab->root.dynamic_sections_created)
9398     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
9399   else
9400     {
9401       BFD_ASSERT (sreloc != NULL);
9402       sreloc->size += RELOC_SIZE (htab) * count;
9403     }
9404 }
9405
9406 /* Add relocation REL to the end of relocation section SRELOC.  */
9407
9408 static void
9409 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
9410                         asection *sreloc, Elf_Internal_Rela *rel)
9411 {
9412   bfd_byte *loc;
9413   struct elf32_arm_link_hash_table *htab;
9414
9415   htab = elf32_arm_hash_table (info);
9416   if (!htab->root.dynamic_sections_created
9417       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
9418     sreloc = htab->root.irelplt;
9419   if (sreloc == NULL)
9420     abort ();
9421   loc = sreloc->contents;
9422   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
9423   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
9424     abort ();
9425   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
9426 }
9427
9428 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
9429    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
9430    to .plt.  */
9431
9432 static void
9433 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
9434                               bfd_boolean is_iplt_entry,
9435                               union gotplt_union *root_plt,
9436                               struct arm_plt_info *arm_plt)
9437 {
9438   struct elf32_arm_link_hash_table *htab;
9439   asection *splt;
9440   asection *sgotplt;
9441
9442   htab = elf32_arm_hash_table (info);
9443
9444   if (is_iplt_entry)
9445     {
9446       splt = htab->root.iplt;
9447       sgotplt = htab->root.igotplt;
9448
9449       /* NaCl uses a special first entry in .iplt too.  */
9450       if (htab->nacl_p && splt->size == 0)
9451         splt->size += htab->plt_header_size;
9452
9453       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
9454       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
9455     }
9456   else
9457     {
9458       splt = htab->root.splt;
9459       sgotplt = htab->root.sgotplt;
9460
9461     if (htab->fdpic_p)
9462       {
9463         /* Allocate room for R_ARM_FUNCDESC_VALUE.  */
9464         /* For lazy binding, relocations will be put into .rel.plt, in
9465            .rel.got otherwise.  */
9466         /* FIXME: today we don't support lazy binding so put it in .rel.got */
9467         if (info->flags & DF_BIND_NOW)
9468           elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
9469         else
9470           elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9471       }
9472     else
9473       {
9474         /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
9475         elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9476       }
9477
9478       /* If this is the first .plt entry, make room for the special
9479          first entry.  */
9480       if (splt->size == 0)
9481         splt->size += htab->plt_header_size;
9482
9483       htab->next_tls_desc_index++;
9484     }
9485
9486   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
9487   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9488     splt->size += PLT_THUMB_STUB_SIZE;
9489   root_plt->offset = splt->size;
9490   splt->size += htab->plt_entry_size;
9491
9492   if (!htab->symbian_p)
9493     {
9494       /* We also need to make an entry in the .got.plt section, which
9495          will be placed in the .got section by the linker script.  */
9496       if (is_iplt_entry)
9497         arm_plt->got_offset = sgotplt->size;
9498       else
9499         arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
9500       if (htab->fdpic_p)
9501         /* Function descriptor takes 64 bits in GOT.  */
9502         sgotplt->size += 8;
9503       else
9504         sgotplt->size += 4;
9505     }
9506 }
9507
9508 static bfd_vma
9509 arm_movw_immediate (bfd_vma value)
9510 {
9511   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
9512 }
9513
9514 static bfd_vma
9515 arm_movt_immediate (bfd_vma value)
9516 {
9517   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
9518 }
9519
9520 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
9521    the entry lives in .iplt and resolves to (*SYM_VALUE)().
9522    Otherwise, DYNINDX is the index of the symbol in the dynamic
9523    symbol table and SYM_VALUE is undefined.
9524
9525    ROOT_PLT points to the offset of the PLT entry from the start of its
9526    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
9527    bookkeeping information.
9528
9529    Returns FALSE if there was a problem.  */
9530
9531 static bfd_boolean
9532 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
9533                               union gotplt_union *root_plt,
9534                               struct arm_plt_info *arm_plt,
9535                               int dynindx, bfd_vma sym_value)
9536 {
9537   struct elf32_arm_link_hash_table *htab;
9538   asection *sgot;
9539   asection *splt;
9540   asection *srel;
9541   bfd_byte *loc;
9542   bfd_vma plt_index;
9543   Elf_Internal_Rela rel;
9544   bfd_vma plt_header_size;
9545   bfd_vma got_header_size;
9546
9547   htab = elf32_arm_hash_table (info);
9548
9549   /* Pick the appropriate sections and sizes.  */
9550   if (dynindx == -1)
9551     {
9552       splt = htab->root.iplt;
9553       sgot = htab->root.igotplt;
9554       srel = htab->root.irelplt;
9555
9556       /* There are no reserved entries in .igot.plt, and no special
9557          first entry in .iplt.  */
9558       got_header_size = 0;
9559       plt_header_size = 0;
9560     }
9561   else
9562     {
9563       splt = htab->root.splt;
9564       sgot = htab->root.sgotplt;
9565       srel = htab->root.srelplt;
9566
9567       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
9568       plt_header_size = htab->plt_header_size;
9569     }
9570   BFD_ASSERT (splt != NULL && srel != NULL);
9571
9572   /* Fill in the entry in the procedure linkage table.  */
9573   if (htab->symbian_p)
9574     {
9575       BFD_ASSERT (dynindx >= 0);
9576       put_arm_insn (htab, output_bfd,
9577                     elf32_arm_symbian_plt_entry[0],
9578                     splt->contents + root_plt->offset);
9579       bfd_put_32 (output_bfd,
9580                   elf32_arm_symbian_plt_entry[1],
9581                   splt->contents + root_plt->offset + 4);
9582
9583       /* Fill in the entry in the .rel.plt section.  */
9584       rel.r_offset = (splt->output_section->vma
9585                       + splt->output_offset
9586                       + root_plt->offset + 4);
9587       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
9588
9589       /* Get the index in the procedure linkage table which
9590          corresponds to this symbol.  This is the index of this symbol
9591          in all the symbols for which we are making plt entries.  The
9592          first entry in the procedure linkage table is reserved.  */
9593       plt_index = ((root_plt->offset - plt_header_size)
9594                    / htab->plt_entry_size);
9595     }
9596   else
9597     {
9598       bfd_vma got_offset, got_address, plt_address;
9599       bfd_vma got_displacement, initial_got_entry;
9600       bfd_byte * ptr;
9601
9602       BFD_ASSERT (sgot != NULL);
9603
9604       /* Get the offset into the .(i)got.plt table of the entry that
9605          corresponds to this function.  */
9606       got_offset = (arm_plt->got_offset & -2);
9607
9608       /* Get the index in the procedure linkage table which
9609          corresponds to this symbol.  This is the index of this symbol
9610          in all the symbols for which we are making plt entries.
9611          After the reserved .got.plt entries, all symbols appear in
9612          the same order as in .plt.  */
9613       if (htab->fdpic_p)
9614         /* Function descriptor takes 8 bytes.  */
9615         plt_index = (got_offset - got_header_size) / 8;
9616       else
9617         plt_index = (got_offset - got_header_size) / 4;
9618
9619       /* Calculate the address of the GOT entry.  */
9620       got_address = (sgot->output_section->vma
9621                      + sgot->output_offset
9622                      + got_offset);
9623
9624       /* ...and the address of the PLT entry.  */
9625       plt_address = (splt->output_section->vma
9626                      + splt->output_offset
9627                      + root_plt->offset);
9628
9629       ptr = splt->contents + root_plt->offset;
9630       if (htab->vxworks_p && bfd_link_pic (info))
9631         {
9632           unsigned int i;
9633           bfd_vma val;
9634
9635           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9636             {
9637               val = elf32_arm_vxworks_shared_plt_entry[i];
9638               if (i == 2)
9639                 val |= got_address - sgot->output_section->vma;
9640               if (i == 5)
9641                 val |= plt_index * RELOC_SIZE (htab);
9642               if (i == 2 || i == 5)
9643                 bfd_put_32 (output_bfd, val, ptr);
9644               else
9645                 put_arm_insn (htab, output_bfd, val, ptr);
9646             }
9647         }
9648       else if (htab->vxworks_p)
9649         {
9650           unsigned int i;
9651           bfd_vma val;
9652
9653           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9654             {
9655               val = elf32_arm_vxworks_exec_plt_entry[i];
9656               if (i == 2)
9657                 val |= got_address;
9658               if (i == 4)
9659                 val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
9660               if (i == 5)
9661                 val |= plt_index * RELOC_SIZE (htab);
9662               if (i == 2 || i == 5)
9663                 bfd_put_32 (output_bfd, val, ptr);
9664               else
9665                 put_arm_insn (htab, output_bfd, val, ptr);
9666             }
9667
9668           loc = (htab->srelplt2->contents
9669                  + (plt_index * 2 + 1) * RELOC_SIZE (htab));
9670
9671           /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
9672              referencing the GOT for this PLT entry.  */
9673           rel.r_offset = plt_address + 8;
9674           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
9675           rel.r_addend = got_offset;
9676           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9677           loc += RELOC_SIZE (htab);
9678
9679           /* Create the R_ARM_ABS32 relocation referencing the
9680              beginning of the PLT for this GOT entry.  */
9681           rel.r_offset = got_address;
9682           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
9683           rel.r_addend = 0;
9684           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9685         }
9686       else if (htab->nacl_p)
9687         {
9688           /* Calculate the displacement between the PLT slot and the
9689              common tail that's part of the special initial PLT slot.  */
9690           int32_t tail_displacement
9691             = ((splt->output_section->vma + splt->output_offset
9692                 + ARM_NACL_PLT_TAIL_OFFSET)
9693                - (plt_address + htab->plt_entry_size + 4));
9694           BFD_ASSERT ((tail_displacement & 3) == 0);
9695           tail_displacement >>= 2;
9696
9697           BFD_ASSERT ((tail_displacement & 0xff000000) == 0
9698                       || (-tail_displacement & 0xff000000) == 0);
9699
9700           /* Calculate the displacement between the PLT slot and the entry
9701              in the GOT.  The offset accounts for the value produced by
9702              adding to pc in the penultimate instruction of the PLT stub.  */
9703           got_displacement = (got_address
9704                               - (plt_address + htab->plt_entry_size));
9705
9706           /* NaCl does not support interworking at all.  */
9707           BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
9708
9709           put_arm_insn (htab, output_bfd,
9710                         elf32_arm_nacl_plt_entry[0]
9711                         | arm_movw_immediate (got_displacement),
9712                         ptr + 0);
9713           put_arm_insn (htab, output_bfd,
9714                         elf32_arm_nacl_plt_entry[1]
9715                         | arm_movt_immediate (got_displacement),
9716                         ptr + 4);
9717           put_arm_insn (htab, output_bfd,
9718                         elf32_arm_nacl_plt_entry[2],
9719                         ptr + 8);
9720           put_arm_insn (htab, output_bfd,
9721                         elf32_arm_nacl_plt_entry[3]
9722                         | (tail_displacement & 0x00ffffff),
9723                         ptr + 12);
9724         }
9725       else if (htab->fdpic_p)
9726         {
9727           /* Fill-up Thumb stub if needed.  */
9728           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9729             {
9730               put_thumb_insn (htab, output_bfd,
9731                               elf32_arm_plt_thumb_stub[0], ptr - 4);
9732               put_thumb_insn (htab, output_bfd,
9733                               elf32_arm_plt_thumb_stub[1], ptr - 2);
9734             }
9735           put_arm_insn(htab, output_bfd,
9736                        elf32_arm_fdpic_plt_entry[0], ptr + 0);
9737           put_arm_insn(htab, output_bfd,
9738                        elf32_arm_fdpic_plt_entry[1], ptr + 4);
9739           put_arm_insn(htab, output_bfd,
9740                        elf32_arm_fdpic_plt_entry[2], ptr + 8);
9741           put_arm_insn(htab, output_bfd,
9742                        elf32_arm_fdpic_plt_entry[3], ptr + 12);
9743           bfd_put_32 (output_bfd, got_offset, ptr + 16);
9744
9745           if (!(info->flags & DF_BIND_NOW))
9746             {
9747               /* funcdesc_value_reloc_offset.  */
9748               bfd_put_32 (output_bfd,
9749                           htab->root.srelplt->reloc_count * RELOC_SIZE (htab),
9750                           ptr + 20);
9751               put_arm_insn(htab, output_bfd,
9752                            elf32_arm_fdpic_plt_entry[6], ptr + 24);
9753               put_arm_insn(htab, output_bfd,
9754                            elf32_arm_fdpic_plt_entry[7], ptr + 28);
9755               put_arm_insn(htab, output_bfd,
9756                            elf32_arm_fdpic_plt_entry[8], ptr + 32);
9757               put_arm_insn(htab, output_bfd,
9758                            elf32_arm_fdpic_plt_entry[9], ptr + 36);
9759             }
9760         }
9761       else if (using_thumb_only (htab))
9762         {
9763           /* PR ld/16017: Generate thumb only PLT entries.  */
9764           if (!using_thumb2 (htab))
9765             {
9766               /* FIXME: We ought to be able to generate thumb-1 PLT
9767                  instructions...  */
9768               _bfd_error_handler (_("%pB: warning: thumb-1 mode PLT generation not currently supported"),
9769                                   output_bfd);
9770               return FALSE;
9771             }
9772
9773           /* Calculate the displacement between the PLT slot and the entry in
9774              the GOT.  The 12-byte offset accounts for the value produced by
9775              adding to pc in the 3rd instruction of the PLT stub.  */
9776           got_displacement = got_address - (plt_address + 12);
9777
9778           /* As we are using 32 bit instructions we have to use 'put_arm_insn'
9779              instead of 'put_thumb_insn'.  */
9780           put_arm_insn (htab, output_bfd,
9781                         elf32_thumb2_plt_entry[0]
9782                         | ((got_displacement & 0x000000ff) << 16)
9783                         | ((got_displacement & 0x00000700) << 20)
9784                         | ((got_displacement & 0x00000800) >>  1)
9785                         | ((got_displacement & 0x0000f000) >> 12),
9786                         ptr + 0);
9787           put_arm_insn (htab, output_bfd,
9788                         elf32_thumb2_plt_entry[1]
9789                         | ((got_displacement & 0x00ff0000)      )
9790                         | ((got_displacement & 0x07000000) <<  4)
9791                         | ((got_displacement & 0x08000000) >> 17)
9792                         | ((got_displacement & 0xf0000000) >> 28),
9793                         ptr + 4);
9794           put_arm_insn (htab, output_bfd,
9795                         elf32_thumb2_plt_entry[2],
9796                         ptr + 8);
9797           put_arm_insn (htab, output_bfd,
9798                         elf32_thumb2_plt_entry[3],
9799                         ptr + 12);
9800         }
9801       else
9802         {
9803           /* Calculate the displacement between the PLT slot and the
9804              entry in the GOT.  The eight-byte offset accounts for the
9805              value produced by adding to pc in the first instruction
9806              of the PLT stub.  */
9807           got_displacement = got_address - (plt_address + 8);
9808
9809           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9810             {
9811               put_thumb_insn (htab, output_bfd,
9812                               elf32_arm_plt_thumb_stub[0], ptr - 4);
9813               put_thumb_insn (htab, output_bfd,
9814                               elf32_arm_plt_thumb_stub[1], ptr - 2);
9815             }
9816
9817           if (!elf32_arm_use_long_plt_entry)
9818             {
9819               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
9820
9821               put_arm_insn (htab, output_bfd,
9822                             elf32_arm_plt_entry_short[0]
9823                             | ((got_displacement & 0x0ff00000) >> 20),
9824                             ptr + 0);
9825               put_arm_insn (htab, output_bfd,
9826                             elf32_arm_plt_entry_short[1]
9827                             | ((got_displacement & 0x000ff000) >> 12),
9828                             ptr+ 4);
9829               put_arm_insn (htab, output_bfd,
9830                             elf32_arm_plt_entry_short[2]
9831                             | (got_displacement & 0x00000fff),
9832                             ptr + 8);
9833 #ifdef FOUR_WORD_PLT
9834               bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
9835 #endif
9836             }
9837           else
9838             {
9839               put_arm_insn (htab, output_bfd,
9840                             elf32_arm_plt_entry_long[0]
9841                             | ((got_displacement & 0xf0000000) >> 28),
9842                             ptr + 0);
9843               put_arm_insn (htab, output_bfd,
9844                             elf32_arm_plt_entry_long[1]
9845                             | ((got_displacement & 0x0ff00000) >> 20),
9846                             ptr + 4);
9847               put_arm_insn (htab, output_bfd,
9848                             elf32_arm_plt_entry_long[2]
9849                             | ((got_displacement & 0x000ff000) >> 12),
9850                             ptr+ 8);
9851               put_arm_insn (htab, output_bfd,
9852                             elf32_arm_plt_entry_long[3]
9853                             | (got_displacement & 0x00000fff),
9854                             ptr + 12);
9855             }
9856         }
9857
9858       /* Fill in the entry in the .rel(a).(i)plt section.  */
9859       rel.r_offset = got_address;
9860       rel.r_addend = 0;
9861       if (dynindx == -1)
9862         {
9863           /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
9864              The dynamic linker or static executable then calls SYM_VALUE
9865              to determine the correct run-time value of the .igot.plt entry.  */
9866           rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9867           initial_got_entry = sym_value;
9868         }
9869       else
9870         {
9871           /* For FDPIC we will have to resolve a R_ARM_FUNCDESC_VALUE
9872              used by PLT entry.  */
9873           if (htab->fdpic_p)
9874             {
9875               rel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
9876               initial_got_entry = 0;
9877             }
9878           else
9879             {
9880               rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
9881               initial_got_entry = (splt->output_section->vma
9882                                    + splt->output_offset);
9883             }
9884         }
9885
9886       /* Fill in the entry in the global offset table.  */
9887       bfd_put_32 (output_bfd, initial_got_entry,
9888                   sgot->contents + got_offset);
9889
9890       if (htab->fdpic_p && !(info->flags & DF_BIND_NOW))
9891         {
9892           /* Setup initial funcdesc value.  */
9893           /* FIXME: we don't support lazy binding because there is a
9894              race condition between both words getting written and
9895              some other thread attempting to read them. The ARM
9896              architecture does not have an atomic 64 bit load/store
9897              instruction that could be used to prevent it; it is
9898              recommended that threaded FDPIC applications run with the
9899              LD_BIND_NOW environment variable set.  */
9900           bfd_put_32(output_bfd, plt_address + 0x18,
9901                      sgot->contents + got_offset);
9902           bfd_put_32(output_bfd, -1 /*TODO*/,
9903                      sgot->contents + got_offset + 4);
9904         }
9905     }
9906
9907   if (dynindx == -1)
9908     elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
9909   else
9910     {
9911       if (htab->fdpic_p)
9912         {
9913           /* For FDPIC we put PLT relocationss into .rel.got when not
9914              lazy binding otherwise we put them in .rel.plt.  For now,
9915              we don't support lazy binding so put it in .rel.got.  */
9916           if (info->flags & DF_BIND_NOW)
9917             elf32_arm_add_dynreloc(output_bfd, info, htab->root.srelgot, &rel);
9918           else
9919             elf32_arm_add_dynreloc(output_bfd, info, htab->root.srelplt, &rel);
9920         }
9921       else
9922         {
9923           loc = srel->contents + plt_index * RELOC_SIZE (htab);
9924           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9925         }
9926     }
9927
9928   return TRUE;
9929 }
9930
9931 /* Some relocations map to different relocations depending on the
9932    target.  Return the real relocation.  */
9933
9934 static int
9935 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
9936                      int r_type)
9937 {
9938   switch (r_type)
9939     {
9940     case R_ARM_TARGET1:
9941       if (globals->target1_is_rel)
9942         return R_ARM_REL32;
9943       else
9944         return R_ARM_ABS32;
9945
9946     case R_ARM_TARGET2:
9947       return globals->target2_reloc;
9948
9949     default:
9950       return r_type;
9951     }
9952 }
9953
9954 /* Return the base VMA address which should be subtracted from real addresses
9955    when resolving @dtpoff relocation.
9956    This is PT_TLS segment p_vaddr.  */
9957
9958 static bfd_vma
9959 dtpoff_base (struct bfd_link_info *info)
9960 {
9961   /* If tls_sec is NULL, we should have signalled an error already.  */
9962   if (elf_hash_table (info)->tls_sec == NULL)
9963     return 0;
9964   return elf_hash_table (info)->tls_sec->vma;
9965 }
9966
9967 /* Return the relocation value for @tpoff relocation
9968    if STT_TLS virtual address is ADDRESS.  */
9969
9970 static bfd_vma
9971 tpoff (struct bfd_link_info *info, bfd_vma address)
9972 {
9973   struct elf_link_hash_table *htab = elf_hash_table (info);
9974   bfd_vma base;
9975
9976   /* If tls_sec is NULL, we should have signalled an error already.  */
9977   if (htab->tls_sec == NULL)
9978     return 0;
9979   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
9980   return address - htab->tls_sec->vma + base;
9981 }
9982
9983 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
9984    VALUE is the relocation value.  */
9985
9986 static bfd_reloc_status_type
9987 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
9988 {
9989   if (value > 0xfff)
9990     return bfd_reloc_overflow;
9991
9992   value |= bfd_get_32 (abfd, data) & 0xfffff000;
9993   bfd_put_32 (abfd, value, data);
9994   return bfd_reloc_ok;
9995 }
9996
9997 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
9998    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
9999    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
10000
10001    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
10002    is to then call final_link_relocate.  Return other values in the
10003    case of error.
10004
10005    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
10006    the pre-relaxed code.  It would be nice if the relocs were updated
10007    to match the optimization.   */
10008
10009 static bfd_reloc_status_type
10010 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
10011                      bfd *input_bfd, asection *input_sec, bfd_byte *contents,
10012                      Elf_Internal_Rela *rel, unsigned long is_local)
10013 {
10014   unsigned long insn;
10015
10016   switch (ELF32_R_TYPE (rel->r_info))
10017     {
10018     default:
10019       return bfd_reloc_notsupported;
10020
10021     case R_ARM_TLS_GOTDESC:
10022       if (is_local)
10023         insn = 0;
10024       else
10025         {
10026           insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10027           if (insn & 1)
10028             insn -= 5; /* THUMB */
10029           else
10030             insn -= 8; /* ARM */
10031         }
10032       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10033       return bfd_reloc_continue;
10034
10035     case R_ARM_THM_TLS_DESCSEQ:
10036       /* Thumb insn.  */
10037       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
10038       if ((insn & 0xff78) == 0x4478)      /* add rx, pc */
10039         {
10040           if (is_local)
10041             /* nop */
10042             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10043         }
10044       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
10045         {
10046           if (is_local)
10047             /* nop */
10048             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10049           else
10050             /* ldr rx,[ry] */
10051             bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
10052         }
10053       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
10054         {
10055           if (is_local)
10056             /* nop */
10057             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10058           else
10059             /* mov r0, rx */
10060             bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
10061                         contents + rel->r_offset);
10062         }
10063       else
10064         {
10065           if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
10066             /* It's a 32 bit instruction, fetch the rest of it for
10067                error generation.  */
10068             insn = (insn << 16)
10069               | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
10070           _bfd_error_handler
10071             /* xgettext:c-format */
10072             (_("%pB(%pA+%#" PRIx64 "): "
10073                "unexpected %s instruction '%#lx' in TLS trampoline"),
10074              input_bfd, input_sec, (uint64_t) rel->r_offset,
10075              "Thumb", insn);
10076           return bfd_reloc_notsupported;
10077         }
10078       break;
10079
10080     case R_ARM_TLS_DESCSEQ:
10081       /* arm insn.  */
10082       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10083       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
10084         {
10085           if (is_local)
10086             /* mov rx, ry */
10087             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
10088                         contents + rel->r_offset);
10089         }
10090       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
10091         {
10092           if (is_local)
10093             /* nop */
10094             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10095           else
10096             /* ldr rx,[ry] */
10097             bfd_put_32 (input_bfd, insn & 0xfffff000,
10098                         contents + rel->r_offset);
10099         }
10100       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
10101         {
10102           if (is_local)
10103             /* nop */
10104             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10105           else
10106             /* mov r0, rx */
10107             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
10108                         contents + rel->r_offset);
10109         }
10110       else
10111         {
10112           _bfd_error_handler
10113             /* xgettext:c-format */
10114             (_("%pB(%pA+%#" PRIx64 "): "
10115                "unexpected %s instruction '%#lx' in TLS trampoline"),
10116              input_bfd, input_sec, (uint64_t) rel->r_offset,
10117              "ARM", insn);
10118           return bfd_reloc_notsupported;
10119         }
10120       break;
10121
10122     case R_ARM_TLS_CALL:
10123       /* GD->IE relaxation, turn the instruction into 'nop' or
10124          'ldr r0, [pc,r0]'  */
10125       insn = is_local ? 0xe1a00000 : 0xe79f0000;
10126       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10127       break;
10128
10129     case R_ARM_THM_TLS_CALL:
10130       /* GD->IE relaxation.  */
10131       if (!is_local)
10132         /* add r0,pc; ldr r0, [r0]  */
10133         insn = 0x44786800;
10134       else if (using_thumb2 (globals))
10135         /* nop.w */
10136         insn = 0xf3af8000;
10137       else
10138         /* nop; nop */
10139         insn = 0xbf00bf00;
10140
10141       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
10142       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
10143       break;
10144     }
10145   return bfd_reloc_ok;
10146 }
10147
10148 /* For a given value of n, calculate the value of G_n as required to
10149    deal with group relocations.  We return it in the form of an
10150    encoded constant-and-rotation, together with the final residual.  If n is
10151    specified as less than zero, then final_residual is filled with the
10152    input value and no further action is performed.  */
10153
10154 static bfd_vma
10155 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
10156 {
10157   int current_n;
10158   bfd_vma g_n;
10159   bfd_vma encoded_g_n = 0;
10160   bfd_vma residual = value; /* Also known as Y_n.  */
10161
10162   for (current_n = 0; current_n <= n; current_n++)
10163     {
10164       int shift;
10165
10166       /* Calculate which part of the value to mask.  */
10167       if (residual == 0)
10168         shift = 0;
10169       else
10170         {
10171           int msb;
10172
10173           /* Determine the most significant bit in the residual and
10174              align the resulting value to a 2-bit boundary.  */
10175           for (msb = 30; msb >= 0; msb -= 2)
10176             if (residual & (3 << msb))
10177               break;
10178
10179           /* The desired shift is now (msb - 6), or zero, whichever
10180              is the greater.  */
10181           shift = msb - 6;
10182           if (shift < 0)
10183             shift = 0;
10184         }
10185
10186       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
10187       g_n = residual & (0xff << shift);
10188       encoded_g_n = (g_n >> shift)
10189                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
10190
10191       /* Calculate the residual for the next time around.  */
10192       residual &= ~g_n;
10193     }
10194
10195   *final_residual = residual;
10196
10197   return encoded_g_n;
10198 }
10199
10200 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
10201    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
10202
10203 static int
10204 identify_add_or_sub (bfd_vma insn)
10205 {
10206   int opcode = insn & 0x1e00000;
10207
10208   if (opcode == 1 << 23) /* ADD */
10209     return 1;
10210
10211   if (opcode == 1 << 22) /* SUB */
10212     return -1;
10213
10214   return 0;
10215 }
10216
10217 /* Perform a relocation as part of a final link.  */
10218
10219 static bfd_reloc_status_type
10220 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
10221                                bfd *                        input_bfd,
10222                                bfd *                        output_bfd,
10223                                asection *                   input_section,
10224                                bfd_byte *                   contents,
10225                                Elf_Internal_Rela *          rel,
10226                                bfd_vma                      value,
10227                                struct bfd_link_info *       info,
10228                                asection *                   sym_sec,
10229                                const char *                 sym_name,
10230                                unsigned char                st_type,
10231                                enum arm_st_branch_type      branch_type,
10232                                struct elf_link_hash_entry * h,
10233                                bfd_boolean *                unresolved_reloc_p,
10234                                char **                      error_message)
10235 {
10236   unsigned long                 r_type = howto->type;
10237   unsigned long                 r_symndx;
10238   bfd_byte *                    hit_data = contents + rel->r_offset;
10239   bfd_vma *                     local_got_offsets;
10240   bfd_vma *                     local_tlsdesc_gotents;
10241   asection *                    sgot;
10242   asection *                    splt;
10243   asection *                    sreloc = NULL;
10244   asection *                    srelgot;
10245   bfd_vma                       addend;
10246   bfd_signed_vma                signed_addend;
10247   unsigned char                 dynreloc_st_type;
10248   bfd_vma                       dynreloc_value;
10249   struct elf32_arm_link_hash_table * globals;
10250   struct elf32_arm_link_hash_entry *eh;
10251   union gotplt_union           *root_plt;
10252   struct arm_plt_info          *arm_plt;
10253   bfd_vma                       plt_offset;
10254   bfd_vma                       gotplt_offset;
10255   bfd_boolean                   has_iplt_entry;
10256   bfd_boolean                   resolved_to_zero;
10257
10258   globals = elf32_arm_hash_table (info);
10259   if (globals == NULL)
10260     return bfd_reloc_notsupported;
10261
10262   BFD_ASSERT (is_arm_elf (input_bfd));
10263   BFD_ASSERT (howto != NULL);
10264
10265   /* Some relocation types map to different relocations depending on the
10266      target.  We pick the right one here.  */
10267   r_type = arm_real_reloc_type (globals, r_type);
10268
10269   /* It is possible to have linker relaxations on some TLS access
10270      models.  Update our information here.  */
10271   r_type = elf32_arm_tls_transition (info, r_type, h);
10272
10273   if (r_type != howto->type)
10274     howto = elf32_arm_howto_from_type (r_type);
10275
10276   eh = (struct elf32_arm_link_hash_entry *) h;
10277   sgot = globals->root.sgot;
10278   local_got_offsets = elf_local_got_offsets (input_bfd);
10279   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
10280
10281   if (globals->root.dynamic_sections_created)
10282     srelgot = globals->root.srelgot;
10283   else
10284     srelgot = NULL;
10285
10286   r_symndx = ELF32_R_SYM (rel->r_info);
10287
10288   if (globals->use_rel)
10289     {
10290       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
10291
10292       if (addend & ((howto->src_mask + 1) >> 1))
10293         {
10294           signed_addend = -1;
10295           signed_addend &= ~ howto->src_mask;
10296           signed_addend |= addend;
10297         }
10298       else
10299         signed_addend = addend;
10300     }
10301   else
10302     addend = signed_addend = rel->r_addend;
10303
10304   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
10305      are resolving a function call relocation.  */
10306   if (using_thumb_only (globals)
10307       && (r_type == R_ARM_THM_CALL
10308           || r_type == R_ARM_THM_JUMP24)
10309       && branch_type == ST_BRANCH_TO_ARM)
10310     branch_type = ST_BRANCH_TO_THUMB;
10311
10312   /* Record the symbol information that should be used in dynamic
10313      relocations.  */
10314   dynreloc_st_type = st_type;
10315   dynreloc_value = value;
10316   if (branch_type == ST_BRANCH_TO_THUMB)
10317     dynreloc_value |= 1;
10318
10319   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
10320      VALUE appropriately for relocations that we resolve at link time.  */
10321   has_iplt_entry = FALSE;
10322   if (elf32_arm_get_plt_info (input_bfd, globals, eh, r_symndx, &root_plt,
10323                               &arm_plt)
10324       && root_plt->offset != (bfd_vma) -1)
10325     {
10326       plt_offset = root_plt->offset;
10327       gotplt_offset = arm_plt->got_offset;
10328
10329       if (h == NULL || eh->is_iplt)
10330         {
10331           has_iplt_entry = TRUE;
10332           splt = globals->root.iplt;
10333
10334           /* Populate .iplt entries here, because not all of them will
10335              be seen by finish_dynamic_symbol.  The lower bit is set if
10336              we have already populated the entry.  */
10337           if (plt_offset & 1)
10338             plt_offset--;
10339           else
10340             {
10341               if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
10342                                                 -1, dynreloc_value))
10343                 root_plt->offset |= 1;
10344               else
10345                 return bfd_reloc_notsupported;
10346             }
10347
10348           /* Static relocations always resolve to the .iplt entry.  */
10349           st_type = STT_FUNC;
10350           value = (splt->output_section->vma
10351                    + splt->output_offset
10352                    + plt_offset);
10353           branch_type = ST_BRANCH_TO_ARM;
10354
10355           /* If there are non-call relocations that resolve to the .iplt
10356              entry, then all dynamic ones must too.  */
10357           if (arm_plt->noncall_refcount != 0)
10358             {
10359               dynreloc_st_type = st_type;
10360               dynreloc_value = value;
10361             }
10362         }
10363       else
10364         /* We populate the .plt entry in finish_dynamic_symbol.  */
10365         splt = globals->root.splt;
10366     }
10367   else
10368     {
10369       splt = NULL;
10370       plt_offset = (bfd_vma) -1;
10371       gotplt_offset = (bfd_vma) -1;
10372     }
10373
10374   resolved_to_zero = (h != NULL
10375                       && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
10376
10377   switch (r_type)
10378     {
10379     case R_ARM_NONE:
10380       /* We don't need to find a value for this symbol.  It's just a
10381          marker.  */
10382       *unresolved_reloc_p = FALSE;
10383       return bfd_reloc_ok;
10384
10385     case R_ARM_ABS12:
10386       if (!globals->vxworks_p)
10387         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10388       /* Fall through.  */
10389
10390     case R_ARM_PC24:
10391     case R_ARM_ABS32:
10392     case R_ARM_ABS32_NOI:
10393     case R_ARM_REL32:
10394     case R_ARM_REL32_NOI:
10395     case R_ARM_CALL:
10396     case R_ARM_JUMP24:
10397     case R_ARM_XPC25:
10398     case R_ARM_PREL31:
10399     case R_ARM_PLT32:
10400       /* Handle relocations which should use the PLT entry.  ABS32/REL32
10401          will use the symbol's value, which may point to a PLT entry, but we
10402          don't need to handle that here.  If we created a PLT entry, all
10403          branches in this object should go to it, except if the PLT is too
10404          far away, in which case a long branch stub should be inserted.  */
10405       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
10406            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
10407            && r_type != R_ARM_CALL
10408            && r_type != R_ARM_JUMP24
10409            && r_type != R_ARM_PLT32)
10410           && plt_offset != (bfd_vma) -1)
10411         {
10412           /* If we've created a .plt section, and assigned a PLT entry
10413              to this function, it must either be a STT_GNU_IFUNC reference
10414              or not be known to bind locally.  In other cases, we should
10415              have cleared the PLT entry by now.  */
10416           BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
10417
10418           value = (splt->output_section->vma
10419                    + splt->output_offset
10420                    + plt_offset);
10421           *unresolved_reloc_p = FALSE;
10422           return _bfd_final_link_relocate (howto, input_bfd, input_section,
10423                                            contents, rel->r_offset, value,
10424                                            rel->r_addend);
10425         }
10426
10427       /* When generating a shared object or relocatable executable, these
10428          relocations are copied into the output file to be resolved at
10429          run time.  */
10430       if ((bfd_link_pic (info)
10431            || globals->root.is_relocatable_executable
10432            || globals->fdpic_p)
10433           && (input_section->flags & SEC_ALLOC)
10434           && !(globals->vxworks_p
10435                && strcmp (input_section->output_section->name,
10436                           ".tls_vars") == 0)
10437           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
10438               || !SYMBOL_CALLS_LOCAL (info, h))
10439           && !(input_bfd == globals->stub_bfd
10440                && strstr (input_section->name, STUB_SUFFIX))
10441           && (h == NULL
10442               || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10443                   && !resolved_to_zero)
10444               || h->root.type != bfd_link_hash_undefweak)
10445           && r_type != R_ARM_PC24
10446           && r_type != R_ARM_CALL
10447           && r_type != R_ARM_JUMP24
10448           && r_type != R_ARM_PREL31
10449           && r_type != R_ARM_PLT32)
10450         {
10451           Elf_Internal_Rela outrel;
10452           bfd_boolean skip, relocate;
10453           int isrofixup = 0;
10454
10455           if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
10456               && !h->def_regular)
10457             {
10458               char *v = _("shared object");
10459
10460               if (bfd_link_executable (info))
10461                 v = _("PIE executable");
10462
10463               _bfd_error_handler
10464                 (_("%pB: relocation %s against external or undefined symbol `%s'"
10465                    " can not be used when making a %s; recompile with -fPIC"), input_bfd,
10466                  elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
10467               return bfd_reloc_notsupported;
10468             }
10469
10470           *unresolved_reloc_p = FALSE;
10471
10472           if (sreloc == NULL && globals->root.dynamic_sections_created)
10473             {
10474               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
10475                                                            ! globals->use_rel);
10476
10477               if (sreloc == NULL)
10478                 return bfd_reloc_notsupported;
10479             }
10480
10481           skip = FALSE;
10482           relocate = FALSE;
10483
10484           outrel.r_addend = addend;
10485           outrel.r_offset =
10486             _bfd_elf_section_offset (output_bfd, info, input_section,
10487                                      rel->r_offset);
10488           if (outrel.r_offset == (bfd_vma) -1)
10489             skip = TRUE;
10490           else if (outrel.r_offset == (bfd_vma) -2)
10491             skip = TRUE, relocate = TRUE;
10492           outrel.r_offset += (input_section->output_section->vma
10493                               + input_section->output_offset);
10494
10495           if (skip)
10496             memset (&outrel, 0, sizeof outrel);
10497           else if (h != NULL
10498                    && h->dynindx != -1
10499                    && (!bfd_link_pic (info)
10500                        || !(bfd_link_pie (info)
10501                             || SYMBOLIC_BIND (info, h))
10502                        || !h->def_regular))
10503             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
10504           else
10505             {
10506               int symbol;
10507
10508               /* This symbol is local, or marked to become local.  */
10509               BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI
10510                           || (globals->fdpic_p && !bfd_link_pic(info)));
10511               if (globals->symbian_p)
10512                 {
10513                   asection *osec;
10514
10515                   /* On Symbian OS, the data segment and text segement
10516                      can be relocated independently.  Therefore, we
10517                      must indicate the segment to which this
10518                      relocation is relative.  The BPABI allows us to
10519                      use any symbol in the right segment; we just use
10520                      the section symbol as it is convenient.  (We
10521                      cannot use the symbol given by "h" directly as it
10522                      will not appear in the dynamic symbol table.)
10523
10524                      Note that the dynamic linker ignores the section
10525                      symbol value, so we don't subtract osec->vma
10526                      from the emitted reloc addend.  */
10527                   if (sym_sec)
10528                     osec = sym_sec->output_section;
10529                   else
10530                     osec = input_section->output_section;
10531                   symbol = elf_section_data (osec)->dynindx;
10532                   if (symbol == 0)
10533                     {
10534                       struct elf_link_hash_table *htab = elf_hash_table (info);
10535
10536                       if ((osec->flags & SEC_READONLY) == 0
10537                           && htab->data_index_section != NULL)
10538                         osec = htab->data_index_section;
10539                       else
10540                         osec = htab->text_index_section;
10541                       symbol = elf_section_data (osec)->dynindx;
10542                     }
10543                   BFD_ASSERT (symbol != 0);
10544                 }
10545               else
10546                 /* On SVR4-ish systems, the dynamic loader cannot
10547                    relocate the text and data segments independently,
10548                    so the symbol does not matter.  */
10549                 symbol = 0;
10550               if (dynreloc_st_type == STT_GNU_IFUNC)
10551                 /* We have an STT_GNU_IFUNC symbol that doesn't resolve
10552                    to the .iplt entry.  Instead, every non-call reference
10553                    must use an R_ARM_IRELATIVE relocation to obtain the
10554                    correct run-time address.  */
10555                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
10556               else if (globals->fdpic_p && !bfd_link_pic(info))
10557                 isrofixup = 1;
10558               else
10559                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
10560               if (globals->use_rel)
10561                 relocate = TRUE;
10562               else
10563                 outrel.r_addend += dynreloc_value;
10564             }
10565
10566           if (isrofixup)
10567             arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
10568           else
10569             elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
10570
10571           /* If this reloc is against an external symbol, we do not want to
10572              fiddle with the addend.  Otherwise, we need to include the symbol
10573              value so that it becomes an addend for the dynamic reloc.  */
10574           if (! relocate)
10575             return bfd_reloc_ok;
10576
10577           return _bfd_final_link_relocate (howto, input_bfd, input_section,
10578                                            contents, rel->r_offset,
10579                                            dynreloc_value, (bfd_vma) 0);
10580         }
10581       else switch (r_type)
10582         {
10583         case R_ARM_ABS12:
10584           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10585
10586         case R_ARM_XPC25:         /* Arm BLX instruction.  */
10587         case R_ARM_CALL:
10588         case R_ARM_JUMP24:
10589         case R_ARM_PC24:          /* Arm B/BL instruction.  */
10590         case R_ARM_PLT32:
10591           {
10592           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
10593
10594           if (r_type == R_ARM_XPC25)
10595             {
10596               /* Check for Arm calling Arm function.  */
10597               /* FIXME: Should we translate the instruction into a BL
10598                  instruction instead ?  */
10599               if (branch_type != ST_BRANCH_TO_THUMB)
10600                 _bfd_error_handler
10601                   (_("\%pB: warning: %s BLX instruction targets"
10602                      " %s function '%s'"),
10603                    input_bfd, "ARM",
10604                    "ARM", h ? h->root.root.string : "(local)");
10605             }
10606           else if (r_type == R_ARM_PC24)
10607             {
10608               /* Check for Arm calling Thumb function.  */
10609               if (branch_type == ST_BRANCH_TO_THUMB)
10610                 {
10611                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
10612                                                output_bfd, input_section,
10613                                                hit_data, sym_sec, rel->r_offset,
10614                                                signed_addend, value,
10615                                                error_message))
10616                     return bfd_reloc_ok;
10617                   else
10618                     return bfd_reloc_dangerous;
10619                 }
10620             }
10621
10622           /* Check if a stub has to be inserted because the
10623              destination is too far or we are changing mode.  */
10624           if (   r_type == R_ARM_CALL
10625               || r_type == R_ARM_JUMP24
10626               || r_type == R_ARM_PLT32)
10627             {
10628               enum elf32_arm_stub_type stub_type = arm_stub_none;
10629               struct elf32_arm_link_hash_entry *hash;
10630
10631               hash = (struct elf32_arm_link_hash_entry *) h;
10632               stub_type = arm_type_of_stub (info, input_section, rel,
10633                                             st_type, &branch_type,
10634                                             hash, value, sym_sec,
10635                                             input_bfd, sym_name);
10636
10637               if (stub_type != arm_stub_none)
10638                 {
10639                   /* The target is out of reach, so redirect the
10640                      branch to the local stub for this function.  */
10641                   stub_entry = elf32_arm_get_stub_entry (input_section,
10642                                                          sym_sec, h,
10643                                                          rel, globals,
10644                                                          stub_type);
10645                   {
10646                     if (stub_entry != NULL)
10647                       value = (stub_entry->stub_offset
10648                                + stub_entry->stub_sec->output_offset
10649                                + stub_entry->stub_sec->output_section->vma);
10650
10651                     if (plt_offset != (bfd_vma) -1)
10652                       *unresolved_reloc_p = FALSE;
10653                   }
10654                 }
10655               else
10656                 {
10657                   /* If the call goes through a PLT entry, make sure to
10658                      check distance to the right destination address.  */
10659                   if (plt_offset != (bfd_vma) -1)
10660                     {
10661                       value = (splt->output_section->vma
10662                                + splt->output_offset
10663                                + plt_offset);
10664                       *unresolved_reloc_p = FALSE;
10665                       /* The PLT entry is in ARM mode, regardless of the
10666                          target function.  */
10667                       branch_type = ST_BRANCH_TO_ARM;
10668                     }
10669                 }
10670             }
10671
10672           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
10673              where:
10674               S is the address of the symbol in the relocation.
10675               P is address of the instruction being relocated.
10676               A is the addend (extracted from the instruction) in bytes.
10677
10678              S is held in 'value'.
10679              P is the base address of the section containing the
10680                instruction plus the offset of the reloc into that
10681                section, ie:
10682                  (input_section->output_section->vma +
10683                   input_section->output_offset +
10684                   rel->r_offset).
10685              A is the addend, converted into bytes, ie:
10686                  (signed_addend * 4)
10687
10688              Note: None of these operations have knowledge of the pipeline
10689              size of the processor, thus it is up to the assembler to
10690              encode this information into the addend.  */
10691           value -= (input_section->output_section->vma
10692                     + input_section->output_offset);
10693           value -= rel->r_offset;
10694           if (globals->use_rel)
10695             value += (signed_addend << howto->size);
10696           else
10697             /* RELA addends do not have to be adjusted by howto->size.  */
10698             value += signed_addend;
10699
10700           signed_addend = value;
10701           signed_addend >>= howto->rightshift;
10702
10703           /* A branch to an undefined weak symbol is turned into a jump to
10704              the next instruction unless a PLT entry will be created.
10705              Do the same for local undefined symbols (but not for STN_UNDEF).
10706              The jump to the next instruction is optimized as a NOP depending
10707              on the architecture.  */
10708           if (h ? (h->root.type == bfd_link_hash_undefweak
10709                    && plt_offset == (bfd_vma) -1)
10710               : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
10711             {
10712               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
10713
10714               if (arch_has_arm_nop (globals))
10715                 value |= 0x0320f000;
10716               else
10717                 value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
10718             }
10719           else
10720             {
10721               /* Perform a signed range check.  */
10722               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
10723                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
10724                 return bfd_reloc_overflow;
10725
10726               addend = (value & 2);
10727
10728               value = (signed_addend & howto->dst_mask)
10729                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
10730
10731               if (r_type == R_ARM_CALL)
10732                 {
10733                   /* Set the H bit in the BLX instruction.  */
10734                   if (branch_type == ST_BRANCH_TO_THUMB)
10735                     {
10736                       if (addend)
10737                         value |= (1 << 24);
10738                       else
10739                         value &= ~(bfd_vma)(1 << 24);
10740                     }
10741
10742                   /* Select the correct instruction (BL or BLX).  */
10743                   /* Only if we are not handling a BL to a stub. In this
10744                      case, mode switching is performed by the stub.  */
10745                   if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
10746                     value |= (1 << 28);
10747                   else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
10748                     {
10749                       value &= ~(bfd_vma)(1 << 28);
10750                       value |= (1 << 24);
10751                     }
10752                 }
10753             }
10754           }
10755           break;
10756
10757         case R_ARM_ABS32:
10758           value += addend;
10759           if (branch_type == ST_BRANCH_TO_THUMB)
10760             value |= 1;
10761           break;
10762
10763         case R_ARM_ABS32_NOI:
10764           value += addend;
10765           break;
10766
10767         case R_ARM_REL32:
10768           value += addend;
10769           if (branch_type == ST_BRANCH_TO_THUMB)
10770             value |= 1;
10771           value -= (input_section->output_section->vma
10772                     + input_section->output_offset + rel->r_offset);
10773           break;
10774
10775         case R_ARM_REL32_NOI:
10776           value += addend;
10777           value -= (input_section->output_section->vma
10778                     + input_section->output_offset + rel->r_offset);
10779           break;
10780
10781         case R_ARM_PREL31:
10782           value -= (input_section->output_section->vma
10783                     + input_section->output_offset + rel->r_offset);
10784           value += signed_addend;
10785           if (! h || h->root.type != bfd_link_hash_undefweak)
10786             {
10787               /* Check for overflow.  */
10788               if ((value ^ (value >> 1)) & (1 << 30))
10789                 return bfd_reloc_overflow;
10790             }
10791           value &= 0x7fffffff;
10792           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
10793           if (branch_type == ST_BRANCH_TO_THUMB)
10794             value |= 1;
10795           break;
10796         }
10797
10798       bfd_put_32 (input_bfd, value, hit_data);
10799       return bfd_reloc_ok;
10800
10801     case R_ARM_ABS8:
10802       /* PR 16202: Refectch the addend using the correct size.  */
10803       if (globals->use_rel)
10804         addend = bfd_get_8 (input_bfd, hit_data);
10805       value += addend;
10806
10807       /* There is no way to tell whether the user intended to use a signed or
10808          unsigned addend.  When checking for overflow we accept either,
10809          as specified by the AAELF.  */
10810       if ((long) value > 0xff || (long) value < -0x80)
10811         return bfd_reloc_overflow;
10812
10813       bfd_put_8 (input_bfd, value, hit_data);
10814       return bfd_reloc_ok;
10815
10816     case R_ARM_ABS16:
10817       /* PR 16202: Refectch the addend using the correct size.  */
10818       if (globals->use_rel)
10819         addend = bfd_get_16 (input_bfd, hit_data);
10820       value += addend;
10821
10822       /* See comment for R_ARM_ABS8.  */
10823       if ((long) value > 0xffff || (long) value < -0x8000)
10824         return bfd_reloc_overflow;
10825
10826       bfd_put_16 (input_bfd, value, hit_data);
10827       return bfd_reloc_ok;
10828
10829     case R_ARM_THM_ABS5:
10830       /* Support ldr and str instructions for the thumb.  */
10831       if (globals->use_rel)
10832         {
10833           /* Need to refetch addend.  */
10834           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
10835           /* ??? Need to determine shift amount from operand size.  */
10836           addend >>= howto->rightshift;
10837         }
10838       value += addend;
10839
10840       /* ??? Isn't value unsigned?  */
10841       if ((long) value > 0x1f || (long) value < -0x10)
10842         return bfd_reloc_overflow;
10843
10844       /* ??? Value needs to be properly shifted into place first.  */
10845       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
10846       bfd_put_16 (input_bfd, value, hit_data);
10847       return bfd_reloc_ok;
10848
10849     case R_ARM_THM_ALU_PREL_11_0:
10850       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
10851       {
10852         bfd_vma insn;
10853         bfd_signed_vma relocation;
10854
10855         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10856              | bfd_get_16 (input_bfd, hit_data + 2);
10857
10858         if (globals->use_rel)
10859           {
10860             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
10861                           | ((insn & (1 << 26)) >> 15);
10862             if (insn & 0xf00000)
10863               signed_addend = -signed_addend;
10864           }
10865
10866         relocation = value + signed_addend;
10867         relocation -= Pa (input_section->output_section->vma
10868                           + input_section->output_offset
10869                           + rel->r_offset);
10870
10871         /* PR 21523: Use an absolute value.  The user of this reloc will
10872            have already selected an ADD or SUB insn appropriately.  */
10873         value = labs (relocation);
10874
10875         if (value >= 0x1000)
10876           return bfd_reloc_overflow;
10877
10878         /* Destination is Thumb.  Force bit 0 to 1 to reflect this.  */
10879         if (branch_type == ST_BRANCH_TO_THUMB)
10880           value |= 1;
10881
10882         insn = (insn & 0xfb0f8f00) | (value & 0xff)
10883              | ((value & 0x700) << 4)
10884              | ((value & 0x800) << 15);
10885         if (relocation < 0)
10886           insn |= 0xa00000;
10887
10888         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10889         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10890
10891         return bfd_reloc_ok;
10892       }
10893
10894     case R_ARM_THM_PC8:
10895       /* PR 10073:  This reloc is not generated by the GNU toolchain,
10896          but it is supported for compatibility with third party libraries
10897          generated by other compilers, specifically the ARM/IAR.  */
10898       {
10899         bfd_vma insn;
10900         bfd_signed_vma relocation;
10901
10902         insn = bfd_get_16 (input_bfd, hit_data);
10903
10904         if (globals->use_rel)
10905           addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
10906
10907         relocation = value + addend;
10908         relocation -= Pa (input_section->output_section->vma
10909                           + input_section->output_offset
10910                           + rel->r_offset);
10911
10912         value = relocation;
10913
10914         /* We do not check for overflow of this reloc.  Although strictly
10915            speaking this is incorrect, it appears to be necessary in order
10916            to work with IAR generated relocs.  Since GCC and GAS do not
10917            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
10918            a problem for them.  */
10919         value &= 0x3fc;
10920
10921         insn = (insn & 0xff00) | (value >> 2);
10922
10923         bfd_put_16 (input_bfd, insn, hit_data);
10924
10925         return bfd_reloc_ok;
10926       }
10927
10928     case R_ARM_THM_PC12:
10929       /* Corresponds to: ldr.w reg, [pc, #offset].  */
10930       {
10931         bfd_vma insn;
10932         bfd_signed_vma relocation;
10933
10934         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10935              | bfd_get_16 (input_bfd, hit_data + 2);
10936
10937         if (globals->use_rel)
10938           {
10939             signed_addend = insn & 0xfff;
10940             if (!(insn & (1 << 23)))
10941               signed_addend = -signed_addend;
10942           }
10943
10944         relocation = value + signed_addend;
10945         relocation -= Pa (input_section->output_section->vma
10946                           + input_section->output_offset
10947                           + rel->r_offset);
10948
10949         value = relocation;
10950
10951         if (value >= 0x1000)
10952           return bfd_reloc_overflow;
10953
10954         insn = (insn & 0xff7ff000) | value;
10955         if (relocation >= 0)
10956           insn |= (1 << 23);
10957
10958         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10959         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10960
10961         return bfd_reloc_ok;
10962       }
10963
10964     case R_ARM_THM_XPC22:
10965     case R_ARM_THM_CALL:
10966     case R_ARM_THM_JUMP24:
10967       /* Thumb BL (branch long instruction).  */
10968       {
10969         bfd_vma relocation;
10970         bfd_vma reloc_sign;
10971         bfd_boolean overflow = FALSE;
10972         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
10973         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
10974         bfd_signed_vma reloc_signed_max;
10975         bfd_signed_vma reloc_signed_min;
10976         bfd_vma check;
10977         bfd_signed_vma signed_check;
10978         int bitsize;
10979         const int thumb2 = using_thumb2 (globals);
10980         const int thumb2_bl = using_thumb2_bl (globals);
10981
10982         /* A branch to an undefined weak symbol is turned into a jump to
10983            the next instruction unless a PLT entry will be created.
10984            The jump to the next instruction is optimized as a NOP.W for
10985            Thumb-2 enabled architectures.  */
10986         if (h && h->root.type == bfd_link_hash_undefweak
10987             && plt_offset == (bfd_vma) -1)
10988           {
10989             if (thumb2)
10990               {
10991                 bfd_put_16 (input_bfd, 0xf3af, hit_data);
10992                 bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
10993               }
10994             else
10995               {
10996                 bfd_put_16 (input_bfd, 0xe000, hit_data);
10997                 bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
10998               }
10999             return bfd_reloc_ok;
11000           }
11001
11002         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
11003            with Thumb-1) involving the J1 and J2 bits.  */
11004         if (globals->use_rel)
11005           {
11006             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
11007             bfd_vma upper = upper_insn & 0x3ff;
11008             bfd_vma lower = lower_insn & 0x7ff;
11009             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
11010             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
11011             bfd_vma i1 = j1 ^ s ? 0 : 1;
11012             bfd_vma i2 = j2 ^ s ? 0 : 1;
11013
11014             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
11015             /* Sign extend.  */
11016             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
11017
11018             signed_addend = addend;
11019           }
11020
11021         if (r_type == R_ARM_THM_XPC22)
11022           {
11023             /* Check for Thumb to Thumb call.  */
11024             /* FIXME: Should we translate the instruction into a BL
11025                instruction instead ?  */
11026             if (branch_type == ST_BRANCH_TO_THUMB)
11027               _bfd_error_handler
11028                 (_("%pB: warning: %s BLX instruction targets"
11029                    " %s function '%s'"),
11030                  input_bfd, "Thumb",
11031                  "Thumb", h ? h->root.root.string : "(local)");
11032           }
11033         else
11034           {
11035             /* If it is not a call to Thumb, assume call to Arm.
11036                If it is a call relative to a section name, then it is not a
11037                function call at all, but rather a long jump.  Calls through
11038                the PLT do not require stubs.  */
11039             if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
11040               {
11041                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
11042                   {
11043                     /* Convert BL to BLX.  */
11044                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
11045                   }
11046                 else if ((   r_type != R_ARM_THM_CALL)
11047                          && (r_type != R_ARM_THM_JUMP24))
11048                   {
11049                     if (elf32_thumb_to_arm_stub
11050                         (info, sym_name, input_bfd, output_bfd, input_section,
11051                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
11052                          error_message))
11053                       return bfd_reloc_ok;
11054                     else
11055                       return bfd_reloc_dangerous;
11056                   }
11057               }
11058             else if (branch_type == ST_BRANCH_TO_THUMB
11059                      && globals->use_blx
11060                      && r_type == R_ARM_THM_CALL)
11061               {
11062                 /* Make sure this is a BL.  */
11063                 lower_insn |= 0x1800;
11064               }
11065           }
11066
11067         enum elf32_arm_stub_type stub_type = arm_stub_none;
11068         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
11069           {
11070             /* Check if a stub has to be inserted because the destination
11071                is too far.  */
11072             struct elf32_arm_stub_hash_entry *stub_entry;
11073             struct elf32_arm_link_hash_entry *hash;
11074
11075             hash = (struct elf32_arm_link_hash_entry *) h;
11076
11077             stub_type = arm_type_of_stub (info, input_section, rel,
11078                                           st_type, &branch_type,
11079                                           hash, value, sym_sec,
11080                                           input_bfd, sym_name);
11081
11082             if (stub_type != arm_stub_none)
11083               {
11084                 /* The target is out of reach or we are changing modes, so
11085                    redirect the branch to the local stub for this
11086                    function.  */
11087                 stub_entry = elf32_arm_get_stub_entry (input_section,
11088                                                        sym_sec, h,
11089                                                        rel, globals,
11090                                                        stub_type);
11091                 if (stub_entry != NULL)
11092                   {
11093                     value = (stub_entry->stub_offset
11094                              + stub_entry->stub_sec->output_offset
11095                              + stub_entry->stub_sec->output_section->vma);
11096
11097                     if (plt_offset != (bfd_vma) -1)
11098                       *unresolved_reloc_p = FALSE;
11099                   }
11100
11101                 /* If this call becomes a call to Arm, force BLX.  */
11102                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
11103                   {
11104                     if ((stub_entry
11105                          && !arm_stub_is_thumb (stub_entry->stub_type))
11106                         || branch_type != ST_BRANCH_TO_THUMB)
11107                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
11108                   }
11109               }
11110           }
11111
11112         /* Handle calls via the PLT.  */
11113         if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
11114           {
11115             value = (splt->output_section->vma
11116                      + splt->output_offset
11117                      + plt_offset);
11118
11119             if (globals->use_blx
11120                 && r_type == R_ARM_THM_CALL
11121                 && ! using_thumb_only (globals))
11122               {
11123                 /* If the Thumb BLX instruction is available, convert
11124                    the BL to a BLX instruction to call the ARM-mode
11125                    PLT entry.  */
11126                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
11127                 branch_type = ST_BRANCH_TO_ARM;
11128               }
11129             else
11130               {
11131                 if (! using_thumb_only (globals))
11132                   /* Target the Thumb stub before the ARM PLT entry.  */
11133                   value -= PLT_THUMB_STUB_SIZE;
11134                 branch_type = ST_BRANCH_TO_THUMB;
11135               }
11136             *unresolved_reloc_p = FALSE;
11137           }
11138
11139         relocation = value + signed_addend;
11140
11141         relocation -= (input_section->output_section->vma
11142                        + input_section->output_offset
11143                        + rel->r_offset);
11144
11145         check = relocation >> howto->rightshift;
11146
11147         /* If this is a signed value, the rightshift just dropped
11148            leading 1 bits (assuming twos complement).  */
11149         if ((bfd_signed_vma) relocation >= 0)
11150           signed_check = check;
11151         else
11152           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
11153
11154         /* Calculate the permissable maximum and minimum values for
11155            this relocation according to whether we're relocating for
11156            Thumb-2 or not.  */
11157         bitsize = howto->bitsize;
11158         if (!thumb2_bl)
11159           bitsize -= 2;
11160         reloc_signed_max = (1 << (bitsize - 1)) - 1;
11161         reloc_signed_min = ~reloc_signed_max;
11162
11163         /* Assumes two's complement.  */
11164         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11165           overflow = TRUE;
11166
11167         if ((lower_insn & 0x5000) == 0x4000)
11168           /* For a BLX instruction, make sure that the relocation is rounded up
11169              to a word boundary.  This follows the semantics of the instruction
11170              which specifies that bit 1 of the target address will come from bit
11171              1 of the base address.  */
11172           relocation = (relocation + 2) & ~ 3;
11173
11174         /* Put RELOCATION back into the insn.  Assumes two's complement.
11175            We use the Thumb-2 encoding, which is safe even if dealing with
11176            a Thumb-1 instruction by virtue of our overflow check above.  */
11177         reloc_sign = (signed_check < 0) ? 1 : 0;
11178         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
11179                      | ((relocation >> 12) & 0x3ff)
11180                      | (reloc_sign << 10);
11181         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
11182                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
11183                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
11184                      | ((relocation >> 1) & 0x7ff);
11185
11186         /* Put the relocated value back in the object file:  */
11187         bfd_put_16 (input_bfd, upper_insn, hit_data);
11188         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11189
11190         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11191       }
11192       break;
11193
11194     case R_ARM_THM_JUMP19:
11195       /* Thumb32 conditional branch instruction.  */
11196       {
11197         bfd_vma relocation;
11198         bfd_boolean overflow = FALSE;
11199         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
11200         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
11201         bfd_signed_vma reloc_signed_max = 0xffffe;
11202         bfd_signed_vma reloc_signed_min = -0x100000;
11203         bfd_signed_vma signed_check;
11204         enum elf32_arm_stub_type stub_type = arm_stub_none;
11205         struct elf32_arm_stub_hash_entry *stub_entry;
11206         struct elf32_arm_link_hash_entry *hash;
11207
11208         /* Need to refetch the addend, reconstruct the top three bits,
11209            and squish the two 11 bit pieces together.  */
11210         if (globals->use_rel)
11211           {
11212             bfd_vma S     = (upper_insn & 0x0400) >> 10;
11213             bfd_vma upper = (upper_insn & 0x003f);
11214             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
11215             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
11216             bfd_vma lower = (lower_insn & 0x07ff);
11217
11218             upper |= J1 << 6;
11219             upper |= J2 << 7;
11220             upper |= (!S) << 8;
11221             upper -= 0x0100; /* Sign extend.  */
11222
11223             addend = (upper << 12) | (lower << 1);
11224             signed_addend = addend;
11225           }
11226
11227         /* Handle calls via the PLT.  */
11228         if (plt_offset != (bfd_vma) -1)
11229           {
11230             value = (splt->output_section->vma
11231                      + splt->output_offset
11232                      + plt_offset);
11233             /* Target the Thumb stub before the ARM PLT entry.  */
11234             value -= PLT_THUMB_STUB_SIZE;
11235             *unresolved_reloc_p = FALSE;
11236           }
11237
11238         hash = (struct elf32_arm_link_hash_entry *)h;
11239
11240         stub_type = arm_type_of_stub (info, input_section, rel,
11241                                       st_type, &branch_type,
11242                                       hash, value, sym_sec,
11243                                       input_bfd, sym_name);
11244         if (stub_type != arm_stub_none)
11245           {
11246             stub_entry = elf32_arm_get_stub_entry (input_section,
11247                                                    sym_sec, h,
11248                                                    rel, globals,
11249                                                    stub_type);
11250             if (stub_entry != NULL)
11251               {
11252                 value = (stub_entry->stub_offset
11253                         + stub_entry->stub_sec->output_offset
11254                         + stub_entry->stub_sec->output_section->vma);
11255               }
11256           }
11257
11258         relocation = value + signed_addend;
11259         relocation -= (input_section->output_section->vma
11260                        + input_section->output_offset
11261                        + rel->r_offset);
11262         signed_check = (bfd_signed_vma) relocation;
11263
11264         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11265           overflow = TRUE;
11266
11267         /* Put RELOCATION back into the insn.  */
11268         {
11269           bfd_vma S  = (relocation & 0x00100000) >> 20;
11270           bfd_vma J2 = (relocation & 0x00080000) >> 19;
11271           bfd_vma J1 = (relocation & 0x00040000) >> 18;
11272           bfd_vma hi = (relocation & 0x0003f000) >> 12;
11273           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
11274
11275           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
11276           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
11277         }
11278
11279         /* Put the relocated value back in the object file:  */
11280         bfd_put_16 (input_bfd, upper_insn, hit_data);
11281         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11282
11283         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11284       }
11285
11286     case R_ARM_THM_JUMP11:
11287     case R_ARM_THM_JUMP8:
11288     case R_ARM_THM_JUMP6:
11289       /* Thumb B (branch) instruction).  */
11290       {
11291         bfd_signed_vma relocation;
11292         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
11293         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
11294         bfd_signed_vma signed_check;
11295
11296         /* CZB cannot jump backward.  */
11297         if (r_type == R_ARM_THM_JUMP6)
11298           reloc_signed_min = 0;
11299
11300         if (globals->use_rel)
11301           {
11302             /* Need to refetch addend.  */
11303             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
11304             if (addend & ((howto->src_mask + 1) >> 1))
11305               {
11306                 signed_addend = -1;
11307                 signed_addend &= ~ howto->src_mask;
11308                 signed_addend |= addend;
11309               }
11310             else
11311               signed_addend = addend;
11312             /* The value in the insn has been right shifted.  We need to
11313                undo this, so that we can perform the address calculation
11314                in terms of bytes.  */
11315             signed_addend <<= howto->rightshift;
11316           }
11317         relocation = value + signed_addend;
11318
11319         relocation -= (input_section->output_section->vma
11320                        + input_section->output_offset
11321                        + rel->r_offset);
11322
11323         relocation >>= howto->rightshift;
11324         signed_check = relocation;
11325
11326         if (r_type == R_ARM_THM_JUMP6)
11327           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
11328         else
11329           relocation &= howto->dst_mask;
11330         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
11331
11332         bfd_put_16 (input_bfd, relocation, hit_data);
11333
11334         /* Assumes two's complement.  */
11335         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11336           return bfd_reloc_overflow;
11337
11338         return bfd_reloc_ok;
11339       }
11340
11341     case R_ARM_ALU_PCREL7_0:
11342     case R_ARM_ALU_PCREL15_8:
11343     case R_ARM_ALU_PCREL23_15:
11344       {
11345         bfd_vma insn;
11346         bfd_vma relocation;
11347
11348         insn = bfd_get_32 (input_bfd, hit_data);
11349         if (globals->use_rel)
11350           {
11351             /* Extract the addend.  */
11352             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
11353             signed_addend = addend;
11354           }
11355         relocation = value + signed_addend;
11356
11357         relocation -= (input_section->output_section->vma
11358                        + input_section->output_offset
11359                        + rel->r_offset);
11360         insn = (insn & ~0xfff)
11361                | ((howto->bitpos << 7) & 0xf00)
11362                | ((relocation >> howto->bitpos) & 0xff);
11363         bfd_put_32 (input_bfd, value, hit_data);
11364       }
11365       return bfd_reloc_ok;
11366
11367     case R_ARM_GNU_VTINHERIT:
11368     case R_ARM_GNU_VTENTRY:
11369       return bfd_reloc_ok;
11370
11371     case R_ARM_GOTOFF32:
11372       /* Relocation is relative to the start of the
11373          global offset table.  */
11374
11375       BFD_ASSERT (sgot != NULL);
11376       if (sgot == NULL)
11377         return bfd_reloc_notsupported;
11378
11379       /* If we are addressing a Thumb function, we need to adjust the
11380          address by one, so that attempts to call the function pointer will
11381          correctly interpret it as Thumb code.  */
11382       if (branch_type == ST_BRANCH_TO_THUMB)
11383         value += 1;
11384
11385       /* Note that sgot->output_offset is not involved in this
11386          calculation.  We always want the start of .got.  If we
11387          define _GLOBAL_OFFSET_TABLE in a different way, as is
11388          permitted by the ABI, we might have to change this
11389          calculation.  */
11390       value -= sgot->output_section->vma;
11391       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11392                                        contents, rel->r_offset, value,
11393                                        rel->r_addend);
11394
11395     case R_ARM_GOTPC:
11396       /* Use global offset table as symbol value.  */
11397       BFD_ASSERT (sgot != NULL);
11398
11399       if (sgot == NULL)
11400         return bfd_reloc_notsupported;
11401
11402       *unresolved_reloc_p = FALSE;
11403       value = sgot->output_section->vma;
11404       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11405                                        contents, rel->r_offset, value,
11406                                        rel->r_addend);
11407
11408     case R_ARM_GOT32:
11409     case R_ARM_GOT_PREL:
11410       /* Relocation is to the entry for this symbol in the
11411          global offset table.  */
11412       if (sgot == NULL)
11413         return bfd_reloc_notsupported;
11414
11415       if (dynreloc_st_type == STT_GNU_IFUNC
11416           && plt_offset != (bfd_vma) -1
11417           && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
11418         {
11419           /* We have a relocation against a locally-binding STT_GNU_IFUNC
11420              symbol, and the relocation resolves directly to the runtime
11421              target rather than to the .iplt entry.  This means that any
11422              .got entry would be the same value as the .igot.plt entry,
11423              so there's no point creating both.  */
11424           sgot = globals->root.igotplt;
11425           value = sgot->output_offset + gotplt_offset;
11426         }
11427       else if (h != NULL)
11428         {
11429           bfd_vma off;
11430
11431           off = h->got.offset;
11432           BFD_ASSERT (off != (bfd_vma) -1);
11433           if ((off & 1) != 0)
11434             {
11435               /* We have already processsed one GOT relocation against
11436                  this symbol.  */
11437               off &= ~1;
11438               if (globals->root.dynamic_sections_created
11439                   && !SYMBOL_REFERENCES_LOCAL (info, h))
11440                 *unresolved_reloc_p = FALSE;
11441             }
11442           else
11443             {
11444               Elf_Internal_Rela outrel;
11445               int isrofixup = 0;
11446
11447               if (((h->dynindx != -1) || globals->fdpic_p)
11448                   && !SYMBOL_REFERENCES_LOCAL (info, h))
11449                 {
11450                   /* If the symbol doesn't resolve locally in a static
11451                      object, we have an undefined reference.  If the
11452                      symbol doesn't resolve locally in a dynamic object,
11453                      it should be resolved by the dynamic linker.  */
11454                   if (globals->root.dynamic_sections_created)
11455                     {
11456                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
11457                       *unresolved_reloc_p = FALSE;
11458                     }
11459                   else
11460                     outrel.r_info = 0;
11461                   outrel.r_addend = 0;
11462                 }
11463               else
11464                 {
11465                   if (dynreloc_st_type == STT_GNU_IFUNC)
11466                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11467                   else if (bfd_link_pic (info)
11468                            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11469                                || h->root.type != bfd_link_hash_undefweak))
11470                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11471                   else if (globals->fdpic_p)
11472                     isrofixup = 1;
11473                   else
11474                     outrel.r_info = 0;
11475                   outrel.r_addend = dynreloc_value;
11476                 }
11477
11478               /* The GOT entry is initialized to zero by default.
11479                  See if we should install a different value.  */
11480               if (outrel.r_addend != 0
11481                   && (outrel.r_info == 0 || globals->use_rel || isrofixup))
11482                 {
11483                   bfd_put_32 (output_bfd, outrel.r_addend,
11484                               sgot->contents + off);
11485                   outrel.r_addend = 0;
11486                 }
11487
11488               if (outrel.r_info != 0 && !isrofixup)
11489                 {
11490                   outrel.r_offset = (sgot->output_section->vma
11491                                      + sgot->output_offset
11492                                      + off);
11493                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11494                 }
11495               else if (isrofixup)
11496                 {
11497                   arm_elf_add_rofixup(output_bfd,
11498                                       elf32_arm_hash_table(info)->srofixup,
11499                                       sgot->output_section->vma
11500                                       + sgot->output_offset + off);
11501                 }
11502               h->got.offset |= 1;
11503             }
11504           value = sgot->output_offset + off;
11505         }
11506       else
11507         {
11508           bfd_vma off;
11509
11510           BFD_ASSERT (local_got_offsets != NULL
11511                       && local_got_offsets[r_symndx] != (bfd_vma) -1);
11512
11513           off = local_got_offsets[r_symndx];
11514
11515           /* The offset must always be a multiple of 4.  We use the
11516              least significant bit to record whether we have already
11517              generated the necessary reloc.  */
11518           if ((off & 1) != 0)
11519             off &= ~1;
11520           else
11521             {
11522               if (globals->use_rel)
11523                 bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
11524
11525               if (bfd_link_pic (info) || dynreloc_st_type == STT_GNU_IFUNC)
11526                 {
11527                   Elf_Internal_Rela outrel;
11528
11529                   outrel.r_addend = addend + dynreloc_value;
11530                   outrel.r_offset = (sgot->output_section->vma
11531                                      + sgot->output_offset
11532                                      + off);
11533                   if (dynreloc_st_type == STT_GNU_IFUNC)
11534                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11535                   else
11536                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11537                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11538                 }
11539               else if (globals->fdpic_p)
11540                 {
11541                   /* For FDPIC executables, we use rofixup to fix
11542                      address at runtime.  */
11543                   arm_elf_add_rofixup(output_bfd, globals->srofixup,
11544                                       sgot->output_section->vma + sgot->output_offset
11545                                       + off);
11546                 }
11547
11548               local_got_offsets[r_symndx] |= 1;
11549             }
11550
11551           value = sgot->output_offset + off;
11552         }
11553       if (r_type != R_ARM_GOT32)
11554         value += sgot->output_section->vma;
11555
11556       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11557                                        contents, rel->r_offset, value,
11558                                        rel->r_addend);
11559
11560     case R_ARM_TLS_LDO32:
11561       value = value - dtpoff_base (info);
11562
11563       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11564                                        contents, rel->r_offset, value,
11565                                        rel->r_addend);
11566
11567     case R_ARM_TLS_LDM32:
11568     case R_ARM_TLS_LDM32_FDPIC:
11569       {
11570         bfd_vma off;
11571
11572         if (sgot == NULL)
11573           abort ();
11574
11575         off = globals->tls_ldm_got.offset;
11576
11577         if ((off & 1) != 0)
11578           off &= ~1;
11579         else
11580           {
11581             /* If we don't know the module number, create a relocation
11582                for it.  */
11583             if (bfd_link_pic (info))
11584               {
11585                 Elf_Internal_Rela outrel;
11586
11587                 if (srelgot == NULL)
11588                   abort ();
11589
11590                 outrel.r_addend = 0;
11591                 outrel.r_offset = (sgot->output_section->vma
11592                                    + sgot->output_offset + off);
11593                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
11594
11595                 if (globals->use_rel)
11596                   bfd_put_32 (output_bfd, outrel.r_addend,
11597                               sgot->contents + off);
11598
11599                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11600               }
11601             else
11602               bfd_put_32 (output_bfd, 1, sgot->contents + off);
11603
11604             globals->tls_ldm_got.offset |= 1;
11605           }
11606
11607         if (r_type == R_ARM_TLS_LDM32_FDPIC)
11608           {
11609             bfd_put_32(output_bfd,
11610                        globals->root.sgot->output_offset + off,
11611                        contents + rel->r_offset);
11612
11613             return bfd_reloc_ok;
11614           }
11615         else
11616           {
11617             value = sgot->output_section->vma + sgot->output_offset + off
11618               - (input_section->output_section->vma
11619                  + input_section->output_offset + rel->r_offset);
11620
11621             return _bfd_final_link_relocate (howto, input_bfd, input_section,
11622                                              contents, rel->r_offset, value,
11623                                              rel->r_addend);
11624           }
11625       }
11626
11627     case R_ARM_TLS_CALL:
11628     case R_ARM_THM_TLS_CALL:
11629     case R_ARM_TLS_GD32:
11630     case R_ARM_TLS_GD32_FDPIC:
11631     case R_ARM_TLS_IE32:
11632     case R_ARM_TLS_IE32_FDPIC:
11633     case R_ARM_TLS_GOTDESC:
11634     case R_ARM_TLS_DESCSEQ:
11635     case R_ARM_THM_TLS_DESCSEQ:
11636       {
11637         bfd_vma off, offplt;
11638         int indx = 0;
11639         char tls_type;
11640
11641         BFD_ASSERT (sgot != NULL);
11642
11643         if (h != NULL)
11644           {
11645             bfd_boolean dyn;
11646             dyn = globals->root.dynamic_sections_created;
11647             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
11648                                                  bfd_link_pic (info),
11649                                                  h)
11650                 && (!bfd_link_pic (info)
11651                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
11652               {
11653                 *unresolved_reloc_p = FALSE;
11654                 indx = h->dynindx;
11655               }
11656             off = h->got.offset;
11657             offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
11658             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
11659           }
11660         else
11661           {
11662             BFD_ASSERT (local_got_offsets != NULL);
11663             off = local_got_offsets[r_symndx];
11664             offplt = local_tlsdesc_gotents[r_symndx];
11665             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
11666           }
11667
11668         /* Linker relaxations happens from one of the
11669            R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
11670         if (ELF32_R_TYPE(rel->r_info) != r_type)
11671           tls_type = GOT_TLS_IE;
11672
11673         BFD_ASSERT (tls_type != GOT_UNKNOWN);
11674
11675         if ((off & 1) != 0)
11676           off &= ~1;
11677         else
11678           {
11679             bfd_boolean need_relocs = FALSE;
11680             Elf_Internal_Rela outrel;
11681             int cur_off = off;
11682
11683             /* The GOT entries have not been initialized yet.  Do it
11684                now, and emit any relocations.  If both an IE GOT and a
11685                GD GOT are necessary, we emit the GD first.  */
11686
11687             if ((bfd_link_pic (info) || indx != 0)
11688                 && (h == NULL
11689                     || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11690                         && !resolved_to_zero)
11691                     || h->root.type != bfd_link_hash_undefweak))
11692               {
11693                 need_relocs = TRUE;
11694                 BFD_ASSERT (srelgot != NULL);
11695               }
11696
11697             if (tls_type & GOT_TLS_GDESC)
11698               {
11699                 bfd_byte *loc;
11700
11701                 /* We should have relaxed, unless this is an undefined
11702                    weak symbol.  */
11703                 BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
11704                             || bfd_link_pic (info));
11705                 BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
11706                             <= globals->root.sgotplt->size);
11707
11708                 outrel.r_addend = 0;
11709                 outrel.r_offset = (globals->root.sgotplt->output_section->vma
11710                                    + globals->root.sgotplt->output_offset
11711                                    + offplt
11712                                    + globals->sgotplt_jump_table_size);
11713
11714                 outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
11715                 sreloc = globals->root.srelplt;
11716                 loc = sreloc->contents;
11717                 loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
11718                 BFD_ASSERT (loc + RELOC_SIZE (globals)
11719                            <= sreloc->contents + sreloc->size);
11720
11721                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
11722
11723                 /* For globals, the first word in the relocation gets
11724                    the relocation index and the top bit set, or zero,
11725                    if we're binding now.  For locals, it gets the
11726                    symbol's offset in the tls section.  */
11727                 bfd_put_32 (output_bfd,
11728                             !h ? value - elf_hash_table (info)->tls_sec->vma
11729                             : info->flags & DF_BIND_NOW ? 0
11730                             : 0x80000000 | ELF32_R_SYM (outrel.r_info),
11731                             globals->root.sgotplt->contents + offplt
11732                             + globals->sgotplt_jump_table_size);
11733
11734                 /* Second word in the relocation is always zero.  */
11735                 bfd_put_32 (output_bfd, 0,
11736                             globals->root.sgotplt->contents + offplt
11737                             + globals->sgotplt_jump_table_size + 4);
11738               }
11739             if (tls_type & GOT_TLS_GD)
11740               {
11741                 if (need_relocs)
11742                   {
11743                     outrel.r_addend = 0;
11744                     outrel.r_offset = (sgot->output_section->vma
11745                                        + sgot->output_offset
11746                                        + cur_off);
11747                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
11748
11749                     if (globals->use_rel)
11750                       bfd_put_32 (output_bfd, outrel.r_addend,
11751                                   sgot->contents + cur_off);
11752
11753                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11754
11755                     if (indx == 0)
11756                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
11757                                   sgot->contents + cur_off + 4);
11758                     else
11759                       {
11760                         outrel.r_addend = 0;
11761                         outrel.r_info = ELF32_R_INFO (indx,
11762                                                       R_ARM_TLS_DTPOFF32);
11763                         outrel.r_offset += 4;
11764
11765                         if (globals->use_rel)
11766                           bfd_put_32 (output_bfd, outrel.r_addend,
11767                                       sgot->contents + cur_off + 4);
11768
11769                         elf32_arm_add_dynreloc (output_bfd, info,
11770                                                 srelgot, &outrel);
11771                       }
11772                   }
11773                 else
11774                   {
11775                     /* If we are not emitting relocations for a
11776                        general dynamic reference, then we must be in a
11777                        static link or an executable link with the
11778                        symbol binding locally.  Mark it as belonging
11779                        to module 1, the executable.  */
11780                     bfd_put_32 (output_bfd, 1,
11781                                 sgot->contents + cur_off);
11782                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
11783                                 sgot->contents + cur_off + 4);
11784                   }
11785
11786                 cur_off += 8;
11787               }
11788
11789             if (tls_type & GOT_TLS_IE)
11790               {
11791                 if (need_relocs)
11792                   {
11793                     if (indx == 0)
11794                       outrel.r_addend = value - dtpoff_base (info);
11795                     else
11796                       outrel.r_addend = 0;
11797                     outrel.r_offset = (sgot->output_section->vma
11798                                        + sgot->output_offset
11799                                        + cur_off);
11800                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
11801
11802                     if (globals->use_rel)
11803                       bfd_put_32 (output_bfd, outrel.r_addend,
11804                                   sgot->contents + cur_off);
11805
11806                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11807                   }
11808                 else
11809                   bfd_put_32 (output_bfd, tpoff (info, value),
11810                               sgot->contents + cur_off);
11811                 cur_off += 4;
11812               }
11813
11814             if (h != NULL)
11815               h->got.offset |= 1;
11816             else
11817               local_got_offsets[r_symndx] |= 1;
11818           }
11819
11820         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32 && r_type != R_ARM_TLS_GD32_FDPIC)
11821           off += 8;
11822         else if (tls_type & GOT_TLS_GDESC)
11823           off = offplt;
11824
11825         if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
11826             || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
11827           {
11828             bfd_signed_vma offset;
11829             /* TLS stubs are arm mode.  The original symbol is a
11830                data object, so branch_type is bogus.  */
11831             branch_type = ST_BRANCH_TO_ARM;
11832             enum elf32_arm_stub_type stub_type
11833               = arm_type_of_stub (info, input_section, rel,
11834                                   st_type, &branch_type,
11835                                   (struct elf32_arm_link_hash_entry *)h,
11836                                   globals->tls_trampoline, globals->root.splt,
11837                                   input_bfd, sym_name);
11838
11839             if (stub_type != arm_stub_none)
11840               {
11841                 struct elf32_arm_stub_hash_entry *stub_entry
11842                   = elf32_arm_get_stub_entry
11843                   (input_section, globals->root.splt, 0, rel,
11844                    globals, stub_type);
11845                 offset = (stub_entry->stub_offset
11846                           + stub_entry->stub_sec->output_offset
11847                           + stub_entry->stub_sec->output_section->vma);
11848               }
11849             else
11850               offset = (globals->root.splt->output_section->vma
11851                         + globals->root.splt->output_offset
11852                         + globals->tls_trampoline);
11853
11854             if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
11855               {
11856                 unsigned long inst;
11857
11858                 offset -= (input_section->output_section->vma
11859                            + input_section->output_offset
11860                            + rel->r_offset + 8);
11861
11862                 inst = offset >> 2;
11863                 inst &= 0x00ffffff;
11864                 value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
11865               }
11866             else
11867               {
11868                 /* Thumb blx encodes the offset in a complicated
11869                    fashion.  */
11870                 unsigned upper_insn, lower_insn;
11871                 unsigned neg;
11872
11873                 offset -= (input_section->output_section->vma
11874                            + input_section->output_offset
11875                            + rel->r_offset + 4);
11876
11877                 if (stub_type != arm_stub_none
11878                     && arm_stub_is_thumb (stub_type))
11879                   {
11880                     lower_insn = 0xd000;
11881                   }
11882                 else
11883                   {
11884                     lower_insn = 0xc000;
11885                     /* Round up the offset to a word boundary.  */
11886                     offset = (offset + 2) & ~2;
11887                   }
11888
11889                 neg = offset < 0;
11890                 upper_insn = (0xf000
11891                               | ((offset >> 12) & 0x3ff)
11892                               | (neg << 10));
11893                 lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
11894                               | (((!((offset >> 22) & 1)) ^ neg) << 11)
11895                               | ((offset >> 1) & 0x7ff);
11896                 bfd_put_16 (input_bfd, upper_insn, hit_data);
11897                 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11898                 return bfd_reloc_ok;
11899               }
11900           }
11901         /* These relocations needs special care, as besides the fact
11902            they point somewhere in .gotplt, the addend must be
11903            adjusted accordingly depending on the type of instruction
11904            we refer to.  */
11905         else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
11906           {
11907             unsigned long data, insn;
11908             unsigned thumb;
11909
11910             data = bfd_get_32 (input_bfd, hit_data);
11911             thumb = data & 1;
11912             data &= ~1u;
11913
11914             if (thumb)
11915               {
11916                 insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
11917                 if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
11918                   insn = (insn << 16)
11919                     | bfd_get_16 (input_bfd,
11920                                   contents + rel->r_offset - data + 2);
11921                 if ((insn & 0xf800c000) == 0xf000c000)
11922                   /* bl/blx */
11923                   value = -6;
11924                 else if ((insn & 0xffffff00) == 0x4400)
11925                   /* add */
11926                   value = -5;
11927                 else
11928                   {
11929                     _bfd_error_handler
11930                       /* xgettext:c-format */
11931                       (_("%pB(%pA+%#" PRIx64 "): "
11932                          "unexpected %s instruction '%#lx' "
11933                          "referenced by TLS_GOTDESC"),
11934                        input_bfd, input_section, (uint64_t) rel->r_offset,
11935                        "Thumb", insn);
11936                     return bfd_reloc_notsupported;
11937                   }
11938               }
11939             else
11940               {
11941                 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
11942
11943                 switch (insn >> 24)
11944                   {
11945                   case 0xeb:  /* bl */
11946                   case 0xfa:  /* blx */
11947                     value = -4;
11948                     break;
11949
11950                   case 0xe0:    /* add */
11951                     value = -8;
11952                     break;
11953
11954                   default:
11955                     _bfd_error_handler
11956                       /* xgettext:c-format */
11957                       (_("%pB(%pA+%#" PRIx64 "): "
11958                          "unexpected %s instruction '%#lx' "
11959                          "referenced by TLS_GOTDESC"),
11960                        input_bfd, input_section, (uint64_t) rel->r_offset,
11961                        "ARM", insn);
11962                     return bfd_reloc_notsupported;
11963                   }
11964               }
11965
11966             value += ((globals->root.sgotplt->output_section->vma
11967                        + globals->root.sgotplt->output_offset + off)
11968                       - (input_section->output_section->vma
11969                          + input_section->output_offset
11970                          + rel->r_offset)
11971                       + globals->sgotplt_jump_table_size);
11972           }
11973         else
11974           value = ((globals->root.sgot->output_section->vma
11975                     + globals->root.sgot->output_offset + off)
11976                    - (input_section->output_section->vma
11977                       + input_section->output_offset + rel->r_offset));
11978
11979         if (globals->fdpic_p && (r_type == R_ARM_TLS_GD32_FDPIC ||
11980                                  r_type == R_ARM_TLS_IE32_FDPIC))
11981           {
11982             /* For FDPIC relocations, resolve to the offset of the GOT
11983                entry from the start of GOT.  */
11984             bfd_put_32(output_bfd,
11985                        globals->root.sgot->output_offset + off,
11986                        contents + rel->r_offset);
11987
11988             return bfd_reloc_ok;
11989           }
11990         else
11991           {
11992             return _bfd_final_link_relocate (howto, input_bfd, input_section,
11993                                              contents, rel->r_offset, value,
11994                                              rel->r_addend);
11995           }
11996       }
11997
11998     case R_ARM_TLS_LE32:
11999       if (bfd_link_dll (info))
12000         {
12001           _bfd_error_handler
12002             /* xgettext:c-format */
12003             (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
12004                "in shared object"),
12005              input_bfd, input_section, (uint64_t) rel->r_offset, howto->name);
12006           return bfd_reloc_notsupported;
12007         }
12008       else
12009         value = tpoff (info, value);
12010
12011       return _bfd_final_link_relocate (howto, input_bfd, input_section,
12012                                        contents, rel->r_offset, value,
12013                                        rel->r_addend);
12014
12015     case R_ARM_V4BX:
12016       if (globals->fix_v4bx)
12017         {
12018           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12019
12020           /* Ensure that we have a BX instruction.  */
12021           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
12022
12023           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
12024             {
12025               /* Branch to veneer.  */
12026               bfd_vma glue_addr;
12027               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
12028               glue_addr -= input_section->output_section->vma
12029                            + input_section->output_offset
12030                            + rel->r_offset + 8;
12031               insn = (insn & 0xf0000000) | 0x0a000000
12032                      | ((glue_addr >> 2) & 0x00ffffff);
12033             }
12034           else
12035             {
12036               /* Preserve Rm (lowest four bits) and the condition code
12037                  (highest four bits). Other bits encode MOV PC,Rm.  */
12038               insn = (insn & 0xf000000f) | 0x01a0f000;
12039             }
12040
12041           bfd_put_32 (input_bfd, insn, hit_data);
12042         }
12043       return bfd_reloc_ok;
12044
12045     case R_ARM_MOVW_ABS_NC:
12046     case R_ARM_MOVT_ABS:
12047     case R_ARM_MOVW_PREL_NC:
12048     case R_ARM_MOVT_PREL:
12049     /* Until we properly support segment-base-relative addressing then
12050        we assume the segment base to be zero, as for the group relocations.
12051        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
12052        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
12053     case R_ARM_MOVW_BREL_NC:
12054     case R_ARM_MOVW_BREL:
12055     case R_ARM_MOVT_BREL:
12056       {
12057         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12058
12059         if (globals->use_rel)
12060           {
12061             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
12062             signed_addend = (addend ^ 0x8000) - 0x8000;
12063           }
12064
12065         value += signed_addend;
12066
12067         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
12068           value -= (input_section->output_section->vma
12069                     + input_section->output_offset + rel->r_offset);
12070
12071         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
12072           return bfd_reloc_overflow;
12073
12074         if (branch_type == ST_BRANCH_TO_THUMB)
12075           value |= 1;
12076
12077         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
12078             || r_type == R_ARM_MOVT_BREL)
12079           value >>= 16;
12080
12081         insn &= 0xfff0f000;
12082         insn |= value & 0xfff;
12083         insn |= (value & 0xf000) << 4;
12084         bfd_put_32 (input_bfd, insn, hit_data);
12085       }
12086       return bfd_reloc_ok;
12087
12088     case R_ARM_THM_MOVW_ABS_NC:
12089     case R_ARM_THM_MOVT_ABS:
12090     case R_ARM_THM_MOVW_PREL_NC:
12091     case R_ARM_THM_MOVT_PREL:
12092     /* Until we properly support segment-base-relative addressing then
12093        we assume the segment base to be zero, as for the above relocations.
12094        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
12095        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
12096        as R_ARM_THM_MOVT_ABS.  */
12097     case R_ARM_THM_MOVW_BREL_NC:
12098     case R_ARM_THM_MOVW_BREL:
12099     case R_ARM_THM_MOVT_BREL:
12100       {
12101         bfd_vma insn;
12102
12103         insn = bfd_get_16 (input_bfd, hit_data) << 16;
12104         insn |= bfd_get_16 (input_bfd, hit_data + 2);
12105
12106         if (globals->use_rel)
12107           {
12108             addend = ((insn >> 4)  & 0xf000)
12109                    | ((insn >> 15) & 0x0800)
12110                    | ((insn >> 4)  & 0x0700)
12111                    | (insn         & 0x00ff);
12112             signed_addend = (addend ^ 0x8000) - 0x8000;
12113           }
12114
12115         value += signed_addend;
12116
12117         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
12118           value -= (input_section->output_section->vma
12119                     + input_section->output_offset + rel->r_offset);
12120
12121         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
12122           return bfd_reloc_overflow;
12123
12124         if (branch_type == ST_BRANCH_TO_THUMB)
12125           value |= 1;
12126
12127         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
12128             || r_type == R_ARM_THM_MOVT_BREL)
12129           value >>= 16;
12130
12131         insn &= 0xfbf08f00;
12132         insn |= (value & 0xf000) << 4;
12133         insn |= (value & 0x0800) << 15;
12134         insn |= (value & 0x0700) << 4;
12135         insn |= (value & 0x00ff);
12136
12137         bfd_put_16 (input_bfd, insn >> 16, hit_data);
12138         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
12139       }
12140       return bfd_reloc_ok;
12141
12142     case R_ARM_ALU_PC_G0_NC:
12143     case R_ARM_ALU_PC_G1_NC:
12144     case R_ARM_ALU_PC_G0:
12145     case R_ARM_ALU_PC_G1:
12146     case R_ARM_ALU_PC_G2:
12147     case R_ARM_ALU_SB_G0_NC:
12148     case R_ARM_ALU_SB_G1_NC:
12149     case R_ARM_ALU_SB_G0:
12150     case R_ARM_ALU_SB_G1:
12151     case R_ARM_ALU_SB_G2:
12152       {
12153         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12154         bfd_vma pc = input_section->output_section->vma
12155                      + input_section->output_offset + rel->r_offset;
12156         /* sb is the origin of the *segment* containing the symbol.  */
12157         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12158         bfd_vma residual;
12159         bfd_vma g_n;
12160         bfd_signed_vma signed_value;
12161         int group = 0;
12162
12163         /* Determine which group of bits to select.  */
12164         switch (r_type)
12165           {
12166           case R_ARM_ALU_PC_G0_NC:
12167           case R_ARM_ALU_PC_G0:
12168           case R_ARM_ALU_SB_G0_NC:
12169           case R_ARM_ALU_SB_G0:
12170             group = 0;
12171             break;
12172
12173           case R_ARM_ALU_PC_G1_NC:
12174           case R_ARM_ALU_PC_G1:
12175           case R_ARM_ALU_SB_G1_NC:
12176           case R_ARM_ALU_SB_G1:
12177             group = 1;
12178             break;
12179
12180           case R_ARM_ALU_PC_G2:
12181           case R_ARM_ALU_SB_G2:
12182             group = 2;
12183             break;
12184
12185           default:
12186             abort ();
12187           }
12188
12189         /* If REL, extract the addend from the insn.  If RELA, it will
12190            have already been fetched for us.  */
12191         if (globals->use_rel)
12192           {
12193             int negative;
12194             bfd_vma constant = insn & 0xff;
12195             bfd_vma rotation = (insn & 0xf00) >> 8;
12196
12197             if (rotation == 0)
12198               signed_addend = constant;
12199             else
12200               {
12201                 /* Compensate for the fact that in the instruction, the
12202                    rotation is stored in multiples of 2 bits.  */
12203                 rotation *= 2;
12204
12205                 /* Rotate "constant" right by "rotation" bits.  */
12206                 signed_addend = (constant >> rotation) |
12207                                 (constant << (8 * sizeof (bfd_vma) - rotation));
12208               }
12209
12210             /* Determine if the instruction is an ADD or a SUB.
12211                (For REL, this determines the sign of the addend.)  */
12212             negative = identify_add_or_sub (insn);
12213             if (negative == 0)
12214               {
12215                 _bfd_error_handler
12216                   /* xgettext:c-format */
12217                   (_("%pB(%pA+%#" PRIx64 "): only ADD or SUB instructions "
12218                      "are allowed for ALU group relocations"),
12219                   input_bfd, input_section, (uint64_t) rel->r_offset);
12220                 return bfd_reloc_overflow;
12221               }
12222
12223             signed_addend *= negative;
12224           }
12225
12226         /* Compute the value (X) to go in the place.  */
12227         if (r_type == R_ARM_ALU_PC_G0_NC
12228             || r_type == R_ARM_ALU_PC_G1_NC
12229             || r_type == R_ARM_ALU_PC_G0
12230             || r_type == R_ARM_ALU_PC_G1
12231             || r_type == R_ARM_ALU_PC_G2)
12232           /* PC relative.  */
12233           signed_value = value - pc + signed_addend;
12234         else
12235           /* Section base relative.  */
12236           signed_value = value - sb + signed_addend;
12237
12238         /* If the target symbol is a Thumb function, then set the
12239            Thumb bit in the address.  */
12240         if (branch_type == ST_BRANCH_TO_THUMB)
12241           signed_value |= 1;
12242
12243         /* Calculate the value of the relevant G_n, in encoded
12244            constant-with-rotation format.  */
12245         g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12246                                           group, &residual);
12247
12248         /* Check for overflow if required.  */
12249         if ((r_type == R_ARM_ALU_PC_G0
12250              || r_type == R_ARM_ALU_PC_G1
12251              || r_type == R_ARM_ALU_PC_G2
12252              || r_type == R_ARM_ALU_SB_G0
12253              || r_type == R_ARM_ALU_SB_G1
12254              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
12255           {
12256             _bfd_error_handler
12257               /* xgettext:c-format */
12258               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12259                  "splitting %#" PRIx64 " for group relocation %s"),
12260                input_bfd, input_section, (uint64_t) rel->r_offset,
12261                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12262                howto->name);
12263             return bfd_reloc_overflow;
12264           }
12265
12266         /* Mask out the value and the ADD/SUB part of the opcode; take care
12267            not to destroy the S bit.  */
12268         insn &= 0xff1ff000;
12269
12270         /* Set the opcode according to whether the value to go in the
12271            place is negative.  */
12272         if (signed_value < 0)
12273           insn |= 1 << 22;
12274         else
12275           insn |= 1 << 23;
12276
12277         /* Encode the offset.  */
12278         insn |= g_n;
12279
12280         bfd_put_32 (input_bfd, insn, hit_data);
12281       }
12282       return bfd_reloc_ok;
12283
12284     case R_ARM_LDR_PC_G0:
12285     case R_ARM_LDR_PC_G1:
12286     case R_ARM_LDR_PC_G2:
12287     case R_ARM_LDR_SB_G0:
12288     case R_ARM_LDR_SB_G1:
12289     case R_ARM_LDR_SB_G2:
12290       {
12291         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12292         bfd_vma pc = input_section->output_section->vma
12293                      + input_section->output_offset + rel->r_offset;
12294         /* sb is the origin of the *segment* containing the symbol.  */
12295         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12296         bfd_vma residual;
12297         bfd_signed_vma signed_value;
12298         int group = 0;
12299
12300         /* Determine which groups of bits to calculate.  */
12301         switch (r_type)
12302           {
12303           case R_ARM_LDR_PC_G0:
12304           case R_ARM_LDR_SB_G0:
12305             group = 0;
12306             break;
12307
12308           case R_ARM_LDR_PC_G1:
12309           case R_ARM_LDR_SB_G1:
12310             group = 1;
12311             break;
12312
12313           case R_ARM_LDR_PC_G2:
12314           case R_ARM_LDR_SB_G2:
12315             group = 2;
12316             break;
12317
12318           default:
12319             abort ();
12320           }
12321
12322         /* If REL, extract the addend from the insn.  If RELA, it will
12323            have already been fetched for us.  */
12324         if (globals->use_rel)
12325           {
12326             int negative = (insn & (1 << 23)) ? 1 : -1;
12327             signed_addend = negative * (insn & 0xfff);
12328           }
12329
12330         /* Compute the value (X) to go in the place.  */
12331         if (r_type == R_ARM_LDR_PC_G0
12332             || r_type == R_ARM_LDR_PC_G1
12333             || r_type == R_ARM_LDR_PC_G2)
12334           /* PC relative.  */
12335           signed_value = value - pc + signed_addend;
12336         else
12337           /* Section base relative.  */
12338           signed_value = value - sb + signed_addend;
12339
12340         /* Calculate the value of the relevant G_{n-1} to obtain
12341            the residual at that stage.  */
12342         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12343                                     group - 1, &residual);
12344
12345         /* Check for overflow.  */
12346         if (residual >= 0x1000)
12347           {
12348             _bfd_error_handler
12349               /* xgettext:c-format */
12350               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12351                  "splitting %#" PRIx64 " for group relocation %s"),
12352                input_bfd, input_section, (uint64_t) rel->r_offset,
12353                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12354                howto->name);
12355             return bfd_reloc_overflow;
12356           }
12357
12358         /* Mask out the value and U bit.  */
12359         insn &= 0xff7ff000;
12360
12361         /* Set the U bit if the value to go in the place is non-negative.  */
12362         if (signed_value >= 0)
12363           insn |= 1 << 23;
12364
12365         /* Encode the offset.  */
12366         insn |= residual;
12367
12368         bfd_put_32 (input_bfd, insn, hit_data);
12369       }
12370       return bfd_reloc_ok;
12371
12372     case R_ARM_LDRS_PC_G0:
12373     case R_ARM_LDRS_PC_G1:
12374     case R_ARM_LDRS_PC_G2:
12375     case R_ARM_LDRS_SB_G0:
12376     case R_ARM_LDRS_SB_G1:
12377     case R_ARM_LDRS_SB_G2:
12378       {
12379         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12380         bfd_vma pc = input_section->output_section->vma
12381                      + input_section->output_offset + rel->r_offset;
12382         /* sb is the origin of the *segment* containing the symbol.  */
12383         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12384         bfd_vma residual;
12385         bfd_signed_vma signed_value;
12386         int group = 0;
12387
12388         /* Determine which groups of bits to calculate.  */
12389         switch (r_type)
12390           {
12391           case R_ARM_LDRS_PC_G0:
12392           case R_ARM_LDRS_SB_G0:
12393             group = 0;
12394             break;
12395
12396           case R_ARM_LDRS_PC_G1:
12397           case R_ARM_LDRS_SB_G1:
12398             group = 1;
12399             break;
12400
12401           case R_ARM_LDRS_PC_G2:
12402           case R_ARM_LDRS_SB_G2:
12403             group = 2;
12404             break;
12405
12406           default:
12407             abort ();
12408           }
12409
12410         /* If REL, extract the addend from the insn.  If RELA, it will
12411            have already been fetched for us.  */
12412         if (globals->use_rel)
12413           {
12414             int negative = (insn & (1 << 23)) ? 1 : -1;
12415             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
12416           }
12417
12418         /* Compute the value (X) to go in the place.  */
12419         if (r_type == R_ARM_LDRS_PC_G0
12420             || r_type == R_ARM_LDRS_PC_G1
12421             || r_type == R_ARM_LDRS_PC_G2)
12422           /* PC relative.  */
12423           signed_value = value - pc + signed_addend;
12424         else
12425           /* Section base relative.  */
12426           signed_value = value - sb + signed_addend;
12427
12428         /* Calculate the value of the relevant G_{n-1} to obtain
12429            the residual at that stage.  */
12430         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12431                                     group - 1, &residual);
12432
12433         /* Check for overflow.  */
12434         if (residual >= 0x100)
12435           {
12436             _bfd_error_handler
12437               /* xgettext:c-format */
12438               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12439                  "splitting %#" PRIx64 " for group relocation %s"),
12440                input_bfd, input_section, (uint64_t) rel->r_offset,
12441                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12442                howto->name);
12443             return bfd_reloc_overflow;
12444           }
12445
12446         /* Mask out the value and U bit.  */
12447         insn &= 0xff7ff0f0;
12448
12449         /* Set the U bit if the value to go in the place is non-negative.  */
12450         if (signed_value >= 0)
12451           insn |= 1 << 23;
12452
12453         /* Encode the offset.  */
12454         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
12455
12456         bfd_put_32 (input_bfd, insn, hit_data);
12457       }
12458       return bfd_reloc_ok;
12459
12460     case R_ARM_LDC_PC_G0:
12461     case R_ARM_LDC_PC_G1:
12462     case R_ARM_LDC_PC_G2:
12463     case R_ARM_LDC_SB_G0:
12464     case R_ARM_LDC_SB_G1:
12465     case R_ARM_LDC_SB_G2:
12466       {
12467         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12468         bfd_vma pc = input_section->output_section->vma
12469                      + input_section->output_offset + rel->r_offset;
12470         /* sb is the origin of the *segment* containing the symbol.  */
12471         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12472         bfd_vma residual;
12473         bfd_signed_vma signed_value;
12474         int group = 0;
12475
12476         /* Determine which groups of bits to calculate.  */
12477         switch (r_type)
12478           {
12479           case R_ARM_LDC_PC_G0:
12480           case R_ARM_LDC_SB_G0:
12481             group = 0;
12482             break;
12483
12484           case R_ARM_LDC_PC_G1:
12485           case R_ARM_LDC_SB_G1:
12486             group = 1;
12487             break;
12488
12489           case R_ARM_LDC_PC_G2:
12490           case R_ARM_LDC_SB_G2:
12491             group = 2;
12492             break;
12493
12494           default:
12495             abort ();
12496           }
12497
12498         /* If REL, extract the addend from the insn.  If RELA, it will
12499            have already been fetched for us.  */
12500         if (globals->use_rel)
12501           {
12502             int negative = (insn & (1 << 23)) ? 1 : -1;
12503             signed_addend = negative * ((insn & 0xff) << 2);
12504           }
12505
12506         /* Compute the value (X) to go in the place.  */
12507         if (r_type == R_ARM_LDC_PC_G0
12508             || r_type == R_ARM_LDC_PC_G1
12509             || r_type == R_ARM_LDC_PC_G2)
12510           /* PC relative.  */
12511           signed_value = value - pc + signed_addend;
12512         else
12513           /* Section base relative.  */
12514           signed_value = value - sb + signed_addend;
12515
12516         /* Calculate the value of the relevant G_{n-1} to obtain
12517            the residual at that stage.  */
12518         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12519                                     group - 1, &residual);
12520
12521         /* Check for overflow.  (The absolute value to go in the place must be
12522            divisible by four and, after having been divided by four, must
12523            fit in eight bits.)  */
12524         if ((residual & 0x3) != 0 || residual >= 0x400)
12525           {
12526             _bfd_error_handler
12527               /* xgettext:c-format */
12528               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12529                  "splitting %#" PRIx64 " for group relocation %s"),
12530                input_bfd, input_section, (uint64_t) rel->r_offset,
12531                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12532                howto->name);
12533             return bfd_reloc_overflow;
12534           }
12535
12536         /* Mask out the value and U bit.  */
12537         insn &= 0xff7fff00;
12538
12539         /* Set the U bit if the value to go in the place is non-negative.  */
12540         if (signed_value >= 0)
12541           insn |= 1 << 23;
12542
12543         /* Encode the offset.  */
12544         insn |= residual >> 2;
12545
12546         bfd_put_32 (input_bfd, insn, hit_data);
12547       }
12548       return bfd_reloc_ok;
12549
12550     case R_ARM_THM_ALU_ABS_G0_NC:
12551     case R_ARM_THM_ALU_ABS_G1_NC:
12552     case R_ARM_THM_ALU_ABS_G2_NC:
12553     case R_ARM_THM_ALU_ABS_G3_NC:
12554         {
12555             const int shift_array[4] = {0, 8, 16, 24};
12556             bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
12557             bfd_vma addr = value;
12558             int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
12559
12560             /* Compute address.  */
12561             if (globals->use_rel)
12562                 signed_addend = insn & 0xff;
12563             addr += signed_addend;
12564             if (branch_type == ST_BRANCH_TO_THUMB)
12565                 addr |= 1;
12566             /* Clean imm8 insn.  */
12567             insn &= 0xff00;
12568             /* And update with correct part of address.  */
12569             insn |= (addr >> shift) & 0xff;
12570             /* Update insn.  */
12571             bfd_put_16 (input_bfd, insn, hit_data);
12572         }
12573
12574         *unresolved_reloc_p = FALSE;
12575         return bfd_reloc_ok;
12576
12577     case R_ARM_GOTOFFFUNCDESC:
12578       {
12579         if (h == NULL)
12580           {
12581             struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts(input_bfd);
12582             int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12583             int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12584             bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12585             bfd_vma seg = -1;
12586
12587             if (bfd_link_pic(info) && dynindx == 0)
12588               abort();
12589
12590             /* Resolve relocation.  */
12591             bfd_put_32(output_bfd, (offset + sgot->output_offset)
12592                        , contents + rel->r_offset);
12593             /* Emit R_ARM_FUNCDESC_VALUE or two fixups on funcdesc if
12594                not done yet.  */
12595             arm_elf_fill_funcdesc(output_bfd, info,
12596                                   &local_fdpic_cnts[r_symndx].funcdesc_offset,
12597                                   dynindx, offset, addr, dynreloc_value, seg);
12598           }
12599         else
12600           {
12601             int dynindx;
12602             int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12603             bfd_vma addr;
12604             bfd_vma seg = -1;
12605
12606             /* For static binaries, sym_sec can be null.  */
12607             if (sym_sec)
12608               {
12609                 dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12610                 addr = dynreloc_value - sym_sec->output_section->vma;
12611               }
12612             else
12613               {
12614                 dynindx = 0;
12615                 addr = 0;
12616               }
12617
12618             if (bfd_link_pic(info) && dynindx == 0)
12619               abort();
12620
12621             /* This case cannot occur since funcdesc is allocated by
12622                the dynamic loader so we cannot resolve the relocation.  */
12623             if (h->dynindx != -1)
12624               abort();
12625
12626             /* Resolve relocation.  */
12627             bfd_put_32(output_bfd, (offset + sgot->output_offset),
12628                        contents + rel->r_offset);
12629             /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12630             arm_elf_fill_funcdesc(output_bfd, info,
12631                                   &eh->fdpic_cnts.funcdesc_offset,
12632                                   dynindx, offset, addr, dynreloc_value, seg);
12633           }
12634       }
12635       *unresolved_reloc_p = FALSE;
12636       return bfd_reloc_ok;
12637
12638     case R_ARM_GOTFUNCDESC:
12639       {
12640         if (h != NULL)
12641           {
12642             Elf_Internal_Rela outrel;
12643
12644             /* Resolve relocation.  */
12645             bfd_put_32(output_bfd, ((eh->fdpic_cnts.gotfuncdesc_offset & ~1)
12646                                     + sgot->output_offset),
12647                        contents + rel->r_offset);
12648             /* Add funcdesc and associated R_ARM_FUNCDESC_VALUE.  */
12649             if(h->dynindx == -1)
12650               {
12651                 int dynindx;
12652                 int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12653                 bfd_vma addr;
12654                 bfd_vma seg = -1;
12655
12656                 /* For static binaries sym_sec can be null.  */
12657                 if (sym_sec)
12658                   {
12659                     dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12660                     addr = dynreloc_value - sym_sec->output_section->vma;
12661                   }
12662                 else
12663                   {
12664                     dynindx = 0;
12665                     addr = 0;
12666                   }
12667
12668                 /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12669                 arm_elf_fill_funcdesc(output_bfd, info,
12670                                       &eh->fdpic_cnts.funcdesc_offset,
12671                                       dynindx, offset, addr, dynreloc_value, seg);
12672               }
12673
12674             /* Add a dynamic relocation on GOT entry if not already done.  */
12675             if ((eh->fdpic_cnts.gotfuncdesc_offset & 1) == 0)
12676               {
12677                 if (h->dynindx == -1)
12678                   {
12679                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12680                     if (h->root.type == bfd_link_hash_undefweak)
12681                       bfd_put_32(output_bfd, 0, sgot->contents
12682                                  + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12683                     else
12684                       bfd_put_32(output_bfd, sgot->output_section->vma
12685                                  + sgot->output_offset
12686                                  + (eh->fdpic_cnts.funcdesc_offset & ~1),
12687                                  sgot->contents
12688                                  + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12689                   }
12690                 else
12691                   {
12692                     outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12693                   }
12694                 outrel.r_offset = sgot->output_section->vma
12695                   + sgot->output_offset
12696                   + (eh->fdpic_cnts.gotfuncdesc_offset & ~1);
12697                 outrel.r_addend = 0;
12698                 if (h->dynindx == -1 && !bfd_link_pic(info))
12699                   if (h->root.type == bfd_link_hash_undefweak)
12700                     arm_elf_add_rofixup(output_bfd, globals->srofixup, -1);
12701                   else
12702                     arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12703                 else
12704                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12705                 eh->fdpic_cnts.gotfuncdesc_offset |= 1;
12706               }
12707           }
12708         else
12709           {
12710             /* Such relocation on static function should not have been
12711                emitted by the compiler.  */
12712             abort();
12713           }
12714       }
12715       *unresolved_reloc_p = FALSE;
12716       return bfd_reloc_ok;
12717
12718     case R_ARM_FUNCDESC:
12719       {
12720         if (h == NULL)
12721           {
12722             struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts(input_bfd);
12723             Elf_Internal_Rela outrel;
12724             int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12725             int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12726             bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12727             bfd_vma seg = -1;
12728
12729             if (bfd_link_pic(info) && dynindx == 0)
12730               abort();
12731
12732             /* Replace static FUNCDESC relocation with a
12733                R_ARM_RELATIVE dynamic relocation or with a rofixup for
12734                executable.  */
12735             outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12736             outrel.r_offset = input_section->output_section->vma
12737               + input_section->output_offset + rel->r_offset;
12738             outrel.r_addend = 0;
12739             if (bfd_link_pic(info))
12740               elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12741             else
12742               arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12743
12744             bfd_put_32 (input_bfd, sgot->output_section->vma
12745                         + sgot->output_offset + offset, hit_data);
12746
12747             /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12748             arm_elf_fill_funcdesc(output_bfd, info,
12749                                   &local_fdpic_cnts[r_symndx].funcdesc_offset,
12750                                   dynindx, offset, addr, dynreloc_value, seg);
12751           }
12752         else
12753           {
12754             if (h->dynindx == -1)
12755               {
12756                 int dynindx;
12757                 int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12758                 bfd_vma addr;
12759                 bfd_vma seg = -1;
12760                 Elf_Internal_Rela outrel;
12761
12762                 /* For static binaries sym_sec can be null.  */
12763                 if (sym_sec)
12764                   {
12765                     dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12766                     addr = dynreloc_value - sym_sec->output_section->vma;
12767                   }
12768                 else
12769                   {
12770                     dynindx = 0;
12771                     addr = 0;
12772                   }
12773
12774                 if (bfd_link_pic(info) && dynindx == 0)
12775                   abort();
12776
12777                 /* Replace static FUNCDESC relocation with a
12778                    R_ARM_RELATIVE dynamic relocation.  */
12779                 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12780                 outrel.r_offset = input_section->output_section->vma
12781                   + input_section->output_offset + rel->r_offset;
12782                 outrel.r_addend = 0;
12783                 if (bfd_link_pic(info))
12784                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12785                 else
12786                   arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12787
12788                 bfd_put_32 (input_bfd, sgot->output_section->vma
12789                             + sgot->output_offset + offset, hit_data);
12790
12791                 /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12792                 arm_elf_fill_funcdesc(output_bfd, info,
12793                                       &eh->fdpic_cnts.funcdesc_offset,
12794                                       dynindx, offset, addr, dynreloc_value, seg);
12795               }
12796             else
12797               {
12798                 Elf_Internal_Rela outrel;
12799
12800                 /* Add a dynamic relocation.  */
12801                 outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12802                 outrel.r_offset = input_section->output_section->vma
12803                   + input_section->output_offset + rel->r_offset;
12804                 outrel.r_addend = 0;
12805                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12806               }
12807           }
12808       }
12809       *unresolved_reloc_p = FALSE;
12810       return bfd_reloc_ok;
12811
12812     default:
12813       return bfd_reloc_notsupported;
12814     }
12815 }
12816
12817 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
12818 static void
12819 arm_add_to_rel (bfd *              abfd,
12820                 bfd_byte *         address,
12821                 reloc_howto_type * howto,
12822                 bfd_signed_vma     increment)
12823 {
12824   bfd_signed_vma addend;
12825
12826   if (howto->type == R_ARM_THM_CALL
12827       || howto->type == R_ARM_THM_JUMP24)
12828     {
12829       int upper_insn, lower_insn;
12830       int upper, lower;
12831
12832       upper_insn = bfd_get_16 (abfd, address);
12833       lower_insn = bfd_get_16 (abfd, address + 2);
12834       upper = upper_insn & 0x7ff;
12835       lower = lower_insn & 0x7ff;
12836
12837       addend = (upper << 12) | (lower << 1);
12838       addend += increment;
12839       addend >>= 1;
12840
12841       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
12842       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
12843
12844       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
12845       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
12846     }
12847   else
12848     {
12849       bfd_vma        contents;
12850
12851       contents = bfd_get_32 (abfd, address);
12852
12853       /* Get the (signed) value from the instruction.  */
12854       addend = contents & howto->src_mask;
12855       if (addend & ((howto->src_mask + 1) >> 1))
12856         {
12857           bfd_signed_vma mask;
12858
12859           mask = -1;
12860           mask &= ~ howto->src_mask;
12861           addend |= mask;
12862         }
12863
12864       /* Add in the increment, (which is a byte value).  */
12865       switch (howto->type)
12866         {
12867         default:
12868           addend += increment;
12869           break;
12870
12871         case R_ARM_PC24:
12872         case R_ARM_PLT32:
12873         case R_ARM_CALL:
12874         case R_ARM_JUMP24:
12875           addend <<= howto->size;
12876           addend += increment;
12877
12878           /* Should we check for overflow here ?  */
12879
12880           /* Drop any undesired bits.  */
12881           addend >>= howto->rightshift;
12882           break;
12883         }
12884
12885       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
12886
12887       bfd_put_32 (abfd, contents, address);
12888     }
12889 }
12890
12891 #define IS_ARM_TLS_RELOC(R_TYPE)        \
12892   ((R_TYPE) == R_ARM_TLS_GD32           \
12893    || (R_TYPE) == R_ARM_TLS_GD32_FDPIC  \
12894    || (R_TYPE) == R_ARM_TLS_LDO32       \
12895    || (R_TYPE) == R_ARM_TLS_LDM32       \
12896    || (R_TYPE) == R_ARM_TLS_LDM32_FDPIC \
12897    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
12898    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
12899    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
12900    || (R_TYPE) == R_ARM_TLS_LE32        \
12901    || (R_TYPE) == R_ARM_TLS_IE32        \
12902    || (R_TYPE) == R_ARM_TLS_IE32_FDPIC  \
12903    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
12904
12905 /* Specific set of relocations for the gnu tls dialect.  */
12906 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)    \
12907   ((R_TYPE) == R_ARM_TLS_GOTDESC        \
12908    || (R_TYPE) == R_ARM_TLS_CALL        \
12909    || (R_TYPE) == R_ARM_THM_TLS_CALL    \
12910    || (R_TYPE) == R_ARM_TLS_DESCSEQ     \
12911    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
12912
12913 /* Relocate an ARM ELF section.  */
12914
12915 static bfd_boolean
12916 elf32_arm_relocate_section (bfd *                  output_bfd,
12917                             struct bfd_link_info * info,
12918                             bfd *                  input_bfd,
12919                             asection *             input_section,
12920                             bfd_byte *             contents,
12921                             Elf_Internal_Rela *    relocs,
12922                             Elf_Internal_Sym *     local_syms,
12923                             asection **            local_sections)
12924 {
12925   Elf_Internal_Shdr *symtab_hdr;
12926   struct elf_link_hash_entry **sym_hashes;
12927   Elf_Internal_Rela *rel;
12928   Elf_Internal_Rela *relend;
12929   const char *name;
12930   struct elf32_arm_link_hash_table * globals;
12931
12932   globals = elf32_arm_hash_table (info);
12933   if (globals == NULL)
12934     return FALSE;
12935
12936   symtab_hdr = & elf_symtab_hdr (input_bfd);
12937   sym_hashes = elf_sym_hashes (input_bfd);
12938
12939   rel = relocs;
12940   relend = relocs + input_section->reloc_count;
12941   for (; rel < relend; rel++)
12942     {
12943       int                          r_type;
12944       reloc_howto_type *           howto;
12945       unsigned long                r_symndx;
12946       Elf_Internal_Sym *           sym;
12947       asection *                   sec;
12948       struct elf_link_hash_entry * h;
12949       bfd_vma                      relocation;
12950       bfd_reloc_status_type        r;
12951       arelent                      bfd_reloc;
12952       char                         sym_type;
12953       bfd_boolean                  unresolved_reloc = FALSE;
12954       char *error_message = NULL;
12955
12956       r_symndx = ELF32_R_SYM (rel->r_info);
12957       r_type   = ELF32_R_TYPE (rel->r_info);
12958       r_type   = arm_real_reloc_type (globals, r_type);
12959
12960       if (   r_type == R_ARM_GNU_VTENTRY
12961           || r_type == R_ARM_GNU_VTINHERIT)
12962         continue;
12963
12964       howto = bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
12965
12966       if (howto == NULL)
12967         return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
12968
12969       h = NULL;
12970       sym = NULL;
12971       sec = NULL;
12972
12973       if (r_symndx < symtab_hdr->sh_info)
12974         {
12975           sym = local_syms + r_symndx;
12976           sym_type = ELF32_ST_TYPE (sym->st_info);
12977           sec = local_sections[r_symndx];
12978
12979           /* An object file might have a reference to a local
12980              undefined symbol.  This is a daft object file, but we
12981              should at least do something about it.  V4BX & NONE
12982              relocations do not use the symbol and are explicitly
12983              allowed to use the undefined symbol, so allow those.
12984              Likewise for relocations against STN_UNDEF.  */
12985           if (r_type != R_ARM_V4BX
12986               && r_type != R_ARM_NONE
12987               && r_symndx != STN_UNDEF
12988               && bfd_is_und_section (sec)
12989               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
12990             (*info->callbacks->undefined_symbol)
12991               (info, bfd_elf_string_from_elf_section
12992                (input_bfd, symtab_hdr->sh_link, sym->st_name),
12993                input_bfd, input_section,
12994                rel->r_offset, TRUE);
12995
12996           if (globals->use_rel)
12997             {
12998               relocation = (sec->output_section->vma
12999                             + sec->output_offset
13000                             + sym->st_value);
13001               if (!bfd_link_relocatable (info)
13002                   && (sec->flags & SEC_MERGE)
13003                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13004                 {
13005                   asection *msec;
13006                   bfd_vma addend, value;
13007
13008                   switch (r_type)
13009                     {
13010                     case R_ARM_MOVW_ABS_NC:
13011                     case R_ARM_MOVT_ABS:
13012                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13013                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
13014                       addend = (addend ^ 0x8000) - 0x8000;
13015                       break;
13016
13017                     case R_ARM_THM_MOVW_ABS_NC:
13018                     case R_ARM_THM_MOVT_ABS:
13019                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
13020                               << 16;
13021                       value |= bfd_get_16 (input_bfd,
13022                                            contents + rel->r_offset + 2);
13023                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
13024                                | ((value & 0x04000000) >> 15);
13025                       addend = (addend ^ 0x8000) - 0x8000;
13026                       break;
13027
13028                     default:
13029                       if (howto->rightshift
13030                           || (howto->src_mask & (howto->src_mask + 1)))
13031                         {
13032                           _bfd_error_handler
13033                             /* xgettext:c-format */
13034                             (_("%pB(%pA+%#" PRIx64 "): "
13035                                "%s relocation against SEC_MERGE section"),
13036                              input_bfd, input_section,
13037                              (uint64_t) rel->r_offset, howto->name);
13038                           return FALSE;
13039                         }
13040
13041                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13042
13043                       /* Get the (signed) value from the instruction.  */
13044                       addend = value & howto->src_mask;
13045                       if (addend & ((howto->src_mask + 1) >> 1))
13046                         {
13047                           bfd_signed_vma mask;
13048
13049                           mask = -1;
13050                           mask &= ~ howto->src_mask;
13051                           addend |= mask;
13052                         }
13053                       break;
13054                     }
13055
13056                   msec = sec;
13057                   addend =
13058                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
13059                     - relocation;
13060                   addend += msec->output_section->vma + msec->output_offset;
13061
13062                   /* Cases here must match those in the preceding
13063                      switch statement.  */
13064                   switch (r_type)
13065                     {
13066                     case R_ARM_MOVW_ABS_NC:
13067                     case R_ARM_MOVT_ABS:
13068                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
13069                               | (addend & 0xfff);
13070                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13071                       break;
13072
13073                     case R_ARM_THM_MOVW_ABS_NC:
13074                     case R_ARM_THM_MOVT_ABS:
13075                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
13076                               | (addend & 0xff) | ((addend & 0x0800) << 15);
13077                       bfd_put_16 (input_bfd, value >> 16,
13078                                   contents + rel->r_offset);
13079                       bfd_put_16 (input_bfd, value,
13080                                   contents + rel->r_offset + 2);
13081                       break;
13082
13083                     default:
13084                       value = (value & ~ howto->dst_mask)
13085                               | (addend & howto->dst_mask);
13086                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13087                       break;
13088                     }
13089                 }
13090             }
13091           else
13092             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13093         }
13094       else
13095         {
13096           bfd_boolean warned, ignored;
13097
13098           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13099                                    r_symndx, symtab_hdr, sym_hashes,
13100                                    h, sec, relocation,
13101                                    unresolved_reloc, warned, ignored);
13102
13103           sym_type = h->type;
13104         }
13105
13106       if (sec != NULL && discarded_section (sec))
13107         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13108                                          rel, 1, relend, howto, 0, contents);
13109
13110       if (bfd_link_relocatable (info))
13111         {
13112           /* This is a relocatable link.  We don't have to change
13113              anything, unless the reloc is against a section symbol,
13114              in which case we have to adjust according to where the
13115              section symbol winds up in the output section.  */
13116           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13117             {
13118               if (globals->use_rel)
13119                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
13120                                 howto, (bfd_signed_vma) sec->output_offset);
13121               else
13122                 rel->r_addend += sec->output_offset;
13123             }
13124           continue;
13125         }
13126
13127       if (h != NULL)
13128         name = h->root.root.string;
13129       else
13130         {
13131           name = (bfd_elf_string_from_elf_section
13132                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
13133           if (name == NULL || *name == '\0')
13134             name = bfd_section_name (input_bfd, sec);
13135         }
13136
13137       if (r_symndx != STN_UNDEF
13138           && r_type != R_ARM_NONE
13139           && (h == NULL
13140               || h->root.type == bfd_link_hash_defined
13141               || h->root.type == bfd_link_hash_defweak)
13142           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
13143         {
13144           _bfd_error_handler
13145             ((sym_type == STT_TLS
13146               /* xgettext:c-format */
13147               ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
13148               /* xgettext:c-format */
13149               : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
13150              input_bfd,
13151              input_section,
13152              (uint64_t) rel->r_offset,
13153              howto->name,
13154              name);
13155         }
13156
13157       /* We call elf32_arm_final_link_relocate unless we're completely
13158          done, i.e., the relaxation produced the final output we want,
13159          and we won't let anybody mess with it. Also, we have to do
13160          addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
13161          both in relaxed and non-relaxed cases.  */
13162       if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
13163           || (IS_ARM_TLS_GNU_RELOC (r_type)
13164               && !((h ? elf32_arm_hash_entry (h)->tls_type :
13165                     elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
13166                    & GOT_TLS_GDESC)))
13167         {
13168           r = elf32_arm_tls_relax (globals, input_bfd, input_section,
13169                                    contents, rel, h == NULL);
13170           /* This may have been marked unresolved because it came from
13171              a shared library.  But we've just dealt with that.  */
13172           unresolved_reloc = 0;
13173         }
13174       else
13175         r = bfd_reloc_continue;
13176
13177       if (r == bfd_reloc_continue)
13178         {
13179           unsigned char branch_type =
13180             h ? ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
13181               : ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
13182
13183           r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
13184                                              input_section, contents, rel,
13185                                              relocation, info, sec, name,
13186                                              sym_type, branch_type, h,
13187                                              &unresolved_reloc,
13188                                              &error_message);
13189         }
13190
13191       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13192          because such sections are not SEC_ALLOC and thus ld.so will
13193          not process them.  */
13194       if (unresolved_reloc
13195           && !((input_section->flags & SEC_DEBUGGING) != 0
13196                && h->def_dynamic)
13197           && _bfd_elf_section_offset (output_bfd, info, input_section,
13198                                       rel->r_offset) != (bfd_vma) -1)
13199         {
13200           _bfd_error_handler
13201             /* xgettext:c-format */
13202             (_("%pB(%pA+%#" PRIx64 "): "
13203                "unresolvable %s relocation against symbol `%s'"),
13204              input_bfd,
13205              input_section,
13206              (uint64_t) rel->r_offset,
13207              howto->name,
13208              h->root.root.string);
13209           return FALSE;
13210         }
13211
13212       if (r != bfd_reloc_ok)
13213         {
13214           switch (r)
13215             {
13216             case bfd_reloc_overflow:
13217               /* If the overflowing reloc was to an undefined symbol,
13218                  we have already printed one error message and there
13219                  is no point complaining again.  */
13220               if (!h || h->root.type != bfd_link_hash_undefined)
13221                 (*info->callbacks->reloc_overflow)
13222                   (info, (h ? &h->root : NULL), name, howto->name,
13223                    (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
13224               break;
13225
13226             case bfd_reloc_undefined:
13227               (*info->callbacks->undefined_symbol)
13228                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
13229               break;
13230
13231             case bfd_reloc_outofrange:
13232               error_message = _("out of range");
13233               goto common_error;
13234
13235             case bfd_reloc_notsupported:
13236               error_message = _("unsupported relocation");
13237               goto common_error;
13238
13239             case bfd_reloc_dangerous:
13240               /* error_message should already be set.  */
13241               goto common_error;
13242
13243             default:
13244               error_message = _("unknown error");
13245               /* Fall through.  */
13246
13247             common_error:
13248               BFD_ASSERT (error_message != NULL);
13249               (*info->callbacks->reloc_dangerous)
13250                 (info, error_message, input_bfd, input_section, rel->r_offset);
13251               break;
13252             }
13253         }
13254     }
13255
13256   return TRUE;
13257 }
13258
13259 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
13260    adds the edit to the start of the list.  (The list must be built in order of
13261    ascending TINDEX: the function's callers are primarily responsible for
13262    maintaining that condition).  */
13263
13264 static void
13265 add_unwind_table_edit (arm_unwind_table_edit **head,
13266                        arm_unwind_table_edit **tail,
13267                        arm_unwind_edit_type type,
13268                        asection *linked_section,
13269                        unsigned int tindex)
13270 {
13271   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
13272       xmalloc (sizeof (arm_unwind_table_edit));
13273
13274   new_edit->type = type;
13275   new_edit->linked_section = linked_section;
13276   new_edit->index = tindex;
13277
13278   if (tindex > 0)
13279     {
13280       new_edit->next = NULL;
13281
13282       if (*tail)
13283         (*tail)->next = new_edit;
13284
13285       (*tail) = new_edit;
13286
13287       if (!*head)
13288         (*head) = new_edit;
13289     }
13290   else
13291     {
13292       new_edit->next = *head;
13293
13294       if (!*tail)
13295         *tail = new_edit;
13296
13297       *head = new_edit;
13298     }
13299 }
13300
13301 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
13302
13303 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
13304 static void
13305 adjust_exidx_size(asection *exidx_sec, int adjust)
13306 {
13307   asection *out_sec;
13308
13309   if (!exidx_sec->rawsize)
13310     exidx_sec->rawsize = exidx_sec->size;
13311
13312   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
13313   out_sec = exidx_sec->output_section;
13314   /* Adjust size of output section.  */
13315   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
13316 }
13317
13318 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
13319 static void
13320 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
13321 {
13322   struct _arm_elf_section_data *exidx_arm_data;
13323
13324   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13325   add_unwind_table_edit (
13326     &exidx_arm_data->u.exidx.unwind_edit_list,
13327     &exidx_arm_data->u.exidx.unwind_edit_tail,
13328     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
13329
13330   exidx_arm_data->additional_reloc_count++;
13331
13332   adjust_exidx_size(exidx_sec, 8);
13333 }
13334
13335 /* Scan .ARM.exidx tables, and create a list describing edits which should be
13336    made to those tables, such that:
13337
13338      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
13339      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
13340         codes which have been inlined into the index).
13341
13342    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
13343
13344    The edits are applied when the tables are written
13345    (in elf32_arm_write_section).  */
13346
13347 bfd_boolean
13348 elf32_arm_fix_exidx_coverage (asection **text_section_order,
13349                               unsigned int num_text_sections,
13350                               struct bfd_link_info *info,
13351                               bfd_boolean merge_exidx_entries)
13352 {
13353   bfd *inp;
13354   unsigned int last_second_word = 0, i;
13355   asection *last_exidx_sec = NULL;
13356   asection *last_text_sec = NULL;
13357   int last_unwind_type = -1;
13358
13359   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
13360      text sections.  */
13361   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
13362     {
13363       asection *sec;
13364
13365       for (sec = inp->sections; sec != NULL; sec = sec->next)
13366         {
13367           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
13368           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
13369
13370           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
13371             continue;
13372
13373           if (elf_sec->linked_to)
13374             {
13375               Elf_Internal_Shdr *linked_hdr
13376                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
13377               struct _arm_elf_section_data *linked_sec_arm_data
13378                 = get_arm_elf_section_data (linked_hdr->bfd_section);
13379
13380               if (linked_sec_arm_data == NULL)
13381                 continue;
13382
13383               /* Link this .ARM.exidx section back from the text section it
13384                  describes.  */
13385               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
13386             }
13387         }
13388     }
13389
13390   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
13391      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
13392      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
13393
13394   for (i = 0; i < num_text_sections; i++)
13395     {
13396       asection *sec = text_section_order[i];
13397       asection *exidx_sec;
13398       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
13399       struct _arm_elf_section_data *exidx_arm_data;
13400       bfd_byte *contents = NULL;
13401       int deleted_exidx_bytes = 0;
13402       bfd_vma j;
13403       arm_unwind_table_edit *unwind_edit_head = NULL;
13404       arm_unwind_table_edit *unwind_edit_tail = NULL;
13405       Elf_Internal_Shdr *hdr;
13406       bfd *ibfd;
13407
13408       if (arm_data == NULL)
13409         continue;
13410
13411       exidx_sec = arm_data->u.text.arm_exidx_sec;
13412       if (exidx_sec == NULL)
13413         {
13414           /* Section has no unwind data.  */
13415           if (last_unwind_type == 0 || !last_exidx_sec)
13416             continue;
13417
13418           /* Ignore zero sized sections.  */
13419           if (sec->size == 0)
13420             continue;
13421
13422           insert_cantunwind_after(last_text_sec, last_exidx_sec);
13423           last_unwind_type = 0;
13424           continue;
13425         }
13426
13427       /* Skip /DISCARD/ sections.  */
13428       if (bfd_is_abs_section (exidx_sec->output_section))
13429         continue;
13430
13431       hdr = &elf_section_data (exidx_sec)->this_hdr;
13432       if (hdr->sh_type != SHT_ARM_EXIDX)
13433         continue;
13434
13435       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13436       if (exidx_arm_data == NULL)
13437         continue;
13438
13439       ibfd = exidx_sec->owner;
13440
13441       if (hdr->contents != NULL)
13442         contents = hdr->contents;
13443       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
13444         /* An error?  */
13445         continue;
13446
13447       if (last_unwind_type > 0)
13448         {
13449           unsigned int first_word = bfd_get_32 (ibfd, contents);
13450           /* Add cantunwind if first unwind item does not match section
13451              start.  */
13452           if (first_word != sec->vma)
13453             {
13454               insert_cantunwind_after (last_text_sec, last_exidx_sec);
13455               last_unwind_type = 0;
13456             }
13457         }
13458
13459       for (j = 0; j < hdr->sh_size; j += 8)
13460         {
13461           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
13462           int unwind_type;
13463           int elide = 0;
13464
13465           /* An EXIDX_CANTUNWIND entry.  */
13466           if (second_word == 1)
13467             {
13468               if (last_unwind_type == 0)
13469                 elide = 1;
13470               unwind_type = 0;
13471             }
13472           /* Inlined unwinding data.  Merge if equal to previous.  */
13473           else if ((second_word & 0x80000000) != 0)
13474             {
13475               if (merge_exidx_entries
13476                    && last_second_word == second_word && last_unwind_type == 1)
13477                 elide = 1;
13478               unwind_type = 1;
13479               last_second_word = second_word;
13480             }
13481           /* Normal table entry.  In theory we could merge these too,
13482              but duplicate entries are likely to be much less common.  */
13483           else
13484             unwind_type = 2;
13485
13486           if (elide && !bfd_link_relocatable (info))
13487             {
13488               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
13489                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
13490
13491               deleted_exidx_bytes += 8;
13492             }
13493
13494           last_unwind_type = unwind_type;
13495         }
13496
13497       /* Free contents if we allocated it ourselves.  */
13498       if (contents != hdr->contents)
13499         free (contents);
13500
13501       /* Record edits to be applied later (in elf32_arm_write_section).  */
13502       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
13503       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
13504
13505       if (deleted_exidx_bytes > 0)
13506         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
13507
13508       last_exidx_sec = exidx_sec;
13509       last_text_sec = sec;
13510     }
13511
13512   /* Add terminating CANTUNWIND entry.  */
13513   if (!bfd_link_relocatable (info) && last_exidx_sec
13514       && last_unwind_type != 0)
13515     insert_cantunwind_after(last_text_sec, last_exidx_sec);
13516
13517   return TRUE;
13518 }
13519
13520 static bfd_boolean
13521 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
13522                                bfd *ibfd, const char *name)
13523 {
13524   asection *sec, *osec;
13525
13526   sec = bfd_get_linker_section (ibfd, name);
13527   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
13528     return TRUE;
13529
13530   osec = sec->output_section;
13531   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
13532     return TRUE;
13533
13534   if (! bfd_set_section_contents (obfd, osec, sec->contents,
13535                                   sec->output_offset, sec->size))
13536     return FALSE;
13537
13538   return TRUE;
13539 }
13540
13541 static bfd_boolean
13542 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
13543 {
13544   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
13545   asection *sec, *osec;
13546
13547   if (globals == NULL)
13548     return FALSE;
13549
13550   /* Invoke the regular ELF backend linker to do all the work.  */
13551   if (!bfd_elf_final_link (abfd, info))
13552     return FALSE;
13553
13554   /* Process stub sections (eg BE8 encoding, ...).  */
13555   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
13556   unsigned int i;
13557   for (i=0; i<htab->top_id; i++)
13558     {
13559       sec = htab->stub_group[i].stub_sec;
13560       /* Only process it once, in its link_sec slot.  */
13561       if (sec && i == htab->stub_group[i].link_sec->id)
13562         {
13563           osec = sec->output_section;
13564           elf32_arm_write_section (abfd, info, sec, sec->contents);
13565           if (! bfd_set_section_contents (abfd, osec, sec->contents,
13566                                           sec->output_offset, sec->size))
13567             return FALSE;
13568         }
13569     }
13570
13571   /* Write out any glue sections now that we have created all the
13572      stubs.  */
13573   if (globals->bfd_of_glue_owner != NULL)
13574     {
13575       if (! elf32_arm_output_glue_section (info, abfd,
13576                                            globals->bfd_of_glue_owner,
13577                                            ARM2THUMB_GLUE_SECTION_NAME))
13578         return FALSE;
13579
13580       if (! elf32_arm_output_glue_section (info, abfd,
13581                                            globals->bfd_of_glue_owner,
13582                                            THUMB2ARM_GLUE_SECTION_NAME))
13583         return FALSE;
13584
13585       if (! elf32_arm_output_glue_section (info, abfd,
13586                                            globals->bfd_of_glue_owner,
13587                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
13588         return FALSE;
13589
13590       if (! elf32_arm_output_glue_section (info, abfd,
13591                                            globals->bfd_of_glue_owner,
13592                                            STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
13593         return FALSE;
13594
13595       if (! elf32_arm_output_glue_section (info, abfd,
13596                                            globals->bfd_of_glue_owner,
13597                                            ARM_BX_GLUE_SECTION_NAME))
13598         return FALSE;
13599     }
13600
13601   return TRUE;
13602 }
13603
13604 /* Return a best guess for the machine number based on the attributes.  */
13605
13606 static unsigned int
13607 bfd_arm_get_mach_from_attributes (bfd * abfd)
13608 {
13609   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
13610
13611   switch (arch)
13612     {
13613     case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
13614     case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
13615     case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
13616
13617     case TAG_CPU_ARCH_V5TE:
13618       {
13619         char * name;
13620
13621         BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
13622         name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
13623
13624         if (name)
13625           {
13626             if (strcmp (name, "IWMMXT2") == 0)
13627               return bfd_mach_arm_iWMMXt2;
13628
13629             if (strcmp (name, "IWMMXT") == 0)
13630               return bfd_mach_arm_iWMMXt;
13631
13632             if (strcmp (name, "XSCALE") == 0)
13633               {
13634                 int wmmx;
13635
13636                 BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
13637                 wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
13638                 switch (wmmx)
13639                   {
13640                   case 1: return bfd_mach_arm_iWMMXt;
13641                   case 2: return bfd_mach_arm_iWMMXt2;
13642                   default: return bfd_mach_arm_XScale;
13643                   }
13644               }
13645           }
13646
13647         return bfd_mach_arm_5TE;
13648       }
13649
13650     default:
13651       return bfd_mach_arm_unknown;
13652     }
13653 }
13654
13655 /* Set the right machine number.  */
13656
13657 static bfd_boolean
13658 elf32_arm_object_p (bfd *abfd)
13659 {
13660   unsigned int mach;
13661
13662   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
13663
13664   if (mach == bfd_mach_arm_unknown)
13665     {
13666       if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
13667         mach = bfd_mach_arm_ep9312;
13668       else
13669         mach = bfd_arm_get_mach_from_attributes (abfd);
13670     }
13671
13672   bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
13673   return TRUE;
13674 }
13675
13676 /* Function to keep ARM specific flags in the ELF header.  */
13677
13678 static bfd_boolean
13679 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
13680 {
13681   if (elf_flags_init (abfd)
13682       && elf_elfheader (abfd)->e_flags != flags)
13683     {
13684       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
13685         {
13686           if (flags & EF_ARM_INTERWORK)
13687             _bfd_error_handler
13688               (_("warning: not setting interworking flag of %pB since it has already been specified as non-interworking"),
13689                abfd);
13690           else
13691             _bfd_error_handler
13692               (_("warning: clearing the interworking flag of %pB due to outside request"),
13693                abfd);
13694         }
13695     }
13696   else
13697     {
13698       elf_elfheader (abfd)->e_flags = flags;
13699       elf_flags_init (abfd) = TRUE;
13700     }
13701
13702   return TRUE;
13703 }
13704
13705 /* Copy backend specific data from one object module to another.  */
13706
13707 static bfd_boolean
13708 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
13709 {
13710   flagword in_flags;
13711   flagword out_flags;
13712
13713   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
13714     return TRUE;
13715
13716   in_flags  = elf_elfheader (ibfd)->e_flags;
13717   out_flags = elf_elfheader (obfd)->e_flags;
13718
13719   if (elf_flags_init (obfd)
13720       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
13721       && in_flags != out_flags)
13722     {
13723       /* Cannot mix APCS26 and APCS32 code.  */
13724       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
13725         return FALSE;
13726
13727       /* Cannot mix float APCS and non-float APCS code.  */
13728       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
13729         return FALSE;
13730
13731       /* If the src and dest have different interworking flags
13732          then turn off the interworking bit.  */
13733       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
13734         {
13735           if (out_flags & EF_ARM_INTERWORK)
13736             _bfd_error_handler
13737               (_("warning: clearing the interworking flag of %pB because non-interworking code in %pB has been linked with it"),
13738                obfd, ibfd);
13739
13740           in_flags &= ~EF_ARM_INTERWORK;
13741         }
13742
13743       /* Likewise for PIC, though don't warn for this case.  */
13744       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
13745         in_flags &= ~EF_ARM_PIC;
13746     }
13747
13748   elf_elfheader (obfd)->e_flags = in_flags;
13749   elf_flags_init (obfd) = TRUE;
13750
13751   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
13752 }
13753
13754 /* Values for Tag_ABI_PCS_R9_use.  */
13755 enum
13756 {
13757   AEABI_R9_V6,
13758   AEABI_R9_SB,
13759   AEABI_R9_TLS,
13760   AEABI_R9_unused
13761 };
13762
13763 /* Values for Tag_ABI_PCS_RW_data.  */
13764 enum
13765 {
13766   AEABI_PCS_RW_data_absolute,
13767   AEABI_PCS_RW_data_PCrel,
13768   AEABI_PCS_RW_data_SBrel,
13769   AEABI_PCS_RW_data_unused
13770 };
13771
13772 /* Values for Tag_ABI_enum_size.  */
13773 enum
13774 {
13775   AEABI_enum_unused,
13776   AEABI_enum_short,
13777   AEABI_enum_wide,
13778   AEABI_enum_forced_wide
13779 };
13780
13781 /* Determine whether an object attribute tag takes an integer, a
13782    string or both.  */
13783
13784 static int
13785 elf32_arm_obj_attrs_arg_type (int tag)
13786 {
13787   if (tag == Tag_compatibility)
13788     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
13789   else if (tag == Tag_nodefaults)
13790     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
13791   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
13792     return ATTR_TYPE_FLAG_STR_VAL;
13793   else if (tag < 32)
13794     return ATTR_TYPE_FLAG_INT_VAL;
13795   else
13796     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
13797 }
13798
13799 /* The ABI defines that Tag_conformance should be emitted first, and that
13800    Tag_nodefaults should be second (if either is defined).  This sets those
13801    two positions, and bumps up the position of all the remaining tags to
13802    compensate.  */
13803 static int
13804 elf32_arm_obj_attrs_order (int num)
13805 {
13806   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
13807     return Tag_conformance;
13808   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
13809     return Tag_nodefaults;
13810   if ((num - 2) < Tag_nodefaults)
13811     return num - 2;
13812   if ((num - 1) < Tag_conformance)
13813     return num - 1;
13814   return num;
13815 }
13816
13817 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
13818 static bfd_boolean
13819 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
13820 {
13821   if ((tag & 127) < 64)
13822     {
13823       _bfd_error_handler
13824         (_("%pB: unknown mandatory EABI object attribute %d"),
13825          abfd, tag);
13826       bfd_set_error (bfd_error_bad_value);
13827       return FALSE;
13828     }
13829   else
13830     {
13831       _bfd_error_handler
13832         (_("warning: %pB: unknown EABI object attribute %d"),
13833          abfd, tag);
13834       return TRUE;
13835     }
13836 }
13837
13838 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
13839    Returns -1 if no architecture could be read.  */
13840
13841 static int
13842 get_secondary_compatible_arch (bfd *abfd)
13843 {
13844   obj_attribute *attr =
13845     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13846
13847   /* Note: the tag and its argument below are uleb128 values, though
13848      currently-defined values fit in one byte for each.  */
13849   if (attr->s
13850       && attr->s[0] == Tag_CPU_arch
13851       && (attr->s[1] & 128) != 128
13852       && attr->s[2] == 0)
13853    return attr->s[1];
13854
13855   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
13856   return -1;
13857 }
13858
13859 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
13860    The tag is removed if ARCH is -1.  */
13861
13862 static void
13863 set_secondary_compatible_arch (bfd *abfd, int arch)
13864 {
13865   obj_attribute *attr =
13866     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13867
13868   if (arch == -1)
13869     {
13870       attr->s = NULL;
13871       return;
13872     }
13873
13874   /* Note: the tag and its argument below are uleb128 values, though
13875      currently-defined values fit in one byte for each.  */
13876   if (!attr->s)
13877     attr->s = (char *) bfd_alloc (abfd, 3);
13878   attr->s[0] = Tag_CPU_arch;
13879   attr->s[1] = arch;
13880   attr->s[2] = '\0';
13881 }
13882
13883 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
13884    into account.  */
13885
13886 static int
13887 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
13888                       int newtag, int secondary_compat)
13889 {
13890 #define T(X) TAG_CPU_ARCH_##X
13891   int tagl, tagh, result;
13892   const int v6t2[] =
13893     {
13894       T(V6T2),   /* PRE_V4.  */
13895       T(V6T2),   /* V4.  */
13896       T(V6T2),   /* V4T.  */
13897       T(V6T2),   /* V5T.  */
13898       T(V6T2),   /* V5TE.  */
13899       T(V6T2),   /* V5TEJ.  */
13900       T(V6T2),   /* V6.  */
13901       T(V7),     /* V6KZ.  */
13902       T(V6T2)    /* V6T2.  */
13903     };
13904   const int v6k[] =
13905     {
13906       T(V6K),    /* PRE_V4.  */
13907       T(V6K),    /* V4.  */
13908       T(V6K),    /* V4T.  */
13909       T(V6K),    /* V5T.  */
13910       T(V6K),    /* V5TE.  */
13911       T(V6K),    /* V5TEJ.  */
13912       T(V6K),    /* V6.  */
13913       T(V6KZ),   /* V6KZ.  */
13914       T(V7),     /* V6T2.  */
13915       T(V6K)     /* V6K.  */
13916     };
13917   const int v7[] =
13918     {
13919       T(V7),     /* PRE_V4.  */
13920       T(V7),     /* V4.  */
13921       T(V7),     /* V4T.  */
13922       T(V7),     /* V5T.  */
13923       T(V7),     /* V5TE.  */
13924       T(V7),     /* V5TEJ.  */
13925       T(V7),     /* V6.  */
13926       T(V7),     /* V6KZ.  */
13927       T(V7),     /* V6T2.  */
13928       T(V7),     /* V6K.  */
13929       T(V7)      /* V7.  */
13930     };
13931   const int v6_m[] =
13932     {
13933       -1,        /* PRE_V4.  */
13934       -1,        /* V4.  */
13935       T(V6K),    /* V4T.  */
13936       T(V6K),    /* V5T.  */
13937       T(V6K),    /* V5TE.  */
13938       T(V6K),    /* V5TEJ.  */
13939       T(V6K),    /* V6.  */
13940       T(V6KZ),   /* V6KZ.  */
13941       T(V7),     /* V6T2.  */
13942       T(V6K),    /* V6K.  */
13943       T(V7),     /* V7.  */
13944       T(V6_M)    /* V6_M.  */
13945     };
13946   const int v6s_m[] =
13947     {
13948       -1,        /* PRE_V4.  */
13949       -1,        /* V4.  */
13950       T(V6K),    /* V4T.  */
13951       T(V6K),    /* V5T.  */
13952       T(V6K),    /* V5TE.  */
13953       T(V6K),    /* V5TEJ.  */
13954       T(V6K),    /* V6.  */
13955       T(V6KZ),   /* V6KZ.  */
13956       T(V7),     /* V6T2.  */
13957       T(V6K),    /* V6K.  */
13958       T(V7),     /* V7.  */
13959       T(V6S_M),  /* V6_M.  */
13960       T(V6S_M)   /* V6S_M.  */
13961     };
13962   const int v7e_m[] =
13963     {
13964       -1,        /* PRE_V4.  */
13965       -1,        /* V4.  */
13966       T(V7E_M),  /* V4T.  */
13967       T(V7E_M),  /* V5T.  */
13968       T(V7E_M),  /* V5TE.  */
13969       T(V7E_M),  /* V5TEJ.  */
13970       T(V7E_M),  /* V6.  */
13971       T(V7E_M),  /* V6KZ.  */
13972       T(V7E_M),  /* V6T2.  */
13973       T(V7E_M),  /* V6K.  */
13974       T(V7E_M),  /* V7.  */
13975       T(V7E_M),  /* V6_M.  */
13976       T(V7E_M),  /* V6S_M.  */
13977       T(V7E_M)   /* V7E_M.  */
13978     };
13979   const int v8[] =
13980     {
13981       T(V8),            /* PRE_V4.  */
13982       T(V8),            /* V4.  */
13983       T(V8),            /* V4T.  */
13984       T(V8),            /* V5T.  */
13985       T(V8),            /* V5TE.  */
13986       T(V8),            /* V5TEJ.  */
13987       T(V8),            /* V6.  */
13988       T(V8),            /* V6KZ.  */
13989       T(V8),            /* V6T2.  */
13990       T(V8),            /* V6K.  */
13991       T(V8),            /* V7.  */
13992       T(V8),            /* V6_M.  */
13993       T(V8),            /* V6S_M.  */
13994       T(V8),            /* V7E_M.  */
13995       T(V8)             /* V8.  */
13996     };
13997   const int v8r[] =
13998     {
13999       T(V8R),           /* PRE_V4.  */
14000       T(V8R),           /* V4.  */
14001       T(V8R),           /* V4T.  */
14002       T(V8R),           /* V5T.  */
14003       T(V8R),           /* V5TE.  */
14004       T(V8R),           /* V5TEJ.  */
14005       T(V8R),           /* V6.  */
14006       T(V8R),           /* V6KZ.  */
14007       T(V8R),           /* V6T2.  */
14008       T(V8R),           /* V6K.  */
14009       T(V8R),           /* V7.  */
14010       T(V8R),           /* V6_M.  */
14011       T(V8R),           /* V6S_M.  */
14012       T(V8R),           /* V7E_M.  */
14013       T(V8),            /* V8.  */
14014       T(V8R),           /* V8R.  */
14015     };
14016   const int v8m_baseline[] =
14017     {
14018       -1,               /* PRE_V4.  */
14019       -1,               /* V4.  */
14020       -1,               /* V4T.  */
14021       -1,               /* V5T.  */
14022       -1,               /* V5TE.  */
14023       -1,               /* V5TEJ.  */
14024       -1,               /* V6.  */
14025       -1,               /* V6KZ.  */
14026       -1,               /* V6T2.  */
14027       -1,               /* V6K.  */
14028       -1,               /* V7.  */
14029       T(V8M_BASE),      /* V6_M.  */
14030       T(V8M_BASE),      /* V6S_M.  */
14031       -1,               /* V7E_M.  */
14032       -1,               /* V8.  */
14033       -1,               /* V8R.  */
14034       T(V8M_BASE)       /* V8-M BASELINE.  */
14035     };
14036   const int v8m_mainline[] =
14037     {
14038       -1,               /* PRE_V4.  */
14039       -1,               /* V4.  */
14040       -1,               /* V4T.  */
14041       -1,               /* V5T.  */
14042       -1,               /* V5TE.  */
14043       -1,               /* V5TEJ.  */
14044       -1,               /* V6.  */
14045       -1,               /* V6KZ.  */
14046       -1,               /* V6T2.  */
14047       -1,               /* V6K.  */
14048       T(V8M_MAIN),      /* V7.  */
14049       T(V8M_MAIN),      /* V6_M.  */
14050       T(V8M_MAIN),      /* V6S_M.  */
14051       T(V8M_MAIN),      /* V7E_M.  */
14052       -1,               /* V8.  */
14053       -1,               /* V8R.  */
14054       T(V8M_MAIN),      /* V8-M BASELINE.  */
14055       T(V8M_MAIN)       /* V8-M MAINLINE.  */
14056     };
14057   const int v4t_plus_v6_m[] =
14058     {
14059       -1,               /* PRE_V4.  */
14060       -1,               /* V4.  */
14061       T(V4T),           /* V4T.  */
14062       T(V5T),           /* V5T.  */
14063       T(V5TE),          /* V5TE.  */
14064       T(V5TEJ),         /* V5TEJ.  */
14065       T(V6),            /* V6.  */
14066       T(V6KZ),          /* V6KZ.  */
14067       T(V6T2),          /* V6T2.  */
14068       T(V6K),           /* V6K.  */
14069       T(V7),            /* V7.  */
14070       T(V6_M),          /* V6_M.  */
14071       T(V6S_M),         /* V6S_M.  */
14072       T(V7E_M),         /* V7E_M.  */
14073       T(V8),            /* V8.  */
14074       -1,               /* V8R.  */
14075       T(V8M_BASE),      /* V8-M BASELINE.  */
14076       T(V8M_MAIN),      /* V8-M MAINLINE.  */
14077       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
14078     };
14079   const int *comb[] =
14080     {
14081       v6t2,
14082       v6k,
14083       v7,
14084       v6_m,
14085       v6s_m,
14086       v7e_m,
14087       v8,
14088       v8r,
14089       v8m_baseline,
14090       v8m_mainline,
14091       /* Pseudo-architecture.  */
14092       v4t_plus_v6_m
14093     };
14094
14095   /* Check we've not got a higher architecture than we know about.  */
14096
14097   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
14098     {
14099       _bfd_error_handler (_("error: %pB: unknown CPU architecture"), ibfd);
14100       return -1;
14101     }
14102
14103   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
14104
14105   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
14106       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
14107     oldtag = T(V4T_PLUS_V6_M);
14108
14109   /* And override the new tag if we have a Tag_also_compatible_with on the
14110      input.  */
14111
14112   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
14113       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
14114     newtag = T(V4T_PLUS_V6_M);
14115
14116   tagl = (oldtag < newtag) ? oldtag : newtag;
14117   result = tagh = (oldtag > newtag) ? oldtag : newtag;
14118
14119   /* Architectures before V6KZ add features monotonically.  */
14120   if (tagh <= TAG_CPU_ARCH_V6KZ)
14121     return result;
14122
14123   result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
14124
14125   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
14126      as the canonical version.  */
14127   if (result == T(V4T_PLUS_V6_M))
14128     {
14129       result = T(V4T);
14130       *secondary_compat_out = T(V6_M);
14131     }
14132   else
14133     *secondary_compat_out = -1;
14134
14135   if (result == -1)
14136     {
14137       _bfd_error_handler (_("error: %pB: conflicting CPU architectures %d/%d"),
14138                           ibfd, oldtag, newtag);
14139       return -1;
14140     }
14141
14142   return result;
14143 #undef T
14144 }
14145
14146 /* Query attributes object to see if integer divide instructions may be
14147    present in an object.  */
14148 static bfd_boolean
14149 elf32_arm_attributes_accept_div (const obj_attribute *attr)
14150 {
14151   int arch = attr[Tag_CPU_arch].i;
14152   int profile = attr[Tag_CPU_arch_profile].i;
14153
14154   switch (attr[Tag_DIV_use].i)
14155     {
14156     case 0:
14157       /* Integer divide allowed if instruction contained in archetecture.  */
14158       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
14159         return TRUE;
14160       else if (arch >= TAG_CPU_ARCH_V7E_M)
14161         return TRUE;
14162       else
14163         return FALSE;
14164
14165     case 1:
14166       /* Integer divide explicitly prohibited.  */
14167       return FALSE;
14168
14169     default:
14170       /* Unrecognised case - treat as allowing divide everywhere.  */
14171     case 2:
14172       /* Integer divide allowed in ARM state.  */
14173       return TRUE;
14174     }
14175 }
14176
14177 /* Query attributes object to see if integer divide instructions are
14178    forbidden to be in the object.  This is not the inverse of
14179    elf32_arm_attributes_accept_div.  */
14180 static bfd_boolean
14181 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
14182 {
14183   return attr[Tag_DIV_use].i == 1;
14184 }
14185
14186 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
14187    are conflicting attributes.  */
14188
14189 static bfd_boolean
14190 elf32_arm_merge_eabi_attributes (bfd *ibfd, struct bfd_link_info *info)
14191 {
14192   bfd *obfd = info->output_bfd;
14193   obj_attribute *in_attr;
14194   obj_attribute *out_attr;
14195   /* Some tags have 0 = don't care, 1 = strong requirement,
14196      2 = weak requirement.  */
14197   static const int order_021[3] = {0, 2, 1};
14198   int i;
14199   bfd_boolean result = TRUE;
14200   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
14201
14202   /* Skip the linker stubs file.  This preserves previous behavior
14203      of accepting unknown attributes in the first input file - but
14204      is that a bug?  */
14205   if (ibfd->flags & BFD_LINKER_CREATED)
14206     return TRUE;
14207
14208   /* Skip any input that hasn't attribute section.
14209      This enables to link object files without attribute section with
14210      any others.  */
14211   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
14212     return TRUE;
14213
14214   if (!elf_known_obj_attributes_proc (obfd)[0].i)
14215     {
14216       /* This is the first object.  Copy the attributes.  */
14217       _bfd_elf_copy_obj_attributes (ibfd, obfd);
14218
14219       out_attr = elf_known_obj_attributes_proc (obfd);
14220
14221       /* Use the Tag_null value to indicate the attributes have been
14222          initialized.  */
14223       out_attr[0].i = 1;
14224
14225       /* We do not output objects with Tag_MPextension_use_legacy - we move
14226          the attribute's value to Tag_MPextension_use.  */
14227       if (out_attr[Tag_MPextension_use_legacy].i != 0)
14228         {
14229           if (out_attr[Tag_MPextension_use].i != 0
14230               && out_attr[Tag_MPextension_use_legacy].i
14231                 != out_attr[Tag_MPextension_use].i)
14232             {
14233               _bfd_error_handler
14234                 (_("Error: %pB has both the current and legacy "
14235                    "Tag_MPextension_use attributes"), ibfd);
14236               result = FALSE;
14237             }
14238
14239           out_attr[Tag_MPextension_use] =
14240             out_attr[Tag_MPextension_use_legacy];
14241           out_attr[Tag_MPextension_use_legacy].type = 0;
14242           out_attr[Tag_MPextension_use_legacy].i = 0;
14243         }
14244
14245       return result;
14246     }
14247
14248   in_attr = elf_known_obj_attributes_proc (ibfd);
14249   out_attr = elf_known_obj_attributes_proc (obfd);
14250   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
14251   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
14252     {
14253       /* Ignore mismatches if the object doesn't use floating point or is
14254          floating point ABI independent.  */
14255       if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
14256           || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14257               && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
14258         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
14259       else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14260                && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
14261         {
14262           _bfd_error_handler
14263             (_("error: %pB uses VFP register arguments, %pB does not"),
14264              in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
14265              in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
14266           result = FALSE;
14267         }
14268     }
14269
14270   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
14271     {
14272       /* Merge this attribute with existing attributes.  */
14273       switch (i)
14274         {
14275         case Tag_CPU_raw_name:
14276         case Tag_CPU_name:
14277           /* These are merged after Tag_CPU_arch.  */
14278           break;
14279
14280         case Tag_ABI_optimization_goals:
14281         case Tag_ABI_FP_optimization_goals:
14282           /* Use the first value seen.  */
14283           break;
14284
14285         case Tag_CPU_arch:
14286           {
14287             int secondary_compat = -1, secondary_compat_out = -1;
14288             unsigned int saved_out_attr = out_attr[i].i;
14289             int arch_attr;
14290             static const char *name_table[] =
14291               {
14292                 /* These aren't real CPU names, but we can't guess
14293                    that from the architecture version alone.  */
14294                 "Pre v4",
14295                 "ARM v4",
14296                 "ARM v4T",
14297                 "ARM v5T",
14298                 "ARM v5TE",
14299                 "ARM v5TEJ",
14300                 "ARM v6",
14301                 "ARM v6KZ",
14302                 "ARM v6T2",
14303                 "ARM v6K",
14304                 "ARM v7",
14305                 "ARM v6-M",
14306                 "ARM v6S-M",
14307                 "ARM v8",
14308                 "",
14309                 "ARM v8-M.baseline",
14310                 "ARM v8-M.mainline",
14311             };
14312
14313             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
14314             secondary_compat = get_secondary_compatible_arch (ibfd);
14315             secondary_compat_out = get_secondary_compatible_arch (obfd);
14316             arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
14317                                               &secondary_compat_out,
14318                                               in_attr[i].i,
14319                                               secondary_compat);
14320
14321             /* Return with error if failed to merge.  */
14322             if (arch_attr == -1)
14323               return FALSE;
14324
14325             out_attr[i].i = arch_attr;
14326
14327             set_secondary_compatible_arch (obfd, secondary_compat_out);
14328
14329             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
14330             if (out_attr[i].i == saved_out_attr)
14331               ; /* Leave the names alone.  */
14332             else if (out_attr[i].i == in_attr[i].i)
14333               {
14334                 /* The output architecture has been changed to match the
14335                    input architecture.  Use the input names.  */
14336                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
14337                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
14338                   : NULL;
14339                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
14340                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
14341                   : NULL;
14342               }
14343             else
14344               {
14345                 out_attr[Tag_CPU_name].s = NULL;
14346                 out_attr[Tag_CPU_raw_name].s = NULL;
14347               }
14348
14349             /* If we still don't have a value for Tag_CPU_name,
14350                make one up now.  Tag_CPU_raw_name remains blank.  */
14351             if (out_attr[Tag_CPU_name].s == NULL
14352                 && out_attr[i].i < ARRAY_SIZE (name_table))
14353               out_attr[Tag_CPU_name].s =
14354                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
14355           }
14356           break;
14357
14358         case Tag_ARM_ISA_use:
14359         case Tag_THUMB_ISA_use:
14360         case Tag_WMMX_arch:
14361         case Tag_Advanced_SIMD_arch:
14362           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
14363         case Tag_ABI_FP_rounding:
14364         case Tag_ABI_FP_exceptions:
14365         case Tag_ABI_FP_user_exceptions:
14366         case Tag_ABI_FP_number_model:
14367         case Tag_FP_HP_extension:
14368         case Tag_CPU_unaligned_access:
14369         case Tag_T2EE_use:
14370         case Tag_MPextension_use:
14371           /* Use the largest value specified.  */
14372           if (in_attr[i].i > out_attr[i].i)
14373             out_attr[i].i = in_attr[i].i;
14374           break;
14375
14376         case Tag_ABI_align_preserved:
14377         case Tag_ABI_PCS_RO_data:
14378           /* Use the smallest value specified.  */
14379           if (in_attr[i].i < out_attr[i].i)
14380             out_attr[i].i = in_attr[i].i;
14381           break;
14382
14383         case Tag_ABI_align_needed:
14384           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
14385               && (in_attr[Tag_ABI_align_preserved].i == 0
14386                   || out_attr[Tag_ABI_align_preserved].i == 0))
14387             {
14388               /* This error message should be enabled once all non-conformant
14389                  binaries in the toolchain have had the attributes set
14390                  properly.
14391               _bfd_error_handler
14392                 (_("error: %pB: 8-byte data alignment conflicts with %pB"),
14393                  obfd, ibfd);
14394               result = FALSE; */
14395             }
14396           /* Fall through.  */
14397         case Tag_ABI_FP_denormal:
14398         case Tag_ABI_PCS_GOT_use:
14399           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
14400              value if greater than 2 (for future-proofing).  */
14401           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
14402               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
14403                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
14404             out_attr[i].i = in_attr[i].i;
14405           break;
14406
14407         case Tag_Virtualization_use:
14408           /* The virtualization tag effectively stores two bits of
14409              information: the intended use of TrustZone (in bit 0), and the
14410              intended use of Virtualization (in bit 1).  */
14411           if (out_attr[i].i == 0)
14412             out_attr[i].i = in_attr[i].i;
14413           else if (in_attr[i].i != 0
14414                    && in_attr[i].i != out_attr[i].i)
14415             {
14416               if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
14417                 out_attr[i].i = 3;
14418               else
14419                 {
14420                   _bfd_error_handler
14421                     (_("error: %pB: unable to merge virtualization attributes "
14422                        "with %pB"),
14423                      obfd, ibfd);
14424                   result = FALSE;
14425                 }
14426             }
14427           break;
14428
14429         case Tag_CPU_arch_profile:
14430           if (out_attr[i].i != in_attr[i].i)
14431             {
14432               /* 0 will merge with anything.
14433                  'A' and 'S' merge to 'A'.
14434                  'R' and 'S' merge to 'R'.
14435                  'M' and 'A|R|S' is an error.  */
14436               if (out_attr[i].i == 0
14437                   || (out_attr[i].i == 'S'
14438                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
14439                 out_attr[i].i = in_attr[i].i;
14440               else if (in_attr[i].i == 0
14441                        || (in_attr[i].i == 'S'
14442                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
14443                 ; /* Do nothing.  */
14444               else
14445                 {
14446                   _bfd_error_handler
14447                     (_("error: %pB: conflicting architecture profiles %c/%c"),
14448                      ibfd,
14449                      in_attr[i].i ? in_attr[i].i : '0',
14450                      out_attr[i].i ? out_attr[i].i : '0');
14451                   result = FALSE;
14452                 }
14453             }
14454           break;
14455
14456         case Tag_DSP_extension:
14457           /* No need to change output value if any of:
14458              - pre (<=) ARMv5T input architecture (do not have DSP)
14459              - M input profile not ARMv7E-M and do not have DSP.  */
14460           if (in_attr[Tag_CPU_arch].i <= 3
14461               || (in_attr[Tag_CPU_arch_profile].i == 'M'
14462                   && in_attr[Tag_CPU_arch].i != 13
14463                   && in_attr[i].i == 0))
14464             ; /* Do nothing.  */
14465           /* Output value should be 0 if DSP part of architecture, ie.
14466              - post (>=) ARMv5te architecture output
14467              - A, R or S profile output or ARMv7E-M output architecture.  */
14468           else if (out_attr[Tag_CPU_arch].i >= 4
14469                    && (out_attr[Tag_CPU_arch_profile].i == 'A'
14470                        || out_attr[Tag_CPU_arch_profile].i == 'R'
14471                        || out_attr[Tag_CPU_arch_profile].i == 'S'
14472                        || out_attr[Tag_CPU_arch].i == 13))
14473             out_attr[i].i = 0;
14474           /* Otherwise, DSP instructions are added and not part of output
14475              architecture.  */
14476           else
14477             out_attr[i].i = 1;
14478           break;
14479
14480         case Tag_FP_arch:
14481             {
14482               /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
14483                  the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
14484                  when it's 0.  It might mean absence of FP hardware if
14485                  Tag_FP_arch is zero.  */
14486
14487 #define VFP_VERSION_COUNT 9
14488               static const struct
14489               {
14490                   int ver;
14491                   int regs;
14492               } vfp_versions[VFP_VERSION_COUNT] =
14493                 {
14494                   {0, 0},
14495                   {1, 16},
14496                   {2, 16},
14497                   {3, 32},
14498                   {3, 16},
14499                   {4, 32},
14500                   {4, 16},
14501                   {8, 32},
14502                   {8, 16}
14503                 };
14504               int ver;
14505               int regs;
14506               int newval;
14507
14508               /* If the output has no requirement about FP hardware,
14509                  follow the requirement of the input.  */
14510               if (out_attr[i].i == 0)
14511                 {
14512                   /* This assert is still reasonable, we shouldn't
14513                      produce the suspicious build attribute
14514                      combination (See below for in_attr).  */
14515                   BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
14516                   out_attr[i].i = in_attr[i].i;
14517                   out_attr[Tag_ABI_HardFP_use].i
14518                     = in_attr[Tag_ABI_HardFP_use].i;
14519                   break;
14520                 }
14521               /* If the input has no requirement about FP hardware, do
14522                  nothing.  */
14523               else if (in_attr[i].i == 0)
14524                 {
14525                   /* We used to assert that Tag_ABI_HardFP_use was
14526                      zero here, but we should never assert when
14527                      consuming an object file that has suspicious
14528                      build attributes.  The single precision variant
14529                      of 'no FP architecture' is still 'no FP
14530                      architecture', so we just ignore the tag in this
14531                      case.  */
14532                   break;
14533                 }
14534
14535               /* Both the input and the output have nonzero Tag_FP_arch.
14536                  So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
14537
14538               /* If both the input and the output have zero Tag_ABI_HardFP_use,
14539                  do nothing.  */
14540               if (in_attr[Tag_ABI_HardFP_use].i == 0
14541                   && out_attr[Tag_ABI_HardFP_use].i == 0)
14542                 ;
14543               /* If the input and the output have different Tag_ABI_HardFP_use,
14544                  the combination of them is 0 (implied by Tag_FP_arch).  */
14545               else if (in_attr[Tag_ABI_HardFP_use].i
14546                        != out_attr[Tag_ABI_HardFP_use].i)
14547                 out_attr[Tag_ABI_HardFP_use].i = 0;
14548
14549               /* Now we can handle Tag_FP_arch.  */
14550
14551               /* Values of VFP_VERSION_COUNT or more aren't defined, so just
14552                  pick the biggest.  */
14553               if (in_attr[i].i >= VFP_VERSION_COUNT
14554                   && in_attr[i].i > out_attr[i].i)
14555                 {
14556                   out_attr[i] = in_attr[i];
14557                   break;
14558                 }
14559               /* The output uses the superset of input features
14560                  (ISA version) and registers.  */
14561               ver = vfp_versions[in_attr[i].i].ver;
14562               if (ver < vfp_versions[out_attr[i].i].ver)
14563                 ver = vfp_versions[out_attr[i].i].ver;
14564               regs = vfp_versions[in_attr[i].i].regs;
14565               if (regs < vfp_versions[out_attr[i].i].regs)
14566                 regs = vfp_versions[out_attr[i].i].regs;
14567               /* This assumes all possible supersets are also a valid
14568                  options.  */
14569               for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
14570                 {
14571                   if (regs == vfp_versions[newval].regs
14572                       && ver == vfp_versions[newval].ver)
14573                     break;
14574                 }
14575               out_attr[i].i = newval;
14576             }
14577           break;
14578         case Tag_PCS_config:
14579           if (out_attr[i].i == 0)
14580             out_attr[i].i = in_attr[i].i;
14581           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
14582             {
14583               /* It's sometimes ok to mix different configs, so this is only
14584                  a warning.  */
14585               _bfd_error_handler
14586                 (_("warning: %pB: conflicting platform configuration"), ibfd);
14587             }
14588           break;
14589         case Tag_ABI_PCS_R9_use:
14590           if (in_attr[i].i != out_attr[i].i
14591               && out_attr[i].i != AEABI_R9_unused
14592               && in_attr[i].i != AEABI_R9_unused)
14593             {
14594               _bfd_error_handler
14595                 (_("error: %pB: conflicting use of R9"), ibfd);
14596               result = FALSE;
14597             }
14598           if (out_attr[i].i == AEABI_R9_unused)
14599             out_attr[i].i = in_attr[i].i;
14600           break;
14601         case Tag_ABI_PCS_RW_data:
14602           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
14603               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
14604               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
14605             {
14606               _bfd_error_handler
14607                 (_("error: %pB: SB relative addressing conflicts with use of R9"),
14608                  ibfd);
14609               result = FALSE;
14610             }
14611           /* Use the smallest value specified.  */
14612           if (in_attr[i].i < out_attr[i].i)
14613             out_attr[i].i = in_attr[i].i;
14614           break;
14615         case Tag_ABI_PCS_wchar_t:
14616           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
14617               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
14618             {
14619               _bfd_error_handler
14620                 (_("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"),
14621                  ibfd, in_attr[i].i, out_attr[i].i);
14622             }
14623           else if (in_attr[i].i && !out_attr[i].i)
14624             out_attr[i].i = in_attr[i].i;
14625           break;
14626         case Tag_ABI_enum_size:
14627           if (in_attr[i].i != AEABI_enum_unused)
14628             {
14629               if (out_attr[i].i == AEABI_enum_unused
14630                   || out_attr[i].i == AEABI_enum_forced_wide)
14631                 {
14632                   /* The existing object is compatible with anything.
14633                      Use whatever requirements the new object has.  */
14634                   out_attr[i].i = in_attr[i].i;
14635                 }
14636               else if (in_attr[i].i != AEABI_enum_forced_wide
14637                        && out_attr[i].i != in_attr[i].i
14638                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
14639                 {
14640                   static const char *aeabi_enum_names[] =
14641                     { "", "variable-size", "32-bit", "" };
14642                   const char *in_name =
14643                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
14644                     ? aeabi_enum_names[in_attr[i].i]
14645                     : "<unknown>";
14646                   const char *out_name =
14647                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
14648                     ? aeabi_enum_names[out_attr[i].i]
14649                     : "<unknown>";
14650                   _bfd_error_handler
14651                     (_("warning: %pB uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
14652                      ibfd, in_name, out_name);
14653                 }
14654             }
14655           break;
14656         case Tag_ABI_VFP_args:
14657           /* Aready done.  */
14658           break;
14659         case Tag_ABI_WMMX_args:
14660           if (in_attr[i].i != out_attr[i].i)
14661             {
14662               _bfd_error_handler
14663                 (_("error: %pB uses iWMMXt register arguments, %pB does not"),
14664                  ibfd, obfd);
14665               result = FALSE;
14666             }
14667           break;
14668         case Tag_compatibility:
14669           /* Merged in target-independent code.  */
14670           break;
14671         case Tag_ABI_HardFP_use:
14672           /* This is handled along with Tag_FP_arch.  */
14673           break;
14674         case Tag_ABI_FP_16bit_format:
14675           if (in_attr[i].i != 0 && out_attr[i].i != 0)
14676             {
14677               if (in_attr[i].i != out_attr[i].i)
14678                 {
14679                   _bfd_error_handler
14680                     (_("error: fp16 format mismatch between %pB and %pB"),
14681                      ibfd, obfd);
14682                   result = FALSE;
14683                 }
14684             }
14685           if (in_attr[i].i != 0)
14686             out_attr[i].i = in_attr[i].i;
14687           break;
14688
14689         case Tag_DIV_use:
14690           /* A value of zero on input means that the divide instruction may
14691              be used if available in the base architecture as specified via
14692              Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
14693              the user did not want divide instructions.  A value of 2
14694              explicitly means that divide instructions were allowed in ARM
14695              and Thumb state.  */
14696           if (in_attr[i].i == out_attr[i].i)
14697             /* Do nothing.  */ ;
14698           else if (elf32_arm_attributes_forbid_div (in_attr)
14699                    && !elf32_arm_attributes_accept_div (out_attr))
14700             out_attr[i].i = 1;
14701           else if (elf32_arm_attributes_forbid_div (out_attr)
14702                    && elf32_arm_attributes_accept_div (in_attr))
14703             out_attr[i].i = in_attr[i].i;
14704           else if (in_attr[i].i == 2)
14705             out_attr[i].i = in_attr[i].i;
14706           break;
14707
14708         case Tag_MPextension_use_legacy:
14709           /* We don't output objects with Tag_MPextension_use_legacy - we
14710              move the value to Tag_MPextension_use.  */
14711           if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
14712             {
14713               if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
14714                 {
14715                   _bfd_error_handler
14716                     (_("%pB has both the current and legacy "
14717                        "Tag_MPextension_use attributes"),
14718                      ibfd);
14719                   result = FALSE;
14720                 }
14721             }
14722
14723           if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
14724             out_attr[Tag_MPextension_use] = in_attr[i];
14725
14726           break;
14727
14728         case Tag_nodefaults:
14729           /* This tag is set if it exists, but the value is unused (and is
14730              typically zero).  We don't actually need to do anything here -
14731              the merge happens automatically when the type flags are merged
14732              below.  */
14733           break;
14734         case Tag_also_compatible_with:
14735           /* Already done in Tag_CPU_arch.  */
14736           break;
14737         case Tag_conformance:
14738           /* Keep the attribute if it matches.  Throw it away otherwise.
14739              No attribute means no claim to conform.  */
14740           if (!in_attr[i].s || !out_attr[i].s
14741               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
14742             out_attr[i].s = NULL;
14743           break;
14744
14745         default:
14746           result
14747             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
14748         }
14749
14750       /* If out_attr was copied from in_attr then it won't have a type yet.  */
14751       if (in_attr[i].type && !out_attr[i].type)
14752         out_attr[i].type = in_attr[i].type;
14753     }
14754
14755   /* Merge Tag_compatibility attributes and any common GNU ones.  */
14756   if (!_bfd_elf_merge_object_attributes (ibfd, info))
14757     return FALSE;
14758
14759   /* Check for any attributes not known on ARM.  */
14760   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
14761
14762   return result;
14763 }
14764
14765
14766 /* Return TRUE if the two EABI versions are incompatible.  */
14767
14768 static bfd_boolean
14769 elf32_arm_versions_compatible (unsigned iver, unsigned over)
14770 {
14771   /* v4 and v5 are the same spec before and after it was released,
14772      so allow mixing them.  */
14773   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
14774       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
14775     return TRUE;
14776
14777   return (iver == over);
14778 }
14779
14780 /* Merge backend specific data from an object file to the output
14781    object file when linking.  */
14782
14783 static bfd_boolean
14784 elf32_arm_merge_private_bfd_data (bfd *, struct bfd_link_info *);
14785
14786 /* Display the flags field.  */
14787
14788 static bfd_boolean
14789 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
14790 {
14791   FILE * file = (FILE *) ptr;
14792   unsigned long flags;
14793
14794   BFD_ASSERT (abfd != NULL && ptr != NULL);
14795
14796   /* Print normal ELF private data.  */
14797   _bfd_elf_print_private_bfd_data (abfd, ptr);
14798
14799   flags = elf_elfheader (abfd)->e_flags;
14800   /* Ignore init flag - it may not be set, despite the flags field
14801      containing valid data.  */
14802
14803   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14804
14805   switch (EF_ARM_EABI_VERSION (flags))
14806     {
14807     case EF_ARM_EABI_UNKNOWN:
14808       /* The following flag bits are GNU extensions and not part of the
14809          official ARM ELF extended ABI.  Hence they are only decoded if
14810          the EABI version is not set.  */
14811       if (flags & EF_ARM_INTERWORK)
14812         fprintf (file, _(" [interworking enabled]"));
14813
14814       if (flags & EF_ARM_APCS_26)
14815         fprintf (file, " [APCS-26]");
14816       else
14817         fprintf (file, " [APCS-32]");
14818
14819       if (flags & EF_ARM_VFP_FLOAT)
14820         fprintf (file, _(" [VFP float format]"));
14821       else if (flags & EF_ARM_MAVERICK_FLOAT)
14822         fprintf (file, _(" [Maverick float format]"));
14823       else
14824         fprintf (file, _(" [FPA float format]"));
14825
14826       if (flags & EF_ARM_APCS_FLOAT)
14827         fprintf (file, _(" [floats passed in float registers]"));
14828
14829       if (flags & EF_ARM_PIC)
14830         fprintf (file, _(" [position independent]"));
14831
14832       if (flags & EF_ARM_NEW_ABI)
14833         fprintf (file, _(" [new ABI]"));
14834
14835       if (flags & EF_ARM_OLD_ABI)
14836         fprintf (file, _(" [old ABI]"));
14837
14838       if (flags & EF_ARM_SOFT_FLOAT)
14839         fprintf (file, _(" [software FP]"));
14840
14841       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
14842                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
14843                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
14844                  | EF_ARM_MAVERICK_FLOAT);
14845       break;
14846
14847     case EF_ARM_EABI_VER1:
14848       fprintf (file, _(" [Version1 EABI]"));
14849
14850       if (flags & EF_ARM_SYMSARESORTED)
14851         fprintf (file, _(" [sorted symbol table]"));
14852       else
14853         fprintf (file, _(" [unsorted symbol table]"));
14854
14855       flags &= ~ EF_ARM_SYMSARESORTED;
14856       break;
14857
14858     case EF_ARM_EABI_VER2:
14859       fprintf (file, _(" [Version2 EABI]"));
14860
14861       if (flags & EF_ARM_SYMSARESORTED)
14862         fprintf (file, _(" [sorted symbol table]"));
14863       else
14864         fprintf (file, _(" [unsorted symbol table]"));
14865
14866       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
14867         fprintf (file, _(" [dynamic symbols use segment index]"));
14868
14869       if (flags & EF_ARM_MAPSYMSFIRST)
14870         fprintf (file, _(" [mapping symbols precede others]"));
14871
14872       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
14873                  | EF_ARM_MAPSYMSFIRST);
14874       break;
14875
14876     case EF_ARM_EABI_VER3:
14877       fprintf (file, _(" [Version3 EABI]"));
14878       break;
14879
14880     case EF_ARM_EABI_VER4:
14881       fprintf (file, _(" [Version4 EABI]"));
14882       goto eabi;
14883
14884     case EF_ARM_EABI_VER5:
14885       fprintf (file, _(" [Version5 EABI]"));
14886
14887       if (flags & EF_ARM_ABI_FLOAT_SOFT)
14888         fprintf (file, _(" [soft-float ABI]"));
14889
14890       if (flags & EF_ARM_ABI_FLOAT_HARD)
14891         fprintf (file, _(" [hard-float ABI]"));
14892
14893       flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
14894
14895     eabi:
14896       if (flags & EF_ARM_BE8)
14897         fprintf (file, _(" [BE8]"));
14898
14899       if (flags & EF_ARM_LE8)
14900         fprintf (file, _(" [LE8]"));
14901
14902       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
14903       break;
14904
14905     default:
14906       fprintf (file, _(" <EABI version unrecognised>"));
14907       break;
14908     }
14909
14910   flags &= ~ EF_ARM_EABIMASK;
14911
14912   if (flags & EF_ARM_RELEXEC)
14913     fprintf (file, _(" [relocatable executable]"));
14914
14915   if (flags & EF_ARM_PIC)
14916     fprintf (file, _(" [position independent]"));
14917
14918   if (elf_elfheader (abfd)->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC)
14919     fprintf (file, _(" [FDPIC ABI supplement]"));
14920
14921   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_PIC);
14922
14923   if (flags)
14924     fprintf (file, _("<Unrecognised flag bits set>"));
14925
14926   fputc ('\n', file);
14927
14928   return TRUE;
14929 }
14930
14931 static int
14932 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
14933 {
14934   switch (ELF_ST_TYPE (elf_sym->st_info))
14935     {
14936     case STT_ARM_TFUNC:
14937       return ELF_ST_TYPE (elf_sym->st_info);
14938
14939     case STT_ARM_16BIT:
14940       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
14941          This allows us to distinguish between data used by Thumb instructions
14942          and non-data (which is probably code) inside Thumb regions of an
14943          executable.  */
14944       if (type != STT_OBJECT && type != STT_TLS)
14945         return ELF_ST_TYPE (elf_sym->st_info);
14946       break;
14947
14948     default:
14949       break;
14950     }
14951
14952   return type;
14953 }
14954
14955 static asection *
14956 elf32_arm_gc_mark_hook (asection *sec,
14957                         struct bfd_link_info *info,
14958                         Elf_Internal_Rela *rel,
14959                         struct elf_link_hash_entry *h,
14960                         Elf_Internal_Sym *sym)
14961 {
14962   if (h != NULL)
14963     switch (ELF32_R_TYPE (rel->r_info))
14964       {
14965       case R_ARM_GNU_VTINHERIT:
14966       case R_ARM_GNU_VTENTRY:
14967         return NULL;
14968       }
14969
14970   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
14971 }
14972
14973 /* Look through the relocs for a section during the first phase.  */
14974
14975 static bfd_boolean
14976 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
14977                         asection *sec, const Elf_Internal_Rela *relocs)
14978 {
14979   Elf_Internal_Shdr *symtab_hdr;
14980   struct elf_link_hash_entry **sym_hashes;
14981   const Elf_Internal_Rela *rel;
14982   const Elf_Internal_Rela *rel_end;
14983   bfd *dynobj;
14984   asection *sreloc;
14985   struct elf32_arm_link_hash_table *htab;
14986   bfd_boolean call_reloc_p;
14987   bfd_boolean may_become_dynamic_p;
14988   bfd_boolean may_need_local_target_p;
14989   unsigned long nsyms;
14990
14991   if (bfd_link_relocatable (info))
14992     return TRUE;
14993
14994   BFD_ASSERT (is_arm_elf (abfd));
14995
14996   htab = elf32_arm_hash_table (info);
14997   if (htab == NULL)
14998     return FALSE;
14999
15000   sreloc = NULL;
15001
15002   /* Create dynamic sections for relocatable executables so that we can
15003      copy relocations.  */
15004   if (htab->root.is_relocatable_executable
15005       && ! htab->root.dynamic_sections_created)
15006     {
15007       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
15008         return FALSE;
15009     }
15010
15011   if (htab->root.dynobj == NULL)
15012     htab->root.dynobj = abfd;
15013   if (!create_ifunc_sections (info))
15014     return FALSE;
15015
15016   dynobj = htab->root.dynobj;
15017
15018   symtab_hdr = & elf_symtab_hdr (abfd);
15019   sym_hashes = elf_sym_hashes (abfd);
15020   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
15021
15022   rel_end = relocs + sec->reloc_count;
15023   for (rel = relocs; rel < rel_end; rel++)
15024     {
15025       Elf_Internal_Sym *isym;
15026       struct elf_link_hash_entry *h;
15027       struct elf32_arm_link_hash_entry *eh;
15028       unsigned int r_symndx;
15029       int r_type;
15030
15031       r_symndx = ELF32_R_SYM (rel->r_info);
15032       r_type = ELF32_R_TYPE (rel->r_info);
15033       r_type = arm_real_reloc_type (htab, r_type);
15034
15035       if (r_symndx >= nsyms
15036           /* PR 9934: It is possible to have relocations that do not
15037              refer to symbols, thus it is also possible to have an
15038              object file containing relocations but no symbol table.  */
15039           && (r_symndx > STN_UNDEF || nsyms > 0))
15040         {
15041           _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15042                               r_symndx);
15043           return FALSE;
15044         }
15045
15046       h = NULL;
15047       isym = NULL;
15048       if (nsyms > 0)
15049         {
15050           if (r_symndx < symtab_hdr->sh_info)
15051             {
15052               /* A local symbol.  */
15053               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
15054                                             abfd, r_symndx);
15055               if (isym == NULL)
15056                 return FALSE;
15057             }
15058           else
15059             {
15060               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
15061               while (h->root.type == bfd_link_hash_indirect
15062                      || h->root.type == bfd_link_hash_warning)
15063                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15064             }
15065         }
15066
15067       eh = (struct elf32_arm_link_hash_entry *) h;
15068
15069       call_reloc_p = FALSE;
15070       may_become_dynamic_p = FALSE;
15071       may_need_local_target_p = FALSE;
15072
15073       /* Could be done earlier, if h were already available.  */
15074       r_type = elf32_arm_tls_transition (info, r_type, h);
15075       switch (r_type)
15076         {
15077         case R_ARM_GOTOFFFUNCDESC:
15078           {
15079             if (h == NULL)
15080               {
15081                 if (!elf32_arm_allocate_local_sym_info (abfd))
15082                   return FALSE;
15083                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].gotofffuncdesc_cnt += 1;
15084                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_offset = -1;
15085               }
15086             else
15087               {
15088                 eh->fdpic_cnts.gotofffuncdesc_cnt++;
15089               }
15090           }
15091           break;
15092
15093         case R_ARM_GOTFUNCDESC:
15094           {
15095             if (h == NULL)
15096               {
15097                 /* Such a relocation is not supposed to be generated
15098                    by gcc on a static function. */
15099                 /* Anyway if needed it could be handled.  */
15100                 abort();
15101               }
15102             else
15103               {
15104                 eh->fdpic_cnts.gotfuncdesc_cnt++;
15105               }
15106           }
15107           break;
15108
15109         case R_ARM_FUNCDESC:
15110           {
15111             if (h == NULL)
15112               {
15113                 if (!elf32_arm_allocate_local_sym_info (abfd))
15114                   return FALSE;
15115                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_cnt += 1;
15116                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_offset = -1;
15117               }
15118             else
15119               {
15120                 eh->fdpic_cnts.funcdesc_cnt++;
15121               }
15122           }
15123           break;
15124
15125           case R_ARM_GOT32:
15126           case R_ARM_GOT_PREL:
15127           case R_ARM_TLS_GD32:
15128           case R_ARM_TLS_GD32_FDPIC:
15129           case R_ARM_TLS_IE32:
15130           case R_ARM_TLS_IE32_FDPIC:
15131           case R_ARM_TLS_GOTDESC:
15132           case R_ARM_TLS_DESCSEQ:
15133           case R_ARM_THM_TLS_DESCSEQ:
15134           case R_ARM_TLS_CALL:
15135           case R_ARM_THM_TLS_CALL:
15136             /* This symbol requires a global offset table entry.  */
15137             {
15138               int tls_type, old_tls_type;
15139
15140               switch (r_type)
15141                 {
15142                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
15143                 case R_ARM_TLS_GD32_FDPIC: tls_type = GOT_TLS_GD; break;
15144
15145                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
15146                 case R_ARM_TLS_IE32_FDPIC: tls_type = GOT_TLS_IE; break;
15147
15148                 case R_ARM_TLS_GOTDESC:
15149                 case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
15150                 case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
15151                   tls_type = GOT_TLS_GDESC; break;
15152
15153                 default: tls_type = GOT_NORMAL; break;
15154                 }
15155
15156               if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
15157                 info->flags |= DF_STATIC_TLS;
15158
15159               if (h != NULL)
15160                 {
15161                   h->got.refcount++;
15162                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
15163                 }
15164               else
15165                 {
15166                   /* This is a global offset table entry for a local symbol.  */
15167                   if (!elf32_arm_allocate_local_sym_info (abfd))
15168                     return FALSE;
15169                   elf_local_got_refcounts (abfd)[r_symndx] += 1;
15170                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
15171                 }
15172
15173               /* If a variable is accessed with both tls methods, two
15174                  slots may be created.  */
15175               if (GOT_TLS_GD_ANY_P (old_tls_type)
15176                   && GOT_TLS_GD_ANY_P (tls_type))
15177                 tls_type |= old_tls_type;
15178
15179               /* We will already have issued an error message if there
15180                  is a TLS/non-TLS mismatch, based on the symbol
15181                  type.  So just combine any TLS types needed.  */
15182               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
15183                   && tls_type != GOT_NORMAL)
15184                 tls_type |= old_tls_type;
15185
15186               /* If the symbol is accessed in both IE and GDESC
15187                  method, we're able to relax. Turn off the GDESC flag,
15188                  without messing up with any other kind of tls types
15189                  that may be involved.  */
15190               if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
15191                 tls_type &= ~GOT_TLS_GDESC;
15192
15193               if (old_tls_type != tls_type)
15194                 {
15195                   if (h != NULL)
15196                     elf32_arm_hash_entry (h)->tls_type = tls_type;
15197                   else
15198                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
15199                 }
15200             }
15201             /* Fall through.  */
15202
15203           case R_ARM_TLS_LDM32:
15204           case R_ARM_TLS_LDM32_FDPIC:
15205             if (r_type == R_ARM_TLS_LDM32 || r_type == R_ARM_TLS_LDM32_FDPIC)
15206                 htab->tls_ldm_got.refcount++;
15207             /* Fall through.  */
15208
15209           case R_ARM_GOTOFF32:
15210           case R_ARM_GOTPC:
15211             if (htab->root.sgot == NULL
15212                 && !create_got_section (htab->root.dynobj, info))
15213               return FALSE;
15214             break;
15215
15216           case R_ARM_PC24:
15217           case R_ARM_PLT32:
15218           case R_ARM_CALL:
15219           case R_ARM_JUMP24:
15220           case R_ARM_PREL31:
15221           case R_ARM_THM_CALL:
15222           case R_ARM_THM_JUMP24:
15223           case R_ARM_THM_JUMP19:
15224             call_reloc_p = TRUE;
15225             may_need_local_target_p = TRUE;
15226             break;
15227
15228           case R_ARM_ABS12:
15229             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
15230                ldr __GOTT_INDEX__ offsets.  */
15231             if (!htab->vxworks_p)
15232               {
15233                 may_need_local_target_p = TRUE;
15234                 break;
15235               }
15236             else goto jump_over;
15237
15238             /* Fall through.  */
15239
15240           case R_ARM_MOVW_ABS_NC:
15241           case R_ARM_MOVT_ABS:
15242           case R_ARM_THM_MOVW_ABS_NC:
15243           case R_ARM_THM_MOVT_ABS:
15244             if (bfd_link_pic (info))
15245               {
15246                 _bfd_error_handler
15247                   (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
15248                    abfd, elf32_arm_howto_table_1[r_type].name,
15249                    (h) ? h->root.root.string : "a local symbol");
15250                 bfd_set_error (bfd_error_bad_value);
15251                 return FALSE;
15252               }
15253
15254             /* Fall through.  */
15255           case R_ARM_ABS32:
15256           case R_ARM_ABS32_NOI:
15257         jump_over:
15258             if (h != NULL && bfd_link_executable (info))
15259               {
15260                 h->pointer_equality_needed = 1;
15261               }
15262             /* Fall through.  */
15263           case R_ARM_REL32:
15264           case R_ARM_REL32_NOI:
15265           case R_ARM_MOVW_PREL_NC:
15266           case R_ARM_MOVT_PREL:
15267           case R_ARM_THM_MOVW_PREL_NC:
15268           case R_ARM_THM_MOVT_PREL:
15269
15270             /* Should the interworking branches be listed here?  */
15271             if ((bfd_link_pic (info) || htab->root.is_relocatable_executable
15272                  || htab->fdpic_p)
15273                 && (sec->flags & SEC_ALLOC) != 0)
15274               {
15275                 if (h == NULL
15276                     && elf32_arm_howto_from_type (r_type)->pc_relative)
15277                   {
15278                     /* In shared libraries and relocatable executables,
15279                        we treat local relative references as calls;
15280                        see the related SYMBOL_CALLS_LOCAL code in
15281                        allocate_dynrelocs.  */
15282                     call_reloc_p = TRUE;
15283                     may_need_local_target_p = TRUE;
15284                   }
15285                 else
15286                   /* We are creating a shared library or relocatable
15287                      executable, and this is a reloc against a global symbol,
15288                      or a non-PC-relative reloc against a local symbol.
15289                      We may need to copy the reloc into the output.  */
15290                   may_become_dynamic_p = TRUE;
15291               }
15292             else
15293               may_need_local_target_p = TRUE;
15294             break;
15295
15296         /* This relocation describes the C++ object vtable hierarchy.
15297            Reconstruct it for later use during GC.  */
15298         case R_ARM_GNU_VTINHERIT:
15299           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
15300             return FALSE;
15301           break;
15302
15303         /* This relocation describes which C++ vtable entries are actually
15304            used.  Record for later use during GC.  */
15305         case R_ARM_GNU_VTENTRY:
15306           BFD_ASSERT (h != NULL);
15307           if (h != NULL
15308               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
15309             return FALSE;
15310           break;
15311         }
15312
15313       if (h != NULL)
15314         {
15315           if (call_reloc_p)
15316             /* We may need a .plt entry if the function this reloc
15317                refers to is in a different object, regardless of the
15318                symbol's type.  We can't tell for sure yet, because
15319                something later might force the symbol local.  */
15320             h->needs_plt = 1;
15321           else if (may_need_local_target_p)
15322             /* If this reloc is in a read-only section, we might
15323                need a copy reloc.  We can't check reliably at this
15324                stage whether the section is read-only, as input
15325                sections have not yet been mapped to output sections.
15326                Tentatively set the flag for now, and correct in
15327                adjust_dynamic_symbol.  */
15328             h->non_got_ref = 1;
15329         }
15330
15331       if (may_need_local_target_p
15332           && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
15333         {
15334           union gotplt_union *root_plt;
15335           struct arm_plt_info *arm_plt;
15336           struct arm_local_iplt_info *local_iplt;
15337
15338           if (h != NULL)
15339             {
15340               root_plt = &h->plt;
15341               arm_plt = &eh->plt;
15342             }
15343           else
15344             {
15345               local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
15346               if (local_iplt == NULL)
15347                 return FALSE;
15348               root_plt = &local_iplt->root;
15349               arm_plt = &local_iplt->arm;
15350             }
15351
15352           /* If the symbol is a function that doesn't bind locally,
15353              this relocation will need a PLT entry.  */
15354           if (root_plt->refcount != -1)
15355             root_plt->refcount += 1;
15356
15357           if (!call_reloc_p)
15358             arm_plt->noncall_refcount++;
15359
15360           /* It's too early to use htab->use_blx here, so we have to
15361              record possible blx references separately from
15362              relocs that definitely need a thumb stub.  */
15363
15364           if (r_type == R_ARM_THM_CALL)
15365             arm_plt->maybe_thumb_refcount += 1;
15366
15367           if (r_type == R_ARM_THM_JUMP24
15368               || r_type == R_ARM_THM_JUMP19)
15369             arm_plt->thumb_refcount += 1;
15370         }
15371
15372       if (may_become_dynamic_p)
15373         {
15374           struct elf_dyn_relocs *p, **head;
15375
15376           /* Create a reloc section in dynobj.  */
15377           if (sreloc == NULL)
15378             {
15379               sreloc = _bfd_elf_make_dynamic_reloc_section
15380                 (sec, dynobj, 2, abfd, ! htab->use_rel);
15381
15382               if (sreloc == NULL)
15383                 return FALSE;
15384
15385               /* BPABI objects never have dynamic relocations mapped.  */
15386               if (htab->symbian_p)
15387                 {
15388                   flagword flags;
15389
15390                   flags = bfd_get_section_flags (dynobj, sreloc);
15391                   flags &= ~(SEC_LOAD | SEC_ALLOC);
15392                   bfd_set_section_flags (dynobj, sreloc, flags);
15393                 }
15394             }
15395
15396           /* If this is a global symbol, count the number of
15397              relocations we need for this symbol.  */
15398           if (h != NULL)
15399             head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
15400           else
15401             {
15402               head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
15403               if (head == NULL)
15404                 return FALSE;
15405             }
15406
15407           p = *head;
15408           if (p == NULL || p->sec != sec)
15409             {
15410               bfd_size_type amt = sizeof *p;
15411
15412               p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
15413               if (p == NULL)
15414                 return FALSE;
15415               p->next = *head;
15416               *head = p;
15417               p->sec = sec;
15418               p->count = 0;
15419               p->pc_count = 0;
15420             }
15421
15422           if (elf32_arm_howto_from_type (r_type)->pc_relative)
15423             p->pc_count += 1;
15424           p->count += 1;
15425           if (h == NULL && htab->fdpic_p && !bfd_link_pic(info)
15426               && r_type != R_ARM_ABS32 && r_type != R_ARM_ABS32_NOI) {
15427             /* Here we only support R_ARM_ABS32 and R_ARM_ABS32_NOI
15428                that will become rofixup.  */
15429             /* This is due to the fact that we suppose all will become rofixup.  */
15430             fprintf(stderr, "FDPIC does not yet support %d relocation to become dynamic for executable\n", r_type);
15431             _bfd_error_handler
15432               (_("FDPIC does not yet support %s relocation"
15433                  " to become dynamic for executable"),
15434                elf32_arm_howto_table_1[r_type].name);
15435             abort();
15436           }
15437         }
15438     }
15439
15440   return TRUE;
15441 }
15442
15443 static void
15444 elf32_arm_update_relocs (asection *o,
15445                          struct bfd_elf_section_reloc_data *reldata)
15446 {
15447   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
15448   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
15449   const struct elf_backend_data *bed;
15450   _arm_elf_section_data *eado;
15451   struct bfd_link_order *p;
15452   bfd_byte *erela_head, *erela;
15453   Elf_Internal_Rela *irela_head, *irela;
15454   Elf_Internal_Shdr *rel_hdr;
15455   bfd *abfd;
15456   unsigned int count;
15457
15458   eado = get_arm_elf_section_data (o);
15459
15460   if (!eado || eado->elf.this_hdr.sh_type != SHT_ARM_EXIDX)
15461     return;
15462
15463   abfd = o->owner;
15464   bed = get_elf_backend_data (abfd);
15465   rel_hdr = reldata->hdr;
15466
15467   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
15468     {
15469       swap_in = bed->s->swap_reloc_in;
15470       swap_out = bed->s->swap_reloc_out;
15471     }
15472   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
15473     {
15474       swap_in = bed->s->swap_reloca_in;
15475       swap_out = bed->s->swap_reloca_out;
15476     }
15477   else
15478     abort ();
15479
15480   erela_head = rel_hdr->contents;
15481   irela_head = (Elf_Internal_Rela *) bfd_zmalloc
15482     ((NUM_SHDR_ENTRIES (rel_hdr) + 1) * sizeof (*irela_head));
15483
15484   erela = erela_head;
15485   irela = irela_head;
15486   count = 0;
15487
15488   for (p = o->map_head.link_order; p; p = p->next)
15489     {
15490       if (p->type == bfd_section_reloc_link_order
15491           || p->type == bfd_symbol_reloc_link_order)
15492         {
15493           (*swap_in) (abfd, erela, irela);
15494           erela += rel_hdr->sh_entsize;
15495           irela++;
15496           count++;
15497         }
15498       else if (p->type == bfd_indirect_link_order)
15499         {
15500           struct bfd_elf_section_reloc_data *input_reldata;
15501           arm_unwind_table_edit *edit_list, *edit_tail;
15502           _arm_elf_section_data *eadi;
15503           bfd_size_type j;
15504           bfd_vma offset;
15505           asection *i;
15506
15507           i = p->u.indirect.section;
15508
15509           eadi = get_arm_elf_section_data (i);
15510           edit_list = eadi->u.exidx.unwind_edit_list;
15511           edit_tail = eadi->u.exidx.unwind_edit_tail;
15512           offset = o->vma + i->output_offset;
15513
15514           if (eadi->elf.rel.hdr &&
15515               eadi->elf.rel.hdr->sh_entsize == rel_hdr->sh_entsize)
15516             input_reldata = &eadi->elf.rel;
15517           else if (eadi->elf.rela.hdr &&
15518                    eadi->elf.rela.hdr->sh_entsize == rel_hdr->sh_entsize)
15519             input_reldata = &eadi->elf.rela;
15520           else
15521             abort ();
15522
15523           if (edit_list)
15524             {
15525               for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15526                 {
15527                   arm_unwind_table_edit *edit_node, *edit_next;
15528                   bfd_vma bias;
15529                   bfd_vma reloc_index;
15530
15531                   (*swap_in) (abfd, erela, irela);
15532                   reloc_index = (irela->r_offset - offset) / 8;
15533
15534                   bias = 0;
15535                   edit_node = edit_list;
15536                   for (edit_next = edit_list;
15537                        edit_next && edit_next->index <= reloc_index;
15538                        edit_next = edit_node->next)
15539                     {
15540                       bias++;
15541                       edit_node = edit_next;
15542                     }
15543
15544                   if (edit_node->type != DELETE_EXIDX_ENTRY
15545                       || edit_node->index != reloc_index)
15546                     {
15547                       irela->r_offset -= bias * 8;
15548                       irela++;
15549                       count++;
15550                     }
15551
15552                   erela += rel_hdr->sh_entsize;
15553                 }
15554
15555               if (edit_tail->type == INSERT_EXIDX_CANTUNWIND_AT_END)
15556                 {
15557                   /* New relocation entity.  */
15558                   asection *text_sec = edit_tail->linked_section;
15559                   asection *text_out = text_sec->output_section;
15560                   bfd_vma exidx_offset = offset + i->size - 8;
15561
15562                   irela->r_addend = 0;
15563                   irela->r_offset = exidx_offset;
15564                   irela->r_info = ELF32_R_INFO
15565                     (text_out->target_index, R_ARM_PREL31);
15566                   irela++;
15567                   count++;
15568                 }
15569             }
15570           else
15571             {
15572               for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15573                 {
15574                   (*swap_in) (abfd, erela, irela);
15575                   erela += rel_hdr->sh_entsize;
15576                   irela++;
15577                 }
15578
15579               count += NUM_SHDR_ENTRIES (input_reldata->hdr);
15580             }
15581         }
15582     }
15583
15584   reldata->count = count;
15585   rel_hdr->sh_size = count * rel_hdr->sh_entsize;
15586
15587   erela = erela_head;
15588   irela = irela_head;
15589   while (count > 0)
15590     {
15591       (*swap_out) (abfd, irela, erela);
15592       erela += rel_hdr->sh_entsize;
15593       irela++;
15594       count--;
15595     }
15596
15597   free (irela_head);
15598
15599   /* Hashes are no longer valid.  */
15600   free (reldata->hashes);
15601   reldata->hashes = NULL;
15602 }
15603
15604 /* Unwinding tables are not referenced directly.  This pass marks them as
15605    required if the corresponding code section is marked.  Similarly, ARMv8-M
15606    secure entry functions can only be referenced by SG veneers which are
15607    created after the GC process. They need to be marked in case they reside in
15608    their own section (as would be the case if code was compiled with
15609    -ffunction-sections).  */
15610
15611 static bfd_boolean
15612 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
15613                                   elf_gc_mark_hook_fn gc_mark_hook)
15614 {
15615   bfd *sub;
15616   Elf_Internal_Shdr **elf_shdrp;
15617   asection *cmse_sec;
15618   obj_attribute *out_attr;
15619   Elf_Internal_Shdr *symtab_hdr;
15620   unsigned i, sym_count, ext_start;
15621   const struct elf_backend_data *bed;
15622   struct elf_link_hash_entry **sym_hashes;
15623   struct elf32_arm_link_hash_entry *cmse_hash;
15624   bfd_boolean again, is_v8m, first_bfd_browse = TRUE;
15625
15626   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15627
15628   out_attr = elf_known_obj_attributes_proc (info->output_bfd);
15629   is_v8m = out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
15630            && out_attr[Tag_CPU_arch_profile].i == 'M';
15631
15632   /* Marking EH data may cause additional code sections to be marked,
15633      requiring multiple passes.  */
15634   again = TRUE;
15635   while (again)
15636     {
15637       again = FALSE;
15638       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
15639         {
15640           asection *o;
15641
15642           if (! is_arm_elf (sub))
15643             continue;
15644
15645           elf_shdrp = elf_elfsections (sub);
15646           for (o = sub->sections; o != NULL; o = o->next)
15647             {
15648               Elf_Internal_Shdr *hdr;
15649
15650               hdr = &elf_section_data (o)->this_hdr;
15651               if (hdr->sh_type == SHT_ARM_EXIDX
15652                   && hdr->sh_link
15653                   && hdr->sh_link < elf_numsections (sub)
15654                   && !o->gc_mark
15655                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
15656                 {
15657                   again = TRUE;
15658                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
15659                     return FALSE;
15660                 }
15661             }
15662
15663           /* Mark section holding ARMv8-M secure entry functions.  We mark all
15664              of them so no need for a second browsing.  */
15665           if (is_v8m && first_bfd_browse)
15666             {
15667               sym_hashes = elf_sym_hashes (sub);
15668               bed = get_elf_backend_data (sub);
15669               symtab_hdr = &elf_tdata (sub)->symtab_hdr;
15670               sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
15671               ext_start = symtab_hdr->sh_info;
15672
15673               /* Scan symbols.  */
15674               for (i = ext_start; i < sym_count; i++)
15675                 {
15676                   cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
15677
15678                   /* Assume it is a special symbol.  If not, cmse_scan will
15679                      warn about it and user can do something about it.  */
15680                   if (ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
15681                     {
15682                       cmse_sec = cmse_hash->root.root.u.def.section;
15683                       if (!cmse_sec->gc_mark
15684                           && !_bfd_elf_gc_mark (info, cmse_sec, gc_mark_hook))
15685                         return FALSE;
15686                     }
15687                 }
15688             }
15689         }
15690       first_bfd_browse = FALSE;
15691     }
15692
15693   return TRUE;
15694 }
15695
15696 /* Treat mapping symbols as special target symbols.  */
15697
15698 static bfd_boolean
15699 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
15700 {
15701   return bfd_is_arm_special_symbol_name (sym->name,
15702                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
15703 }
15704
15705 /* This is a copy of elf_find_function() from elf.c except that
15706    ARM mapping symbols are ignored when looking for function names
15707    and STT_ARM_TFUNC is considered to a function type.  */
15708
15709 static bfd_boolean
15710 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
15711                        asymbol **    symbols,
15712                        asection *    section,
15713                        bfd_vma       offset,
15714                        const char ** filename_ptr,
15715                        const char ** functionname_ptr)
15716 {
15717   const char * filename = NULL;
15718   asymbol * func = NULL;
15719   bfd_vma low_func = 0;
15720   asymbol ** p;
15721
15722   for (p = symbols; *p != NULL; p++)
15723     {
15724       elf_symbol_type *q;
15725
15726       q = (elf_symbol_type *) *p;
15727
15728       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
15729         {
15730         default:
15731           break;
15732         case STT_FILE:
15733           filename = bfd_asymbol_name (&q->symbol);
15734           break;
15735         case STT_FUNC:
15736         case STT_ARM_TFUNC:
15737         case STT_NOTYPE:
15738           /* Skip mapping symbols.  */
15739           if ((q->symbol.flags & BSF_LOCAL)
15740               && bfd_is_arm_special_symbol_name (q->symbol.name,
15741                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
15742             continue;
15743           /* Fall through.  */
15744           if (bfd_get_section (&q->symbol) == section
15745               && q->symbol.value >= low_func
15746               && q->symbol.value <= offset)
15747             {
15748               func = (asymbol *) q;
15749               low_func = q->symbol.value;
15750             }
15751           break;
15752         }
15753     }
15754
15755   if (func == NULL)
15756     return FALSE;
15757
15758   if (filename_ptr)
15759     *filename_ptr = filename;
15760   if (functionname_ptr)
15761     *functionname_ptr = bfd_asymbol_name (func);
15762
15763   return TRUE;
15764 }
15765
15766
15767 /* Find the nearest line to a particular section and offset, for error
15768    reporting.   This code is a duplicate of the code in elf.c, except
15769    that it uses arm_elf_find_function.  */
15770
15771 static bfd_boolean
15772 elf32_arm_find_nearest_line (bfd *          abfd,
15773                              asymbol **     symbols,
15774                              asection *     section,
15775                              bfd_vma        offset,
15776                              const char **  filename_ptr,
15777                              const char **  functionname_ptr,
15778                              unsigned int * line_ptr,
15779                              unsigned int * discriminator_ptr)
15780 {
15781   bfd_boolean found = FALSE;
15782
15783   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
15784                                      filename_ptr, functionname_ptr,
15785                                      line_ptr, discriminator_ptr,
15786                                      dwarf_debug_sections, 0,
15787                                      & elf_tdata (abfd)->dwarf2_find_line_info))
15788     {
15789       if (!*functionname_ptr)
15790         arm_elf_find_function (abfd, symbols, section, offset,
15791                                *filename_ptr ? NULL : filename_ptr,
15792                                functionname_ptr);
15793
15794       return TRUE;
15795     }
15796
15797   /* Skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain
15798      uses DWARF1.  */
15799
15800   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
15801                                              & found, filename_ptr,
15802                                              functionname_ptr, line_ptr,
15803                                              & elf_tdata (abfd)->line_info))
15804     return FALSE;
15805
15806   if (found && (*functionname_ptr || *line_ptr))
15807     return TRUE;
15808
15809   if (symbols == NULL)
15810     return FALSE;
15811
15812   if (! arm_elf_find_function (abfd, symbols, section, offset,
15813                                filename_ptr, functionname_ptr))
15814     return FALSE;
15815
15816   *line_ptr = 0;
15817   return TRUE;
15818 }
15819
15820 static bfd_boolean
15821 elf32_arm_find_inliner_info (bfd *          abfd,
15822                              const char **  filename_ptr,
15823                              const char **  functionname_ptr,
15824                              unsigned int * line_ptr)
15825 {
15826   bfd_boolean found;
15827   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
15828                                          functionname_ptr, line_ptr,
15829                                          & elf_tdata (abfd)->dwarf2_find_line_info);
15830   return found;
15831 }
15832
15833 /* Find dynamic relocs for H that apply to read-only sections.  */
15834
15835 static asection *
15836 readonly_dynrelocs (struct elf_link_hash_entry *h)
15837 {
15838   struct elf_dyn_relocs *p;
15839
15840   for (p = elf32_arm_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
15841     {
15842       asection *s = p->sec->output_section;
15843
15844       if (s != NULL && (s->flags & SEC_READONLY) != 0)
15845         return p->sec;
15846     }
15847   return NULL;
15848 }
15849
15850 /* Adjust a symbol defined by a dynamic object and referenced by a
15851    regular object.  The current definition is in some section of the
15852    dynamic object, but we're not including those sections.  We have to
15853    change the definition to something the rest of the link can
15854    understand.  */
15855
15856 static bfd_boolean
15857 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
15858                                  struct elf_link_hash_entry * h)
15859 {
15860   bfd * dynobj;
15861   asection *s, *srel;
15862   struct elf32_arm_link_hash_entry * eh;
15863   struct elf32_arm_link_hash_table *globals;
15864
15865   globals = elf32_arm_hash_table (info);
15866   if (globals == NULL)
15867     return FALSE;
15868
15869   dynobj = elf_hash_table (info)->dynobj;
15870
15871   /* Make sure we know what is going on here.  */
15872   BFD_ASSERT (dynobj != NULL
15873               && (h->needs_plt
15874                   || h->type == STT_GNU_IFUNC
15875                   || h->is_weakalias
15876                   || (h->def_dynamic
15877                       && h->ref_regular
15878                       && !h->def_regular)));
15879
15880   eh = (struct elf32_arm_link_hash_entry *) h;
15881
15882   /* If this is a function, put it in the procedure linkage table.  We
15883      will fill in the contents of the procedure linkage table later,
15884      when we know the address of the .got section.  */
15885   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
15886     {
15887       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
15888          symbol binds locally.  */
15889       if (h->plt.refcount <= 0
15890           || (h->type != STT_GNU_IFUNC
15891               && (SYMBOL_CALLS_LOCAL (info, h)
15892                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
15893                       && h->root.type == bfd_link_hash_undefweak))))
15894         {
15895           /* This case can occur if we saw a PLT32 reloc in an input
15896              file, but the symbol was never referred to by a dynamic
15897              object, or if all references were garbage collected.  In
15898              such a case, we don't actually need to build a procedure
15899              linkage table, and we can just do a PC24 reloc instead.  */
15900           h->plt.offset = (bfd_vma) -1;
15901           eh->plt.thumb_refcount = 0;
15902           eh->plt.maybe_thumb_refcount = 0;
15903           eh->plt.noncall_refcount = 0;
15904           h->needs_plt = 0;
15905         }
15906
15907       return TRUE;
15908     }
15909   else
15910     {
15911       /* It's possible that we incorrectly decided a .plt reloc was
15912          needed for an R_ARM_PC24 or similar reloc to a non-function sym
15913          in check_relocs.  We can't decide accurately between function
15914          and non-function syms in check-relocs; Objects loaded later in
15915          the link may change h->type.  So fix it now.  */
15916       h->plt.offset = (bfd_vma) -1;
15917       eh->plt.thumb_refcount = 0;
15918       eh->plt.maybe_thumb_refcount = 0;
15919       eh->plt.noncall_refcount = 0;
15920     }
15921
15922   /* If this is a weak symbol, and there is a real definition, the
15923      processor independent code will have arranged for us to see the
15924      real definition first, and we can just use the same value.  */
15925   if (h->is_weakalias)
15926     {
15927       struct elf_link_hash_entry *def = weakdef (h);
15928       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
15929       h->root.u.def.section = def->root.u.def.section;
15930       h->root.u.def.value = def->root.u.def.value;
15931       return TRUE;
15932     }
15933
15934   /* If there are no non-GOT references, we do not need a copy
15935      relocation.  */
15936   if (!h->non_got_ref)
15937     return TRUE;
15938
15939   /* This is a reference to a symbol defined by a dynamic object which
15940      is not a function.  */
15941
15942   /* If we are creating a shared library, we must presume that the
15943      only references to the symbol are via the global offset table.
15944      For such cases we need not do anything here; the relocations will
15945      be handled correctly by relocate_section.  Relocatable executables
15946      can reference data in shared objects directly, so we don't need to
15947      do anything here.  */
15948   if (bfd_link_pic (info) || globals->root.is_relocatable_executable)
15949     return TRUE;
15950
15951   /* We must allocate the symbol in our .dynbss section, which will
15952      become part of the .bss section of the executable.  There will be
15953      an entry for this symbol in the .dynsym section.  The dynamic
15954      object will contain position independent code, so all references
15955      from the dynamic object to this symbol will go through the global
15956      offset table.  The dynamic linker will use the .dynsym entry to
15957      determine the address it must put in the global offset table, so
15958      both the dynamic object and the regular object will refer to the
15959      same memory location for the variable.  */
15960   /* If allowed, we must generate a R_ARM_COPY reloc to tell the dynamic
15961      linker to copy the initial value out of the dynamic object and into
15962      the runtime process image.  We need to remember the offset into the
15963      .rel(a).bss section we are going to use.  */
15964   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
15965     {
15966       s = globals->root.sdynrelro;
15967       srel = globals->root.sreldynrelro;
15968     }
15969   else
15970     {
15971       s = globals->root.sdynbss;
15972       srel = globals->root.srelbss;
15973     }
15974   if (info->nocopyreloc == 0
15975       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
15976       && h->size != 0)
15977     {
15978       elf32_arm_allocate_dynrelocs (info, srel, 1);
15979       h->needs_copy = 1;
15980     }
15981
15982   return _bfd_elf_adjust_dynamic_copy (info, h, s);
15983 }
15984
15985 /* Allocate space in .plt, .got and associated reloc sections for
15986    dynamic relocs.  */
15987
15988 static bfd_boolean
15989 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
15990 {
15991   struct bfd_link_info *info;
15992   struct elf32_arm_link_hash_table *htab;
15993   struct elf32_arm_link_hash_entry *eh;
15994   struct elf_dyn_relocs *p;
15995
15996   if (h->root.type == bfd_link_hash_indirect)
15997     return TRUE;
15998
15999   eh = (struct elf32_arm_link_hash_entry *) h;
16000
16001   info = (struct bfd_link_info *) inf;
16002   htab = elf32_arm_hash_table (info);
16003   if (htab == NULL)
16004     return FALSE;
16005
16006   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
16007       && h->plt.refcount > 0)
16008     {
16009       /* Make sure this symbol is output as a dynamic symbol.
16010          Undefined weak syms won't yet be marked as dynamic.  */
16011       if (h->dynindx == -1 && !h->forced_local
16012           && h->root.type == bfd_link_hash_undefweak)
16013         {
16014           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16015             return FALSE;
16016         }
16017
16018       /* If the call in the PLT entry binds locally, the associated
16019          GOT entry should use an R_ARM_IRELATIVE relocation instead of
16020          the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
16021          than the .plt section.  */
16022       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
16023         {
16024           eh->is_iplt = 1;
16025           if (eh->plt.noncall_refcount == 0
16026               && SYMBOL_REFERENCES_LOCAL (info, h))
16027             /* All non-call references can be resolved directly.
16028                This means that they can (and in some cases, must)
16029                resolve directly to the run-time target, rather than
16030                to the PLT.  That in turns means that any .got entry
16031                would be equal to the .igot.plt entry, so there's
16032                no point having both.  */
16033             h->got.refcount = 0;
16034         }
16035
16036       if (bfd_link_pic (info)
16037           || eh->is_iplt
16038           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
16039         {
16040           elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
16041
16042           /* If this symbol is not defined in a regular file, and we are
16043              not generating a shared library, then set the symbol to this
16044              location in the .plt.  This is required to make function
16045              pointers compare as equal between the normal executable and
16046              the shared library.  */
16047           if (! bfd_link_pic (info)
16048               && !h->def_regular)
16049             {
16050               h->root.u.def.section = htab->root.splt;
16051               h->root.u.def.value = h->plt.offset;
16052
16053               /* Make sure the function is not marked as Thumb, in case
16054                  it is the target of an ABS32 relocation, which will
16055                  point to the PLT entry.  */
16056               ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16057             }
16058
16059           /* VxWorks executables have a second set of relocations for
16060              each PLT entry.  They go in a separate relocation section,
16061              which is processed by the kernel loader.  */
16062           if (htab->vxworks_p && !bfd_link_pic (info))
16063             {
16064               /* There is a relocation for the initial PLT entry:
16065                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
16066               if (h->plt.offset == htab->plt_header_size)
16067                 elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
16068
16069               /* There are two extra relocations for each subsequent
16070                  PLT entry: an R_ARM_32 relocation for the GOT entry,
16071                  and an R_ARM_32 relocation for the PLT entry.  */
16072               elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
16073             }
16074         }
16075       else
16076         {
16077           h->plt.offset = (bfd_vma) -1;
16078           h->needs_plt = 0;
16079         }
16080     }
16081   else
16082     {
16083       h->plt.offset = (bfd_vma) -1;
16084       h->needs_plt = 0;
16085     }
16086
16087   eh = (struct elf32_arm_link_hash_entry *) h;
16088   eh->tlsdesc_got = (bfd_vma) -1;
16089
16090   if (h->got.refcount > 0)
16091     {
16092       asection *s;
16093       bfd_boolean dyn;
16094       int tls_type = elf32_arm_hash_entry (h)->tls_type;
16095       int indx;
16096
16097       /* Make sure this symbol is output as a dynamic symbol.
16098          Undefined weak syms won't yet be marked as dynamic.  */
16099       if (htab->root.dynamic_sections_created && h->dynindx == -1 && !h->forced_local
16100           && h->root.type == bfd_link_hash_undefweak)
16101         {
16102           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16103             return FALSE;
16104         }
16105
16106       if (!htab->symbian_p)
16107         {
16108           s = htab->root.sgot;
16109           h->got.offset = s->size;
16110
16111           if (tls_type == GOT_UNKNOWN)
16112             abort ();
16113
16114           if (tls_type == GOT_NORMAL)
16115             /* Non-TLS symbols need one GOT slot.  */
16116             s->size += 4;
16117           else
16118             {
16119               if (tls_type & GOT_TLS_GDESC)
16120                 {
16121                   /* R_ARM_TLS_DESC needs 2 GOT slots.  */
16122                   eh->tlsdesc_got
16123                     = (htab->root.sgotplt->size
16124                        - elf32_arm_compute_jump_table_size (htab));
16125                   htab->root.sgotplt->size += 8;
16126                   h->got.offset = (bfd_vma) -2;
16127                   /* plt.got_offset needs to know there's a TLS_DESC
16128                      reloc in the middle of .got.plt.  */
16129                   htab->num_tls_desc++;
16130                 }
16131
16132               if (tls_type & GOT_TLS_GD)
16133                 {
16134                   /* R_ARM_TLS_GD32 and R_ARM_TLS_GD32_FDPIC need two
16135                      consecutive GOT slots.  If the symbol is both GD
16136                      and GDESC, got.offset may have been
16137                      overwritten.  */
16138                   h->got.offset = s->size;
16139                   s->size += 8;
16140                 }
16141
16142               if (tls_type & GOT_TLS_IE)
16143                 /* R_ARM_TLS_IE32/R_ARM_TLS_IE32_FDPIC need one GOT
16144                    slot.  */
16145                 s->size += 4;
16146             }
16147
16148           dyn = htab->root.dynamic_sections_created;
16149
16150           indx = 0;
16151           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
16152                                                bfd_link_pic (info),
16153                                                h)
16154               && (!bfd_link_pic (info)
16155                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
16156             indx = h->dynindx;
16157
16158           if (tls_type != GOT_NORMAL
16159               && (bfd_link_pic (info) || indx != 0)
16160               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16161                   || h->root.type != bfd_link_hash_undefweak))
16162             {
16163               if (tls_type & GOT_TLS_IE)
16164                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16165
16166               if (tls_type & GOT_TLS_GD)
16167                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16168
16169               if (tls_type & GOT_TLS_GDESC)
16170                 {
16171                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
16172                   /* GDESC needs a trampoline to jump to.  */
16173                   htab->tls_trampoline = -1;
16174                 }
16175
16176               /* Only GD needs it.  GDESC just emits one relocation per
16177                  2 entries.  */
16178               if ((tls_type & GOT_TLS_GD) && indx != 0)
16179                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16180             }
16181           else if (((indx != -1) || htab->fdpic_p)
16182                    && !SYMBOL_REFERENCES_LOCAL (info, h))
16183             {
16184               if (htab->root.dynamic_sections_created)
16185                 /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
16186                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16187             }
16188           else if (h->type == STT_GNU_IFUNC
16189                    && eh->plt.noncall_refcount == 0)
16190             /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
16191                they all resolve dynamically instead.  Reserve room for the
16192                GOT entry's R_ARM_IRELATIVE relocation.  */
16193             elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
16194           else if (bfd_link_pic (info)
16195                    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16196                        || h->root.type != bfd_link_hash_undefweak))
16197             /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
16198             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16199           else if (htab->fdpic_p && tls_type == GOT_NORMAL)
16200             /* Reserve room for rofixup for FDPIC executable.  */
16201             /* TLS relocs do not need space since they are completely
16202                resolved.  */
16203             htab->srofixup->size += 4;
16204         }
16205     }
16206   else
16207     h->got.offset = (bfd_vma) -1;
16208
16209   /* FDPIC support.  */
16210   if (eh->fdpic_cnts.gotofffuncdesc_cnt > 0)
16211     {
16212       /* Symbol musn't be exported.  */
16213       if (h->dynindx != -1)
16214         abort();
16215
16216       /* We only allocate one function descriptor with its associated relocation.  */
16217       if (eh->fdpic_cnts.funcdesc_offset == -1)
16218         {
16219           asection *s = htab->root.sgot;
16220
16221           eh->fdpic_cnts.funcdesc_offset = s->size;
16222           s->size += 8;
16223           /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16224           if (bfd_link_pic(info))
16225             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16226           else
16227             htab->srofixup->size += 8;
16228         }
16229     }
16230
16231   if (eh->fdpic_cnts.gotfuncdesc_cnt > 0)
16232     {
16233       asection *s = htab->root.sgot;
16234
16235       if (htab->root.dynamic_sections_created && h->dynindx == -1
16236           && !h->forced_local)
16237         if (! bfd_elf_link_record_dynamic_symbol (info, h))
16238           return FALSE;
16239
16240       if (h->dynindx == -1)
16241         {
16242           /* We only allocate one function descriptor with its associated relocation. q */
16243           if (eh->fdpic_cnts.funcdesc_offset == -1)
16244             {
16245
16246               eh->fdpic_cnts.funcdesc_offset = s->size;
16247               s->size += 8;
16248               /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16249               if (bfd_link_pic(info))
16250                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16251               else
16252                 htab->srofixup->size += 8;
16253             }
16254         }
16255
16256       /* Add one entry into the GOT and a R_ARM_FUNCDESC or
16257          R_ARM_RELATIVE/rofixup relocation on it.  */
16258       eh->fdpic_cnts.gotfuncdesc_offset = s->size;
16259       s->size += 4;
16260       if (h->dynindx == -1 && !bfd_link_pic(info))
16261         htab->srofixup->size += 4;
16262       else
16263         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16264     }
16265
16266   if (eh->fdpic_cnts.funcdesc_cnt > 0)
16267     {
16268       if (htab->root.dynamic_sections_created && h->dynindx == -1
16269           && !h->forced_local)
16270         if (! bfd_elf_link_record_dynamic_symbol (info, h))
16271           return FALSE;
16272
16273       if (h->dynindx == -1)
16274         {
16275           /* We only allocate one function descriptor with its associated relocation.  */
16276           if (eh->fdpic_cnts.funcdesc_offset == -1)
16277             {
16278               asection *s = htab->root.sgot;
16279
16280               eh->fdpic_cnts.funcdesc_offset = s->size;
16281               s->size += 8;
16282               /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16283               if (bfd_link_pic(info))
16284                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16285               else
16286                 htab->srofixup->size += 8;
16287             }
16288         }
16289       if (h->dynindx == -1 && !bfd_link_pic(info))
16290         {
16291           /* For FDPIC executable we replace R_ARM_RELATIVE with a rofixup.  */
16292           htab->srofixup->size += 4 * eh->fdpic_cnts.funcdesc_cnt;
16293         }
16294       else
16295         {
16296           /* Will need one dynamic reloc per reference. will be either
16297              R_ARM_FUNCDESC or R_ARM_RELATIVE for hidden symbols.  */
16298           elf32_arm_allocate_dynrelocs (info, htab->root.srelgot,
16299                                         eh->fdpic_cnts.funcdesc_cnt);
16300         }
16301     }
16302
16303   /* Allocate stubs for exported Thumb functions on v4t.  */
16304   if (!htab->use_blx && h->dynindx != -1
16305       && h->def_regular
16306       && ARM_GET_SYM_BRANCH_TYPE (h->target_internal) == ST_BRANCH_TO_THUMB
16307       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
16308     {
16309       struct elf_link_hash_entry * th;
16310       struct bfd_link_hash_entry * bh;
16311       struct elf_link_hash_entry * myh;
16312       char name[1024];
16313       asection *s;
16314       bh = NULL;
16315       /* Create a new symbol to regist the real location of the function.  */
16316       s = h->root.u.def.section;
16317       sprintf (name, "__real_%s", h->root.root.string);
16318       _bfd_generic_link_add_one_symbol (info, s->owner,
16319                                         name, BSF_GLOBAL, s,
16320                                         h->root.u.def.value,
16321                                         NULL, TRUE, FALSE, &bh);
16322
16323       myh = (struct elf_link_hash_entry *) bh;
16324       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
16325       myh->forced_local = 1;
16326       ARM_SET_SYM_BRANCH_TYPE (myh->target_internal, ST_BRANCH_TO_THUMB);
16327       eh->export_glue = myh;
16328       th = record_arm_to_thumb_glue (info, h);
16329       /* Point the symbol at the stub.  */
16330       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
16331       ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16332       h->root.u.def.section = th->root.u.def.section;
16333       h->root.u.def.value = th->root.u.def.value & ~1;
16334     }
16335
16336   if (eh->dyn_relocs == NULL)
16337     return TRUE;
16338
16339   /* In the shared -Bsymbolic case, discard space allocated for
16340      dynamic pc-relative relocs against symbols which turn out to be
16341      defined in regular objects.  For the normal shared case, discard
16342      space for pc-relative relocs that have become local due to symbol
16343      visibility changes.  */
16344
16345   if (bfd_link_pic (info) || htab->root.is_relocatable_executable || htab->fdpic_p)
16346     {
16347       /* Relocs that use pc_count are PC-relative forms, which will appear
16348          on something like ".long foo - ." or "movw REG, foo - .".  We want
16349          calls to protected symbols to resolve directly to the function
16350          rather than going via the plt.  If people want function pointer
16351          comparisons to work as expected then they should avoid writing
16352          assembly like ".long foo - .".  */
16353       if (SYMBOL_CALLS_LOCAL (info, h))
16354         {
16355           struct elf_dyn_relocs **pp;
16356
16357           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
16358             {
16359               p->count -= p->pc_count;
16360               p->pc_count = 0;
16361               if (p->count == 0)
16362                 *pp = p->next;
16363               else
16364                 pp = &p->next;
16365             }
16366         }
16367
16368       if (htab->vxworks_p)
16369         {
16370           struct elf_dyn_relocs **pp;
16371
16372           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
16373             {
16374               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
16375                 *pp = p->next;
16376               else
16377                 pp = &p->next;
16378             }
16379         }
16380
16381       /* Also discard relocs on undefined weak syms with non-default
16382          visibility.  */
16383       if (eh->dyn_relocs != NULL
16384           && h->root.type == bfd_link_hash_undefweak)
16385         {
16386           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16387               || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16388             eh->dyn_relocs = NULL;
16389
16390           /* Make sure undefined weak symbols are output as a dynamic
16391              symbol in PIEs.  */
16392           else if (htab->root.dynamic_sections_created && h->dynindx == -1
16393                    && !h->forced_local)
16394             {
16395               if (! bfd_elf_link_record_dynamic_symbol (info, h))
16396                 return FALSE;
16397             }
16398         }
16399
16400       else if (htab->root.is_relocatable_executable && h->dynindx == -1
16401                && h->root.type == bfd_link_hash_new)
16402         {
16403           /* Output absolute symbols so that we can create relocations
16404              against them.  For normal symbols we output a relocation
16405              against the section that contains them.  */
16406           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16407             return FALSE;
16408         }
16409
16410     }
16411   else
16412     {
16413       /* For the non-shared case, discard space for relocs against
16414          symbols which turn out to need copy relocs or are not
16415          dynamic.  */
16416
16417       if (!h->non_got_ref
16418           && ((h->def_dynamic
16419                && !h->def_regular)
16420               || (htab->root.dynamic_sections_created
16421                   && (h->root.type == bfd_link_hash_undefweak
16422                       || h->root.type == bfd_link_hash_undefined))))
16423         {
16424           /* Make sure this symbol is output as a dynamic symbol.
16425              Undefined weak syms won't yet be marked as dynamic.  */
16426           if (h->dynindx == -1 && !h->forced_local
16427               && h->root.type == bfd_link_hash_undefweak)
16428             {
16429               if (! bfd_elf_link_record_dynamic_symbol (info, h))
16430                 return FALSE;
16431             }
16432
16433           /* If that succeeded, we know we'll be keeping all the
16434              relocs.  */
16435           if (h->dynindx != -1)
16436             goto keep;
16437         }
16438
16439       eh->dyn_relocs = NULL;
16440
16441     keep: ;
16442     }
16443
16444   /* Finally, allocate space.  */
16445   for (p = eh->dyn_relocs; p != NULL; p = p->next)
16446     {
16447       asection *sreloc = elf_section_data (p->sec)->sreloc;
16448
16449       if (h->type == STT_GNU_IFUNC
16450           && eh->plt.noncall_refcount == 0
16451           && SYMBOL_REFERENCES_LOCAL (info, h))
16452         elf32_arm_allocate_irelocs (info, sreloc, p->count);
16453       else if (h->dynindx != -1 && (!bfd_link_pic(info) || !info->symbolic || !h->def_regular))
16454         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16455       else if (htab->fdpic_p && !bfd_link_pic(info))
16456         htab->srofixup->size += 4 * p->count;
16457       else
16458         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16459     }
16460
16461   return TRUE;
16462 }
16463
16464 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
16465    read-only sections.  */
16466
16467 static bfd_boolean
16468 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
16469 {
16470   asection *sec;
16471
16472   if (h->root.type == bfd_link_hash_indirect)
16473     return TRUE;
16474
16475   sec = readonly_dynrelocs (h);
16476   if (sec != NULL)
16477     {
16478       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
16479
16480       info->flags |= DF_TEXTREL;
16481       info->callbacks->minfo
16482         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
16483          sec->owner, h->root.root.string, sec);
16484
16485       /* Not an error, just cut short the traversal.  */
16486       return FALSE;
16487     }
16488   return TRUE;
16489 }
16490
16491 void
16492 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
16493                                  int byteswap_code)
16494 {
16495   struct elf32_arm_link_hash_table *globals;
16496
16497   globals = elf32_arm_hash_table (info);
16498   if (globals == NULL)
16499     return;
16500
16501   globals->byteswap_code = byteswap_code;
16502 }
16503
16504 /* Set the sizes of the dynamic sections.  */
16505
16506 static bfd_boolean
16507 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
16508                                  struct bfd_link_info * info)
16509 {
16510   bfd * dynobj;
16511   asection * s;
16512   bfd_boolean plt;
16513   bfd_boolean relocs;
16514   bfd *ibfd;
16515   struct elf32_arm_link_hash_table *htab;
16516
16517   htab = elf32_arm_hash_table (info);
16518   if (htab == NULL)
16519     return FALSE;
16520
16521   dynobj = elf_hash_table (info)->dynobj;
16522   BFD_ASSERT (dynobj != NULL);
16523   check_use_blx (htab);
16524
16525   if (elf_hash_table (info)->dynamic_sections_created)
16526     {
16527       /* Set the contents of the .interp section to the interpreter.  */
16528       if (bfd_link_executable (info) && !info->nointerp)
16529         {
16530           s = bfd_get_linker_section (dynobj, ".interp");
16531           BFD_ASSERT (s != NULL);
16532           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
16533           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
16534         }
16535     }
16536
16537   /* Set up .got offsets for local syms, and space for local dynamic
16538      relocs.  */
16539   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16540     {
16541       bfd_signed_vma *local_got;
16542       bfd_signed_vma *end_local_got;
16543       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
16544       char *local_tls_type;
16545       bfd_vma *local_tlsdesc_gotent;
16546       bfd_size_type locsymcount;
16547       Elf_Internal_Shdr *symtab_hdr;
16548       asection *srel;
16549       bfd_boolean is_vxworks = htab->vxworks_p;
16550       unsigned int symndx;
16551       struct fdpic_local *local_fdpic_cnts;
16552
16553       if (! is_arm_elf (ibfd))
16554         continue;
16555
16556       for (s = ibfd->sections; s != NULL; s = s->next)
16557         {
16558           struct elf_dyn_relocs *p;
16559
16560           for (p = (struct elf_dyn_relocs *)
16561                    elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
16562             {
16563               if (!bfd_is_abs_section (p->sec)
16564                   && bfd_is_abs_section (p->sec->output_section))
16565                 {
16566                   /* Input section has been discarded, either because
16567                      it is a copy of a linkonce section or due to
16568                      linker script /DISCARD/, so we'll be discarding
16569                      the relocs too.  */
16570                 }
16571               else if (is_vxworks
16572                        && strcmp (p->sec->output_section->name,
16573                                   ".tls_vars") == 0)
16574                 {
16575                   /* Relocations in vxworks .tls_vars sections are
16576                      handled specially by the loader.  */
16577                 }
16578               else if (p->count != 0)
16579                 {
16580                   srel = elf_section_data (p->sec)->sreloc;
16581                   if (htab->fdpic_p && !bfd_link_pic(info))
16582                     htab->srofixup->size += 4 * p->count;
16583                   else
16584                     elf32_arm_allocate_dynrelocs (info, srel, p->count);
16585                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
16586                     info->flags |= DF_TEXTREL;
16587                 }
16588             }
16589         }
16590
16591       local_got = elf_local_got_refcounts (ibfd);
16592       if (!local_got)
16593         continue;
16594
16595       symtab_hdr = & elf_symtab_hdr (ibfd);
16596       locsymcount = symtab_hdr->sh_info;
16597       end_local_got = local_got + locsymcount;
16598       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
16599       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
16600       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
16601       local_fdpic_cnts = elf32_arm_local_fdpic_cnts (ibfd);
16602       symndx = 0;
16603       s = htab->root.sgot;
16604       srel = htab->root.srelgot;
16605       for (; local_got < end_local_got;
16606            ++local_got, ++local_iplt_ptr, ++local_tls_type,
16607            ++local_tlsdesc_gotent, ++symndx, ++local_fdpic_cnts)
16608         {
16609           *local_tlsdesc_gotent = (bfd_vma) -1;
16610           local_iplt = *local_iplt_ptr;
16611
16612           /* FDPIC support.  */
16613           if (local_fdpic_cnts->gotofffuncdesc_cnt > 0)
16614             {
16615               if (local_fdpic_cnts->funcdesc_offset == -1)
16616                 {
16617                   local_fdpic_cnts->funcdesc_offset = s->size;
16618                   s->size += 8;
16619
16620                   /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16621                   if (bfd_link_pic(info))
16622                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16623                   else
16624                     htab->srofixup->size += 8;
16625                 }
16626             }
16627
16628           if (local_fdpic_cnts->funcdesc_cnt > 0)
16629             {
16630               if (local_fdpic_cnts->funcdesc_offset == -1)
16631                 {
16632                   local_fdpic_cnts->funcdesc_offset = s->size;
16633                   s->size += 8;
16634
16635                   /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16636                   if (bfd_link_pic(info))
16637                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16638                   else
16639                     htab->srofixup->size += 8;
16640                 }
16641
16642               /* We will add n R_ARM_RELATIVE relocations or n rofixups.  */
16643               if (bfd_link_pic(info))
16644                 elf32_arm_allocate_dynrelocs (info, srel, local_fdpic_cnts->funcdesc_cnt);
16645               else
16646                 htab->srofixup->size += 4 * local_fdpic_cnts->funcdesc_cnt;
16647             }
16648
16649           if (local_iplt != NULL)
16650             {
16651               struct elf_dyn_relocs *p;
16652
16653               if (local_iplt->root.refcount > 0)
16654                 {
16655                   elf32_arm_allocate_plt_entry (info, TRUE,
16656                                                 &local_iplt->root,
16657                                                 &local_iplt->arm);
16658                   if (local_iplt->arm.noncall_refcount == 0)
16659                     /* All references to the PLT are calls, so all
16660                        non-call references can resolve directly to the
16661                        run-time target.  This means that the .got entry
16662                        would be the same as the .igot.plt entry, so there's
16663                        no point creating both.  */
16664                     *local_got = 0;
16665                 }
16666               else
16667                 {
16668                   BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
16669                   local_iplt->root.offset = (bfd_vma) -1;
16670                 }
16671
16672               for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
16673                 {
16674                   asection *psrel;
16675
16676                   psrel = elf_section_data (p->sec)->sreloc;
16677                   if (local_iplt->arm.noncall_refcount == 0)
16678                     elf32_arm_allocate_irelocs (info, psrel, p->count);
16679                   else
16680                     elf32_arm_allocate_dynrelocs (info, psrel, p->count);
16681                 }
16682             }
16683           if (*local_got > 0)
16684             {
16685               Elf_Internal_Sym *isym;
16686
16687               *local_got = s->size;
16688               if (*local_tls_type & GOT_TLS_GD)
16689                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
16690                 s->size += 8;
16691               if (*local_tls_type & GOT_TLS_GDESC)
16692                 {
16693                   *local_tlsdesc_gotent = htab->root.sgotplt->size
16694                     - elf32_arm_compute_jump_table_size (htab);
16695                   htab->root.sgotplt->size += 8;
16696                   *local_got = (bfd_vma) -2;
16697                   /* plt.got_offset needs to know there's a TLS_DESC
16698                      reloc in the middle of .got.plt.  */
16699                   htab->num_tls_desc++;
16700                 }
16701               if (*local_tls_type & GOT_TLS_IE)
16702                 s->size += 4;
16703
16704               if (*local_tls_type & GOT_NORMAL)
16705                 {
16706                   /* If the symbol is both GD and GDESC, *local_got
16707                      may have been overwritten.  */
16708                   *local_got = s->size;
16709                   s->size += 4;
16710                 }
16711
16712               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
16713               if (isym == NULL)
16714                 return FALSE;
16715
16716               /* If all references to an STT_GNU_IFUNC PLT are calls,
16717                  then all non-call references, including this GOT entry,
16718                  resolve directly to the run-time target.  */
16719               if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
16720                   && (local_iplt == NULL
16721                       || local_iplt->arm.noncall_refcount == 0))
16722                 elf32_arm_allocate_irelocs (info, srel, 1);
16723               else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC || htab->fdpic_p)
16724                 {
16725                   if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC)))
16726                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16727                   else if (htab->fdpic_p && *local_tls_type & GOT_NORMAL)
16728                     htab->srofixup->size += 4;
16729
16730                   if ((bfd_link_pic (info) || htab->fdpic_p)
16731                       && *local_tls_type & GOT_TLS_GDESC)
16732                     {
16733                       elf32_arm_allocate_dynrelocs (info,
16734                                                     htab->root.srelplt, 1);
16735                       htab->tls_trampoline = -1;
16736                     }
16737                 }
16738             }
16739           else
16740             *local_got = (bfd_vma) -1;
16741         }
16742     }
16743
16744   if (htab->tls_ldm_got.refcount > 0)
16745     {
16746       /* Allocate two GOT entries and one dynamic relocation (if necessary)
16747          for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
16748       htab->tls_ldm_got.offset = htab->root.sgot->size;
16749       htab->root.sgot->size += 8;
16750       if (bfd_link_pic (info))
16751         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16752     }
16753   else
16754     htab->tls_ldm_got.offset = -1;
16755
16756   /* At the very end of the .rofixup section is a pointer to the GOT,
16757      reserve space for it. */
16758   if (htab->fdpic_p && htab->srofixup != NULL)
16759     htab->srofixup->size += 4;
16760
16761   /* Allocate global sym .plt and .got entries, and space for global
16762      sym dynamic relocs.  */
16763   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
16764
16765   /* Here we rummage through the found bfds to collect glue information.  */
16766   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16767     {
16768       if (! is_arm_elf (ibfd))
16769         continue;
16770
16771       /* Initialise mapping tables for code/data.  */
16772       bfd_elf32_arm_init_maps (ibfd);
16773
16774       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
16775           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
16776           || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
16777         _bfd_error_handler (_("errors encountered processing file %pB"), ibfd);
16778     }
16779
16780   /* Allocate space for the glue sections now that we've sized them.  */
16781   bfd_elf32_arm_allocate_interworking_sections (info);
16782
16783   /* For every jump slot reserved in the sgotplt, reloc_count is
16784      incremented.  However, when we reserve space for TLS descriptors,
16785      it's not incremented, so in order to compute the space reserved
16786      for them, it suffices to multiply the reloc count by the jump
16787      slot size.  */
16788   if (htab->root.srelplt)
16789     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
16790
16791   if (htab->tls_trampoline)
16792     {
16793       if (htab->root.splt->size == 0)
16794         htab->root.splt->size += htab->plt_header_size;
16795
16796       htab->tls_trampoline = htab->root.splt->size;
16797       htab->root.splt->size += htab->plt_entry_size;
16798
16799       /* If we're not using lazy TLS relocations, don't generate the
16800          PLT and GOT entries they require.  */
16801       if (!(info->flags & DF_BIND_NOW))
16802         {
16803           htab->dt_tlsdesc_got = htab->root.sgot->size;
16804           htab->root.sgot->size += 4;
16805
16806           htab->dt_tlsdesc_plt = htab->root.splt->size;
16807           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
16808         }
16809     }
16810
16811   /* The check_relocs and adjust_dynamic_symbol entry points have
16812      determined the sizes of the various dynamic sections.  Allocate
16813      memory for them.  */
16814   plt = FALSE;
16815   relocs = FALSE;
16816   for (s = dynobj->sections; s != NULL; s = s->next)
16817     {
16818       const char * name;
16819
16820       if ((s->flags & SEC_LINKER_CREATED) == 0)
16821         continue;
16822
16823       /* It's OK to base decisions on the section name, because none
16824          of the dynobj section names depend upon the input files.  */
16825       name = bfd_get_section_name (dynobj, s);
16826
16827       if (s == htab->root.splt)
16828         {
16829           /* Remember whether there is a PLT.  */
16830           plt = s->size != 0;
16831         }
16832       else if (CONST_STRNEQ (name, ".rel"))
16833         {
16834           if (s->size != 0)
16835             {
16836               /* Remember whether there are any reloc sections other
16837                  than .rel(a).plt and .rela.plt.unloaded.  */
16838               if (s != htab->root.srelplt && s != htab->srelplt2)
16839                 relocs = TRUE;
16840
16841               /* We use the reloc_count field as a counter if we need
16842                  to copy relocs into the output file.  */
16843               s->reloc_count = 0;
16844             }
16845         }
16846       else if (s != htab->root.sgot
16847                && s != htab->root.sgotplt
16848                && s != htab->root.iplt
16849                && s != htab->root.igotplt
16850                && s != htab->root.sdynbss
16851                && s != htab->root.sdynrelro
16852                && s != htab->srofixup)
16853         {
16854           /* It's not one of our sections, so don't allocate space.  */
16855           continue;
16856         }
16857
16858       if (s->size == 0)
16859         {
16860           /* If we don't need this section, strip it from the
16861              output file.  This is mostly to handle .rel(a).bss and
16862              .rel(a).plt.  We must create both sections in
16863              create_dynamic_sections, because they must be created
16864              before the linker maps input sections to output
16865              sections.  The linker does that before
16866              adjust_dynamic_symbol is called, and it is that
16867              function which decides whether anything needs to go
16868              into these sections.  */
16869           s->flags |= SEC_EXCLUDE;
16870           continue;
16871         }
16872
16873       if ((s->flags & SEC_HAS_CONTENTS) == 0)
16874         continue;
16875
16876       /* Allocate memory for the section contents.  */
16877       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
16878       if (s->contents == NULL)
16879         return FALSE;
16880     }
16881
16882   if (elf_hash_table (info)->dynamic_sections_created)
16883     {
16884       /* Add some entries to the .dynamic section.  We fill in the
16885          values later, in elf32_arm_finish_dynamic_sections, but we
16886          must add the entries now so that we get the correct size for
16887          the .dynamic section.  The DT_DEBUG entry is filled in by the
16888          dynamic linker and used by the debugger.  */
16889 #define add_dynamic_entry(TAG, VAL) \
16890   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
16891
16892      if (bfd_link_executable (info))
16893         {
16894           if (!add_dynamic_entry (DT_DEBUG, 0))
16895             return FALSE;
16896         }
16897
16898       if (plt)
16899         {
16900           if (   !add_dynamic_entry (DT_PLTGOT, 0)
16901               || !add_dynamic_entry (DT_PLTRELSZ, 0)
16902               || !add_dynamic_entry (DT_PLTREL,
16903                                      htab->use_rel ? DT_REL : DT_RELA)
16904               || !add_dynamic_entry (DT_JMPREL, 0))
16905             return FALSE;
16906
16907           if (htab->dt_tlsdesc_plt
16908               && (!add_dynamic_entry (DT_TLSDESC_PLT,0)
16909                   || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
16910             return FALSE;
16911         }
16912
16913       if (relocs)
16914         {
16915           if (htab->use_rel)
16916             {
16917               if (!add_dynamic_entry (DT_REL, 0)
16918                   || !add_dynamic_entry (DT_RELSZ, 0)
16919                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
16920                 return FALSE;
16921             }
16922           else
16923             {
16924               if (!add_dynamic_entry (DT_RELA, 0)
16925                   || !add_dynamic_entry (DT_RELASZ, 0)
16926                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
16927                 return FALSE;
16928             }
16929         }
16930
16931       /* If any dynamic relocs apply to a read-only section,
16932          then we need a DT_TEXTREL entry.  */
16933       if ((info->flags & DF_TEXTREL) == 0)
16934         elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
16935
16936       if ((info->flags & DF_TEXTREL) != 0)
16937         {
16938           if (!add_dynamic_entry (DT_TEXTREL, 0))
16939             return FALSE;
16940         }
16941       if (htab->vxworks_p
16942           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
16943         return FALSE;
16944     }
16945 #undef add_dynamic_entry
16946
16947   return TRUE;
16948 }
16949
16950 /* Size sections even though they're not dynamic.  We use it to setup
16951    _TLS_MODULE_BASE_, if needed.  */
16952
16953 static bfd_boolean
16954 elf32_arm_always_size_sections (bfd *output_bfd,
16955                                 struct bfd_link_info *info)
16956 {
16957   asection *tls_sec;
16958
16959   if (bfd_link_relocatable (info))
16960     return TRUE;
16961
16962   tls_sec = elf_hash_table (info)->tls_sec;
16963
16964   if (tls_sec)
16965     {
16966       struct elf_link_hash_entry *tlsbase;
16967
16968       tlsbase = elf_link_hash_lookup
16969         (elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
16970
16971       if (tlsbase)
16972         {
16973           struct bfd_link_hash_entry *bh = NULL;
16974           const struct elf_backend_data *bed
16975             = get_elf_backend_data (output_bfd);
16976
16977           if (!(_bfd_generic_link_add_one_symbol
16978                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
16979                  tls_sec, 0, NULL, FALSE,
16980                  bed->collect, &bh)))
16981             return FALSE;
16982
16983           tlsbase->type = STT_TLS;
16984           tlsbase = (struct elf_link_hash_entry *)bh;
16985           tlsbase->def_regular = 1;
16986           tlsbase->other = STV_HIDDEN;
16987           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
16988         }
16989     }
16990   return TRUE;
16991 }
16992
16993 /* Finish up dynamic symbol handling.  We set the contents of various
16994    dynamic sections here.  */
16995
16996 static bfd_boolean
16997 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
16998                                  struct bfd_link_info * info,
16999                                  struct elf_link_hash_entry * h,
17000                                  Elf_Internal_Sym * sym)
17001 {
17002   struct elf32_arm_link_hash_table *htab;
17003   struct elf32_arm_link_hash_entry *eh;
17004
17005   htab = elf32_arm_hash_table (info);
17006   if (htab == NULL)
17007     return FALSE;
17008
17009   eh = (struct elf32_arm_link_hash_entry *) h;
17010
17011   if (h->plt.offset != (bfd_vma) -1)
17012     {
17013       if (!eh->is_iplt)
17014         {
17015           BFD_ASSERT (h->dynindx != -1);
17016           if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
17017                                               h->dynindx, 0))
17018             return FALSE;
17019         }
17020
17021       if (!h->def_regular)
17022         {
17023           /* Mark the symbol as undefined, rather than as defined in
17024              the .plt section.  */
17025           sym->st_shndx = SHN_UNDEF;
17026           /* If the symbol is weak we need to clear the value.
17027              Otherwise, the PLT entry would provide a definition for
17028              the symbol even if the symbol wasn't defined anywhere,
17029              and so the symbol would never be NULL.  Leave the value if
17030              there were any relocations where pointer equality matters
17031              (this is a clue for the dynamic linker, to make function
17032              pointer comparisons work between an application and shared
17033              library).  */
17034           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
17035             sym->st_value = 0;
17036         }
17037       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
17038         {
17039           /* At least one non-call relocation references this .iplt entry,
17040              so the .iplt entry is the function's canonical address.  */
17041           sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
17042           ARM_SET_SYM_BRANCH_TYPE (sym->st_target_internal, ST_BRANCH_TO_ARM);
17043           sym->st_shndx = (_bfd_elf_section_from_bfd_section
17044                            (output_bfd, htab->root.iplt->output_section));
17045           sym->st_value = (h->plt.offset
17046                            + htab->root.iplt->output_section->vma
17047                            + htab->root.iplt->output_offset);
17048         }
17049     }
17050
17051   if (h->needs_copy)
17052     {
17053       asection * s;
17054       Elf_Internal_Rela rel;
17055
17056       /* This symbol needs a copy reloc.  Set it up.  */
17057       BFD_ASSERT (h->dynindx != -1
17058                   && (h->root.type == bfd_link_hash_defined
17059                       || h->root.type == bfd_link_hash_defweak));
17060
17061       rel.r_addend = 0;
17062       rel.r_offset = (h->root.u.def.value
17063                       + h->root.u.def.section->output_section->vma
17064                       + h->root.u.def.section->output_offset);
17065       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
17066       if (h->root.u.def.section == htab->root.sdynrelro)
17067         s = htab->root.sreldynrelro;
17068       else
17069         s = htab->root.srelbss;
17070       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
17071     }
17072
17073   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
17074      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
17075      to the ".got" section.  */
17076   if (h == htab->root.hdynamic
17077       || (!htab->vxworks_p && h == htab->root.hgot))
17078     sym->st_shndx = SHN_ABS;
17079
17080   return TRUE;
17081 }
17082
17083 static void
17084 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17085                     void *contents,
17086                     const unsigned long *template, unsigned count)
17087 {
17088   unsigned ix;
17089
17090   for (ix = 0; ix != count; ix++)
17091     {
17092       unsigned long insn = template[ix];
17093
17094       /* Emit mov pc,rx if bx is not permitted.  */
17095       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
17096         insn = (insn & 0xf000000f) | 0x01a0f000;
17097       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
17098     }
17099 }
17100
17101 /* Install the special first PLT entry for elf32-arm-nacl.  Unlike
17102    other variants, NaCl needs this entry in a static executable's
17103    .iplt too.  When we're handling that case, GOT_DISPLACEMENT is
17104    zero.  For .iplt really only the last bundle is useful, and .iplt
17105    could have a shorter first entry, with each individual PLT entry's
17106    relative branch calculated differently so it targets the last
17107    bundle instead of the instruction before it (labelled .Lplt_tail
17108    above).  But it's simpler to keep the size and layout of PLT0
17109    consistent with the dynamic case, at the cost of some dead code at
17110    the start of .iplt and the one dead store to the stack at the start
17111    of .Lplt_tail.  */
17112 static void
17113 arm_nacl_put_plt0 (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17114                    asection *plt, bfd_vma got_displacement)
17115 {
17116   unsigned int i;
17117
17118   put_arm_insn (htab, output_bfd,
17119                 elf32_arm_nacl_plt0_entry[0]
17120                 | arm_movw_immediate (got_displacement),
17121                 plt->contents + 0);
17122   put_arm_insn (htab, output_bfd,
17123                 elf32_arm_nacl_plt0_entry[1]
17124                 | arm_movt_immediate (got_displacement),
17125                 plt->contents + 4);
17126
17127   for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
17128     put_arm_insn (htab, output_bfd,
17129                   elf32_arm_nacl_plt0_entry[i],
17130                   plt->contents + (i * 4));
17131 }
17132
17133 /* Finish up the dynamic sections.  */
17134
17135 static bfd_boolean
17136 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
17137 {
17138   bfd * dynobj;
17139   asection * sgot;
17140   asection * sdyn;
17141   struct elf32_arm_link_hash_table *htab;
17142
17143   htab = elf32_arm_hash_table (info);
17144   if (htab == NULL)
17145     return FALSE;
17146
17147   dynobj = elf_hash_table (info)->dynobj;
17148
17149   sgot = htab->root.sgotplt;
17150   /* A broken linker script might have discarded the dynamic sections.
17151      Catch this here so that we do not seg-fault later on.  */
17152   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
17153     return FALSE;
17154   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17155
17156   if (elf_hash_table (info)->dynamic_sections_created)
17157     {
17158       asection *splt;
17159       Elf32_External_Dyn *dyncon, *dynconend;
17160
17161       splt = htab->root.splt;
17162       BFD_ASSERT (splt != NULL && sdyn != NULL);
17163       BFD_ASSERT (htab->symbian_p || sgot != NULL);
17164
17165       dyncon = (Elf32_External_Dyn *) sdyn->contents;
17166       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
17167
17168       for (; dyncon < dynconend; dyncon++)
17169         {
17170           Elf_Internal_Dyn dyn;
17171           const char * name;
17172           asection * s;
17173
17174           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
17175
17176           switch (dyn.d_tag)
17177             {
17178               unsigned int type;
17179
17180             default:
17181               if (htab->vxworks_p
17182                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
17183                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17184               break;
17185
17186             case DT_HASH:
17187               name = ".hash";
17188               goto get_vma_if_bpabi;
17189             case DT_STRTAB:
17190               name = ".dynstr";
17191               goto get_vma_if_bpabi;
17192             case DT_SYMTAB:
17193               name = ".dynsym";
17194               goto get_vma_if_bpabi;
17195             case DT_VERSYM:
17196               name = ".gnu.version";
17197               goto get_vma_if_bpabi;
17198             case DT_VERDEF:
17199               name = ".gnu.version_d";
17200               goto get_vma_if_bpabi;
17201             case DT_VERNEED:
17202               name = ".gnu.version_r";
17203               goto get_vma_if_bpabi;
17204
17205             case DT_PLTGOT:
17206               name = htab->symbian_p ? ".got" : ".got.plt";
17207               goto get_vma;
17208             case DT_JMPREL:
17209               name = RELOC_SECTION (htab, ".plt");
17210             get_vma:
17211               s = bfd_get_linker_section (dynobj, name);
17212               if (s == NULL)
17213                 {
17214                   _bfd_error_handler
17215                     (_("could not find section %s"), name);
17216                   bfd_set_error (bfd_error_invalid_operation);
17217                   return FALSE;
17218                 }
17219               if (!htab->symbian_p)
17220                 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17221               else
17222                 /* In the BPABI, tags in the PT_DYNAMIC section point
17223                    at the file offset, not the memory address, for the
17224                    convenience of the post linker.  */
17225                 dyn.d_un.d_ptr = s->output_section->filepos + s->output_offset;
17226               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17227               break;
17228
17229             get_vma_if_bpabi:
17230               if (htab->symbian_p)
17231                 goto get_vma;
17232               break;
17233
17234             case DT_PLTRELSZ:
17235               s = htab->root.srelplt;
17236               BFD_ASSERT (s != NULL);
17237               dyn.d_un.d_val = s->size;
17238               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17239               break;
17240
17241             case DT_RELSZ:
17242             case DT_RELASZ:
17243             case DT_REL:
17244             case DT_RELA:
17245               /* In the BPABI, the DT_REL tag must point at the file
17246                  offset, not the VMA, of the first relocation
17247                  section.  So, we use code similar to that in
17248                  elflink.c, but do not check for SHF_ALLOC on the
17249                  relocation section, since relocation sections are
17250                  never allocated under the BPABI.  PLT relocs are also
17251                  included.  */
17252               if (htab->symbian_p)
17253                 {
17254                   unsigned int i;
17255                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
17256                           ? SHT_REL : SHT_RELA);
17257                   dyn.d_un.d_val = 0;
17258                   for (i = 1; i < elf_numsections (output_bfd); i++)
17259                     {
17260                       Elf_Internal_Shdr *hdr
17261                         = elf_elfsections (output_bfd)[i];
17262                       if (hdr->sh_type == type)
17263                         {
17264                           if (dyn.d_tag == DT_RELSZ
17265                               || dyn.d_tag == DT_RELASZ)
17266                             dyn.d_un.d_val += hdr->sh_size;
17267                           else if ((ufile_ptr) hdr->sh_offset
17268                                    <= dyn.d_un.d_val - 1)
17269                             dyn.d_un.d_val = hdr->sh_offset;
17270                         }
17271                     }
17272                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17273                 }
17274               break;
17275
17276             case DT_TLSDESC_PLT:
17277               s = htab->root.splt;
17278               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17279                                 + htab->dt_tlsdesc_plt);
17280               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17281               break;
17282
17283             case DT_TLSDESC_GOT:
17284               s = htab->root.sgot;
17285               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17286                                 + htab->dt_tlsdesc_got);
17287               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17288               break;
17289
17290               /* Set the bottom bit of DT_INIT/FINI if the
17291                  corresponding function is Thumb.  */
17292             case DT_INIT:
17293               name = info->init_function;
17294               goto get_sym;
17295             case DT_FINI:
17296               name = info->fini_function;
17297             get_sym:
17298               /* If it wasn't set by elf_bfd_final_link
17299                  then there is nothing to adjust.  */
17300               if (dyn.d_un.d_val != 0)
17301                 {
17302                   struct elf_link_hash_entry * eh;
17303
17304                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
17305                                              FALSE, FALSE, TRUE);
17306                   if (eh != NULL
17307                       && ARM_GET_SYM_BRANCH_TYPE (eh->target_internal)
17308                          == ST_BRANCH_TO_THUMB)
17309                     {
17310                       dyn.d_un.d_val |= 1;
17311                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17312                     }
17313                 }
17314               break;
17315             }
17316         }
17317
17318       /* Fill in the first entry in the procedure linkage table.  */
17319       if (splt->size > 0 && htab->plt_header_size)
17320         {
17321           const bfd_vma *plt0_entry;
17322           bfd_vma got_address, plt_address, got_displacement;
17323
17324           /* Calculate the addresses of the GOT and PLT.  */
17325           got_address = sgot->output_section->vma + sgot->output_offset;
17326           plt_address = splt->output_section->vma + splt->output_offset;
17327
17328           if (htab->vxworks_p)
17329             {
17330               /* The VxWorks GOT is relocated by the dynamic linker.
17331                  Therefore, we must emit relocations rather than simply
17332                  computing the values now.  */
17333               Elf_Internal_Rela rel;
17334
17335               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
17336               put_arm_insn (htab, output_bfd, plt0_entry[0],
17337                             splt->contents + 0);
17338               put_arm_insn (htab, output_bfd, plt0_entry[1],
17339                             splt->contents + 4);
17340               put_arm_insn (htab, output_bfd, plt0_entry[2],
17341                             splt->contents + 8);
17342               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
17343
17344               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
17345               rel.r_offset = plt_address + 12;
17346               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17347               rel.r_addend = 0;
17348               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
17349                                      htab->srelplt2->contents);
17350             }
17351           else if (htab->nacl_p)
17352             arm_nacl_put_plt0 (htab, output_bfd, splt,
17353                                got_address + 8 - (plt_address + 16));
17354           else if (using_thumb_only (htab))
17355             {
17356               got_displacement = got_address - (plt_address + 12);
17357
17358               plt0_entry = elf32_thumb2_plt0_entry;
17359               put_arm_insn (htab, output_bfd, plt0_entry[0],
17360                             splt->contents + 0);
17361               put_arm_insn (htab, output_bfd, plt0_entry[1],
17362                             splt->contents + 4);
17363               put_arm_insn (htab, output_bfd, plt0_entry[2],
17364                             splt->contents + 8);
17365
17366               bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
17367             }
17368           else
17369             {
17370               got_displacement = got_address - (plt_address + 16);
17371
17372               plt0_entry = elf32_arm_plt0_entry;
17373               put_arm_insn (htab, output_bfd, plt0_entry[0],
17374                             splt->contents + 0);
17375               put_arm_insn (htab, output_bfd, plt0_entry[1],
17376                             splt->contents + 4);
17377               put_arm_insn (htab, output_bfd, plt0_entry[2],
17378                             splt->contents + 8);
17379               put_arm_insn (htab, output_bfd, plt0_entry[3],
17380                             splt->contents + 12);
17381
17382 #ifdef FOUR_WORD_PLT
17383               /* The displacement value goes in the otherwise-unused
17384                  last word of the second entry.  */
17385               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
17386 #else
17387               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
17388 #endif
17389             }
17390         }
17391
17392       /* UnixWare sets the entsize of .plt to 4, although that doesn't
17393          really seem like the right value.  */
17394       if (splt->output_section->owner == output_bfd)
17395         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
17396
17397       if (htab->dt_tlsdesc_plt)
17398         {
17399           bfd_vma got_address
17400             = sgot->output_section->vma + sgot->output_offset;
17401           bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
17402                                     + htab->root.sgot->output_offset);
17403           bfd_vma plt_address
17404             = splt->output_section->vma + splt->output_offset;
17405
17406           arm_put_trampoline (htab, output_bfd,
17407                               splt->contents + htab->dt_tlsdesc_plt,
17408                               dl_tlsdesc_lazy_trampoline, 6);
17409
17410           bfd_put_32 (output_bfd,
17411                       gotplt_address + htab->dt_tlsdesc_got
17412                       - (plt_address + htab->dt_tlsdesc_plt)
17413                       - dl_tlsdesc_lazy_trampoline[6],
17414                       splt->contents + htab->dt_tlsdesc_plt + 24);
17415           bfd_put_32 (output_bfd,
17416                       got_address - (plt_address + htab->dt_tlsdesc_plt)
17417                       - dl_tlsdesc_lazy_trampoline[7],
17418                       splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
17419         }
17420
17421       if (htab->tls_trampoline)
17422         {
17423           arm_put_trampoline (htab, output_bfd,
17424                               splt->contents + htab->tls_trampoline,
17425                               tls_trampoline, 3);
17426 #ifdef FOUR_WORD_PLT
17427           bfd_put_32 (output_bfd, 0x00000000,
17428                       splt->contents + htab->tls_trampoline + 12);
17429 #endif
17430         }
17431
17432       if (htab->vxworks_p
17433           && !bfd_link_pic (info)
17434           && htab->root.splt->size > 0)
17435         {
17436           /* Correct the .rel(a).plt.unloaded relocations.  They will have
17437              incorrect symbol indexes.  */
17438           int num_plts;
17439           unsigned char *p;
17440
17441           num_plts = ((htab->root.splt->size - htab->plt_header_size)
17442                       / htab->plt_entry_size);
17443           p = htab->srelplt2->contents + RELOC_SIZE (htab);
17444
17445           for (; num_plts; num_plts--)
17446             {
17447               Elf_Internal_Rela rel;
17448
17449               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17450               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17451               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17452               p += RELOC_SIZE (htab);
17453
17454               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17455               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
17456               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17457               p += RELOC_SIZE (htab);
17458             }
17459         }
17460     }
17461
17462   if (htab->nacl_p && htab->root.iplt != NULL && htab->root.iplt->size > 0)
17463     /* NaCl uses a special first entry in .iplt too.  */
17464     arm_nacl_put_plt0 (htab, output_bfd, htab->root.iplt, 0);
17465
17466   /* Fill in the first three entries in the global offset table.  */
17467   if (sgot)
17468     {
17469       if (sgot->size > 0)
17470         {
17471           if (sdyn == NULL)
17472             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
17473           else
17474             bfd_put_32 (output_bfd,
17475                         sdyn->output_section->vma + sdyn->output_offset,
17476                         sgot->contents);
17477           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
17478           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
17479         }
17480
17481       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
17482     }
17483
17484   /* At the very end of the .rofixup section is a pointer to the GOT.  */
17485   if (htab->fdpic_p && htab->srofixup != NULL)
17486     {
17487       struct elf_link_hash_entry *hgot = htab->root.hgot;
17488
17489       bfd_vma got_value = hgot->root.u.def.value
17490         + hgot->root.u.def.section->output_section->vma
17491         + hgot->root.u.def.section->output_offset;
17492
17493       arm_elf_add_rofixup(output_bfd, htab->srofixup, got_value);
17494
17495       /* Make sure we allocated and generated the same number of fixups.  */
17496       BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
17497     }
17498
17499   return TRUE;
17500 }
17501
17502 static void
17503 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
17504 {
17505   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
17506   struct elf32_arm_link_hash_table *globals;
17507   struct elf_segment_map *m;
17508
17509   i_ehdrp = elf_elfheader (abfd);
17510
17511   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
17512     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
17513   else
17514     _bfd_elf_post_process_headers (abfd, link_info);
17515   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
17516
17517   if (link_info)
17518     {
17519       globals = elf32_arm_hash_table (link_info);
17520       if (globals != NULL && globals->byteswap_code)
17521         i_ehdrp->e_flags |= EF_ARM_BE8;
17522
17523       if (globals->fdpic_p)
17524         i_ehdrp->e_ident[EI_OSABI] |= ELFOSABI_ARM_FDPIC;
17525     }
17526
17527   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
17528       && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
17529     {
17530       int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
17531       if (abi == AEABI_VFP_args_vfp)
17532         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
17533       else
17534         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
17535     }
17536
17537   /* Scan segment to set p_flags attribute if it contains only sections with
17538      SHF_ARM_PURECODE flag.  */
17539   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
17540     {
17541       unsigned int j;
17542
17543       if (m->count == 0)
17544         continue;
17545       for (j = 0; j < m->count; j++)
17546         {
17547           if (!(elf_section_flags (m->sections[j]) & SHF_ARM_PURECODE))
17548             break;
17549         }
17550       if (j == m->count)
17551         {
17552           m->p_flags = PF_X;
17553           m->p_flags_valid = 1;
17554         }
17555     }
17556 }
17557
17558 static enum elf_reloc_type_class
17559 elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
17560                             const asection *rel_sec ATTRIBUTE_UNUSED,
17561                             const Elf_Internal_Rela *rela)
17562 {
17563   switch ((int) ELF32_R_TYPE (rela->r_info))
17564     {
17565     case R_ARM_RELATIVE:
17566       return reloc_class_relative;
17567     case R_ARM_JUMP_SLOT:
17568       return reloc_class_plt;
17569     case R_ARM_COPY:
17570       return reloc_class_copy;
17571     case R_ARM_IRELATIVE:
17572       return reloc_class_ifunc;
17573     default:
17574       return reloc_class_normal;
17575     }
17576 }
17577
17578 static void
17579 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
17580 {
17581   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
17582 }
17583
17584 /* Return TRUE if this is an unwinding table entry.  */
17585
17586 static bfd_boolean
17587 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
17588 {
17589   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
17590           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
17591 }
17592
17593
17594 /* Set the type and flags for an ARM section.  We do this by
17595    the section name, which is a hack, but ought to work.  */
17596
17597 static bfd_boolean
17598 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
17599 {
17600   const char * name;
17601
17602   name = bfd_get_section_name (abfd, sec);
17603
17604   if (is_arm_elf_unwind_section_name (abfd, name))
17605     {
17606       hdr->sh_type = SHT_ARM_EXIDX;
17607       hdr->sh_flags |= SHF_LINK_ORDER;
17608     }
17609
17610   if (sec->flags & SEC_ELF_PURECODE)
17611     hdr->sh_flags |= SHF_ARM_PURECODE;
17612
17613   return TRUE;
17614 }
17615
17616 /* Handle an ARM specific section when reading an object file.  This is
17617    called when bfd_section_from_shdr finds a section with an unknown
17618    type.  */
17619
17620 static bfd_boolean
17621 elf32_arm_section_from_shdr (bfd *abfd,
17622                              Elf_Internal_Shdr * hdr,
17623                              const char *name,
17624                              int shindex)
17625 {
17626   /* There ought to be a place to keep ELF backend specific flags, but
17627      at the moment there isn't one.  We just keep track of the
17628      sections by their name, instead.  Fortunately, the ABI gives
17629      names for all the ARM specific sections, so we will probably get
17630      away with this.  */
17631   switch (hdr->sh_type)
17632     {
17633     case SHT_ARM_EXIDX:
17634     case SHT_ARM_PREEMPTMAP:
17635     case SHT_ARM_ATTRIBUTES:
17636       break;
17637
17638     default:
17639       return FALSE;
17640     }
17641
17642   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
17643     return FALSE;
17644
17645   return TRUE;
17646 }
17647
17648 static _arm_elf_section_data *
17649 get_arm_elf_section_data (asection * sec)
17650 {
17651   if (sec && sec->owner && is_arm_elf (sec->owner))
17652     return elf32_arm_section_data (sec);
17653   else
17654     return NULL;
17655 }
17656
17657 typedef struct
17658 {
17659   void *flaginfo;
17660   struct bfd_link_info *info;
17661   asection *sec;
17662   int sec_shndx;
17663   int (*func) (void *, const char *, Elf_Internal_Sym *,
17664                asection *, struct elf_link_hash_entry *);
17665 } output_arch_syminfo;
17666
17667 enum map_symbol_type
17668 {
17669   ARM_MAP_ARM,
17670   ARM_MAP_THUMB,
17671   ARM_MAP_DATA
17672 };
17673
17674
17675 /* Output a single mapping symbol.  */
17676
17677 static bfd_boolean
17678 elf32_arm_output_map_sym (output_arch_syminfo *osi,
17679                           enum map_symbol_type type,
17680                           bfd_vma offset)
17681 {
17682   static const char *names[3] = {"$a", "$t", "$d"};
17683   Elf_Internal_Sym sym;
17684
17685   sym.st_value = osi->sec->output_section->vma
17686                  + osi->sec->output_offset
17687                  + offset;
17688   sym.st_size = 0;
17689   sym.st_other = 0;
17690   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
17691   sym.st_shndx = osi->sec_shndx;
17692   sym.st_target_internal = 0;
17693   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
17694   return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
17695 }
17696
17697 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
17698    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
17699
17700 static bfd_boolean
17701 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
17702                             bfd_boolean is_iplt_entry_p,
17703                             union gotplt_union *root_plt,
17704                             struct arm_plt_info *arm_plt)
17705 {
17706   struct elf32_arm_link_hash_table *htab;
17707   bfd_vma addr, plt_header_size;
17708
17709   if (root_plt->offset == (bfd_vma) -1)
17710     return TRUE;
17711
17712   htab = elf32_arm_hash_table (osi->info);
17713   if (htab == NULL)
17714     return FALSE;
17715
17716   if (is_iplt_entry_p)
17717     {
17718       osi->sec = htab->root.iplt;
17719       plt_header_size = 0;
17720     }
17721   else
17722     {
17723       osi->sec = htab->root.splt;
17724       plt_header_size = htab->plt_header_size;
17725     }
17726   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
17727                     (osi->info->output_bfd, osi->sec->output_section));
17728
17729   addr = root_plt->offset & -2;
17730   if (htab->symbian_p)
17731     {
17732       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17733         return FALSE;
17734       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
17735         return FALSE;
17736     }
17737   else if (htab->vxworks_p)
17738     {
17739       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17740         return FALSE;
17741       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
17742         return FALSE;
17743       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
17744         return FALSE;
17745       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
17746         return FALSE;
17747     }
17748   else if (htab->nacl_p)
17749     {
17750       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17751         return FALSE;
17752     }
17753   else if (htab->fdpic_p)
17754     {
17755       if (elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt))
17756         if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17757           return FALSE;
17758       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17759         return FALSE;
17760       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 16))
17761         return FALSE;
17762       if (htab->plt_entry_size == 4 * ARRAY_SIZE(elf32_arm_fdpic_plt_entry))
17763         if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 24))
17764           return FALSE;
17765     }
17766   else if (using_thumb_only (htab))
17767     {
17768       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
17769         return FALSE;
17770     }
17771   else
17772     {
17773       bfd_boolean thumb_stub_p;
17774
17775       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
17776       if (thumb_stub_p)
17777         {
17778           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17779             return FALSE;
17780         }
17781 #ifdef FOUR_WORD_PLT
17782       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17783         return FALSE;
17784       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
17785         return FALSE;
17786 #else
17787       /* A three-word PLT with no Thumb thunk contains only Arm code,
17788          so only need to output a mapping symbol for the first PLT entry and
17789          entries with thumb thunks.  */
17790       if (thumb_stub_p || addr == plt_header_size)
17791         {
17792           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17793             return FALSE;
17794         }
17795 #endif
17796     }
17797
17798   return TRUE;
17799 }
17800
17801 /* Output mapping symbols for PLT entries associated with H.  */
17802
17803 static bfd_boolean
17804 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
17805 {
17806   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
17807   struct elf32_arm_link_hash_entry *eh;
17808
17809   if (h->root.type == bfd_link_hash_indirect)
17810     return TRUE;
17811
17812   if (h->root.type == bfd_link_hash_warning)
17813     /* When warning symbols are created, they **replace** the "real"
17814        entry in the hash table, thus we never get to see the real
17815        symbol in a hash traversal.  So look at it now.  */
17816     h = (struct elf_link_hash_entry *) h->root.u.i.link;
17817
17818   eh = (struct elf32_arm_link_hash_entry *) h;
17819   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
17820                                      &h->plt, &eh->plt);
17821 }
17822
17823 /* Bind a veneered symbol to its veneer identified by its hash entry
17824    STUB_ENTRY.  The veneered location thus loose its symbol.  */
17825
17826 static void
17827 arm_stub_claim_sym (struct elf32_arm_stub_hash_entry *stub_entry)
17828 {
17829   struct elf32_arm_link_hash_entry *hash = stub_entry->h;
17830
17831   BFD_ASSERT (hash);
17832   hash->root.root.u.def.section = stub_entry->stub_sec;
17833   hash->root.root.u.def.value = stub_entry->stub_offset;
17834   hash->root.size = stub_entry->stub_size;
17835 }
17836
17837 /* Output a single local symbol for a generated stub.  */
17838
17839 static bfd_boolean
17840 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
17841                            bfd_vma offset, bfd_vma size)
17842 {
17843   Elf_Internal_Sym sym;
17844
17845   sym.st_value = osi->sec->output_section->vma
17846                  + osi->sec->output_offset
17847                  + offset;
17848   sym.st_size = size;
17849   sym.st_other = 0;
17850   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
17851   sym.st_shndx = osi->sec_shndx;
17852   sym.st_target_internal = 0;
17853   return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
17854 }
17855
17856 static bfd_boolean
17857 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
17858                   void * in_arg)
17859 {
17860   struct elf32_arm_stub_hash_entry *stub_entry;
17861   asection *stub_sec;
17862   bfd_vma addr;
17863   char *stub_name;
17864   output_arch_syminfo *osi;
17865   const insn_sequence *template_sequence;
17866   enum stub_insn_type prev_type;
17867   int size;
17868   int i;
17869   enum map_symbol_type sym_type;
17870
17871   /* Massage our args to the form they really have.  */
17872   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
17873   osi = (output_arch_syminfo *) in_arg;
17874
17875   stub_sec = stub_entry->stub_sec;
17876
17877   /* Ensure this stub is attached to the current section being
17878      processed.  */
17879   if (stub_sec != osi->sec)
17880     return TRUE;
17881
17882   addr = (bfd_vma) stub_entry->stub_offset;
17883   template_sequence = stub_entry->stub_template;
17884
17885   if (arm_stub_sym_claimed (stub_entry->stub_type))
17886     arm_stub_claim_sym (stub_entry);
17887   else
17888     {
17889       stub_name = stub_entry->output_name;
17890       switch (template_sequence[0].type)
17891         {
17892         case ARM_TYPE:
17893           if (!elf32_arm_output_stub_sym (osi, stub_name, addr,
17894                                           stub_entry->stub_size))
17895             return FALSE;
17896           break;
17897         case THUMB16_TYPE:
17898         case THUMB32_TYPE:
17899           if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
17900                                           stub_entry->stub_size))
17901             return FALSE;
17902           break;
17903         default:
17904           BFD_FAIL ();
17905           return 0;
17906         }
17907     }
17908
17909   prev_type = DATA_TYPE;
17910   size = 0;
17911   for (i = 0; i < stub_entry->stub_template_size; i++)
17912     {
17913       switch (template_sequence[i].type)
17914         {
17915         case ARM_TYPE:
17916           sym_type = ARM_MAP_ARM;
17917           break;
17918
17919         case THUMB16_TYPE:
17920         case THUMB32_TYPE:
17921           sym_type = ARM_MAP_THUMB;
17922           break;
17923
17924         case DATA_TYPE:
17925           sym_type = ARM_MAP_DATA;
17926           break;
17927
17928         default:
17929           BFD_FAIL ();
17930           return FALSE;
17931         }
17932
17933       if (template_sequence[i].type != prev_type)
17934         {
17935           prev_type = template_sequence[i].type;
17936           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
17937             return FALSE;
17938         }
17939
17940       switch (template_sequence[i].type)
17941         {
17942         case ARM_TYPE:
17943         case THUMB32_TYPE:
17944           size += 4;
17945           break;
17946
17947         case THUMB16_TYPE:
17948           size += 2;
17949           break;
17950
17951         case DATA_TYPE:
17952           size += 4;
17953           break;
17954
17955         default:
17956           BFD_FAIL ();
17957           return FALSE;
17958         }
17959     }
17960
17961   return TRUE;
17962 }
17963
17964 /* Output mapping symbols for linker generated sections,
17965    and for those data-only sections that do not have a
17966    $d.  */
17967
17968 static bfd_boolean
17969 elf32_arm_output_arch_local_syms (bfd *output_bfd,
17970                                   struct bfd_link_info *info,
17971                                   void *flaginfo,
17972                                   int (*func) (void *, const char *,
17973                                                Elf_Internal_Sym *,
17974                                                asection *,
17975                                                struct elf_link_hash_entry *))
17976 {
17977   output_arch_syminfo osi;
17978   struct elf32_arm_link_hash_table *htab;
17979   bfd_vma offset;
17980   bfd_size_type size;
17981   bfd *input_bfd;
17982
17983   htab = elf32_arm_hash_table (info);
17984   if (htab == NULL)
17985     return FALSE;
17986
17987   check_use_blx (htab);
17988
17989   osi.flaginfo = flaginfo;
17990   osi.info = info;
17991   osi.func = func;
17992
17993   /* Add a $d mapping symbol to data-only sections that
17994      don't have any mapping symbol.  This may result in (harmless) redundant
17995      mapping symbols.  */
17996   for (input_bfd = info->input_bfds;
17997        input_bfd != NULL;
17998        input_bfd = input_bfd->link.next)
17999     {
18000       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
18001         for (osi.sec = input_bfd->sections;
18002              osi.sec != NULL;
18003              osi.sec = osi.sec->next)
18004           {
18005             if (osi.sec->output_section != NULL
18006                 && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
18007                     != 0)
18008                 && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
18009                    == SEC_HAS_CONTENTS
18010                 && get_arm_elf_section_data (osi.sec) != NULL
18011                 && get_arm_elf_section_data (osi.sec)->mapcount == 0
18012                 && osi.sec->size > 0
18013                 && (osi.sec->flags & SEC_EXCLUDE) == 0)
18014               {
18015                 osi.sec_shndx = _bfd_elf_section_from_bfd_section
18016                   (output_bfd, osi.sec->output_section);
18017                 if (osi.sec_shndx != (int)SHN_BAD)
18018                   elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
18019               }
18020           }
18021     }
18022
18023   /* ARM->Thumb glue.  */
18024   if (htab->arm_glue_size > 0)
18025     {
18026       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18027                                         ARM2THUMB_GLUE_SECTION_NAME);
18028
18029       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18030           (output_bfd, osi.sec->output_section);
18031       if (bfd_link_pic (info) || htab->root.is_relocatable_executable
18032           || htab->pic_veneer)
18033         size = ARM2THUMB_PIC_GLUE_SIZE;
18034       else if (htab->use_blx)
18035         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
18036       else
18037         size = ARM2THUMB_STATIC_GLUE_SIZE;
18038
18039       for (offset = 0; offset < htab->arm_glue_size; offset += size)
18040         {
18041           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
18042           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
18043         }
18044     }
18045
18046   /* Thumb->ARM glue.  */
18047   if (htab->thumb_glue_size > 0)
18048     {
18049       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18050                                         THUMB2ARM_GLUE_SECTION_NAME);
18051
18052       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18053           (output_bfd, osi.sec->output_section);
18054       size = THUMB2ARM_GLUE_SIZE;
18055
18056       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
18057         {
18058           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
18059           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
18060         }
18061     }
18062
18063   /* ARMv4 BX veneers.  */
18064   if (htab->bx_glue_size > 0)
18065     {
18066       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18067                                         ARM_BX_GLUE_SECTION_NAME);
18068
18069       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18070           (output_bfd, osi.sec->output_section);
18071
18072       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
18073     }
18074
18075   /* Long calls stubs.  */
18076   if (htab->stub_bfd && htab->stub_bfd->sections)
18077     {
18078       asection* stub_sec;
18079
18080       for (stub_sec = htab->stub_bfd->sections;
18081            stub_sec != NULL;
18082            stub_sec = stub_sec->next)
18083         {
18084           /* Ignore non-stub sections.  */
18085           if (!strstr (stub_sec->name, STUB_SUFFIX))
18086             continue;
18087
18088           osi.sec = stub_sec;
18089
18090           osi.sec_shndx = _bfd_elf_section_from_bfd_section
18091             (output_bfd, osi.sec->output_section);
18092
18093           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
18094         }
18095     }
18096
18097   /* Finally, output mapping symbols for the PLT.  */
18098   if (htab->root.splt && htab->root.splt->size > 0)
18099     {
18100       osi.sec = htab->root.splt;
18101       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18102                        (output_bfd, osi.sec->output_section));
18103
18104       /* Output mapping symbols for the plt header.  SymbianOS does not have a
18105          plt header.  */
18106       if (htab->vxworks_p)
18107         {
18108           /* VxWorks shared libraries have no PLT header.  */
18109           if (!bfd_link_pic (info))
18110             {
18111               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18112                 return FALSE;
18113               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18114                 return FALSE;
18115             }
18116         }
18117       else if (htab->nacl_p)
18118         {
18119           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18120             return FALSE;
18121         }
18122       else if (using_thumb_only (htab))
18123         {
18124           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
18125             return FALSE;
18126           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18127             return FALSE;
18128           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
18129             return FALSE;
18130         }
18131       else if (!htab->symbian_p && !htab->fdpic_p)
18132         {
18133           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18134             return FALSE;
18135 #ifndef FOUR_WORD_PLT
18136           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
18137             return FALSE;
18138 #endif
18139         }
18140     }
18141   if (htab->nacl_p && htab->root.iplt && htab->root.iplt->size > 0)
18142     {
18143       /* NaCl uses a special first entry in .iplt too.  */
18144       osi.sec = htab->root.iplt;
18145       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18146                        (output_bfd, osi.sec->output_section));
18147       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18148         return FALSE;
18149     }
18150   if ((htab->root.splt && htab->root.splt->size > 0)
18151       || (htab->root.iplt && htab->root.iplt->size > 0))
18152     {
18153       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
18154       for (input_bfd = info->input_bfds;
18155            input_bfd != NULL;
18156            input_bfd = input_bfd->link.next)
18157         {
18158           struct arm_local_iplt_info **local_iplt;
18159           unsigned int i, num_syms;
18160
18161           local_iplt = elf32_arm_local_iplt (input_bfd);
18162           if (local_iplt != NULL)
18163             {
18164               num_syms = elf_symtab_hdr (input_bfd).sh_info;
18165               for (i = 0; i < num_syms; i++)
18166                 if (local_iplt[i] != NULL
18167                     && !elf32_arm_output_plt_map_1 (&osi, TRUE,
18168                                                     &local_iplt[i]->root,
18169                                                     &local_iplt[i]->arm))
18170                   return FALSE;
18171             }
18172         }
18173     }
18174   if (htab->dt_tlsdesc_plt != 0)
18175     {
18176       /* Mapping symbols for the lazy tls trampoline.  */
18177       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
18178         return FALSE;
18179
18180       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18181                                      htab->dt_tlsdesc_plt + 24))
18182         return FALSE;
18183     }
18184   if (htab->tls_trampoline != 0)
18185     {
18186       /* Mapping symbols for the tls trampoline.  */
18187       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
18188         return FALSE;
18189 #ifdef FOUR_WORD_PLT
18190       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18191                                      htab->tls_trampoline + 12))
18192         return FALSE;
18193 #endif
18194     }
18195
18196   return TRUE;
18197 }
18198
18199 /* Filter normal symbols of CMSE entry functions of ABFD to include in
18200    the import library.  All SYMCOUNT symbols of ABFD can be examined
18201    from their pointers in SYMS.  Pointers of symbols to keep should be
18202    stored continuously at the beginning of that array.
18203
18204    Returns the number of symbols to keep.  */
18205
18206 static unsigned int
18207 elf32_arm_filter_cmse_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18208                                struct bfd_link_info *info,
18209                                asymbol **syms, long symcount)
18210 {
18211   size_t maxnamelen;
18212   char *cmse_name;
18213   long src_count, dst_count = 0;
18214   struct elf32_arm_link_hash_table *htab;
18215
18216   htab = elf32_arm_hash_table (info);
18217   if (!htab->stub_bfd || !htab->stub_bfd->sections)
18218     symcount = 0;
18219
18220   maxnamelen = 128;
18221   cmse_name = (char *) bfd_malloc (maxnamelen);
18222   for (src_count = 0; src_count < symcount; src_count++)
18223     {
18224       struct elf32_arm_link_hash_entry *cmse_hash;
18225       asymbol *sym;
18226       flagword flags;
18227       char *name;
18228       size_t namelen;
18229
18230       sym = syms[src_count];
18231       flags = sym->flags;
18232       name = (char *) bfd_asymbol_name (sym);
18233
18234       if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
18235         continue;
18236       if (!(flags & (BSF_GLOBAL | BSF_WEAK)))
18237         continue;
18238
18239       namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
18240       if (namelen > maxnamelen)
18241         {
18242           cmse_name = (char *)
18243             bfd_realloc (cmse_name, namelen);
18244           maxnamelen = namelen;
18245         }
18246       snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
18247       cmse_hash = (struct elf32_arm_link_hash_entry *)
18248         elf_link_hash_lookup (&(htab)->root, cmse_name, FALSE, FALSE, TRUE);
18249
18250       if (!cmse_hash
18251           || (cmse_hash->root.root.type != bfd_link_hash_defined
18252               && cmse_hash->root.root.type != bfd_link_hash_defweak)
18253           || cmse_hash->root.type != STT_FUNC)
18254         continue;
18255
18256       if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
18257         continue;
18258
18259       syms[dst_count++] = sym;
18260     }
18261   free (cmse_name);
18262
18263   syms[dst_count] = NULL;
18264
18265   return dst_count;
18266 }
18267
18268 /* Filter symbols of ABFD to include in the import library.  All
18269    SYMCOUNT symbols of ABFD can be examined from their pointers in
18270    SYMS.  Pointers of symbols to keep should be stored continuously at
18271    the beginning of that array.
18272
18273    Returns the number of symbols to keep.  */
18274
18275 static unsigned int
18276 elf32_arm_filter_implib_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18277                                  struct bfd_link_info *info,
18278                                  asymbol **syms, long symcount)
18279 {
18280   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
18281
18282   /* Requirement 8 of "ARM v8-M Security Extensions: Requirements on
18283      Development Tools" (ARM-ECM-0359818) mandates Secure Gateway import
18284      library to be a relocatable object file.  */
18285   BFD_ASSERT (!(bfd_get_file_flags (info->out_implib_bfd) & EXEC_P));
18286   if (globals->cmse_implib)
18287     return elf32_arm_filter_cmse_symbols (abfd, info, syms, symcount);
18288   else
18289     return _bfd_elf_filter_global_symbols (abfd, info, syms, symcount);
18290 }
18291
18292 /* Allocate target specific section data.  */
18293
18294 static bfd_boolean
18295 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
18296 {
18297   if (!sec->used_by_bfd)
18298     {
18299       _arm_elf_section_data *sdata;
18300       bfd_size_type amt = sizeof (*sdata);
18301
18302       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
18303       if (sdata == NULL)
18304         return FALSE;
18305       sec->used_by_bfd = sdata;
18306     }
18307
18308   return _bfd_elf_new_section_hook (abfd, sec);
18309 }
18310
18311
18312 /* Used to order a list of mapping symbols by address.  */
18313
18314 static int
18315 elf32_arm_compare_mapping (const void * a, const void * b)
18316 {
18317   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
18318   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
18319
18320   if (amap->vma > bmap->vma)
18321     return 1;
18322   else if (amap->vma < bmap->vma)
18323     return -1;
18324   else if (amap->type > bmap->type)
18325     /* Ensure results do not depend on the host qsort for objects with
18326        multiple mapping symbols at the same address by sorting on type
18327        after vma.  */
18328     return 1;
18329   else if (amap->type < bmap->type)
18330     return -1;
18331   else
18332     return 0;
18333 }
18334
18335 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
18336
18337 static unsigned long
18338 offset_prel31 (unsigned long addr, bfd_vma offset)
18339 {
18340   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
18341 }
18342
18343 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
18344    relocations.  */
18345
18346 static void
18347 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
18348 {
18349   unsigned long first_word = bfd_get_32 (output_bfd, from);
18350   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
18351
18352   /* High bit of first word is supposed to be zero.  */
18353   if ((first_word & 0x80000000ul) == 0)
18354     first_word = offset_prel31 (first_word, offset);
18355
18356   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
18357      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
18358   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
18359     second_word = offset_prel31 (second_word, offset);
18360
18361   bfd_put_32 (output_bfd, first_word, to);
18362   bfd_put_32 (output_bfd, second_word, to + 4);
18363 }
18364
18365 /* Data for make_branch_to_a8_stub().  */
18366
18367 struct a8_branch_to_stub_data
18368 {
18369   asection *writing_section;
18370   bfd_byte *contents;
18371 };
18372
18373
18374 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
18375    places for a particular section.  */
18376
18377 static bfd_boolean
18378 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
18379                        void *in_arg)
18380 {
18381   struct elf32_arm_stub_hash_entry *stub_entry;
18382   struct a8_branch_to_stub_data *data;
18383   bfd_byte *contents;
18384   unsigned long branch_insn;
18385   bfd_vma veneered_insn_loc, veneer_entry_loc;
18386   bfd_signed_vma branch_offset;
18387   bfd *abfd;
18388   unsigned int loc;
18389
18390   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
18391   data = (struct a8_branch_to_stub_data *) in_arg;
18392
18393   if (stub_entry->target_section != data->writing_section
18394       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
18395     return TRUE;
18396
18397   contents = data->contents;
18398
18399   /* We use target_section as Cortex-A8 erratum workaround stubs are only
18400      generated when both source and target are in the same section.  */
18401   veneered_insn_loc = stub_entry->target_section->output_section->vma
18402                       + stub_entry->target_section->output_offset
18403                       + stub_entry->source_value;
18404
18405   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
18406                      + stub_entry->stub_sec->output_offset
18407                      + stub_entry->stub_offset;
18408
18409   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
18410     veneered_insn_loc &= ~3u;
18411
18412   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
18413
18414   abfd = stub_entry->target_section->owner;
18415   loc = stub_entry->source_value;
18416
18417   /* We attempt to avoid this condition by setting stubs_always_after_branch
18418      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
18419      This check is just to be on the safe side...  */
18420   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
18421     {
18422       _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub is "
18423                             "allocated in unsafe location"), abfd);
18424       return FALSE;
18425     }
18426
18427   switch (stub_entry->stub_type)
18428     {
18429     case arm_stub_a8_veneer_b:
18430     case arm_stub_a8_veneer_b_cond:
18431       branch_insn = 0xf0009000;
18432       goto jump24;
18433
18434     case arm_stub_a8_veneer_blx:
18435       branch_insn = 0xf000e800;
18436       goto jump24;
18437
18438     case arm_stub_a8_veneer_bl:
18439       {
18440         unsigned int i1, j1, i2, j2, s;
18441
18442         branch_insn = 0xf000d000;
18443
18444       jump24:
18445         if (branch_offset < -16777216 || branch_offset > 16777214)
18446           {
18447             /* There's not much we can do apart from complain if this
18448                happens.  */
18449             _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub out "
18450                                   "of range (input file too large)"), abfd);
18451             return FALSE;
18452           }
18453
18454         /* i1 = not(j1 eor s), so:
18455            not i1 = j1 eor s
18456            j1 = (not i1) eor s.  */
18457
18458         branch_insn |= (branch_offset >> 1) & 0x7ff;
18459         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
18460         i2 = (branch_offset >> 22) & 1;
18461         i1 = (branch_offset >> 23) & 1;
18462         s = (branch_offset >> 24) & 1;
18463         j1 = (!i1) ^ s;
18464         j2 = (!i2) ^ s;
18465         branch_insn |= j2 << 11;
18466         branch_insn |= j1 << 13;
18467         branch_insn |= s << 26;
18468       }
18469       break;
18470
18471     default:
18472       BFD_FAIL ();
18473       return FALSE;
18474     }
18475
18476   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[loc]);
18477   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[loc + 2]);
18478
18479   return TRUE;
18480 }
18481
18482 /* Beginning of stm32l4xx work-around.  */
18483
18484 /* Functions encoding instructions necessary for the emission of the
18485    fix-stm32l4xx-629360.
18486    Encoding is extracted from the
18487    ARM (C) Architecture Reference Manual
18488    ARMv7-A and ARMv7-R edition
18489    ARM DDI 0406C.b (ID072512).  */
18490
18491 static inline bfd_vma
18492 create_instruction_branch_absolute (int branch_offset)
18493 {
18494   /* A8.8.18 B (A8-334)
18495      B target_address (Encoding T4).  */
18496   /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
18497   /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
18498   /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
18499
18500   int s = ((branch_offset & 0x1000000) >> 24);
18501   int j1 = s ^ !((branch_offset & 0x800000) >> 23);
18502   int j2 = s ^ !((branch_offset & 0x400000) >> 22);
18503
18504   if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
18505     BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
18506
18507   bfd_vma patched_inst = 0xf0009000
18508     | s << 26 /* S.  */
18509     | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
18510     | j1 << 13 /* J1.  */
18511     | j2 << 11 /* J2.  */
18512     | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
18513
18514   return patched_inst;
18515 }
18516
18517 static inline bfd_vma
18518 create_instruction_ldmia (int base_reg, int wback, int reg_mask)
18519 {
18520   /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
18521      LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
18522   bfd_vma patched_inst = 0xe8900000
18523     | (/*W=*/wback << 21)
18524     | (base_reg << 16)
18525     | (reg_mask & 0x0000ffff);
18526
18527   return patched_inst;
18528 }
18529
18530 static inline bfd_vma
18531 create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
18532 {
18533   /* A8.8.60 LDMDB/LDMEA (A8-402)
18534      LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
18535   bfd_vma patched_inst = 0xe9100000
18536     | (/*W=*/wback << 21)
18537     | (base_reg << 16)
18538     | (reg_mask & 0x0000ffff);
18539
18540   return patched_inst;
18541 }
18542
18543 static inline bfd_vma
18544 create_instruction_mov (int target_reg, int source_reg)
18545 {
18546   /* A8.8.103 MOV (register) (A8-486)
18547      MOV Rd, Rm (Encoding T1).  */
18548   bfd_vma patched_inst = 0x4600
18549     | (target_reg & 0x7)
18550     | ((target_reg & 0x8) >> 3) << 7
18551     | (source_reg << 3);
18552
18553   return patched_inst;
18554 }
18555
18556 static inline bfd_vma
18557 create_instruction_sub (int target_reg, int source_reg, int value)
18558 {
18559   /* A8.8.221 SUB (immediate) (A8-708)
18560      SUB Rd, Rn, #value (Encoding T3).  */
18561   bfd_vma patched_inst = 0xf1a00000
18562     | (target_reg << 8)
18563     | (source_reg << 16)
18564     | (/*S=*/0 << 20)
18565     | ((value & 0x800) >> 11) << 26
18566     | ((value & 0x700) >>  8) << 12
18567     | (value & 0x0ff);
18568
18569   return patched_inst;
18570 }
18571
18572 static inline bfd_vma
18573 create_instruction_vldmia (int base_reg, int is_dp, int wback, int num_words,
18574                            int first_reg)
18575 {
18576   /* A8.8.332 VLDM (A8-922)
18577      VLMD{MODE} Rn{!}, {list} (Encoding T1 or T2).  */
18578   bfd_vma patched_inst = (is_dp ? 0xec900b00 : 0xec900a00)
18579     | (/*W=*/wback << 21)
18580     | (base_reg << 16)
18581     | (num_words & 0x000000ff)
18582     | (((unsigned)first_reg >> 1) & 0x0000000f) << 12
18583     | (first_reg & 0x00000001) << 22;
18584
18585   return patched_inst;
18586 }
18587
18588 static inline bfd_vma
18589 create_instruction_vldmdb (int base_reg, int is_dp, int num_words,
18590                            int first_reg)
18591 {
18592   /* A8.8.332 VLDM (A8-922)
18593      VLMD{MODE} Rn!, {} (Encoding T1 or T2).  */
18594   bfd_vma patched_inst = (is_dp ? 0xed300b00 : 0xed300a00)
18595     | (base_reg << 16)
18596     | (num_words & 0x000000ff)
18597     | (((unsigned)first_reg >>1 ) & 0x0000000f) << 12
18598     | (first_reg & 0x00000001) << 22;
18599
18600   return patched_inst;
18601 }
18602
18603 static inline bfd_vma
18604 create_instruction_udf_w (int value)
18605 {
18606   /* A8.8.247 UDF (A8-758)
18607      Undefined (Encoding T2).  */
18608   bfd_vma patched_inst = 0xf7f0a000
18609     | (value & 0x00000fff)
18610     | (value & 0x000f0000) << 16;
18611
18612   return patched_inst;
18613 }
18614
18615 static inline bfd_vma
18616 create_instruction_udf (int value)
18617 {
18618   /* A8.8.247 UDF (A8-758)
18619      Undefined (Encoding T1).  */
18620   bfd_vma patched_inst = 0xde00
18621     | (value & 0xff);
18622
18623   return patched_inst;
18624 }
18625
18626 /* Functions writing an instruction in memory, returning the next
18627    memory position to write to.  */
18628
18629 static inline bfd_byte *
18630 push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
18631                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
18632 {
18633   put_thumb2_insn (htab, output_bfd, insn, pt);
18634   return pt + 4;
18635 }
18636
18637 static inline bfd_byte *
18638 push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
18639                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
18640 {
18641   put_thumb_insn (htab, output_bfd, insn, pt);
18642   return pt + 2;
18643 }
18644
18645 /* Function filling up a region in memory with T1 and T2 UDFs taking
18646    care of alignment.  */
18647
18648 static bfd_byte *
18649 stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
18650                          bfd *                   output_bfd,
18651                          const bfd_byte * const  base_stub_contents,
18652                          bfd_byte * const        from_stub_contents,
18653                          const bfd_byte * const  end_stub_contents)
18654 {
18655   bfd_byte *current_stub_contents = from_stub_contents;
18656
18657   /* Fill the remaining of the stub with deterministic contents : UDF
18658      instructions.
18659      Check if realignment is needed on modulo 4 frontier using T1, to
18660      further use T2.  */
18661   if ((current_stub_contents < end_stub_contents)
18662       && !((current_stub_contents - base_stub_contents) % 2)
18663       && ((current_stub_contents - base_stub_contents) % 4))
18664     current_stub_contents =
18665       push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18666                           create_instruction_udf (0));
18667
18668   for (; current_stub_contents < end_stub_contents;)
18669     current_stub_contents =
18670       push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18671                           create_instruction_udf_w (0));
18672
18673   return current_stub_contents;
18674 }
18675
18676 /* Functions writing the stream of instructions equivalent to the
18677    derived sequence for ldmia, ldmdb, vldm respectively.  */
18678
18679 static void
18680 stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
18681                                        bfd * output_bfd,
18682                                        const insn32 initial_insn,
18683                                        const bfd_byte *const initial_insn_addr,
18684                                        bfd_byte *const base_stub_contents)
18685 {
18686   int wback = (initial_insn & 0x00200000) >> 21;
18687   int ri, rn = (initial_insn & 0x000F0000) >> 16;
18688   int insn_all_registers = initial_insn & 0x0000ffff;
18689   int insn_low_registers, insn_high_registers;
18690   int usable_register_mask;
18691   int nb_registers = elf32_arm_popcount (insn_all_registers);
18692   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18693   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18694   bfd_byte *current_stub_contents = base_stub_contents;
18695
18696   BFD_ASSERT (is_thumb2_ldmia (initial_insn));
18697
18698   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18699      smaller than 8 registers load sequences that do not cause the
18700      hardware issue.  */
18701   if (nb_registers <= 8)
18702     {
18703       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18704       current_stub_contents =
18705         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18706                             initial_insn);
18707
18708       /* B initial_insn_addr+4.  */
18709       if (!restore_pc)
18710         current_stub_contents =
18711           push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18712                               create_instruction_branch_absolute
18713                               (initial_insn_addr - current_stub_contents));
18714
18715       /* Fill the remaining of the stub with deterministic contents.  */
18716       current_stub_contents =
18717         stm32l4xx_fill_stub_udf (htab, output_bfd,
18718                                  base_stub_contents, current_stub_contents,
18719                                  base_stub_contents +
18720                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18721
18722       return;
18723     }
18724
18725   /* - reg_list[13] == 0.  */
18726   BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
18727
18728   /* - reg_list[14] & reg_list[15] != 1.  */
18729   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18730
18731   /* - if (wback==1) reg_list[rn] == 0.  */
18732   BFD_ASSERT (!wback || !restore_rn);
18733
18734   /* - nb_registers > 8.  */
18735   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18736
18737   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18738
18739   /* In the following algorithm, we split this wide LDM using 2 LDM insns:
18740     - One with the 7 lowest registers (register mask 0x007F)
18741       This LDM will finally contain between 2 and 7 registers
18742     - One with the 7 highest registers (register mask 0xDF80)
18743       This ldm will finally contain between 2 and 7 registers.  */
18744   insn_low_registers = insn_all_registers & 0x007F;
18745   insn_high_registers = insn_all_registers & 0xDF80;
18746
18747   /* A spare register may be needed during this veneer to temporarily
18748      handle the base register.  This register will be restored with the
18749      last LDM operation.
18750      The usable register may be any general purpose register (that
18751      excludes PC, SP, LR : register mask is 0x1FFF).  */
18752   usable_register_mask = 0x1FFF;
18753
18754   /* Generate the stub function.  */
18755   if (wback)
18756     {
18757       /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
18758       current_stub_contents =
18759         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18760                             create_instruction_ldmia
18761                             (rn, /*wback=*/1, insn_low_registers));
18762
18763       /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
18764       current_stub_contents =
18765         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18766                             create_instruction_ldmia
18767                             (rn, /*wback=*/1, insn_high_registers));
18768       if (!restore_pc)
18769         {
18770           /* B initial_insn_addr+4.  */
18771           current_stub_contents =
18772             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18773                                 create_instruction_branch_absolute
18774                                 (initial_insn_addr - current_stub_contents));
18775        }
18776     }
18777   else /* if (!wback).  */
18778     {
18779       ri = rn;
18780
18781       /* If Rn is not part of the high-register-list, move it there.  */
18782       if (!(insn_high_registers & (1 << rn)))
18783         {
18784           /* Choose a Ri in the high-register-list that will be restored.  */
18785           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18786
18787           /* MOV Ri, Rn.  */
18788           current_stub_contents =
18789             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18790                                 create_instruction_mov (ri, rn));
18791         }
18792
18793       /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
18794       current_stub_contents =
18795         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18796                             create_instruction_ldmia
18797                             (ri, /*wback=*/1, insn_low_registers));
18798
18799       /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
18800       current_stub_contents =
18801         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18802                             create_instruction_ldmia
18803                             (ri, /*wback=*/0, insn_high_registers));
18804
18805       if (!restore_pc)
18806         {
18807           /* B initial_insn_addr+4.  */
18808           current_stub_contents =
18809             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18810                                 create_instruction_branch_absolute
18811                                 (initial_insn_addr - current_stub_contents));
18812         }
18813     }
18814
18815   /* Fill the remaining of the stub with deterministic contents.  */
18816   current_stub_contents =
18817     stm32l4xx_fill_stub_udf (htab, output_bfd,
18818                              base_stub_contents, current_stub_contents,
18819                              base_stub_contents +
18820                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18821 }
18822
18823 static void
18824 stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
18825                                        bfd * output_bfd,
18826                                        const insn32 initial_insn,
18827                                        const bfd_byte *const initial_insn_addr,
18828                                        bfd_byte *const base_stub_contents)
18829 {
18830   int wback = (initial_insn & 0x00200000) >> 21;
18831   int ri, rn = (initial_insn & 0x000f0000) >> 16;
18832   int insn_all_registers = initial_insn & 0x0000ffff;
18833   int insn_low_registers, insn_high_registers;
18834   int usable_register_mask;
18835   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18836   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18837   int nb_registers = elf32_arm_popcount (insn_all_registers);
18838   bfd_byte *current_stub_contents = base_stub_contents;
18839
18840   BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
18841
18842   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18843      smaller than 8 registers load sequences that do not cause the
18844      hardware issue.  */
18845   if (nb_registers <= 8)
18846     {
18847       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18848       current_stub_contents =
18849         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18850                             initial_insn);
18851
18852       /* B initial_insn_addr+4.  */
18853       current_stub_contents =
18854         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18855                             create_instruction_branch_absolute
18856                             (initial_insn_addr - current_stub_contents));
18857
18858       /* Fill the remaining of the stub with deterministic contents.  */
18859       current_stub_contents =
18860         stm32l4xx_fill_stub_udf (htab, output_bfd,
18861                                  base_stub_contents, current_stub_contents,
18862                                  base_stub_contents +
18863                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18864
18865       return;
18866     }
18867
18868   /* - reg_list[13] == 0.  */
18869   BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
18870
18871   /* - reg_list[14] & reg_list[15] != 1.  */
18872   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18873
18874   /* - if (wback==1) reg_list[rn] == 0.  */
18875   BFD_ASSERT (!wback || !restore_rn);
18876
18877   /* - nb_registers > 8.  */
18878   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18879
18880   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18881
18882   /* In the following algorithm, we split this wide LDM using 2 LDM insn:
18883     - One with the 7 lowest registers (register mask 0x007F)
18884       This LDM will finally contain between 2 and 7 registers
18885     - One with the 7 highest registers (register mask 0xDF80)
18886       This ldm will finally contain between 2 and 7 registers.  */
18887   insn_low_registers = insn_all_registers & 0x007F;
18888   insn_high_registers = insn_all_registers & 0xDF80;
18889
18890   /* A spare register may be needed during this veneer to temporarily
18891      handle the base register.  This register will be restored with
18892      the last LDM operation.
18893      The usable register may be any general purpose register (that excludes
18894      PC, SP, LR : register mask is 0x1FFF).  */
18895   usable_register_mask = 0x1FFF;
18896
18897   /* Generate the stub function.  */
18898   if (!wback && !restore_pc && !restore_rn)
18899     {
18900       /* Choose a Ri in the low-register-list that will be restored.  */
18901       ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
18902
18903       /* MOV Ri, Rn.  */
18904       current_stub_contents =
18905         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18906                             create_instruction_mov (ri, rn));
18907
18908       /* LDMDB Ri!, {R-high-register-list}.  */
18909       current_stub_contents =
18910         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18911                             create_instruction_ldmdb
18912                             (ri, /*wback=*/1, insn_high_registers));
18913
18914       /* LDMDB Ri, {R-low-register-list}.  */
18915       current_stub_contents =
18916         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18917                             create_instruction_ldmdb
18918                             (ri, /*wback=*/0, insn_low_registers));
18919
18920       /* B initial_insn_addr+4.  */
18921       current_stub_contents =
18922         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18923                             create_instruction_branch_absolute
18924                             (initial_insn_addr - current_stub_contents));
18925     }
18926   else if (wback && !restore_pc && !restore_rn)
18927     {
18928       /* LDMDB Rn!, {R-high-register-list}.  */
18929       current_stub_contents =
18930         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18931                             create_instruction_ldmdb
18932                             (rn, /*wback=*/1, insn_high_registers));
18933
18934       /* LDMDB Rn!, {R-low-register-list}.  */
18935       current_stub_contents =
18936         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18937                             create_instruction_ldmdb
18938                             (rn, /*wback=*/1, insn_low_registers));
18939
18940       /* B initial_insn_addr+4.  */
18941       current_stub_contents =
18942         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18943                             create_instruction_branch_absolute
18944                             (initial_insn_addr - current_stub_contents));
18945     }
18946   else if (!wback && restore_pc && !restore_rn)
18947     {
18948       /* Choose a Ri in the high-register-list that will be restored.  */
18949       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18950
18951       /* SUB Ri, Rn, #(4*nb_registers).  */
18952       current_stub_contents =
18953         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18954                             create_instruction_sub (ri, rn, (4 * nb_registers)));
18955
18956       /* LDMIA Ri!, {R-low-register-list}.  */
18957       current_stub_contents =
18958         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18959                             create_instruction_ldmia
18960                             (ri, /*wback=*/1, insn_low_registers));
18961
18962       /* LDMIA Ri, {R-high-register-list}.  */
18963       current_stub_contents =
18964         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18965                             create_instruction_ldmia
18966                             (ri, /*wback=*/0, insn_high_registers));
18967     }
18968   else if (wback && restore_pc && !restore_rn)
18969     {
18970       /* Choose a Ri in the high-register-list that will be restored.  */
18971       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18972
18973       /* SUB Rn, Rn, #(4*nb_registers)  */
18974       current_stub_contents =
18975         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18976                             create_instruction_sub (rn, rn, (4 * nb_registers)));
18977
18978       /* MOV Ri, Rn.  */
18979       current_stub_contents =
18980         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18981                             create_instruction_mov (ri, rn));
18982
18983       /* LDMIA Ri!, {R-low-register-list}.  */
18984       current_stub_contents =
18985         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18986                             create_instruction_ldmia
18987                             (ri, /*wback=*/1, insn_low_registers));
18988
18989       /* LDMIA Ri, {R-high-register-list}.  */
18990       current_stub_contents =
18991         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18992                             create_instruction_ldmia
18993                             (ri, /*wback=*/0, insn_high_registers));
18994     }
18995   else if (!wback && !restore_pc && restore_rn)
18996     {
18997       ri = rn;
18998       if (!(insn_low_registers & (1 << rn)))
18999         {
19000           /* Choose a Ri in the low-register-list that will be restored.  */
19001           ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
19002
19003           /* MOV Ri, Rn.  */
19004           current_stub_contents =
19005             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19006                                 create_instruction_mov (ri, rn));
19007         }
19008
19009       /* LDMDB Ri!, {R-high-register-list}.  */
19010       current_stub_contents =
19011         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19012                             create_instruction_ldmdb
19013                             (ri, /*wback=*/1, insn_high_registers));
19014
19015       /* LDMDB Ri, {R-low-register-list}.  */
19016       current_stub_contents =
19017         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19018                             create_instruction_ldmdb
19019                             (ri, /*wback=*/0, insn_low_registers));
19020
19021       /* B initial_insn_addr+4.  */
19022       current_stub_contents =
19023         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19024                             create_instruction_branch_absolute
19025                             (initial_insn_addr - current_stub_contents));
19026     }
19027   else if (!wback && restore_pc && restore_rn)
19028     {
19029       ri = rn;
19030       if (!(insn_high_registers & (1 << rn)))
19031         {
19032           /* Choose a Ri in the high-register-list that will be restored.  */
19033           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19034         }
19035
19036       /* SUB Ri, Rn, #(4*nb_registers).  */
19037       current_stub_contents =
19038         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19039                             create_instruction_sub (ri, rn, (4 * nb_registers)));
19040
19041       /* LDMIA Ri!, {R-low-register-list}.  */
19042       current_stub_contents =
19043         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19044                             create_instruction_ldmia
19045                             (ri, /*wback=*/1, insn_low_registers));
19046
19047       /* LDMIA Ri, {R-high-register-list}.  */
19048       current_stub_contents =
19049         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19050                             create_instruction_ldmia
19051                             (ri, /*wback=*/0, insn_high_registers));
19052     }
19053   else if (wback && restore_rn)
19054     {
19055       /* The assembler should not have accepted to encode this.  */
19056       BFD_ASSERT (0 && "Cannot patch an instruction that has an "
19057         "undefined behavior.\n");
19058     }
19059
19060   /* Fill the remaining of the stub with deterministic contents.  */
19061   current_stub_contents =
19062     stm32l4xx_fill_stub_udf (htab, output_bfd,
19063                              base_stub_contents, current_stub_contents,
19064                              base_stub_contents +
19065                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
19066
19067 }
19068
19069 static void
19070 stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
19071                                       bfd * output_bfd,
19072                                       const insn32 initial_insn,
19073                                       const bfd_byte *const initial_insn_addr,
19074                                       bfd_byte *const base_stub_contents)
19075 {
19076   int num_words = ((unsigned int) initial_insn << 24) >> 24;
19077   bfd_byte *current_stub_contents = base_stub_contents;
19078
19079   BFD_ASSERT (is_thumb2_vldm (initial_insn));
19080
19081   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
19082      smaller than 8 words load sequences that do not cause the
19083      hardware issue.  */
19084   if (num_words <= 8)
19085     {
19086       /* Untouched instruction.  */
19087       current_stub_contents =
19088         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19089                             initial_insn);
19090
19091       /* B initial_insn_addr+4.  */
19092       current_stub_contents =
19093         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19094                             create_instruction_branch_absolute
19095                             (initial_insn_addr - current_stub_contents));
19096     }
19097   else
19098     {
19099       bfd_boolean is_dp = /* DP encoding.  */
19100         (initial_insn & 0xfe100f00) == 0xec100b00;
19101       bfd_boolean is_ia_nobang = /* (IA without !).  */
19102         (((initial_insn << 7) >> 28) & 0xd) == 0x4;
19103       bfd_boolean is_ia_bang = /* (IA with !) - includes VPOP.  */
19104         (((initial_insn << 7) >> 28) & 0xd) == 0x5;
19105       bfd_boolean is_db_bang = /* (DB with !).  */
19106         (((initial_insn << 7) >> 28) & 0xd) == 0x9;
19107       int base_reg = ((unsigned int) initial_insn << 12) >> 28;
19108       /* d = UInt (Vd:D);.  */
19109       int first_reg = ((((unsigned int) initial_insn << 16) >> 28) << 1)
19110         | (((unsigned int)initial_insn << 9) >> 31);
19111
19112       /* Compute the number of 8-words chunks needed to split.  */
19113       int chunks = (num_words % 8) ? (num_words / 8 + 1) : (num_words / 8);
19114       int chunk;
19115
19116       /* The test coverage has been done assuming the following
19117          hypothesis that exactly one of the previous is_ predicates is
19118          true.  */
19119       BFD_ASSERT (    (is_ia_nobang ^ is_ia_bang ^ is_db_bang)
19120                   && !(is_ia_nobang & is_ia_bang & is_db_bang));
19121
19122       /* We treat the cutting of the words in one pass for all
19123          cases, then we emit the adjustments:
19124
19125          vldm rx, {...}
19126          -> vldm rx!, {8_words_or_less} for each needed 8_word
19127          -> sub rx, rx, #size (list)
19128
19129          vldm rx!, {...}
19130          -> vldm rx!, {8_words_or_less} for each needed 8_word
19131          This also handles vpop instruction (when rx is sp)
19132
19133          vldmd rx!, {...}
19134          -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
19135       for (chunk = 0; chunk < chunks; ++chunk)
19136         {
19137           bfd_vma new_insn = 0;
19138
19139           if (is_ia_nobang || is_ia_bang)
19140             {
19141               new_insn = create_instruction_vldmia
19142                 (base_reg,
19143                  is_dp,
19144                  /*wback= .  */1,
19145                  chunks - (chunk + 1) ?
19146                  8 : num_words - chunk * 8,
19147                  first_reg + chunk * 8);
19148             }
19149           else if (is_db_bang)
19150             {
19151               new_insn = create_instruction_vldmdb
19152                 (base_reg,
19153                  is_dp,
19154                  chunks - (chunk + 1) ?
19155                  8 : num_words - chunk * 8,
19156                  first_reg + chunk * 8);
19157             }
19158
19159           if (new_insn)
19160             current_stub_contents =
19161               push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19162                                   new_insn);
19163         }
19164
19165       /* Only this case requires the base register compensation
19166          subtract.  */
19167       if (is_ia_nobang)
19168         {
19169           current_stub_contents =
19170             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19171                                 create_instruction_sub
19172                                 (base_reg, base_reg, 4*num_words));
19173         }
19174
19175       /* B initial_insn_addr+4.  */
19176       current_stub_contents =
19177         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19178                             create_instruction_branch_absolute
19179                             (initial_insn_addr - current_stub_contents));
19180     }
19181
19182   /* Fill the remaining of the stub with deterministic contents.  */
19183   current_stub_contents =
19184     stm32l4xx_fill_stub_udf (htab, output_bfd,
19185                              base_stub_contents, current_stub_contents,
19186                              base_stub_contents +
19187                              STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
19188 }
19189
19190 static void
19191 stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
19192                                  bfd * output_bfd,
19193                                  const insn32 wrong_insn,
19194                                  const bfd_byte *const wrong_insn_addr,
19195                                  bfd_byte *const stub_contents)
19196 {
19197   if (is_thumb2_ldmia (wrong_insn))
19198     stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
19199                                            wrong_insn, wrong_insn_addr,
19200                                            stub_contents);
19201   else if (is_thumb2_ldmdb (wrong_insn))
19202     stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
19203                                            wrong_insn, wrong_insn_addr,
19204                                            stub_contents);
19205   else if (is_thumb2_vldm (wrong_insn))
19206     stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
19207                                           wrong_insn, wrong_insn_addr,
19208                                           stub_contents);
19209 }
19210
19211 /* End of stm32l4xx work-around.  */
19212
19213
19214 /* Do code byteswapping.  Return FALSE afterwards so that the section is
19215    written out as normal.  */
19216
19217 static bfd_boolean
19218 elf32_arm_write_section (bfd *output_bfd,
19219                          struct bfd_link_info *link_info,
19220                          asection *sec,
19221                          bfd_byte *contents)
19222 {
19223   unsigned int mapcount, errcount;
19224   _arm_elf_section_data *arm_data;
19225   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
19226   elf32_arm_section_map *map;
19227   elf32_vfp11_erratum_list *errnode;
19228   elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
19229   bfd_vma ptr;
19230   bfd_vma end;
19231   bfd_vma offset = sec->output_section->vma + sec->output_offset;
19232   bfd_byte tmp;
19233   unsigned int i;
19234
19235   if (globals == NULL)
19236     return FALSE;
19237
19238   /* If this section has not been allocated an _arm_elf_section_data
19239      structure then we cannot record anything.  */
19240   arm_data = get_arm_elf_section_data (sec);
19241   if (arm_data == NULL)
19242     return FALSE;
19243
19244   mapcount = arm_data->mapcount;
19245   map = arm_data->map;
19246   errcount = arm_data->erratumcount;
19247
19248   if (errcount != 0)
19249     {
19250       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
19251
19252       for (errnode = arm_data->erratumlist; errnode != 0;
19253            errnode = errnode->next)
19254         {
19255           bfd_vma target = errnode->vma - offset;
19256
19257           switch (errnode->type)
19258             {
19259             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
19260               {
19261                 bfd_vma branch_to_veneer;
19262                 /* Original condition code of instruction, plus bit mask for
19263                    ARM B instruction.  */
19264                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
19265                                   | 0x0a000000;
19266
19267                 /* The instruction is before the label.  */
19268                 target -= 4;
19269
19270                 /* Above offset included in -4 below.  */
19271                 branch_to_veneer = errnode->u.b.veneer->vma
19272                                    - errnode->vma - 4;
19273
19274                 if ((signed) branch_to_veneer < -(1 << 25)
19275                     || (signed) branch_to_veneer >= (1 << 25))
19276                   _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19277                                         "range"), output_bfd);
19278
19279                 insn |= (branch_to_veneer >> 2) & 0xffffff;
19280                 contents[endianflip ^ target] = insn & 0xff;
19281                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19282                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19283                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19284               }
19285               break;
19286
19287             case VFP11_ERRATUM_ARM_VENEER:
19288               {
19289                 bfd_vma branch_from_veneer;
19290                 unsigned int insn;
19291
19292                 /* Take size of veneer into account.  */
19293                 branch_from_veneer = errnode->u.v.branch->vma
19294                                      - errnode->vma - 12;
19295
19296                 if ((signed) branch_from_veneer < -(1 << 25)
19297                     || (signed) branch_from_veneer >= (1 << 25))
19298                   _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19299                                         "range"), output_bfd);
19300
19301                 /* Original instruction.  */
19302                 insn = errnode->u.v.branch->u.b.vfp_insn;
19303                 contents[endianflip ^ target] = insn & 0xff;
19304                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19305                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19306                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19307
19308                 /* Branch back to insn after original insn.  */
19309                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
19310                 contents[endianflip ^ (target + 4)] = insn & 0xff;
19311                 contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
19312                 contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
19313                 contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
19314               }
19315               break;
19316
19317             default:
19318               abort ();
19319             }
19320         }
19321     }
19322
19323   if (arm_data->stm32l4xx_erratumcount != 0)
19324     {
19325       for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
19326            stm32l4xx_errnode != 0;
19327            stm32l4xx_errnode = stm32l4xx_errnode->next)
19328         {
19329           bfd_vma target = stm32l4xx_errnode->vma - offset;
19330
19331           switch (stm32l4xx_errnode->type)
19332             {
19333             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
19334               {
19335                 unsigned int insn;
19336                 bfd_vma branch_to_veneer =
19337                   stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
19338
19339                 if ((signed) branch_to_veneer < -(1 << 24)
19340                     || (signed) branch_to_veneer >= (1 << 24))
19341                   {
19342                     bfd_vma out_of_range =
19343                       ((signed) branch_to_veneer < -(1 << 24)) ?
19344                       - branch_to_veneer - (1 << 24) :
19345                       ((signed) branch_to_veneer >= (1 << 24)) ?
19346                       branch_to_veneer - (1 << 24) : 0;
19347
19348                     _bfd_error_handler
19349                       (_("%pB(%#" PRIx64 "): error: "
19350                          "cannot create STM32L4XX veneer; "
19351                          "jump out of range by %" PRId64 " bytes; "
19352                          "cannot encode branch instruction"),
19353                        output_bfd,
19354                        (uint64_t) (stm32l4xx_errnode->vma - 4),
19355                        (int64_t) out_of_range);
19356                     continue;
19357                   }
19358
19359                 insn = create_instruction_branch_absolute
19360                   (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
19361
19362                 /* The instruction is before the label.  */
19363                 target -= 4;
19364
19365                 put_thumb2_insn (globals, output_bfd,
19366                                  (bfd_vma) insn, contents + target);
19367               }
19368               break;
19369
19370             case STM32L4XX_ERRATUM_VENEER:
19371               {
19372                 bfd_byte * veneer;
19373                 bfd_byte * veneer_r;
19374                 unsigned int insn;
19375
19376                 veneer = contents + target;
19377                 veneer_r = veneer
19378                   + stm32l4xx_errnode->u.b.veneer->vma
19379                   - stm32l4xx_errnode->vma - 4;
19380
19381                 if ((signed) (veneer_r - veneer -
19382                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
19383                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
19384                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
19385                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
19386                     || (signed) (veneer_r - veneer) >= (1 << 24))
19387                   {
19388                     _bfd_error_handler (_("%pB: error: cannot create STM32L4XX "
19389                                           "veneer"), output_bfd);
19390                      continue;
19391                   }
19392
19393                 /* Original instruction.  */
19394                 insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
19395
19396                 stm32l4xx_create_replacing_stub
19397                   (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
19398               }
19399               break;
19400
19401             default:
19402               abort ();
19403             }
19404         }
19405     }
19406
19407   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
19408     {
19409       arm_unwind_table_edit *edit_node
19410         = arm_data->u.exidx.unwind_edit_list;
19411       /* Now, sec->size is the size of the section we will write.  The original
19412          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
19413          markers) was sec->rawsize.  (This isn't the case if we perform no
19414          edits, then rawsize will be zero and we should use size).  */
19415       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
19416       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
19417       unsigned int in_index, out_index;
19418       bfd_vma add_to_offsets = 0;
19419
19420       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
19421         {
19422           if (edit_node)
19423             {
19424               unsigned int edit_index = edit_node->index;
19425
19426               if (in_index < edit_index && in_index * 8 < input_size)
19427                 {
19428                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19429                                     contents + in_index * 8, add_to_offsets);
19430                   out_index++;
19431                   in_index++;
19432                 }
19433               else if (in_index == edit_index
19434                        || (in_index * 8 >= input_size
19435                            && edit_index == UINT_MAX))
19436                 {
19437                   switch (edit_node->type)
19438                     {
19439                     case DELETE_EXIDX_ENTRY:
19440                       in_index++;
19441                       add_to_offsets += 8;
19442                       break;
19443
19444                     case INSERT_EXIDX_CANTUNWIND_AT_END:
19445                       {
19446                         asection *text_sec = edit_node->linked_section;
19447                         bfd_vma text_offset = text_sec->output_section->vma
19448                                               + text_sec->output_offset
19449                                               + text_sec->size;
19450                         bfd_vma exidx_offset = offset + out_index * 8;
19451                         unsigned long prel31_offset;
19452
19453                         /* Note: this is meant to be equivalent to an
19454                            R_ARM_PREL31 relocation.  These synthetic
19455                            EXIDX_CANTUNWIND markers are not relocated by the
19456                            usual BFD method.  */
19457                         prel31_offset = (text_offset - exidx_offset)
19458                                         & 0x7ffffffful;
19459                         if (bfd_link_relocatable (link_info))
19460                           {
19461                             /* Here relocation for new EXIDX_CANTUNWIND is
19462                                created, so there is no need to
19463                                adjust offset by hand.  */
19464                             prel31_offset = text_sec->output_offset
19465                                             + text_sec->size;
19466                           }
19467
19468                         /* First address we can't unwind.  */
19469                         bfd_put_32 (output_bfd, prel31_offset,
19470                                     &edited_contents[out_index * 8]);
19471
19472                         /* Code for EXIDX_CANTUNWIND.  */
19473                         bfd_put_32 (output_bfd, 0x1,
19474                                     &edited_contents[out_index * 8 + 4]);
19475
19476                         out_index++;
19477                         add_to_offsets -= 8;
19478                       }
19479                       break;
19480                     }
19481
19482                   edit_node = edit_node->next;
19483                 }
19484             }
19485           else
19486             {
19487               /* No more edits, copy remaining entries verbatim.  */
19488               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19489                                 contents + in_index * 8, add_to_offsets);
19490               out_index++;
19491               in_index++;
19492             }
19493         }
19494
19495       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
19496         bfd_set_section_contents (output_bfd, sec->output_section,
19497                                   edited_contents,
19498                                   (file_ptr) sec->output_offset, sec->size);
19499
19500       return TRUE;
19501     }
19502
19503   /* Fix code to point to Cortex-A8 erratum stubs.  */
19504   if (globals->fix_cortex_a8)
19505     {
19506       struct a8_branch_to_stub_data data;
19507
19508       data.writing_section = sec;
19509       data.contents = contents;
19510
19511       bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
19512                          & data);
19513     }
19514
19515   if (mapcount == 0)
19516     return FALSE;
19517
19518   if (globals->byteswap_code)
19519     {
19520       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
19521
19522       ptr = map[0].vma;
19523       for (i = 0; i < mapcount; i++)
19524         {
19525           if (i == mapcount - 1)
19526             end = sec->size;
19527           else
19528             end = map[i + 1].vma;
19529
19530           switch (map[i].type)
19531             {
19532             case 'a':
19533               /* Byte swap code words.  */
19534               while (ptr + 3 < end)
19535                 {
19536                   tmp = contents[ptr];
19537                   contents[ptr] = contents[ptr + 3];
19538                   contents[ptr + 3] = tmp;
19539                   tmp = contents[ptr + 1];
19540                   contents[ptr + 1] = contents[ptr + 2];
19541                   contents[ptr + 2] = tmp;
19542                   ptr += 4;
19543                 }
19544               break;
19545
19546             case 't':
19547               /* Byte swap code halfwords.  */
19548               while (ptr + 1 < end)
19549                 {
19550                   tmp = contents[ptr];
19551                   contents[ptr] = contents[ptr + 1];
19552                   contents[ptr + 1] = tmp;
19553                   ptr += 2;
19554                 }
19555               break;
19556
19557             case 'd':
19558               /* Leave data alone.  */
19559               break;
19560             }
19561           ptr = end;
19562         }
19563     }
19564
19565   free (map);
19566   arm_data->mapcount = -1;
19567   arm_data->mapsize = 0;
19568   arm_data->map = NULL;
19569
19570   return FALSE;
19571 }
19572
19573 /* Mangle thumb function symbols as we read them in.  */
19574
19575 static bfd_boolean
19576 elf32_arm_swap_symbol_in (bfd * abfd,
19577                           const void *psrc,
19578                           const void *pshn,
19579                           Elf_Internal_Sym *dst)
19580 {
19581   Elf_Internal_Shdr *symtab_hdr;
19582   const char *name = NULL;
19583
19584   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
19585     return FALSE;
19586   dst->st_target_internal = 0;
19587
19588   /* New EABI objects mark thumb function symbols by setting the low bit of
19589      the address.  */
19590   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
19591       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
19592     {
19593       if (dst->st_value & 1)
19594         {
19595           dst->st_value &= ~(bfd_vma) 1;
19596           ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal,
19597                                    ST_BRANCH_TO_THUMB);
19598         }
19599       else
19600         ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_ARM);
19601     }
19602   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
19603     {
19604       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
19605       ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_THUMB);
19606     }
19607   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
19608     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_LONG);
19609   else
19610     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_UNKNOWN);
19611
19612   /* Mark CMSE special symbols.  */
19613   symtab_hdr = & elf_symtab_hdr (abfd);
19614   if (symtab_hdr->sh_size)
19615     name = bfd_elf_sym_name (abfd, symtab_hdr, dst, NULL);
19616   if (name && CONST_STRNEQ (name, CMSE_PREFIX))
19617     ARM_SET_SYM_CMSE_SPCL (dst->st_target_internal);
19618
19619   return TRUE;
19620 }
19621
19622
19623 /* Mangle thumb function symbols as we write them out.  */
19624
19625 static void
19626 elf32_arm_swap_symbol_out (bfd *abfd,
19627                            const Elf_Internal_Sym *src,
19628                            void *cdst,
19629                            void *shndx)
19630 {
19631   Elf_Internal_Sym newsym;
19632
19633   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
19634      of the address set, as per the new EABI.  We do this unconditionally
19635      because objcopy does not set the elf header flags until after
19636      it writes out the symbol table.  */
19637   if (ARM_GET_SYM_BRANCH_TYPE (src->st_target_internal) == ST_BRANCH_TO_THUMB)
19638     {
19639       newsym = *src;
19640       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
19641         newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
19642       if (newsym.st_shndx != SHN_UNDEF)
19643         {
19644           /* Do this only for defined symbols. At link type, the static
19645              linker will simulate the work of dynamic linker of resolving
19646              symbols and will carry over the thumbness of found symbols to
19647              the output symbol table. It's not clear how it happens, but
19648              the thumbness of undefined symbols can well be different at
19649              runtime, and writing '1' for them will be confusing for users
19650              and possibly for dynamic linker itself.
19651           */
19652           newsym.st_value |= 1;
19653         }
19654
19655       src = &newsym;
19656     }
19657   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
19658 }
19659
19660 /* Add the PT_ARM_EXIDX program header.  */
19661
19662 static bfd_boolean
19663 elf32_arm_modify_segment_map (bfd *abfd,
19664                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
19665 {
19666   struct elf_segment_map *m;
19667   asection *sec;
19668
19669   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19670   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19671     {
19672       /* If there is already a PT_ARM_EXIDX header, then we do not
19673          want to add another one.  This situation arises when running
19674          "strip"; the input binary already has the header.  */
19675       m = elf_seg_map (abfd);
19676       while (m && m->p_type != PT_ARM_EXIDX)
19677         m = m->next;
19678       if (!m)
19679         {
19680           m = (struct elf_segment_map *)
19681               bfd_zalloc (abfd, sizeof (struct elf_segment_map));
19682           if (m == NULL)
19683             return FALSE;
19684           m->p_type = PT_ARM_EXIDX;
19685           m->count = 1;
19686           m->sections[0] = sec;
19687
19688           m->next = elf_seg_map (abfd);
19689           elf_seg_map (abfd) = m;
19690         }
19691     }
19692
19693   return TRUE;
19694 }
19695
19696 /* We may add a PT_ARM_EXIDX program header.  */
19697
19698 static int
19699 elf32_arm_additional_program_headers (bfd *abfd,
19700                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
19701 {
19702   asection *sec;
19703
19704   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19705   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19706     return 1;
19707   else
19708     return 0;
19709 }
19710
19711 /* Hook called by the linker routine which adds symbols from an object
19712    file.  */
19713
19714 static bfd_boolean
19715 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
19716                            Elf_Internal_Sym *sym, const char **namep,
19717                            flagword *flagsp, asection **secp, bfd_vma *valp)
19718 {
19719   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
19720       && (abfd->flags & DYNAMIC) == 0
19721       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
19722     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
19723
19724   if (elf32_arm_hash_table (info) == NULL)
19725     return FALSE;
19726
19727   if (elf32_arm_hash_table (info)->vxworks_p
19728       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
19729                                        flagsp, secp, valp))
19730     return FALSE;
19731
19732   return TRUE;
19733 }
19734
19735 /* We use this to override swap_symbol_in and swap_symbol_out.  */
19736 const struct elf_size_info elf32_arm_size_info =
19737 {
19738   sizeof (Elf32_External_Ehdr),
19739   sizeof (Elf32_External_Phdr),
19740   sizeof (Elf32_External_Shdr),
19741   sizeof (Elf32_External_Rel),
19742   sizeof (Elf32_External_Rela),
19743   sizeof (Elf32_External_Sym),
19744   sizeof (Elf32_External_Dyn),
19745   sizeof (Elf_External_Note),
19746   4,
19747   1,
19748   32, 2,
19749   ELFCLASS32, EV_CURRENT,
19750   bfd_elf32_write_out_phdrs,
19751   bfd_elf32_write_shdrs_and_ehdr,
19752   bfd_elf32_checksum_contents,
19753   bfd_elf32_write_relocs,
19754   elf32_arm_swap_symbol_in,
19755   elf32_arm_swap_symbol_out,
19756   bfd_elf32_slurp_reloc_table,
19757   bfd_elf32_slurp_symbol_table,
19758   bfd_elf32_swap_dyn_in,
19759   bfd_elf32_swap_dyn_out,
19760   bfd_elf32_swap_reloc_in,
19761   bfd_elf32_swap_reloc_out,
19762   bfd_elf32_swap_reloca_in,
19763   bfd_elf32_swap_reloca_out
19764 };
19765
19766 static bfd_vma
19767 read_code32 (const bfd *abfd, const bfd_byte *addr)
19768 {
19769   /* V7 BE8 code is always little endian.  */
19770   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19771     return bfd_getl32 (addr);
19772
19773   return bfd_get_32 (abfd, addr);
19774 }
19775
19776 static bfd_vma
19777 read_code16 (const bfd *abfd, const bfd_byte *addr)
19778 {
19779   /* V7 BE8 code is always little endian.  */
19780   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19781     return bfd_getl16 (addr);
19782
19783   return bfd_get_16 (abfd, addr);
19784 }
19785
19786 /* Return size of plt0 entry starting at ADDR
19787    or (bfd_vma) -1 if size can not be determined.  */
19788
19789 static bfd_vma
19790 elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr)
19791 {
19792   bfd_vma first_word;
19793   bfd_vma plt0_size;
19794
19795   first_word = read_code32 (abfd, addr);
19796
19797   if (first_word == elf32_arm_plt0_entry[0])
19798     plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
19799   else if (first_word == elf32_thumb2_plt0_entry[0])
19800     plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
19801   else
19802     /* We don't yet handle this PLT format.  */
19803     return (bfd_vma) -1;
19804
19805   return plt0_size;
19806 }
19807
19808 /* Return size of plt entry starting at offset OFFSET
19809    of plt section located at address START
19810    or (bfd_vma) -1 if size can not be determined.  */
19811
19812 static bfd_vma
19813 elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset)
19814 {
19815   bfd_vma first_insn;
19816   bfd_vma plt_size = 0;
19817   const bfd_byte *addr = start + offset;
19818
19819   /* PLT entry size if fixed on Thumb-only platforms.  */
19820   if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
19821       return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
19822
19823   /* Respect Thumb stub if necessary.  */
19824   if (read_code16 (abfd, addr) == elf32_arm_plt_thumb_stub[0])
19825     {
19826       plt_size += 2 * ARRAY_SIZE(elf32_arm_plt_thumb_stub);
19827     }
19828
19829   /* Strip immediate from first add.  */
19830   first_insn = read_code32 (abfd, addr + plt_size) & 0xffffff00;
19831
19832 #ifdef FOUR_WORD_PLT
19833   if (first_insn == elf32_arm_plt_entry[0])
19834     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
19835 #else
19836   if (first_insn == elf32_arm_plt_entry_long[0])
19837     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
19838   else if (first_insn == elf32_arm_plt_entry_short[0])
19839     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
19840 #endif
19841   else
19842     /* We don't yet handle this PLT format.  */
19843     return (bfd_vma) -1;
19844
19845   return plt_size;
19846 }
19847
19848 /* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
19849
19850 static long
19851 elf32_arm_get_synthetic_symtab (bfd *abfd,
19852                                long symcount ATTRIBUTE_UNUSED,
19853                                asymbol **syms ATTRIBUTE_UNUSED,
19854                                long dynsymcount,
19855                                asymbol **dynsyms,
19856                                asymbol **ret)
19857 {
19858   asection *relplt;
19859   asymbol *s;
19860   arelent *p;
19861   long count, i, n;
19862   size_t size;
19863   Elf_Internal_Shdr *hdr;
19864   char *names;
19865   asection *plt;
19866   bfd_vma offset;
19867   bfd_byte *data;
19868
19869   *ret = NULL;
19870
19871   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
19872     return 0;
19873
19874   if (dynsymcount <= 0)
19875     return 0;
19876
19877   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
19878   if (relplt == NULL)
19879     return 0;
19880
19881   hdr = &elf_section_data (relplt)->this_hdr;
19882   if (hdr->sh_link != elf_dynsymtab (abfd)
19883       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
19884     return 0;
19885
19886   plt = bfd_get_section_by_name (abfd, ".plt");
19887   if (plt == NULL)
19888     return 0;
19889
19890   if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, TRUE))
19891     return -1;
19892
19893   data = plt->contents;
19894   if (data == NULL)
19895     {
19896       if (!bfd_get_full_section_contents(abfd, (asection *) plt, &data) || data == NULL)
19897         return -1;
19898       bfd_cache_section_contents((asection *) plt, data);
19899     }
19900
19901   count = relplt->size / hdr->sh_entsize;
19902   size = count * sizeof (asymbol);
19903   p = relplt->relocation;
19904   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19905     {
19906       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
19907       if (p->addend != 0)
19908         size += sizeof ("+0x") - 1 + 8;
19909     }
19910
19911   s = *ret = (asymbol *) bfd_malloc (size);
19912   if (s == NULL)
19913     return -1;
19914
19915   offset = elf32_arm_plt0_size (abfd, data);
19916   if (offset == (bfd_vma) -1)
19917     return -1;
19918
19919   names = (char *) (s + count);
19920   p = relplt->relocation;
19921   n = 0;
19922   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19923     {
19924       size_t len;
19925
19926       bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset);
19927       if (plt_size == (bfd_vma) -1)
19928         break;
19929
19930       *s = **p->sym_ptr_ptr;
19931       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
19932          we are defining a symbol, ensure one of them is set.  */
19933       if ((s->flags & BSF_LOCAL) == 0)
19934         s->flags |= BSF_GLOBAL;
19935       s->flags |= BSF_SYNTHETIC;
19936       s->section = plt;
19937       s->value = offset;
19938       s->name = names;
19939       s->udata.p = NULL;
19940       len = strlen ((*p->sym_ptr_ptr)->name);
19941       memcpy (names, (*p->sym_ptr_ptr)->name, len);
19942       names += len;
19943       if (p->addend != 0)
19944         {
19945           char buf[30], *a;
19946
19947           memcpy (names, "+0x", sizeof ("+0x") - 1);
19948           names += sizeof ("+0x") - 1;
19949           bfd_sprintf_vma (abfd, buf, p->addend);
19950           for (a = buf; *a == '0'; ++a)
19951             ;
19952           len = strlen (a);
19953           memcpy (names, a, len);
19954           names += len;
19955         }
19956       memcpy (names, "@plt", sizeof ("@plt"));
19957       names += sizeof ("@plt");
19958       ++s, ++n;
19959       offset += plt_size;
19960     }
19961
19962   return n;
19963 }
19964
19965 static bfd_boolean
19966 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr * hdr)
19967 {
19968   if (hdr->sh_flags & SHF_ARM_PURECODE)
19969     *flags |= SEC_ELF_PURECODE;
19970   return TRUE;
19971 }
19972
19973 static flagword
19974 elf32_arm_lookup_section_flags (char *flag_name)
19975 {
19976   if (!strcmp (flag_name, "SHF_ARM_PURECODE"))
19977     return SHF_ARM_PURECODE;
19978
19979   return SEC_NO_FLAGS;
19980 }
19981
19982 static unsigned int
19983 elf32_arm_count_additional_relocs (asection *sec)
19984 {
19985   struct _arm_elf_section_data *arm_data;
19986   arm_data = get_arm_elf_section_data (sec);
19987
19988   return arm_data == NULL ? 0 : arm_data->additional_reloc_count;
19989 }
19990
19991 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
19992    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
19993    FALSE otherwise.  ISECTION is the best guess matching section from the
19994    input bfd IBFD, but it might be NULL.  */
19995
19996 static bfd_boolean
19997 elf32_arm_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
19998                                        bfd *obfd ATTRIBUTE_UNUSED,
19999                                        const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
20000                                        Elf_Internal_Shdr *osection)
20001 {
20002   switch (osection->sh_type)
20003     {
20004     case SHT_ARM_EXIDX:
20005       {
20006         Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
20007         Elf_Internal_Shdr **iheaders = elf_elfsections (ibfd);
20008         unsigned i = 0;
20009
20010         osection->sh_flags = SHF_ALLOC | SHF_LINK_ORDER;
20011         osection->sh_info = 0;
20012
20013         /* The sh_link field must be set to the text section associated with
20014            this index section.  Unfortunately the ARM EHABI does not specify
20015            exactly how to determine this association.  Our caller does try
20016            to match up OSECTION with its corresponding input section however
20017            so that is a good first guess.  */
20018         if (isection != NULL
20019             && osection->bfd_section != NULL
20020             && isection->bfd_section != NULL
20021             && isection->bfd_section->output_section != NULL
20022             && isection->bfd_section->output_section == osection->bfd_section
20023             && iheaders != NULL
20024             && isection->sh_link > 0
20025             && isection->sh_link < elf_numsections (ibfd)
20026             && iheaders[isection->sh_link]->bfd_section != NULL
20027             && iheaders[isection->sh_link]->bfd_section->output_section != NULL
20028             )
20029           {
20030             for (i = elf_numsections (obfd); i-- > 0;)
20031               if (oheaders[i]->bfd_section
20032                   == iheaders[isection->sh_link]->bfd_section->output_section)
20033                 break;
20034           }
20035
20036         if (i == 0)
20037           {
20038             /* Failing that we have to find a matching section ourselves.  If
20039                we had the output section name available we could compare that
20040                with input section names.  Unfortunately we don't.  So instead
20041                we use a simple heuristic and look for the nearest executable
20042                section before this one.  */
20043             for (i = elf_numsections (obfd); i-- > 0;)
20044               if (oheaders[i] == osection)
20045                 break;
20046             if (i == 0)
20047               break;
20048
20049             while (i-- > 0)
20050               if (oheaders[i]->sh_type == SHT_PROGBITS
20051                   && (oheaders[i]->sh_flags & (SHF_ALLOC | SHF_EXECINSTR))
20052                   == (SHF_ALLOC | SHF_EXECINSTR))
20053                 break;
20054           }
20055
20056         if (i)
20057           {
20058             osection->sh_link = i;
20059             /* If the text section was part of a group
20060                then the index section should be too.  */
20061             if (oheaders[i]->sh_flags & SHF_GROUP)
20062               osection->sh_flags |= SHF_GROUP;
20063             return TRUE;
20064           }
20065       }
20066       break;
20067
20068     case SHT_ARM_PREEMPTMAP:
20069       osection->sh_flags = SHF_ALLOC;
20070       break;
20071
20072     case SHT_ARM_ATTRIBUTES:
20073     case SHT_ARM_DEBUGOVERLAY:
20074     case SHT_ARM_OVERLAYSECTION:
20075     default:
20076       break;
20077     }
20078
20079   return FALSE;
20080 }
20081
20082 /* Returns TRUE if NAME is an ARM mapping symbol.
20083    Traditionally the symbols $a, $d and $t have been used.
20084    The ARM ELF standard also defines $x (for A64 code).  It also allows a
20085    period initiated suffix to be added to the symbol: "$[adtx]\.[:sym_char]+".
20086    Other tools might also produce $b (Thumb BL), $f, $p, $m and $v, but we do
20087    not support them here.  $t.x indicates the start of ThumbEE instructions.  */
20088
20089 static bfd_boolean
20090 is_arm_mapping_symbol (const char * name)
20091 {
20092   return name != NULL /* Paranoia.  */
20093     && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
20094                          the mapping symbols could have acquired a prefix.
20095                          We do not support this here, since such symbols no
20096                          longer conform to the ARM ELF ABI.  */
20097     && (name[1] == 'a' || name[1] == 'd' || name[1] == 't' || name[1] == 'x')
20098     && (name[2] == 0 || name[2] == '.');
20099   /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
20100      any characters that follow the period are legal characters for the body
20101      of a symbol's name.  For now we just assume that this is the case.  */
20102 }
20103
20104 /* Make sure that mapping symbols in object files are not removed via the
20105    "strip --strip-unneeded" tool.  These symbols are needed in order to
20106    correctly generate interworking veneers, and for byte swapping code
20107    regions.  Once an object file has been linked, it is safe to remove the
20108    symbols as they will no longer be needed.  */
20109
20110 static void
20111 elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
20112 {
20113   if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
20114       && sym->section != bfd_abs_section_ptr
20115       && is_arm_mapping_symbol (sym->name))
20116     sym->flags |= BSF_KEEP;
20117 }
20118
20119 #undef  elf_backend_copy_special_section_fields
20120 #define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
20121
20122 #define ELF_ARCH                        bfd_arch_arm
20123 #define ELF_TARGET_ID                   ARM_ELF_DATA
20124 #define ELF_MACHINE_CODE                EM_ARM
20125 #ifdef __QNXTARGET__
20126 #define ELF_MAXPAGESIZE                 0x1000
20127 #else
20128 #define ELF_MAXPAGESIZE                 0x10000
20129 #endif
20130 #define ELF_MINPAGESIZE                 0x1000
20131 #define ELF_COMMONPAGESIZE              0x1000
20132
20133 #define bfd_elf32_mkobject                      elf32_arm_mkobject
20134
20135 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
20136 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
20137 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
20138 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
20139 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
20140 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
20141 #define bfd_elf32_bfd_reloc_name_lookup         elf32_arm_reloc_name_lookup
20142 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
20143 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
20144 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
20145 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
20146 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
20147 #define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
20148
20149 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
20150 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
20151 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
20152 #define elf_backend_check_relocs                elf32_arm_check_relocs
20153 #define elf_backend_update_relocs               elf32_arm_update_relocs
20154 #define elf_backend_relocate_section            elf32_arm_relocate_section
20155 #define elf_backend_write_section               elf32_arm_write_section
20156 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
20157 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
20158 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
20159 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
20160 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
20161 #define elf_backend_always_size_sections        elf32_arm_always_size_sections
20162 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
20163 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
20164 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
20165 #define elf_backend_object_p                    elf32_arm_object_p
20166 #define elf_backend_fake_sections               elf32_arm_fake_sections
20167 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
20168 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20169 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
20170 #define elf_backend_size_info                   elf32_arm_size_info
20171 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
20172 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
20173 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
20174 #define elf_backend_filter_implib_symbols       elf32_arm_filter_implib_symbols
20175 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
20176 #define elf_backend_add_symbol_hook             elf32_arm_add_symbol_hook
20177 #define elf_backend_count_additional_relocs     elf32_arm_count_additional_relocs
20178 #define elf_backend_symbol_processing           elf32_arm_backend_symbol_processing
20179
20180 #define elf_backend_can_refcount       1
20181 #define elf_backend_can_gc_sections    1
20182 #define elf_backend_plt_readonly       1
20183 #define elf_backend_want_got_plt       1
20184 #define elf_backend_want_plt_sym       0
20185 #define elf_backend_want_dynrelro      1
20186 #define elf_backend_may_use_rel_p      1
20187 #define elf_backend_may_use_rela_p     0
20188 #define elf_backend_default_use_rela_p 0
20189 #define elf_backend_dtrel_excludes_plt 1
20190
20191 #define elf_backend_got_header_size     12
20192 #define elf_backend_extern_protected_data 1
20193
20194 #undef  elf_backend_obj_attrs_vendor
20195 #define elf_backend_obj_attrs_vendor            "aeabi"
20196 #undef  elf_backend_obj_attrs_section
20197 #define elf_backend_obj_attrs_section           ".ARM.attributes"
20198 #undef  elf_backend_obj_attrs_arg_type
20199 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
20200 #undef  elf_backend_obj_attrs_section_type
20201 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
20202 #define elf_backend_obj_attrs_order             elf32_arm_obj_attrs_order
20203 #define elf_backend_obj_attrs_handle_unknown    elf32_arm_obj_attrs_handle_unknown
20204
20205 #undef  elf_backend_section_flags
20206 #define elf_backend_section_flags               elf32_arm_section_flags
20207 #undef  elf_backend_lookup_section_flags_hook
20208 #define elf_backend_lookup_section_flags_hook   elf32_arm_lookup_section_flags
20209
20210 #define elf_backend_linux_prpsinfo32_ugid16     TRUE
20211
20212 #include "elf32-target.h"
20213
20214 /* Native Client targets.  */
20215
20216 #undef  TARGET_LITTLE_SYM
20217 #define TARGET_LITTLE_SYM               arm_elf32_nacl_le_vec
20218 #undef  TARGET_LITTLE_NAME
20219 #define TARGET_LITTLE_NAME              "elf32-littlearm-nacl"
20220 #undef  TARGET_BIG_SYM
20221 #define TARGET_BIG_SYM                  arm_elf32_nacl_be_vec
20222 #undef  TARGET_BIG_NAME
20223 #define TARGET_BIG_NAME                 "elf32-bigarm-nacl"
20224
20225 /* Like elf32_arm_link_hash_table_create -- but overrides
20226    appropriately for NaCl.  */
20227
20228 static struct bfd_link_hash_table *
20229 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
20230 {
20231   struct bfd_link_hash_table *ret;
20232
20233   ret = elf32_arm_link_hash_table_create (abfd);
20234   if (ret)
20235     {
20236       struct elf32_arm_link_hash_table *htab
20237         = (struct elf32_arm_link_hash_table *) ret;
20238
20239       htab->nacl_p = 1;
20240
20241       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
20242       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
20243     }
20244   return ret;
20245 }
20246
20247 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
20248    really need to use elf32_arm_modify_segment_map.  But we do it
20249    anyway just to reduce gratuitous differences with the stock ARM backend.  */
20250
20251 static bfd_boolean
20252 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
20253 {
20254   return (elf32_arm_modify_segment_map (abfd, info)
20255           && nacl_modify_segment_map (abfd, info));
20256 }
20257
20258 static void
20259 elf32_arm_nacl_final_write_processing (bfd *abfd, bfd_boolean linker)
20260 {
20261   elf32_arm_final_write_processing (abfd, linker);
20262   nacl_final_write_processing (abfd, linker);
20263 }
20264
20265 static bfd_vma
20266 elf32_arm_nacl_plt_sym_val (bfd_vma i, const asection *plt,
20267                             const arelent *rel ATTRIBUTE_UNUSED)
20268 {
20269   return plt->vma
20270     + 4 * (ARRAY_SIZE (elf32_arm_nacl_plt0_entry) +
20271            i * ARRAY_SIZE (elf32_arm_nacl_plt_entry));
20272 }
20273
20274 #undef  elf32_bed
20275 #define elf32_bed                               elf32_arm_nacl_bed
20276 #undef  bfd_elf32_bfd_link_hash_table_create
20277 #define bfd_elf32_bfd_link_hash_table_create    \
20278   elf32_arm_nacl_link_hash_table_create
20279 #undef  elf_backend_plt_alignment
20280 #define elf_backend_plt_alignment               4
20281 #undef  elf_backend_modify_segment_map
20282 #define elf_backend_modify_segment_map          elf32_arm_nacl_modify_segment_map
20283 #undef  elf_backend_modify_program_headers
20284 #define elf_backend_modify_program_headers      nacl_modify_program_headers
20285 #undef  elf_backend_final_write_processing
20286 #define elf_backend_final_write_processing      elf32_arm_nacl_final_write_processing
20287 #undef bfd_elf32_get_synthetic_symtab
20288 #undef  elf_backend_plt_sym_val
20289 #define elf_backend_plt_sym_val                 elf32_arm_nacl_plt_sym_val
20290 #undef  elf_backend_copy_special_section_fields
20291
20292 #undef  ELF_MINPAGESIZE
20293 #undef  ELF_COMMONPAGESIZE
20294
20295
20296 #include "elf32-target.h"
20297
20298 /* Reset to defaults.  */
20299 #undef  elf_backend_plt_alignment
20300 #undef  elf_backend_modify_segment_map
20301 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
20302 #undef  elf_backend_modify_program_headers
20303 #undef  elf_backend_final_write_processing
20304 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20305 #undef  ELF_MINPAGESIZE
20306 #define ELF_MINPAGESIZE                 0x1000
20307 #undef  ELF_COMMONPAGESIZE
20308 #define ELF_COMMONPAGESIZE              0x1000
20309
20310
20311 /* FDPIC Targets.  */
20312
20313 #undef  TARGET_LITTLE_SYM
20314 #define TARGET_LITTLE_SYM               arm_elf32_fdpic_le_vec
20315 #undef  TARGET_LITTLE_NAME
20316 #define TARGET_LITTLE_NAME              "elf32-littlearm-fdpic"
20317 #undef  TARGET_BIG_SYM
20318 #define TARGET_BIG_SYM                  arm_elf32_fdpic_be_vec
20319 #undef  TARGET_BIG_NAME
20320 #define TARGET_BIG_NAME                 "elf32-bigarm-fdpic"
20321 #undef elf_match_priority
20322 #define elf_match_priority              128
20323 #undef ELF_OSABI
20324 #define ELF_OSABI               ELFOSABI_ARM_FDPIC
20325
20326 /* Like elf32_arm_link_hash_table_create -- but overrides
20327    appropriately for FDPIC.  */
20328
20329 static struct bfd_link_hash_table *
20330 elf32_arm_fdpic_link_hash_table_create (bfd *abfd)
20331 {
20332   struct bfd_link_hash_table *ret;
20333
20334   ret = elf32_arm_link_hash_table_create (abfd);
20335   if (ret)
20336     {
20337       struct elf32_arm_link_hash_table *htab = (struct elf32_arm_link_hash_table *) ret;
20338
20339       htab->fdpic_p = 1;
20340     }
20341   return ret;
20342 }
20343
20344 /* We need dynamic symbols for every section, since segments can
20345    relocate independently.  */
20346 static bfd_boolean
20347 elf32_arm_fdpic_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
20348                                     struct bfd_link_info *info
20349                                     ATTRIBUTE_UNUSED,
20350                                     asection *p ATTRIBUTE_UNUSED)
20351 {
20352   switch (elf_section_data (p)->this_hdr.sh_type)
20353     {
20354     case SHT_PROGBITS:
20355     case SHT_NOBITS:
20356       /* If sh_type is yet undecided, assume it could be
20357          SHT_PROGBITS/SHT_NOBITS.  */
20358     case SHT_NULL:
20359       return FALSE;
20360
20361       /* There shouldn't be section relative relocations
20362          against any other section.  */
20363     default:
20364       return TRUE;
20365     }
20366 }
20367
20368 #undef  elf32_bed
20369 #define elf32_bed                               elf32_arm_fdpic_bed
20370
20371 #undef  bfd_elf32_bfd_link_hash_table_create
20372 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_fdpic_link_hash_table_create
20373
20374 #undef elf_backend_omit_section_dynsym
20375 #define elf_backend_omit_section_dynsym         elf32_arm_fdpic_omit_section_dynsym
20376
20377 #include "elf32-target.h"
20378
20379 #undef elf_match_priority
20380 #undef ELF_OSABI
20381 #undef elf_backend_omit_section_dynsym
20382
20383 /* VxWorks Targets.  */
20384
20385 #undef  TARGET_LITTLE_SYM
20386 #define TARGET_LITTLE_SYM               arm_elf32_vxworks_le_vec
20387 #undef  TARGET_LITTLE_NAME
20388 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
20389 #undef  TARGET_BIG_SYM
20390 #define TARGET_BIG_SYM                  arm_elf32_vxworks_be_vec
20391 #undef  TARGET_BIG_NAME
20392 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
20393
20394 /* Like elf32_arm_link_hash_table_create -- but overrides
20395    appropriately for VxWorks.  */
20396
20397 static struct bfd_link_hash_table *
20398 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
20399 {
20400   struct bfd_link_hash_table *ret;
20401
20402   ret = elf32_arm_link_hash_table_create (abfd);
20403   if (ret)
20404     {
20405       struct elf32_arm_link_hash_table *htab
20406         = (struct elf32_arm_link_hash_table *) ret;
20407       htab->use_rel = 0;
20408       htab->vxworks_p = 1;
20409     }
20410   return ret;
20411 }
20412
20413 static void
20414 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
20415 {
20416   elf32_arm_final_write_processing (abfd, linker);
20417   elf_vxworks_final_write_processing (abfd, linker);
20418 }
20419
20420 #undef  elf32_bed
20421 #define elf32_bed elf32_arm_vxworks_bed
20422
20423 #undef  bfd_elf32_bfd_link_hash_table_create
20424 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
20425 #undef  elf_backend_final_write_processing
20426 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
20427 #undef  elf_backend_emit_relocs
20428 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
20429
20430 #undef  elf_backend_may_use_rel_p
20431 #define elf_backend_may_use_rel_p       0
20432 #undef  elf_backend_may_use_rela_p
20433 #define elf_backend_may_use_rela_p      1
20434 #undef  elf_backend_default_use_rela_p
20435 #define elf_backend_default_use_rela_p  1
20436 #undef  elf_backend_want_plt_sym
20437 #define elf_backend_want_plt_sym        1
20438 #undef  ELF_MAXPAGESIZE
20439 #define ELF_MAXPAGESIZE                 0x1000
20440
20441 #include "elf32-target.h"
20442
20443
20444 /* Merge backend specific data from an object file to the output
20445    object file when linking.  */
20446
20447 static bfd_boolean
20448 elf32_arm_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
20449 {
20450   bfd *obfd = info->output_bfd;
20451   flagword out_flags;
20452   flagword in_flags;
20453   bfd_boolean flags_compatible = TRUE;
20454   asection *sec;
20455
20456   /* Check if we have the same endianness.  */
20457   if (! _bfd_generic_verify_endian_match (ibfd, info))
20458     return FALSE;
20459
20460   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
20461     return TRUE;
20462
20463   if (!elf32_arm_merge_eabi_attributes (ibfd, info))
20464     return FALSE;
20465
20466   /* The input BFD must have had its flags initialised.  */
20467   /* The following seems bogus to me -- The flags are initialized in
20468      the assembler but I don't think an elf_flags_init field is
20469      written into the object.  */
20470   /* BFD_ASSERT (elf_flags_init (ibfd)); */
20471
20472   in_flags  = elf_elfheader (ibfd)->e_flags;
20473   out_flags = elf_elfheader (obfd)->e_flags;
20474
20475   /* In theory there is no reason why we couldn't handle this.  However
20476      in practice it isn't even close to working and there is no real
20477      reason to want it.  */
20478   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
20479       && !(ibfd->flags & DYNAMIC)
20480       && (in_flags & EF_ARM_BE8))
20481     {
20482       _bfd_error_handler (_("error: %pB is already in final BE8 format"),
20483                           ibfd);
20484       return FALSE;
20485     }
20486
20487   if (!elf_flags_init (obfd))
20488     {
20489       /* If the input is the default architecture and had the default
20490          flags then do not bother setting the flags for the output
20491          architecture, instead allow future merges to do this.  If no
20492          future merges ever set these flags then they will retain their
20493          uninitialised values, which surprise surprise, correspond
20494          to the default values.  */
20495       if (bfd_get_arch_info (ibfd)->the_default
20496           && elf_elfheader (ibfd)->e_flags == 0)
20497         return TRUE;
20498
20499       elf_flags_init (obfd) = TRUE;
20500       elf_elfheader (obfd)->e_flags = in_flags;
20501
20502       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
20503           && bfd_get_arch_info (obfd)->the_default)
20504         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
20505
20506       return TRUE;
20507     }
20508
20509   /* Determine what should happen if the input ARM architecture
20510      does not match the output ARM architecture.  */
20511   if (! bfd_arm_merge_machines (ibfd, obfd))
20512     return FALSE;
20513
20514   /* Identical flags must be compatible.  */
20515   if (in_flags == out_flags)
20516     return TRUE;
20517
20518   /* Check to see if the input BFD actually contains any sections.  If
20519      not, its flags may not have been initialised either, but it
20520      cannot actually cause any incompatiblity.  Do not short-circuit
20521      dynamic objects; their section list may be emptied by
20522     elf_link_add_object_symbols.
20523
20524     Also check to see if there are no code sections in the input.
20525     In this case there is no need to check for code specific flags.
20526     XXX - do we need to worry about floating-point format compatability
20527     in data sections ?  */
20528   if (!(ibfd->flags & DYNAMIC))
20529     {
20530       bfd_boolean null_input_bfd = TRUE;
20531       bfd_boolean only_data_sections = TRUE;
20532
20533       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
20534         {
20535           /* Ignore synthetic glue sections.  */
20536           if (strcmp (sec->name, ".glue_7")
20537               && strcmp (sec->name, ".glue_7t"))
20538             {
20539               if ((bfd_get_section_flags (ibfd, sec)
20540                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20541                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20542                 only_data_sections = FALSE;
20543
20544               null_input_bfd = FALSE;
20545               break;
20546             }
20547         }
20548
20549       if (null_input_bfd || only_data_sections)
20550         return TRUE;
20551     }
20552
20553   /* Complain about various flag mismatches.  */
20554   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
20555                                       EF_ARM_EABI_VERSION (out_flags)))
20556     {
20557       _bfd_error_handler
20558         (_("error: source object %pB has EABI version %d, but target %pB has EABI version %d"),
20559          ibfd, (in_flags & EF_ARM_EABIMASK) >> 24,
20560          obfd, (out_flags & EF_ARM_EABIMASK) >> 24);
20561       return FALSE;
20562     }
20563
20564   /* Not sure what needs to be checked for EABI versions >= 1.  */
20565   /* VxWorks libraries do not use these flags.  */
20566   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
20567       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
20568       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
20569     {
20570       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
20571         {
20572           _bfd_error_handler
20573             (_("error: %pB is compiled for APCS-%d, whereas target %pB uses APCS-%d"),
20574              ibfd, in_flags & EF_ARM_APCS_26 ? 26 : 32,
20575              obfd, out_flags & EF_ARM_APCS_26 ? 26 : 32);
20576           flags_compatible = FALSE;
20577         }
20578
20579       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
20580         {
20581           if (in_flags & EF_ARM_APCS_FLOAT)
20582             _bfd_error_handler
20583               (_("error: %pB passes floats in float registers, whereas %pB passes them in integer registers"),
20584                ibfd, obfd);
20585           else
20586             _bfd_error_handler
20587               (_("error: %pB passes floats in integer registers, whereas %pB passes them in float registers"),
20588                ibfd, obfd);
20589
20590           flags_compatible = FALSE;
20591         }
20592
20593       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
20594         {
20595           if (in_flags & EF_ARM_VFP_FLOAT)
20596             _bfd_error_handler
20597               (_("error: %pB uses %s instructions, whereas %pB does not"),
20598                ibfd, "VFP", obfd);
20599           else
20600             _bfd_error_handler
20601               (_("error: %pB uses %s instructions, whereas %pB does not"),
20602                ibfd, "FPA", obfd);
20603
20604           flags_compatible = FALSE;
20605         }
20606
20607       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
20608         {
20609           if (in_flags & EF_ARM_MAVERICK_FLOAT)
20610             _bfd_error_handler
20611               (_("error: %pB uses %s instructions, whereas %pB does not"),
20612                ibfd, "Maverick", obfd);
20613           else
20614             _bfd_error_handler
20615               (_("error: %pB does not use %s instructions, whereas %pB does"),
20616                ibfd, "Maverick", obfd);
20617
20618           flags_compatible = FALSE;
20619         }
20620
20621 #ifdef EF_ARM_SOFT_FLOAT
20622       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
20623         {
20624           /* We can allow interworking between code that is VFP format
20625              layout, and uses either soft float or integer regs for
20626              passing floating point arguments and results.  We already
20627              know that the APCS_FLOAT flags match; similarly for VFP
20628              flags.  */
20629           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
20630               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
20631             {
20632               if (in_flags & EF_ARM_SOFT_FLOAT)
20633                 _bfd_error_handler
20634                   (_("error: %pB uses software FP, whereas %pB uses hardware FP"),
20635                    ibfd, obfd);
20636               else
20637                 _bfd_error_handler
20638                   (_("error: %pB uses hardware FP, whereas %pB uses software FP"),
20639                    ibfd, obfd);
20640
20641               flags_compatible = FALSE;
20642             }
20643         }
20644 #endif
20645
20646       /* Interworking mismatch is only a warning.  */
20647       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
20648         {
20649           if (in_flags & EF_ARM_INTERWORK)
20650             {
20651               _bfd_error_handler
20652                 (_("warning: %pB supports interworking, whereas %pB does not"),
20653                  ibfd, obfd);
20654             }
20655           else
20656             {
20657               _bfd_error_handler
20658                 (_("warning: %pB does not support interworking, whereas %pB does"),
20659                  ibfd, obfd);
20660             }
20661         }
20662     }
20663
20664   return flags_compatible;
20665 }
20666
20667
20668 /* Symbian OS Targets.  */
20669
20670 #undef  TARGET_LITTLE_SYM
20671 #define TARGET_LITTLE_SYM               arm_elf32_symbian_le_vec
20672 #undef  TARGET_LITTLE_NAME
20673 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
20674 #undef  TARGET_BIG_SYM
20675 #define TARGET_BIG_SYM                  arm_elf32_symbian_be_vec
20676 #undef  TARGET_BIG_NAME
20677 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
20678
20679 /* Like elf32_arm_link_hash_table_create -- but overrides
20680    appropriately for Symbian OS.  */
20681
20682 static struct bfd_link_hash_table *
20683 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
20684 {
20685   struct bfd_link_hash_table *ret;
20686
20687   ret = elf32_arm_link_hash_table_create (abfd);
20688   if (ret)
20689     {
20690       struct elf32_arm_link_hash_table *htab
20691         = (struct elf32_arm_link_hash_table *)ret;
20692       /* There is no PLT header for Symbian OS.  */
20693       htab->plt_header_size = 0;
20694       /* The PLT entries are each one instruction and one word.  */
20695       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
20696       htab->symbian_p = 1;
20697       /* Symbian uses armv5t or above, so use_blx is always true.  */
20698       htab->use_blx = 1;
20699       htab->root.is_relocatable_executable = 1;
20700     }
20701   return ret;
20702 }
20703
20704 static const struct bfd_elf_special_section
20705 elf32_arm_symbian_special_sections[] =
20706 {
20707   /* In a BPABI executable, the dynamic linking sections do not go in
20708      the loadable read-only segment.  The post-linker may wish to
20709      refer to these sections, but they are not part of the final
20710      program image.  */
20711   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
20712   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
20713   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
20714   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
20715   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
20716   /* These sections do not need to be writable as the SymbianOS
20717      postlinker will arrange things so that no dynamic relocation is
20718      required.  */
20719   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
20720   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
20721   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
20722   { NULL,                             0, 0, 0,                 0 }
20723 };
20724
20725 static void
20726 elf32_arm_symbian_begin_write_processing (bfd *abfd,
20727                                           struct bfd_link_info *link_info)
20728 {
20729   /* BPABI objects are never loaded directly by an OS kernel; they are
20730      processed by a postlinker first, into an OS-specific format.  If
20731      the D_PAGED bit is set on the file, BFD will align segments on
20732      page boundaries, so that an OS can directly map the file.  With
20733      BPABI objects, that just results in wasted space.  In addition,
20734      because we clear the D_PAGED bit, map_sections_to_segments will
20735      recognize that the program headers should not be mapped into any
20736      loadable segment.  */
20737   abfd->flags &= ~D_PAGED;
20738   elf32_arm_begin_write_processing (abfd, link_info);
20739 }
20740
20741 static bfd_boolean
20742 elf32_arm_symbian_modify_segment_map (bfd *abfd,
20743                                       struct bfd_link_info *info)
20744 {
20745   struct elf_segment_map *m;
20746   asection *dynsec;
20747
20748   /* BPABI shared libraries and executables should have a PT_DYNAMIC
20749      segment.  However, because the .dynamic section is not marked
20750      with SEC_LOAD, the generic ELF code will not create such a
20751      segment.  */
20752   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
20753   if (dynsec)
20754     {
20755       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
20756         if (m->p_type == PT_DYNAMIC)
20757           break;
20758
20759       if (m == NULL)
20760         {
20761           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
20762           m->next = elf_seg_map (abfd);
20763           elf_seg_map (abfd) = m;
20764         }
20765     }
20766
20767   /* Also call the generic arm routine.  */
20768   return elf32_arm_modify_segment_map (abfd, info);
20769 }
20770
20771 /* Return address for Ith PLT stub in section PLT, for relocation REL
20772    or (bfd_vma) -1 if it should not be included.  */
20773
20774 static bfd_vma
20775 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
20776                                const arelent *rel ATTRIBUTE_UNUSED)
20777 {
20778   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
20779 }
20780
20781 #undef  elf32_bed
20782 #define elf32_bed elf32_arm_symbian_bed
20783
20784 /* The dynamic sections are not allocated on SymbianOS; the postlinker
20785    will process them and then discard them.  */
20786 #undef  ELF_DYNAMIC_SEC_FLAGS
20787 #define ELF_DYNAMIC_SEC_FLAGS \
20788   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
20789
20790 #undef elf_backend_emit_relocs
20791
20792 #undef  bfd_elf32_bfd_link_hash_table_create
20793 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
20794 #undef  elf_backend_special_sections
20795 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
20796 #undef  elf_backend_begin_write_processing
20797 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
20798 #undef  elf_backend_final_write_processing
20799 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20800
20801 #undef  elf_backend_modify_segment_map
20802 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
20803
20804 /* There is no .got section for BPABI objects, and hence no header.  */
20805 #undef  elf_backend_got_header_size
20806 #define elf_backend_got_header_size 0
20807
20808 /* Similarly, there is no .got.plt section.  */
20809 #undef  elf_backend_want_got_plt
20810 #define elf_backend_want_got_plt 0
20811
20812 #undef  elf_backend_plt_sym_val
20813 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
20814
20815 #undef  elf_backend_may_use_rel_p
20816 #define elf_backend_may_use_rel_p       1
20817 #undef  elf_backend_may_use_rela_p
20818 #define elf_backend_may_use_rela_p      0
20819 #undef  elf_backend_default_use_rela_p
20820 #define elf_backend_default_use_rela_p  0
20821 #undef  elf_backend_want_plt_sym
20822 #define elf_backend_want_plt_sym        0
20823 #undef  elf_backend_dtrel_excludes_plt
20824 #define elf_backend_dtrel_excludes_plt  0
20825 #undef  ELF_MAXPAGESIZE
20826 #define ELF_MAXPAGESIZE                 0x8000
20827
20828 #include "elf32-target.h"