Add support for ARM's NOREAD section flag.
[external/binutils.git] / bfd / elf32-arm.c
1 /* 32-bit ELF support for ARM
2    Copyright (C) 1998-2015 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               0
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[1] =
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 };
1765
1766 /* 249-255 extended, currently unused, relocations:  */
1767 static reloc_howto_type elf32_arm_howto_table_3[4] =
1768 {
1769   HOWTO (R_ARM_RREL32,          /* type */
1770          0,                     /* rightshift */
1771          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1772          0,                     /* bitsize */
1773          FALSE,                 /* pc_relative */
1774          0,                     /* bitpos */
1775          complain_overflow_dont,/* complain_on_overflow */
1776          bfd_elf_generic_reloc, /* special_function */
1777          "R_ARM_RREL32",        /* name */
1778          FALSE,                 /* partial_inplace */
1779          0,                     /* src_mask */
1780          0,                     /* dst_mask */
1781          FALSE),                /* pcrel_offset */
1782
1783   HOWTO (R_ARM_RABS32,          /* type */
1784          0,                     /* rightshift */
1785          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1786          0,                     /* bitsize */
1787          FALSE,                 /* pc_relative */
1788          0,                     /* bitpos */
1789          complain_overflow_dont,/* complain_on_overflow */
1790          bfd_elf_generic_reloc, /* special_function */
1791          "R_ARM_RABS32",        /* name */
1792          FALSE,                 /* partial_inplace */
1793          0,                     /* src_mask */
1794          0,                     /* dst_mask */
1795          FALSE),                /* pcrel_offset */
1796
1797   HOWTO (R_ARM_RPC24,           /* type */
1798          0,                     /* rightshift */
1799          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1800          0,                     /* bitsize */
1801          FALSE,                 /* pc_relative */
1802          0,                     /* bitpos */
1803          complain_overflow_dont,/* complain_on_overflow */
1804          bfd_elf_generic_reloc, /* special_function */
1805          "R_ARM_RPC24",         /* name */
1806          FALSE,                 /* partial_inplace */
1807          0,                     /* src_mask */
1808          0,                     /* dst_mask */
1809          FALSE),                /* pcrel_offset */
1810
1811   HOWTO (R_ARM_RBASE,           /* type */
1812          0,                     /* rightshift */
1813          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          0,                     /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_dont,/* complain_on_overflow */
1818          bfd_elf_generic_reloc, /* special_function */
1819          "R_ARM_RBASE",         /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0,                     /* dst_mask */
1823          FALSE)                 /* pcrel_offset */
1824 };
1825
1826 static reloc_howto_type *
1827 elf32_arm_howto_from_type (unsigned int r_type)
1828 {
1829   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1830     return &elf32_arm_howto_table_1[r_type];
1831
1832   if (r_type == R_ARM_IRELATIVE)
1833     return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
1834
1835   if (r_type >= R_ARM_RREL32
1836       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
1837     return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1838
1839   return NULL;
1840 }
1841
1842 static void
1843 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
1844                          Elf_Internal_Rela * elf_reloc)
1845 {
1846   unsigned int r_type;
1847
1848   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1849   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
1850 }
1851
1852 struct elf32_arm_reloc_map
1853   {
1854     bfd_reloc_code_real_type  bfd_reloc_val;
1855     unsigned char             elf_reloc_val;
1856   };
1857
1858 /* All entries in this list must also be present in elf32_arm_howto_table.  */
1859 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1860   {
1861     {BFD_RELOC_NONE,                 R_ARM_NONE},
1862     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1863     {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
1864     {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
1865     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1866     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1867     {BFD_RELOC_32,                   R_ARM_ABS32},
1868     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1869     {BFD_RELOC_8,                    R_ARM_ABS8},
1870     {BFD_RELOC_16,                   R_ARM_ABS16},
1871     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1872     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1873     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1874     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1875     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1876     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1877     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1878     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1879     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1880     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1881     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1882     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1883     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1884     {BFD_RELOC_ARM_GOT_PREL,         R_ARM_GOT_PREL},
1885     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1886     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1887     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
1888     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
1889     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
1890     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
1891     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
1892     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1893     {BFD_RELOC_ARM_TLS_GOTDESC,      R_ARM_TLS_GOTDESC},
1894     {BFD_RELOC_ARM_TLS_CALL,         R_ARM_TLS_CALL},
1895     {BFD_RELOC_ARM_THM_TLS_CALL,     R_ARM_THM_TLS_CALL},
1896     {BFD_RELOC_ARM_TLS_DESCSEQ,      R_ARM_TLS_DESCSEQ},
1897     {BFD_RELOC_ARM_THM_TLS_DESCSEQ,  R_ARM_THM_TLS_DESCSEQ},
1898     {BFD_RELOC_ARM_TLS_DESC,         R_ARM_TLS_DESC},
1899     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
1900     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
1901     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
1902     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
1903     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
1904     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
1905     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
1906     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
1907     {BFD_RELOC_ARM_IRELATIVE,        R_ARM_IRELATIVE},
1908     {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
1909     {BFD_RELOC_VTABLE_ENTRY,         R_ARM_GNU_VTENTRY},
1910     {BFD_RELOC_ARM_MOVW,             R_ARM_MOVW_ABS_NC},
1911     {BFD_RELOC_ARM_MOVT,             R_ARM_MOVT_ABS},
1912     {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
1913     {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
1914     {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
1915     {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
1916     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
1917     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
1918     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
1919     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
1920     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
1921     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
1922     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
1923     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
1924     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
1925     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
1926     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
1927     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
1928     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
1929     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
1930     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
1931     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
1932     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
1933     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
1934     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
1935     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
1936     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
1937     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
1938     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
1939     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
1940     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
1941     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
1942     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
1943     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
1944     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
1945     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
1946     {BFD_RELOC_ARM_V4BX,             R_ARM_V4BX},
1947     {BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC, R_ARM_THM_ALU_ABS_G3_NC},
1948     {BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC, R_ARM_THM_ALU_ABS_G2_NC},
1949     {BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC, R_ARM_THM_ALU_ABS_G1_NC},
1950     {BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC, R_ARM_THM_ALU_ABS_G0_NC}
1951   };
1952
1953 static reloc_howto_type *
1954 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1955                              bfd_reloc_code_real_type code)
1956 {
1957   unsigned int i;
1958
1959   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
1960     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1961       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
1962
1963   return NULL;
1964 }
1965
1966 static reloc_howto_type *
1967 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1968                              const char *r_name)
1969 {
1970   unsigned int i;
1971
1972   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
1973     if (elf32_arm_howto_table_1[i].name != NULL
1974         && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
1975       return &elf32_arm_howto_table_1[i];
1976
1977   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
1978     if (elf32_arm_howto_table_2[i].name != NULL
1979         && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
1980       return &elf32_arm_howto_table_2[i];
1981
1982   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_3); i++)
1983     if (elf32_arm_howto_table_3[i].name != NULL
1984         && strcasecmp (elf32_arm_howto_table_3[i].name, r_name) == 0)
1985       return &elf32_arm_howto_table_3[i];
1986
1987   return NULL;
1988 }
1989
1990 /* Support for core dump NOTE sections.  */
1991
1992 static bfd_boolean
1993 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1994 {
1995   int offset;
1996   size_t size;
1997
1998   switch (note->descsz)
1999     {
2000       default:
2001         return FALSE;
2002
2003       case 148:         /* Linux/ARM 32-bit.  */
2004         /* pr_cursig */
2005         elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2006
2007         /* pr_pid */
2008         elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2009
2010         /* pr_reg */
2011         offset = 72;
2012         size = 72;
2013
2014         break;
2015     }
2016
2017   /* Make a ".reg/999" section.  */
2018   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2019                                           size, note->descpos + offset);
2020 }
2021
2022 static bfd_boolean
2023 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2024 {
2025   switch (note->descsz)
2026     {
2027       default:
2028         return FALSE;
2029
2030       case 124:         /* Linux/ARM elf_prpsinfo.  */
2031         elf_tdata (abfd)->core->pid
2032          = bfd_get_32 (abfd, note->descdata + 12);
2033         elf_tdata (abfd)->core->program
2034          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2035         elf_tdata (abfd)->core->command
2036          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2037     }
2038
2039   /* Note that for some reason, a spurious space is tacked
2040      onto the end of the args in some (at least one anyway)
2041      implementations, so strip it off if it exists.  */
2042   {
2043     char *command = elf_tdata (abfd)->core->command;
2044     int n = strlen (command);
2045
2046     if (0 < n && command[n - 1] == ' ')
2047       command[n - 1] = '\0';
2048   }
2049
2050   return TRUE;
2051 }
2052
2053 static char *
2054 elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
2055                                 int note_type, ...)
2056 {
2057   switch (note_type)
2058     {
2059     default:
2060       return NULL;
2061
2062     case NT_PRPSINFO:
2063       {
2064         char data[124];
2065         va_list ap;
2066
2067         va_start (ap, note_type);
2068         memset (data, 0, sizeof (data));
2069         strncpy (data + 28, va_arg (ap, const char *), 16);
2070         strncpy (data + 44, va_arg (ap, const char *), 80);
2071         va_end (ap);
2072
2073         return elfcore_write_note (abfd, buf, bufsiz,
2074                                    "CORE", note_type, data, sizeof (data));
2075       }
2076
2077     case NT_PRSTATUS:
2078       {
2079         char data[148];
2080         va_list ap;
2081         long pid;
2082         int cursig;
2083         const void *greg;
2084
2085         va_start (ap, note_type);
2086         memset (data, 0, sizeof (data));
2087         pid = va_arg (ap, long);
2088         bfd_put_32 (abfd, pid, data + 24);
2089         cursig = va_arg (ap, int);
2090         bfd_put_16 (abfd, cursig, data + 12);
2091         greg = va_arg (ap, const void *);
2092         memcpy (data + 72, greg, 72);
2093         va_end (ap);
2094
2095         return elfcore_write_note (abfd, buf, bufsiz,
2096                                    "CORE", note_type, data, sizeof (data));
2097       }
2098     }
2099 }
2100
2101 #define TARGET_LITTLE_SYM               arm_elf32_le_vec
2102 #define TARGET_LITTLE_NAME              "elf32-littlearm"
2103 #define TARGET_BIG_SYM                  arm_elf32_be_vec
2104 #define TARGET_BIG_NAME                 "elf32-bigarm"
2105
2106 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
2107 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
2108 #define elf_backend_write_core_note     elf32_arm_nabi_write_core_note
2109
2110 typedef unsigned long int insn32;
2111 typedef unsigned short int insn16;
2112
2113 /* In lieu of proper flags, assume all EABIv4 or later objects are
2114    interworkable.  */
2115 #define INTERWORK_FLAG(abfd)  \
2116   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
2117   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
2118   || ((abfd)->flags & BFD_LINKER_CREATED))
2119
2120 /* The linker script knows the section names for placement.
2121    The entry_names are used to do simple name mangling on the stubs.
2122    Given a function name, and its type, the stub can be found. The
2123    name can be changed. The only requirement is the %s be present.  */
2124 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
2125 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
2126
2127 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
2128 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
2129
2130 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
2131 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
2132
2133 #define STM32L4XX_ERRATUM_VENEER_SECTION_NAME ".text.stm32l4xx_veneer"
2134 #define STM32L4XX_ERRATUM_VENEER_ENTRY_NAME   "__stm32l4xx_veneer_%x"
2135
2136 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
2137 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
2138
2139 #define STUB_ENTRY_NAME   "__%s_veneer"
2140
2141 /* The name of the dynamic interpreter.  This is put in the .interp
2142    section.  */
2143 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
2144
2145 static const unsigned long tls_trampoline [] =
2146 {
2147   0xe08e0000,           /* add r0, lr, r0 */
2148   0xe5901004,           /* ldr r1, [r0,#4] */
2149   0xe12fff11,           /* bx  r1 */
2150 };
2151
2152 static const unsigned long dl_tlsdesc_lazy_trampoline [] =
2153 {
2154   0xe52d2004, /*        push    {r2}                    */
2155   0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8]     */
2156   0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8]     */
2157   0xe79f2002, /* 1:   ldr     r2, [pc, r2]              */
2158   0xe081100f, /* 2:   add     r1, pc                    */
2159   0xe12fff12, /*      bx      r2                        */
2160   0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
2161                                 + dl_tlsdesc_lazy_resolver(GOT)   */
2162   0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
2163 };
2164
2165 #ifdef FOUR_WORD_PLT
2166
2167 /* The first entry in a procedure linkage table looks like
2168    this.  It is set up so that any shared library function that is
2169    called before the relocation has been set up calls the dynamic
2170    linker first.  */
2171 static const bfd_vma elf32_arm_plt0_entry [] =
2172 {
2173   0xe52de004,           /* str   lr, [sp, #-4]! */
2174   0xe59fe010,           /* ldr   lr, [pc, #16]  */
2175   0xe08fe00e,           /* add   lr, pc, lr     */
2176   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2177 };
2178
2179 /* Subsequent entries in a procedure linkage table look like
2180    this.  */
2181 static const bfd_vma elf32_arm_plt_entry [] =
2182 {
2183   0xe28fc600,           /* add   ip, pc, #NN    */
2184   0xe28cca00,           /* add   ip, ip, #NN    */
2185   0xe5bcf000,           /* ldr   pc, [ip, #NN]! */
2186   0x00000000,           /* unused               */
2187 };
2188
2189 #else /* not FOUR_WORD_PLT */
2190
2191 /* The first entry in a procedure linkage table looks like
2192    this.  It is set up so that any shared library function that is
2193    called before the relocation has been set up calls the dynamic
2194    linker first.  */
2195 static const bfd_vma elf32_arm_plt0_entry [] =
2196 {
2197   0xe52de004,           /* str   lr, [sp, #-4]! */
2198   0xe59fe004,           /* ldr   lr, [pc, #4]   */
2199   0xe08fe00e,           /* add   lr, pc, lr     */
2200   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2201   0x00000000,           /* &GOT[0] - .          */
2202 };
2203
2204 /* By default subsequent entries in a procedure linkage table look like
2205    this. Offsets that don't fit into 28 bits will cause link error.  */
2206 static const bfd_vma elf32_arm_plt_entry_short [] =
2207 {
2208   0xe28fc600,           /* add   ip, pc, #0xNN00000 */
2209   0xe28cca00,           /* add   ip, ip, #0xNN000   */
2210   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!  */
2211 };
2212
2213 /* When explicitly asked, we'll use this "long" entry format
2214    which can cope with arbitrary displacements.  */
2215 static const bfd_vma elf32_arm_plt_entry_long [] =
2216 {
2217   0xe28fc200,           /* add   ip, pc, #0xN0000000 */
2218   0xe28cc600,           /* add   ip, ip, #0xNN00000  */
2219   0xe28cca00,           /* add   ip, ip, #0xNN000    */
2220   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!   */
2221 };
2222
2223 static bfd_boolean elf32_arm_use_long_plt_entry = FALSE;
2224
2225 #endif /* not FOUR_WORD_PLT */
2226
2227 /* The first entry in a procedure linkage table looks like this.
2228    It is set up so that any shared library function that is called before the
2229    relocation has been set up calls the dynamic linker first.  */
2230 static const bfd_vma elf32_thumb2_plt0_entry [] =
2231 {
2232   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2233      an instruction maybe encoded to one or two array elements.  */
2234   0xf8dfb500,           /* push    {lr}          */
2235   0x44fee008,           /* ldr.w   lr, [pc, #8]  */
2236                         /* add     lr, pc        */
2237   0xff08f85e,           /* ldr.w   pc, [lr, #8]! */
2238   0x00000000,           /* &GOT[0] - .           */
2239 };
2240
2241 /* Subsequent entries in a procedure linkage table for thumb only target
2242    look like this.  */
2243 static const bfd_vma elf32_thumb2_plt_entry [] =
2244 {
2245   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2246      an instruction maybe encoded to one or two array elements.  */
2247   0x0c00f240,           /* movw    ip, #0xNNNN    */
2248   0x0c00f2c0,           /* movt    ip, #0xNNNN    */
2249   0xf8dc44fc,           /* add     ip, pc         */
2250   0xbf00f000            /* ldr.w   pc, [ip]       */
2251                         /* nop                    */
2252 };
2253
2254 /* The format of the first entry in the procedure linkage table
2255    for a VxWorks executable.  */
2256 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
2257 {
2258   0xe52dc008,           /* str    ip,[sp,#-8]!                  */
2259   0xe59fc000,           /* ldr    ip,[pc]                       */
2260   0xe59cf008,           /* ldr    pc,[ip,#8]                    */
2261   0x00000000,           /* .long  _GLOBAL_OFFSET_TABLE_         */
2262 };
2263
2264 /* The format of subsequent entries in a VxWorks executable.  */
2265 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
2266 {
2267   0xe59fc000,         /* ldr    ip,[pc]                 */
2268   0xe59cf000,         /* ldr    pc,[ip]                 */
2269   0x00000000,         /* .long  @got                            */
2270   0xe59fc000,         /* ldr    ip,[pc]                 */
2271   0xea000000,         /* b      _PLT                            */
2272   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2273 };
2274
2275 /* The format of entries in a VxWorks shared library.  */
2276 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
2277 {
2278   0xe59fc000,         /* ldr    ip,[pc]                 */
2279   0xe79cf009,         /* ldr    pc,[ip,r9]                      */
2280   0x00000000,         /* .long  @got                            */
2281   0xe59fc000,         /* ldr    ip,[pc]                 */
2282   0xe599f008,         /* ldr    pc,[r9,#8]                      */
2283   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2284 };
2285
2286 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
2287 #define PLT_THUMB_STUB_SIZE 4
2288 static const bfd_vma elf32_arm_plt_thumb_stub [] =
2289 {
2290   0x4778,               /* bx pc */
2291   0x46c0                /* nop   */
2292 };
2293
2294 /* The entries in a PLT when using a DLL-based target with multiple
2295    address spaces.  */
2296 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2297 {
2298   0xe51ff004,         /* ldr   pc, [pc, #-4] */
2299   0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2300 };
2301
2302 /* The first entry in a procedure linkage table looks like
2303    this.  It is set up so that any shared library function that is
2304    called before the relocation has been set up calls the dynamic
2305    linker first.  */
2306 static const bfd_vma elf32_arm_nacl_plt0_entry [] =
2307 {
2308   /* First bundle: */
2309   0xe300c000,           /* movw ip, #:lower16:&GOT[2]-.+8       */
2310   0xe340c000,           /* movt ip, #:upper16:&GOT[2]-.+8       */
2311   0xe08cc00f,           /* add  ip, ip, pc                      */
2312   0xe52dc008,           /* str  ip, [sp, #-8]!                  */
2313   /* Second bundle: */
2314   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2315   0xe59cc000,           /* ldr  ip, [ip]                        */
2316   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2317   0xe12fff1c,           /* bx   ip                              */
2318   /* Third bundle: */
2319   0xe320f000,           /* nop                                  */
2320   0xe320f000,           /* nop                                  */
2321   0xe320f000,           /* nop                                  */
2322   /* .Lplt_tail: */
2323   0xe50dc004,           /* str  ip, [sp, #-4]                   */
2324   /* Fourth bundle: */
2325   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2326   0xe59cc000,           /* ldr  ip, [ip]                        */
2327   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2328   0xe12fff1c,           /* bx   ip                              */
2329 };
2330 #define ARM_NACL_PLT_TAIL_OFFSET        (11 * 4)
2331
2332 /* Subsequent entries in a procedure linkage table look like this.  */
2333 static const bfd_vma elf32_arm_nacl_plt_entry [] =
2334 {
2335   0xe300c000,           /* movw ip, #:lower16:&GOT[n]-.+8       */
2336   0xe340c000,           /* movt ip, #:upper16:&GOT[n]-.+8       */
2337   0xe08cc00f,           /* add  ip, ip, pc                      */
2338   0xea000000,           /* b    .Lplt_tail                      */
2339 };
2340
2341 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2342 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2343 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2344 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2345 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2346 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2347 #define THM2_MAX_FWD_COND_BRANCH_OFFSET (((1 << 20) -2) + 4)
2348 #define THM2_MAX_BWD_COND_BRANCH_OFFSET (-(1 << 20) + 4)
2349
2350 enum stub_insn_type
2351 {
2352   THUMB16_TYPE = 1,
2353   THUMB32_TYPE,
2354   ARM_TYPE,
2355   DATA_TYPE
2356 };
2357
2358 #define THUMB16_INSN(X)         {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2359 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2360    is inserted in arm_build_one_stub().  */
2361 #define THUMB16_BCOND_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2362 #define THUMB32_INSN(X)         {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2363 #define THUMB32_B_INSN(X, Z)    {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2364 #define ARM_INSN(X)             {(X), ARM_TYPE, R_ARM_NONE, 0}
2365 #define ARM_REL_INSN(X, Z)      {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2366 #define DATA_WORD(X,Y,Z)        {(X), DATA_TYPE, (Y), (Z)}
2367
2368 typedef struct
2369 {
2370   bfd_vma              data;
2371   enum stub_insn_type  type;
2372   unsigned int         r_type;
2373   int                  reloc_addend;
2374 }  insn_sequence;
2375
2376 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2377    to reach the stub if necessary.  */
2378 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2379 {
2380   ARM_INSN (0xe51ff004),            /* ldr   pc, [pc, #-4] */
2381   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2382 };
2383
2384 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2385    available.  */
2386 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2387 {
2388   ARM_INSN (0xe59fc000),            /* ldr   ip, [pc, #0] */
2389   ARM_INSN (0xe12fff1c),            /* bx    ip */
2390   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2391 };
2392
2393 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2394 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2395 {
2396   THUMB16_INSN (0xb401),             /* push {r0} */
2397   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2398   THUMB16_INSN (0x4684),             /* mov  ip, r0 */
2399   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2400   THUMB16_INSN (0x4760),             /* bx   ip */
2401   THUMB16_INSN (0xbf00),             /* nop */
2402   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2403 };
2404
2405 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
2406    allowed.  */
2407 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2408 {
2409   THUMB16_INSN (0x4778),             /* bx   pc */
2410   THUMB16_INSN (0x46c0),             /* nop */
2411   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2412   ARM_INSN (0xe12fff1c),             /* bx   ip */
2413   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2414 };
2415
2416 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2417    available.  */
2418 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2419 {
2420   THUMB16_INSN (0x4778),             /* bx   pc */
2421   THUMB16_INSN (0x46c0),             /* nop   */
2422   ARM_INSN (0xe51ff004),             /* ldr   pc, [pc, #-4] */
2423   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2424 };
2425
2426 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2427    one, when the destination is close enough.  */
2428 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2429 {
2430   THUMB16_INSN (0x4778),             /* bx   pc */
2431   THUMB16_INSN (0x46c0),             /* nop   */
2432   ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
2433 };
2434
2435 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2436    blx to reach the stub if necessary.  */
2437 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2438 {
2439   ARM_INSN (0xe59fc000),             /* ldr   ip, [pc] */
2440   ARM_INSN (0xe08ff00c),             /* add   pc, pc, ip */
2441   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2442 };
2443
2444 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2445    blx to reach the stub if necessary.  We can not add into pc;
2446    it is not guaranteed to mode switch (different in ARMv6 and
2447    ARMv7).  */
2448 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2449 {
2450   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2451   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2452   ARM_INSN (0xe12fff1c),             /* bx    ip */
2453   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2454 };
2455
2456 /* V4T ARM -> ARM long branch stub, PIC.  */
2457 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2458 {
2459   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2460   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2461   ARM_INSN (0xe12fff1c),             /* bx    ip */
2462   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2463 };
2464
2465 /* V4T Thumb -> ARM long branch stub, PIC.  */
2466 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2467 {
2468   THUMB16_INSN (0x4778),             /* bx   pc */
2469   THUMB16_INSN (0x46c0),             /* nop  */
2470   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2471   ARM_INSN (0xe08cf00f),             /* add  pc, ip, pc */
2472   DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2473 };
2474
2475 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2476    architectures.  */
2477 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2478 {
2479   THUMB16_INSN (0xb401),             /* push {r0} */
2480   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2481   THUMB16_INSN (0x46fc),             /* mov  ip, pc */
2482   THUMB16_INSN (0x4484),             /* add  ip, r0 */
2483   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2484   THUMB16_INSN (0x4760),             /* bx   ip */
2485   DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2486 };
2487
2488 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2489    allowed.  */
2490 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2491 {
2492   THUMB16_INSN (0x4778),             /* bx   pc */
2493   THUMB16_INSN (0x46c0),             /* nop */
2494   ARM_INSN (0xe59fc004),             /* ldr  ip, [pc, #4] */
2495   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2496   ARM_INSN (0xe12fff1c),             /* bx   ip */
2497   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2498 };
2499
2500 /* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
2501    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2502 static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
2503 {
2504   ARM_INSN (0xe59f1000),             /* ldr   r1, [pc] */
2505   ARM_INSN (0xe08ff001),             /* add   pc, pc, r1 */
2506   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2507 };
2508
2509 /* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
2510    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2511 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
2512 {
2513   THUMB16_INSN (0x4778),             /* bx   pc */
2514   THUMB16_INSN (0x46c0),             /* nop */
2515   ARM_INSN (0xe59f1000),             /* ldr  r1, [pc, #0] */
2516   ARM_INSN (0xe081f00f),             /* add  pc, r1, pc */
2517   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
2518 };
2519
2520 /* NaCl ARM -> ARM long branch stub.  */
2521 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl[] =
2522 {
2523   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2524   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2525   ARM_INSN (0xe12fff1c),                /* bx   ip */
2526   ARM_INSN (0xe320f000),                /* nop */
2527   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2528   DATA_WORD (0, R_ARM_ABS32, 0),        /* dcd  R_ARM_ABS32(X) */
2529   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2530   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2531 };
2532
2533 /* NaCl ARM -> ARM long branch stub, PIC.  */
2534 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl_pic[] =
2535 {
2536   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2537   ARM_INSN (0xe08cc00f),                /* add  ip, ip, pc */
2538   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2539   ARM_INSN (0xe12fff1c),                /* bx   ip */
2540   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2541   DATA_WORD (0, R_ARM_REL32, 8),        /* dcd  R_ARM_REL32(X+8) */
2542   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2543   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2544 };
2545
2546
2547 /* Cortex-A8 erratum-workaround stubs.  */
2548
2549 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2550    can't use a conditional branch to reach this stub).  */
2551
2552 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2553 {
2554   THUMB16_BCOND_INSN (0xd001),         /* b<cond>.n true.  */
2555   THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2556   THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2557 };
2558
2559 /* Stub used for b.w and bl.w instructions.  */
2560
2561 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2562 {
2563   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2564 };
2565
2566 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2567 {
2568   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2569 };
2570
2571 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2572    instruction (which switches to ARM mode) to point to this stub.  Jump to the
2573    real destination using an ARM-mode branch.  */
2574
2575 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2576 {
2577   ARM_REL_INSN (0xea000000, -8) /* b original_branch_dest.  */
2578 };
2579
2580 /* For each section group there can be a specially created linker section
2581    to hold the stubs for that group.  The name of the stub section is based
2582    upon the name of another section within that group with the suffix below
2583    applied.
2584
2585    PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
2586    create what appeared to be a linker stub section when it actually
2587    contained user code/data.  For example, consider this fragment:
2588
2589      const char * stubborn_problems[] = { "np" };
2590
2591    If this is compiled with "-fPIC -fdata-sections" then gcc produces a
2592    section called:
2593
2594      .data.rel.local.stubborn_problems
2595
2596    This then causes problems in arm32_arm_build_stubs() as it triggers:
2597
2598       // Ignore non-stub sections.
2599       if (!strstr (stub_sec->name, STUB_SUFFIX))
2600         continue;
2601
2602    And so the section would be ignored instead of being processed.  Hence
2603    the change in definition of STUB_SUFFIX to a name that cannot be a valid
2604    C identifier.  */
2605 #define STUB_SUFFIX ".__stub"
2606
2607 /* One entry per long/short branch stub defined above.  */
2608 #define DEF_STUBS \
2609   DEF_STUB(long_branch_any_any) \
2610   DEF_STUB(long_branch_v4t_arm_thumb) \
2611   DEF_STUB(long_branch_thumb_only) \
2612   DEF_STUB(long_branch_v4t_thumb_thumb) \
2613   DEF_STUB(long_branch_v4t_thumb_arm) \
2614   DEF_STUB(short_branch_v4t_thumb_arm) \
2615   DEF_STUB(long_branch_any_arm_pic) \
2616   DEF_STUB(long_branch_any_thumb_pic) \
2617   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
2618   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
2619   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
2620   DEF_STUB(long_branch_thumb_only_pic) \
2621   DEF_STUB(long_branch_any_tls_pic) \
2622   DEF_STUB(long_branch_v4t_thumb_tls_pic) \
2623   DEF_STUB(long_branch_arm_nacl) \
2624   DEF_STUB(long_branch_arm_nacl_pic) \
2625   DEF_STUB(a8_veneer_b_cond) \
2626   DEF_STUB(a8_veneer_b) \
2627   DEF_STUB(a8_veneer_bl) \
2628   DEF_STUB(a8_veneer_blx)
2629
2630 #define DEF_STUB(x) arm_stub_##x,
2631 enum elf32_arm_stub_type
2632 {
2633   arm_stub_none,
2634   DEF_STUBS
2635   /* Note the first a8_veneer type.  */
2636   arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond
2637 };
2638 #undef DEF_STUB
2639
2640 typedef struct
2641 {
2642   const insn_sequence* template_sequence;
2643   int template_size;
2644 } stub_def;
2645
2646 #define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
2647 static const stub_def stub_definitions[] =
2648 {
2649   {NULL, 0},
2650   DEF_STUBS
2651 };
2652
2653 struct elf32_arm_stub_hash_entry
2654 {
2655   /* Base hash table entry structure.  */
2656   struct bfd_hash_entry root;
2657
2658   /* The stub section.  */
2659   asection *stub_sec;
2660
2661   /* Offset within stub_sec of the beginning of this stub.  */
2662   bfd_vma stub_offset;
2663
2664   /* Given the symbol's value and its section we can determine its final
2665      value when building the stubs (so the stub knows where to jump).  */
2666   bfd_vma target_value;
2667   asection *target_section;
2668
2669   /* Offset to apply to relocation referencing target_value.  */
2670   bfd_vma target_addend;
2671
2672   /* The instruction which caused this stub to be generated (only valid for
2673      Cortex-A8 erratum workaround stubs at present).  */
2674   unsigned long orig_insn;
2675
2676   /* The stub type.  */
2677   enum elf32_arm_stub_type stub_type;
2678   /* Its encoding size in bytes.  */
2679   int stub_size;
2680   /* Its template.  */
2681   const insn_sequence *stub_template;
2682   /* The size of the template (number of entries).  */
2683   int stub_template_size;
2684
2685   /* The symbol table entry, if any, that this was derived from.  */
2686   struct elf32_arm_link_hash_entry *h;
2687
2688   /* Type of branch.  */
2689   enum arm_st_branch_type branch_type;
2690
2691   /* Where this stub is being called from, or, in the case of combined
2692      stub sections, the first input section in the group.  */
2693   asection *id_sec;
2694
2695   /* The name for the local symbol at the start of this stub.  The
2696      stub name in the hash table has to be unique; this does not, so
2697      it can be friendlier.  */
2698   char *output_name;
2699 };
2700
2701 /* Used to build a map of a section.  This is required for mixed-endian
2702    code/data.  */
2703
2704 typedef struct elf32_elf_section_map
2705 {
2706   bfd_vma vma;
2707   char type;
2708 }
2709 elf32_arm_section_map;
2710
2711 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2712
2713 typedef enum
2714 {
2715   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2716   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2717   VFP11_ERRATUM_ARM_VENEER,
2718   VFP11_ERRATUM_THUMB_VENEER
2719 }
2720 elf32_vfp11_erratum_type;
2721
2722 typedef struct elf32_vfp11_erratum_list
2723 {
2724   struct elf32_vfp11_erratum_list *next;
2725   bfd_vma vma;
2726   union
2727   {
2728     struct
2729     {
2730       struct elf32_vfp11_erratum_list *veneer;
2731       unsigned int vfp_insn;
2732     } b;
2733     struct
2734     {
2735       struct elf32_vfp11_erratum_list *branch;
2736       unsigned int id;
2737     } v;
2738   } u;
2739   elf32_vfp11_erratum_type type;
2740 }
2741 elf32_vfp11_erratum_list;
2742
2743 /* Information about a STM32L4XX erratum veneer, or a branch to such a
2744    veneer.  */
2745 typedef enum
2746 {
2747   STM32L4XX_ERRATUM_BRANCH_TO_VENEER,
2748   STM32L4XX_ERRATUM_VENEER
2749 }
2750 elf32_stm32l4xx_erratum_type;
2751
2752 typedef struct elf32_stm32l4xx_erratum_list
2753 {
2754   struct elf32_stm32l4xx_erratum_list *next;
2755   bfd_vma vma;
2756   union
2757   {
2758     struct
2759     {
2760       struct elf32_stm32l4xx_erratum_list *veneer;
2761       unsigned int insn;
2762     } b;
2763     struct
2764     {
2765       struct elf32_stm32l4xx_erratum_list *branch;
2766       unsigned int id;
2767     } v;
2768   } u;
2769   elf32_stm32l4xx_erratum_type type;
2770 }
2771 elf32_stm32l4xx_erratum_list;
2772
2773 typedef enum
2774 {
2775   DELETE_EXIDX_ENTRY,
2776   INSERT_EXIDX_CANTUNWIND_AT_END
2777 }
2778 arm_unwind_edit_type;
2779
2780 /* A (sorted) list of edits to apply to an unwind table.  */
2781 typedef struct arm_unwind_table_edit
2782 {
2783   arm_unwind_edit_type type;
2784   /* Note: we sometimes want to insert an unwind entry corresponding to a
2785      section different from the one we're currently writing out, so record the
2786      (text) section this edit relates to here.  */
2787   asection *linked_section;
2788   unsigned int index;
2789   struct arm_unwind_table_edit *next;
2790 }
2791 arm_unwind_table_edit;
2792
2793 typedef struct _arm_elf_section_data
2794 {
2795   /* Information about mapping symbols.  */
2796   struct bfd_elf_section_data elf;
2797   unsigned int mapcount;
2798   unsigned int mapsize;
2799   elf32_arm_section_map *map;
2800   /* Information about CPU errata.  */
2801   unsigned int erratumcount;
2802   elf32_vfp11_erratum_list *erratumlist;
2803   unsigned int stm32l4xx_erratumcount;
2804   elf32_stm32l4xx_erratum_list *stm32l4xx_erratumlist;
2805   /* Information about unwind tables.  */
2806   union
2807   {
2808     /* Unwind info attached to a text section.  */
2809     struct
2810     {
2811       asection *arm_exidx_sec;
2812     } text;
2813
2814     /* Unwind info attached to an .ARM.exidx section.  */
2815     struct
2816     {
2817       arm_unwind_table_edit *unwind_edit_list;
2818       arm_unwind_table_edit *unwind_edit_tail;
2819     } exidx;
2820   } u;
2821 }
2822 _arm_elf_section_data;
2823
2824 #define elf32_arm_section_data(sec) \
2825   ((_arm_elf_section_data *) elf_section_data (sec))
2826
2827 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
2828    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
2829    so may be created multiple times: we use an array of these entries whilst
2830    relaxing which we can refresh easily, then create stubs for each potentially
2831    erratum-triggering instruction once we've settled on a solution.  */
2832
2833 struct a8_erratum_fix
2834 {
2835   bfd *input_bfd;
2836   asection *section;
2837   bfd_vma offset;
2838   bfd_vma addend;
2839   unsigned long orig_insn;
2840   char *stub_name;
2841   enum elf32_arm_stub_type stub_type;
2842   enum arm_st_branch_type branch_type;
2843 };
2844
2845 /* A table of relocs applied to branches which might trigger Cortex-A8
2846    erratum.  */
2847
2848 struct a8_erratum_reloc
2849 {
2850   bfd_vma from;
2851   bfd_vma destination;
2852   struct elf32_arm_link_hash_entry *hash;
2853   const char *sym_name;
2854   unsigned int r_type;
2855   enum arm_st_branch_type branch_type;
2856   bfd_boolean non_a8_stub;
2857 };
2858
2859 /* The size of the thread control block.  */
2860 #define TCB_SIZE        8
2861
2862 /* ARM-specific information about a PLT entry, over and above the usual
2863    gotplt_union.  */
2864 struct arm_plt_info
2865 {
2866   /* We reference count Thumb references to a PLT entry separately,
2867      so that we can emit the Thumb trampoline only if needed.  */
2868   bfd_signed_vma thumb_refcount;
2869
2870   /* Some references from Thumb code may be eliminated by BL->BLX
2871      conversion, so record them separately.  */
2872   bfd_signed_vma maybe_thumb_refcount;
2873
2874   /* How many of the recorded PLT accesses were from non-call relocations.
2875      This information is useful when deciding whether anything takes the
2876      address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
2877      non-call references to the function should resolve directly to the
2878      real runtime target.  */
2879   unsigned int noncall_refcount;
2880
2881   /* Since PLT entries have variable size if the Thumb prologue is
2882      used, we need to record the index into .got.plt instead of
2883      recomputing it from the PLT offset.  */
2884   bfd_signed_vma got_offset;
2885 };
2886
2887 /* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
2888 struct arm_local_iplt_info
2889 {
2890   /* The information that is usually found in the generic ELF part of
2891      the hash table entry.  */
2892   union gotplt_union root;
2893
2894   /* The information that is usually found in the ARM-specific part of
2895      the hash table entry.  */
2896   struct arm_plt_info arm;
2897
2898   /* A list of all potential dynamic relocations against this symbol.  */
2899   struct elf_dyn_relocs *dyn_relocs;
2900 };
2901
2902 struct elf_arm_obj_tdata
2903 {
2904   struct elf_obj_tdata root;
2905
2906   /* tls_type for each local got entry.  */
2907   char *local_got_tls_type;
2908
2909   /* GOTPLT entries for TLS descriptors.  */
2910   bfd_vma *local_tlsdesc_gotent;
2911
2912   /* Information for local symbols that need entries in .iplt.  */
2913   struct arm_local_iplt_info **local_iplt;
2914
2915   /* Zero to warn when linking objects with incompatible enum sizes.  */
2916   int no_enum_size_warning;
2917
2918   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
2919   int no_wchar_size_warning;
2920 };
2921
2922 #define elf_arm_tdata(bfd) \
2923   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
2924
2925 #define elf32_arm_local_got_tls_type(bfd) \
2926   (elf_arm_tdata (bfd)->local_got_tls_type)
2927
2928 #define elf32_arm_local_tlsdesc_gotent(bfd) \
2929   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
2930
2931 #define elf32_arm_local_iplt(bfd) \
2932   (elf_arm_tdata (bfd)->local_iplt)
2933
2934 #define is_arm_elf(bfd) \
2935   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2936    && elf_tdata (bfd) != NULL \
2937    && elf_object_id (bfd) == ARM_ELF_DATA)
2938
2939 static bfd_boolean
2940 elf32_arm_mkobject (bfd *abfd)
2941 {
2942   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
2943                                   ARM_ELF_DATA);
2944 }
2945
2946 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2947
2948 /* Arm ELF linker hash entry.  */
2949 struct elf32_arm_link_hash_entry
2950 {
2951   struct elf_link_hash_entry root;
2952
2953   /* Track dynamic relocs copied for this symbol.  */
2954   struct elf_dyn_relocs *dyn_relocs;
2955
2956   /* ARM-specific PLT information.  */
2957   struct arm_plt_info plt;
2958
2959 #define GOT_UNKNOWN     0
2960 #define GOT_NORMAL      1
2961 #define GOT_TLS_GD      2
2962 #define GOT_TLS_IE      4
2963 #define GOT_TLS_GDESC   8
2964 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
2965   unsigned int tls_type : 8;
2966
2967   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
2968   unsigned int is_iplt : 1;
2969
2970   unsigned int unused : 23;
2971
2972   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
2973      starting at the end of the jump table.  */
2974   bfd_vma tlsdesc_got;
2975
2976   /* The symbol marking the real symbol location for exported thumb
2977      symbols with Arm stubs.  */
2978   struct elf_link_hash_entry *export_glue;
2979
2980   /* A pointer to the most recently used stub hash entry against this
2981      symbol.  */
2982   struct elf32_arm_stub_hash_entry *stub_cache;
2983 };
2984
2985 /* Traverse an arm ELF linker hash table.  */
2986 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2987   (elf_link_hash_traverse                                               \
2988    (&(table)->root,                                                     \
2989     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2990     (info)))
2991
2992 /* Get the ARM elf linker hash table from a link_info structure.  */
2993 #define elf32_arm_hash_table(info) \
2994   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
2995   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
2996
2997 #define arm_stub_hash_lookup(table, string, create, copy) \
2998   ((struct elf32_arm_stub_hash_entry *) \
2999    bfd_hash_lookup ((table), (string), (create), (copy)))
3000
3001 /* Array to keep track of which stub sections have been created, and
3002    information on stub grouping.  */
3003 struct map_stub
3004 {
3005   /* This is the section to which stubs in the group will be
3006      attached.  */
3007   asection *link_sec;
3008   /* The stub section.  */
3009   asection *stub_sec;
3010 };
3011
3012 #define elf32_arm_compute_jump_table_size(htab) \
3013   ((htab)->next_tls_desc_index * 4)
3014
3015 /* ARM ELF linker hash table.  */
3016 struct elf32_arm_link_hash_table
3017 {
3018   /* The main hash table.  */
3019   struct elf_link_hash_table root;
3020
3021   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
3022   bfd_size_type thumb_glue_size;
3023
3024   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
3025   bfd_size_type arm_glue_size;
3026
3027   /* The size in bytes of section containing the ARMv4 BX veneers.  */
3028   bfd_size_type bx_glue_size;
3029
3030   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
3031      veneer has been populated.  */
3032   bfd_vma bx_glue_offset[15];
3033
3034   /* The size in bytes of the section containing glue for VFP11 erratum
3035      veneers.  */
3036   bfd_size_type vfp11_erratum_glue_size;
3037
3038  /* The size in bytes of the section containing glue for STM32L4XX erratum
3039      veneers.  */
3040   bfd_size_type stm32l4xx_erratum_glue_size;
3041
3042   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
3043      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
3044      elf32_arm_write_section().  */
3045   struct a8_erratum_fix *a8_erratum_fixes;
3046   unsigned int num_a8_erratum_fixes;
3047
3048   /* An arbitrary input BFD chosen to hold the glue sections.  */
3049   bfd * bfd_of_glue_owner;
3050
3051   /* Nonzero to output a BE8 image.  */
3052   int byteswap_code;
3053
3054   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
3055      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
3056   int target1_is_rel;
3057
3058   /* The relocation to use for R_ARM_TARGET2 relocations.  */
3059   int target2_reloc;
3060
3061   /* 0 = Ignore R_ARM_V4BX.
3062      1 = Convert BX to MOV PC.
3063      2 = Generate v4 interworing stubs.  */
3064   int fix_v4bx;
3065
3066   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
3067   int fix_cortex_a8;
3068
3069   /* Whether we should fix the ARM1176 BLX immediate issue.  */
3070   int fix_arm1176;
3071
3072   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
3073   int use_blx;
3074
3075   /* What sort of code sequences we should look for which may trigger the
3076      VFP11 denorm erratum.  */
3077   bfd_arm_vfp11_fix vfp11_fix;
3078
3079   /* Global counter for the number of fixes we have emitted.  */
3080   int num_vfp11_fixes;
3081
3082   /* What sort of code sequences we should look for which may trigger the
3083      STM32L4XX erratum.  */
3084   bfd_arm_stm32l4xx_fix stm32l4xx_fix;
3085
3086   /* Global counter for the number of fixes we have emitted.  */
3087   int num_stm32l4xx_fixes;
3088
3089   /* Nonzero to force PIC branch veneers.  */
3090   int pic_veneer;
3091
3092   /* The number of bytes in the initial entry in the PLT.  */
3093   bfd_size_type plt_header_size;
3094
3095   /* The number of bytes in the subsequent PLT etries.  */
3096   bfd_size_type plt_entry_size;
3097
3098   /* True if the target system is VxWorks.  */
3099   int vxworks_p;
3100
3101   /* True if the target system is Symbian OS.  */
3102   int symbian_p;
3103
3104   /* True if the target system is Native Client.  */
3105   int nacl_p;
3106
3107   /* True if the target uses REL relocations.  */
3108   int use_rel;
3109
3110   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
3111   bfd_vma next_tls_desc_index;
3112
3113   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
3114   bfd_vma num_tls_desc;
3115
3116   /* Short-cuts to get to dynamic linker sections.  */
3117   asection *sdynbss;
3118   asection *srelbss;
3119
3120   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
3121   asection *srelplt2;
3122
3123   /* The offset into splt of the PLT entry for the TLS descriptor
3124      resolver.  Special values are 0, if not necessary (or not found
3125      to be necessary yet), and -1 if needed but not determined
3126      yet.  */
3127   bfd_vma dt_tlsdesc_plt;
3128
3129   /* The offset into sgot of the GOT entry used by the PLT entry
3130      above.  */
3131   bfd_vma dt_tlsdesc_got;
3132
3133   /* Offset in .plt section of tls_arm_trampoline.  */
3134   bfd_vma tls_trampoline;
3135
3136   /* Data for R_ARM_TLS_LDM32 relocations.  */
3137   union
3138   {
3139     bfd_signed_vma refcount;
3140     bfd_vma offset;
3141   } tls_ldm_got;
3142
3143   /* Small local sym cache.  */
3144   struct sym_cache sym_cache;
3145
3146   /* For convenience in allocate_dynrelocs.  */
3147   bfd * obfd;
3148
3149   /* The amount of space used by the reserved portion of the sgotplt
3150      section, plus whatever space is used by the jump slots.  */
3151   bfd_vma sgotplt_jump_table_size;
3152
3153   /* The stub hash table.  */
3154   struct bfd_hash_table stub_hash_table;
3155
3156   /* Linker stub bfd.  */
3157   bfd *stub_bfd;
3158
3159   /* Linker call-backs.  */
3160   asection * (*add_stub_section) (const char *, asection *, unsigned int);
3161   void (*layout_sections_again) (void);
3162
3163   /* Array to keep track of which stub sections have been created, and
3164      information on stub grouping.  */
3165   struct map_stub *stub_group;
3166
3167   /* Number of elements in stub_group.  */
3168   unsigned int top_id;
3169
3170   /* Assorted information used by elf32_arm_size_stubs.  */
3171   unsigned int bfd_count;
3172   unsigned int top_index;
3173   asection **input_list;
3174 };
3175
3176 static inline int
3177 ctz (unsigned int mask)
3178 {
3179 #if GCC_VERSION >= 3004
3180   return __builtin_ctz (mask);
3181 #else
3182   unsigned int i;
3183
3184   for (i = 0; i < 8 * sizeof (mask); i++)
3185     {
3186       if (mask & 0x1)
3187         break;
3188       mask = (mask >> 1);
3189     }
3190   return i;
3191 #endif
3192 }
3193
3194 static inline int
3195 popcount (unsigned int mask)
3196 {
3197 #if GCC_VERSION >= 3004
3198   return __builtin_popcount (mask);
3199 #else
3200   unsigned int i, sum = 0;
3201
3202   for (i = 0; i < 8 * sizeof (mask); i++)
3203     {
3204       if (mask & 0x1)
3205         sum++;
3206       mask = (mask >> 1);
3207     }
3208   return sum;
3209 #endif
3210 }
3211
3212 /* Create an entry in an ARM ELF linker hash table.  */
3213
3214 static struct bfd_hash_entry *
3215 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3216                              struct bfd_hash_table * table,
3217                              const char * string)
3218 {
3219   struct elf32_arm_link_hash_entry * ret =
3220     (struct elf32_arm_link_hash_entry *) entry;
3221
3222   /* Allocate the structure if it has not already been allocated by a
3223      subclass.  */
3224   if (ret == NULL)
3225     ret = (struct elf32_arm_link_hash_entry *)
3226         bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3227   if (ret == NULL)
3228     return (struct bfd_hash_entry *) ret;
3229
3230   /* Call the allocation method of the superclass.  */
3231   ret = ((struct elf32_arm_link_hash_entry *)
3232          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3233                                      table, string));
3234   if (ret != NULL)
3235     {
3236       ret->dyn_relocs = NULL;
3237       ret->tls_type = GOT_UNKNOWN;
3238       ret->tlsdesc_got = (bfd_vma) -1;
3239       ret->plt.thumb_refcount = 0;
3240       ret->plt.maybe_thumb_refcount = 0;
3241       ret->plt.noncall_refcount = 0;
3242       ret->plt.got_offset = -1;
3243       ret->is_iplt = FALSE;
3244       ret->export_glue = NULL;
3245
3246       ret->stub_cache = NULL;
3247     }
3248
3249   return (struct bfd_hash_entry *) ret;
3250 }
3251
3252 /* Ensure that we have allocated bookkeeping structures for ABFD's local
3253    symbols.  */
3254
3255 static bfd_boolean
3256 elf32_arm_allocate_local_sym_info (bfd *abfd)
3257 {
3258   if (elf_local_got_refcounts (abfd) == NULL)
3259     {
3260       bfd_size_type num_syms;
3261       bfd_size_type size;
3262       char *data;
3263
3264       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3265       size = num_syms * (sizeof (bfd_signed_vma)
3266                          + sizeof (struct arm_local_iplt_info *)
3267                          + sizeof (bfd_vma)
3268                          + sizeof (char));
3269       data = bfd_zalloc (abfd, size);
3270       if (data == NULL)
3271         return FALSE;
3272
3273       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
3274       data += num_syms * sizeof (bfd_signed_vma);
3275
3276       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
3277       data += num_syms * sizeof (struct arm_local_iplt_info *);
3278
3279       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
3280       data += num_syms * sizeof (bfd_vma);
3281
3282       elf32_arm_local_got_tls_type (abfd) = data;
3283     }
3284   return TRUE;
3285 }
3286
3287 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
3288    to input bfd ABFD.  Create the information if it doesn't already exist.
3289    Return null if an allocation fails.  */
3290
3291 static struct arm_local_iplt_info *
3292 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3293 {
3294   struct arm_local_iplt_info **ptr;
3295
3296   if (!elf32_arm_allocate_local_sym_info (abfd))
3297     return NULL;
3298
3299   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3300   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3301   if (*ptr == NULL)
3302     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3303   return *ptr;
3304 }
3305
3306 /* Try to obtain PLT information for the symbol with index R_SYMNDX
3307    in ABFD's symbol table.  If the symbol is global, H points to its
3308    hash table entry, otherwise H is null.
3309
3310    Return true if the symbol does have PLT information.  When returning
3311    true, point *ROOT_PLT at the target-independent reference count/offset
3312    union and *ARM_PLT at the ARM-specific information.  */
3313
3314 static bfd_boolean
3315 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_entry *h,
3316                         unsigned long r_symndx, union gotplt_union **root_plt,
3317                         struct arm_plt_info **arm_plt)
3318 {
3319   struct arm_local_iplt_info *local_iplt;
3320
3321   if (h != NULL)
3322     {
3323       *root_plt = &h->root.plt;
3324       *arm_plt = &h->plt;
3325       return TRUE;
3326     }
3327
3328   if (elf32_arm_local_iplt (abfd) == NULL)
3329     return FALSE;
3330
3331   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3332   if (local_iplt == NULL)
3333     return FALSE;
3334
3335   *root_plt = &local_iplt->root;
3336   *arm_plt = &local_iplt->arm;
3337   return TRUE;
3338 }
3339
3340 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
3341    before it.  */
3342
3343 static bfd_boolean
3344 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3345                                   struct arm_plt_info *arm_plt)
3346 {
3347   struct elf32_arm_link_hash_table *htab;
3348
3349   htab = elf32_arm_hash_table (info);
3350   return (arm_plt->thumb_refcount != 0
3351           || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0));
3352 }
3353
3354 /* Return a pointer to the head of the dynamic reloc list that should
3355    be used for local symbol ISYM, which is symbol number R_SYMNDX in
3356    ABFD's symbol table.  Return null if an error occurs.  */
3357
3358 static struct elf_dyn_relocs **
3359 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3360                                    Elf_Internal_Sym *isym)
3361 {
3362   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3363     {
3364       struct arm_local_iplt_info *local_iplt;
3365
3366       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3367       if (local_iplt == NULL)
3368         return NULL;
3369       return &local_iplt->dyn_relocs;
3370     }
3371   else
3372     {
3373       /* Track dynamic relocs needed for local syms too.
3374          We really need local syms available to do this
3375          easily.  Oh well.  */
3376       asection *s;
3377       void *vpp;
3378
3379       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3380       if (s == NULL)
3381         abort ();
3382
3383       vpp = &elf_section_data (s)->local_dynrel;
3384       return (struct elf_dyn_relocs **) vpp;
3385     }
3386 }
3387
3388 /* Initialize an entry in the stub hash table.  */
3389
3390 static struct bfd_hash_entry *
3391 stub_hash_newfunc (struct bfd_hash_entry *entry,
3392                    struct bfd_hash_table *table,
3393                    const char *string)
3394 {
3395   /* Allocate the structure if it has not already been allocated by a
3396      subclass.  */
3397   if (entry == NULL)
3398     {
3399       entry = (struct bfd_hash_entry *)
3400           bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3401       if (entry == NULL)
3402         return entry;
3403     }
3404
3405   /* Call the allocation method of the superclass.  */
3406   entry = bfd_hash_newfunc (entry, table, string);
3407   if (entry != NULL)
3408     {
3409       struct elf32_arm_stub_hash_entry *eh;
3410
3411       /* Initialize the local fields.  */
3412       eh = (struct elf32_arm_stub_hash_entry *) entry;
3413       eh->stub_sec = NULL;
3414       eh->stub_offset = 0;
3415       eh->target_value = 0;
3416       eh->target_section = NULL;
3417       eh->target_addend = 0;
3418       eh->orig_insn = 0;
3419       eh->stub_type = arm_stub_none;
3420       eh->stub_size = 0;
3421       eh->stub_template = NULL;
3422       eh->stub_template_size = 0;
3423       eh->h = NULL;
3424       eh->id_sec = NULL;
3425       eh->output_name = NULL;
3426     }
3427
3428   return entry;
3429 }
3430
3431 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3432    shortcuts to them in our hash table.  */
3433
3434 static bfd_boolean
3435 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3436 {
3437   struct elf32_arm_link_hash_table *htab;
3438
3439   htab = elf32_arm_hash_table (info);
3440   if (htab == NULL)
3441     return FALSE;
3442
3443   /* BPABI objects never have a GOT, or associated sections.  */
3444   if (htab->symbian_p)
3445     return TRUE;
3446
3447   if (! _bfd_elf_create_got_section (dynobj, info))
3448     return FALSE;
3449
3450   return TRUE;
3451 }
3452
3453 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3454
3455 static bfd_boolean
3456 create_ifunc_sections (struct bfd_link_info *info)
3457 {
3458   struct elf32_arm_link_hash_table *htab;
3459   const struct elf_backend_data *bed;
3460   bfd *dynobj;
3461   asection *s;
3462   flagword flags;
3463
3464   htab = elf32_arm_hash_table (info);
3465   dynobj = htab->root.dynobj;
3466   bed = get_elf_backend_data (dynobj);
3467   flags = bed->dynamic_sec_flags;
3468
3469   if (htab->root.iplt == NULL)
3470     {
3471       s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3472                                               flags | SEC_READONLY | SEC_CODE);
3473       if (s == NULL
3474           || !bfd_set_section_alignment (dynobj, s, bed->plt_alignment))
3475         return FALSE;
3476       htab->root.iplt = s;
3477     }
3478
3479   if (htab->root.irelplt == NULL)
3480     {
3481       s = bfd_make_section_anyway_with_flags (dynobj,
3482                                               RELOC_SECTION (htab, ".iplt"),
3483                                               flags | SEC_READONLY);
3484       if (s == NULL
3485           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3486         return FALSE;
3487       htab->root.irelplt = s;
3488     }
3489
3490   if (htab->root.igotplt == NULL)
3491     {
3492       s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3493       if (s == NULL
3494           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3495         return FALSE;
3496       htab->root.igotplt = s;
3497     }
3498   return TRUE;
3499 }
3500
3501 /* Determine if we're dealing with a Thumb only architecture.  */
3502
3503 static bfd_boolean
3504 using_thumb_only (struct elf32_arm_link_hash_table *globals)
3505 {
3506   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3507                                        Tag_CPU_arch);
3508   int profile;
3509
3510   if (arch == TAG_CPU_ARCH_V6_M || arch == TAG_CPU_ARCH_V6S_M)
3511     return TRUE;
3512
3513   if (arch != TAG_CPU_ARCH_V7 && arch != TAG_CPU_ARCH_V7E_M)
3514     return FALSE;
3515
3516   profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3517                                       Tag_CPU_arch_profile);
3518
3519   return profile == 'M';
3520 }
3521
3522 /* Determine if we're dealing with a Thumb-2 object.  */
3523
3524 static bfd_boolean
3525 using_thumb2 (struct elf32_arm_link_hash_table *globals)
3526 {
3527   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3528                                        Tag_CPU_arch);
3529   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
3530 }
3531
3532 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3533    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3534    hash table.  */
3535
3536 static bfd_boolean
3537 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3538 {
3539   struct elf32_arm_link_hash_table *htab;
3540
3541   htab = elf32_arm_hash_table (info);
3542   if (htab == NULL)
3543     return FALSE;
3544
3545   if (!htab->root.sgot && !create_got_section (dynobj, info))
3546     return FALSE;
3547
3548   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3549     return FALSE;
3550
3551   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
3552   if (!bfd_link_pic (info))
3553     htab->srelbss = bfd_get_linker_section (dynobj,
3554                                             RELOC_SECTION (htab, ".bss"));
3555
3556   if (htab->vxworks_p)
3557     {
3558       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3559         return FALSE;
3560
3561       if (bfd_link_pic (info))
3562         {
3563           htab->plt_header_size = 0;
3564           htab->plt_entry_size
3565             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3566         }
3567       else
3568         {
3569           htab->plt_header_size
3570             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3571           htab->plt_entry_size
3572             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3573         }
3574     }
3575   else
3576     {
3577       /* PR ld/16017
3578          Test for thumb only architectures.  Note - we cannot just call
3579          using_thumb_only() as the attributes in the output bfd have not been
3580          initialised at this point, so instead we use the input bfd.  */
3581       bfd * saved_obfd = htab->obfd;
3582
3583       htab->obfd = dynobj;
3584       if (using_thumb_only (htab))
3585         {
3586           htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
3587           htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
3588         }
3589       htab->obfd = saved_obfd;
3590     }
3591
3592   if (!htab->root.splt
3593       || !htab->root.srelplt
3594       || !htab->sdynbss
3595       || (!bfd_link_pic (info) && !htab->srelbss))
3596     abort ();
3597
3598   return TRUE;
3599 }
3600
3601 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3602
3603 static void
3604 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3605                                 struct elf_link_hash_entry *dir,
3606                                 struct elf_link_hash_entry *ind)
3607 {
3608   struct elf32_arm_link_hash_entry *edir, *eind;
3609
3610   edir = (struct elf32_arm_link_hash_entry *) dir;
3611   eind = (struct elf32_arm_link_hash_entry *) ind;
3612
3613   if (eind->dyn_relocs != NULL)
3614     {
3615       if (edir->dyn_relocs != NULL)
3616         {
3617           struct elf_dyn_relocs **pp;
3618           struct elf_dyn_relocs *p;
3619
3620           /* Add reloc counts against the indirect sym to the direct sym
3621              list.  Merge any entries against the same section.  */
3622           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3623             {
3624               struct elf_dyn_relocs *q;
3625
3626               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3627                 if (q->sec == p->sec)
3628                   {
3629                     q->pc_count += p->pc_count;
3630                     q->count += p->count;
3631                     *pp = p->next;
3632                     break;
3633                   }
3634               if (q == NULL)
3635                 pp = &p->next;
3636             }
3637           *pp = edir->dyn_relocs;
3638         }
3639
3640       edir->dyn_relocs = eind->dyn_relocs;
3641       eind->dyn_relocs = NULL;
3642     }
3643
3644   if (ind->root.type == bfd_link_hash_indirect)
3645     {
3646       /* Copy over PLT info.  */
3647       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3648       eind->plt.thumb_refcount = 0;
3649       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3650       eind->plt.maybe_thumb_refcount = 0;
3651       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3652       eind->plt.noncall_refcount = 0;
3653
3654       /* We should only allocate a function to .iplt once the final
3655          symbol information is known.  */
3656       BFD_ASSERT (!eind->is_iplt);
3657
3658       if (dir->got.refcount <= 0)
3659         {
3660           edir->tls_type = eind->tls_type;
3661           eind->tls_type = GOT_UNKNOWN;
3662         }
3663     }
3664
3665   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3666 }
3667
3668 /* Destroy an ARM elf linker hash table.  */
3669
3670 static void
3671 elf32_arm_link_hash_table_free (bfd *obfd)
3672 {
3673   struct elf32_arm_link_hash_table *ret
3674     = (struct elf32_arm_link_hash_table *) obfd->link.hash;
3675
3676   bfd_hash_table_free (&ret->stub_hash_table);
3677   _bfd_elf_link_hash_table_free (obfd);
3678 }
3679
3680 /* Create an ARM elf linker hash table.  */
3681
3682 static struct bfd_link_hash_table *
3683 elf32_arm_link_hash_table_create (bfd *abfd)
3684 {
3685   struct elf32_arm_link_hash_table *ret;
3686   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
3687
3688   ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
3689   if (ret == NULL)
3690     return NULL;
3691
3692   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
3693                                       elf32_arm_link_hash_newfunc,
3694                                       sizeof (struct elf32_arm_link_hash_entry),
3695                                       ARM_ELF_DATA))
3696     {
3697       free (ret);
3698       return NULL;
3699     }
3700
3701   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
3702   ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
3703 #ifdef FOUR_WORD_PLT
3704   ret->plt_header_size = 16;
3705   ret->plt_entry_size = 16;
3706 #else
3707   ret->plt_header_size = 20;
3708   ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
3709 #endif
3710   ret->use_rel = 1;
3711   ret->obfd = abfd;
3712
3713   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
3714                             sizeof (struct elf32_arm_stub_hash_entry)))
3715     {
3716       _bfd_elf_link_hash_table_free (abfd);
3717       return NULL;
3718     }
3719   ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
3720
3721   return &ret->root.root;
3722 }
3723
3724 /* Determine what kind of NOPs are available.  */
3725
3726 static bfd_boolean
3727 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
3728 {
3729   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3730                                              Tag_CPU_arch);
3731   return arch == TAG_CPU_ARCH_V6T2
3732          || arch == TAG_CPU_ARCH_V6K
3733          || arch == TAG_CPU_ARCH_V7
3734          || arch == TAG_CPU_ARCH_V7E_M;
3735 }
3736
3737 static bfd_boolean
3738 arch_has_thumb2_nop (struct elf32_arm_link_hash_table *globals)
3739 {
3740   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3741                                              Tag_CPU_arch);
3742   return (arch == TAG_CPU_ARCH_V6T2 || arch == TAG_CPU_ARCH_V7
3743           || arch == TAG_CPU_ARCH_V7E_M);
3744 }
3745
3746 static bfd_boolean
3747 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
3748 {
3749   switch (stub_type)
3750     {
3751     case arm_stub_long_branch_thumb_only:
3752     case arm_stub_long_branch_v4t_thumb_arm:
3753     case arm_stub_short_branch_v4t_thumb_arm:
3754     case arm_stub_long_branch_v4t_thumb_arm_pic:
3755     case arm_stub_long_branch_v4t_thumb_tls_pic:
3756     case arm_stub_long_branch_thumb_only_pic:
3757       return TRUE;
3758     case arm_stub_none:
3759       BFD_FAIL ();
3760       return FALSE;
3761       break;
3762     default:
3763       return FALSE;
3764     }
3765 }
3766
3767 /* Determine the type of stub needed, if any, for a call.  */
3768
3769 static enum elf32_arm_stub_type
3770 arm_type_of_stub (struct bfd_link_info *info,
3771                   asection *input_sec,
3772                   const Elf_Internal_Rela *rel,
3773                   unsigned char st_type,
3774                   enum arm_st_branch_type *actual_branch_type,
3775                   struct elf32_arm_link_hash_entry *hash,
3776                   bfd_vma destination,
3777                   asection *sym_sec,
3778                   bfd *input_bfd,
3779                   const char *name)
3780 {
3781   bfd_vma location;
3782   bfd_signed_vma branch_offset;
3783   unsigned int r_type;
3784   struct elf32_arm_link_hash_table * globals;
3785   int thumb2;
3786   int thumb_only;
3787   enum elf32_arm_stub_type stub_type = arm_stub_none;
3788   int use_plt = 0;
3789   enum arm_st_branch_type branch_type = *actual_branch_type;
3790   union gotplt_union *root_plt;
3791   struct arm_plt_info *arm_plt;
3792
3793   if (branch_type == ST_BRANCH_LONG)
3794     return stub_type;
3795
3796   globals = elf32_arm_hash_table (info);
3797   if (globals == NULL)
3798     return stub_type;
3799
3800   thumb_only = using_thumb_only (globals);
3801
3802   thumb2 = using_thumb2 (globals);
3803
3804   /* Determine where the call point is.  */
3805   location = (input_sec->output_offset
3806               + input_sec->output_section->vma
3807               + rel->r_offset);
3808
3809   r_type = ELF32_R_TYPE (rel->r_info);
3810
3811   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
3812      are considering a function call relocation.  */
3813   if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
3814                      || r_type == R_ARM_THM_JUMP19)
3815       && branch_type == ST_BRANCH_TO_ARM)
3816     branch_type = ST_BRANCH_TO_THUMB;
3817
3818   /* For TLS call relocs, it is the caller's responsibility to provide
3819      the address of the appropriate trampoline.  */
3820   if (r_type != R_ARM_TLS_CALL
3821       && r_type != R_ARM_THM_TLS_CALL
3822       && elf32_arm_get_plt_info (input_bfd, hash, ELF32_R_SYM (rel->r_info),
3823                                  &root_plt, &arm_plt)
3824       && root_plt->offset != (bfd_vma) -1)
3825     {
3826       asection *splt;
3827
3828       if (hash == NULL || hash->is_iplt)
3829         splt = globals->root.iplt;
3830       else
3831         splt = globals->root.splt;
3832       if (splt != NULL)
3833         {
3834           use_plt = 1;
3835
3836           /* Note when dealing with PLT entries: the main PLT stub is in
3837              ARM mode, so if the branch is in Thumb mode, another
3838              Thumb->ARM stub will be inserted later just before the ARM
3839              PLT stub. We don't take this extra distance into account
3840              here, because if a long branch stub is needed, we'll add a
3841              Thumb->Arm one and branch directly to the ARM PLT entry
3842              because it avoids spreading offset corrections in several
3843              places.  */
3844
3845           destination = (splt->output_section->vma
3846                          + splt->output_offset
3847                          + root_plt->offset);
3848           st_type = STT_FUNC;
3849           branch_type = ST_BRANCH_TO_ARM;
3850         }
3851     }
3852   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
3853   BFD_ASSERT (st_type != STT_GNU_IFUNC);
3854
3855   branch_offset = (bfd_signed_vma)(destination - location);
3856
3857   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
3858       || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
3859     {
3860       /* Handle cases where:
3861          - this call goes too far (different Thumb/Thumb2 max
3862            distance)
3863          - it's a Thumb->Arm call and blx is not available, or it's a
3864            Thumb->Arm branch (not bl). A stub is needed in this case,
3865            but only if this call is not through a PLT entry. Indeed,
3866            PLT stubs handle mode switching already.
3867       */
3868       if ((!thumb2
3869             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3870                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3871           || (thumb2
3872               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3873                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3874           || (thumb2
3875               && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
3876                   || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
3877               && (r_type == R_ARM_THM_JUMP19))
3878           || (branch_type == ST_BRANCH_TO_ARM
3879               && (((r_type == R_ARM_THM_CALL
3880                     || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
3881                   || (r_type == R_ARM_THM_JUMP24)
3882                   || (r_type == R_ARM_THM_JUMP19))
3883               && !use_plt))
3884         {
3885           if (branch_type == ST_BRANCH_TO_THUMB)
3886             {
3887               /* Thumb to thumb.  */
3888               if (!thumb_only)
3889                 {
3890                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3891                     /* PIC stubs.  */
3892                     ? ((globals->use_blx
3893                         && (r_type == R_ARM_THM_CALL))
3894                        /* V5T and above. Stub starts with ARM code, so
3895                           we must be able to switch mode before
3896                           reaching it, which is only possible for 'bl'
3897                           (ie R_ARM_THM_CALL relocation).  */
3898                        ? arm_stub_long_branch_any_thumb_pic
3899                        /* On V4T, use Thumb code only.  */
3900                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
3901
3902                     /* non-PIC stubs.  */
3903                     : ((globals->use_blx
3904                         && (r_type == R_ARM_THM_CALL))
3905                        /* V5T and above.  */
3906                        ? arm_stub_long_branch_any_any
3907                        /* V4T.  */
3908                        : arm_stub_long_branch_v4t_thumb_thumb);
3909                 }
3910               else
3911                 {
3912                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3913                     /* PIC stub.  */
3914                     ? arm_stub_long_branch_thumb_only_pic
3915                     /* non-PIC stub.  */
3916                     : arm_stub_long_branch_thumb_only;
3917                 }
3918             }
3919           else
3920             {
3921               /* Thumb to arm.  */
3922               if (sym_sec != NULL
3923                   && sym_sec->owner != NULL
3924                   && !INTERWORK_FLAG (sym_sec->owner))
3925                 {
3926                   (*_bfd_error_handler)
3927                     (_("%B(%s): warning: interworking not enabled.\n"
3928                        "  first occurrence: %B: Thumb call to ARM"),
3929                      sym_sec->owner, input_bfd, name);
3930                 }
3931
3932               stub_type =
3933                 (bfd_link_pic (info) | globals->pic_veneer)
3934                 /* PIC stubs.  */
3935                 ? (r_type == R_ARM_THM_TLS_CALL
3936                    /* TLS PIC stubs.  */
3937                    ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
3938                       : arm_stub_long_branch_v4t_thumb_tls_pic)
3939                    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3940                       /* V5T PIC and above.  */
3941                       ? arm_stub_long_branch_any_arm_pic
3942                       /* V4T PIC stub.  */
3943                       : arm_stub_long_branch_v4t_thumb_arm_pic))
3944
3945                 /* non-PIC stubs.  */
3946                 : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3947                    /* V5T and above.  */
3948                    ? arm_stub_long_branch_any_any
3949                    /* V4T.  */
3950                    : arm_stub_long_branch_v4t_thumb_arm);
3951
3952               /* Handle v4t short branches.  */
3953               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3954                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3955                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3956                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3957             }
3958         }
3959     }
3960   else if (r_type == R_ARM_CALL
3961            || r_type == R_ARM_JUMP24
3962            || r_type == R_ARM_PLT32
3963            || r_type == R_ARM_TLS_CALL)
3964     {
3965       if (branch_type == ST_BRANCH_TO_THUMB)
3966         {
3967           /* Arm to thumb.  */
3968
3969           if (sym_sec != NULL
3970               && sym_sec->owner != NULL
3971               && !INTERWORK_FLAG (sym_sec->owner))
3972             {
3973               (*_bfd_error_handler)
3974                 (_("%B(%s): warning: interworking not enabled.\n"
3975                    "  first occurrence: %B: ARM call to Thumb"),
3976                  sym_sec->owner, input_bfd, name);
3977             }
3978
3979           /* We have an extra 2-bytes reach because of
3980              the mode change (bit 24 (H) of BLX encoding).  */
3981           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3982               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3983               || (r_type == R_ARM_CALL && !globals->use_blx)
3984               || (r_type == R_ARM_JUMP24)
3985               || (r_type == R_ARM_PLT32))
3986             {
3987               stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3988                 /* PIC stubs.  */
3989                 ? ((globals->use_blx)
3990                    /* V5T and above.  */
3991                    ? arm_stub_long_branch_any_thumb_pic
3992                    /* V4T stub.  */
3993                    : arm_stub_long_branch_v4t_arm_thumb_pic)
3994
3995                 /* non-PIC stubs.  */
3996                 : ((globals->use_blx)
3997                    /* V5T and above.  */
3998                    ? arm_stub_long_branch_any_any
3999                    /* V4T.  */
4000                    : arm_stub_long_branch_v4t_arm_thumb);
4001             }
4002         }
4003       else
4004         {
4005           /* Arm to arm.  */
4006           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4007               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4008             {
4009               stub_type =
4010                 (bfd_link_pic (info) | globals->pic_veneer)
4011                 /* PIC stubs.  */
4012                 ? (r_type == R_ARM_TLS_CALL
4013                    /* TLS PIC Stub.  */
4014                    ? arm_stub_long_branch_any_tls_pic
4015                    : (globals->nacl_p
4016                       ? arm_stub_long_branch_arm_nacl_pic
4017                       : arm_stub_long_branch_any_arm_pic))
4018                 /* non-PIC stubs.  */
4019                 : (globals->nacl_p
4020                    ? arm_stub_long_branch_arm_nacl
4021                    : arm_stub_long_branch_any_any);
4022             }
4023         }
4024     }
4025
4026   /* If a stub is needed, record the actual destination type.  */
4027   if (stub_type != arm_stub_none)
4028     *actual_branch_type = branch_type;
4029
4030   return stub_type;
4031 }
4032
4033 /* Build a name for an entry in the stub hash table.  */
4034
4035 static char *
4036 elf32_arm_stub_name (const asection *input_section,
4037                      const asection *sym_sec,
4038                      const struct elf32_arm_link_hash_entry *hash,
4039                      const Elf_Internal_Rela *rel,
4040                      enum elf32_arm_stub_type stub_type)
4041 {
4042   char *stub_name;
4043   bfd_size_type len;
4044
4045   if (hash)
4046     {
4047       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4048       stub_name = (char *) bfd_malloc (len);
4049       if (stub_name != NULL)
4050         sprintf (stub_name, "%08x_%s+%x_%d",
4051                  input_section->id & 0xffffffff,
4052                  hash->root.root.root.string,
4053                  (int) rel->r_addend & 0xffffffff,
4054                  (int) stub_type);
4055     }
4056   else
4057     {
4058       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4059       stub_name = (char *) bfd_malloc (len);
4060       if (stub_name != NULL)
4061         sprintf (stub_name, "%08x_%x:%x+%x_%d",
4062                  input_section->id & 0xffffffff,
4063                  sym_sec->id & 0xffffffff,
4064                  ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4065                  || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4066                  ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4067                  (int) rel->r_addend & 0xffffffff,
4068                  (int) stub_type);
4069     }
4070
4071   return stub_name;
4072 }
4073
4074 /* Look up an entry in the stub hash.  Stub entries are cached because
4075    creating the stub name takes a bit of time.  */
4076
4077 static struct elf32_arm_stub_hash_entry *
4078 elf32_arm_get_stub_entry (const asection *input_section,
4079                           const asection *sym_sec,
4080                           struct elf_link_hash_entry *hash,
4081                           const Elf_Internal_Rela *rel,
4082                           struct elf32_arm_link_hash_table *htab,
4083                           enum elf32_arm_stub_type stub_type)
4084 {
4085   struct elf32_arm_stub_hash_entry *stub_entry;
4086   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4087   const asection *id_sec;
4088
4089   if ((input_section->flags & SEC_CODE) == 0)
4090     return NULL;
4091
4092   /* If this input section is part of a group of sections sharing one
4093      stub section, then use the id of the first section in the group.
4094      Stub names need to include a section id, as there may well be
4095      more than one stub used to reach say, printf, and we need to
4096      distinguish between them.  */
4097   id_sec = htab->stub_group[input_section->id].link_sec;
4098
4099   if (h != NULL && h->stub_cache != NULL
4100       && h->stub_cache->h == h
4101       && h->stub_cache->id_sec == id_sec
4102       && h->stub_cache->stub_type == stub_type)
4103     {
4104       stub_entry = h->stub_cache;
4105     }
4106   else
4107     {
4108       char *stub_name;
4109
4110       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4111       if (stub_name == NULL)
4112         return NULL;
4113
4114       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4115                                         stub_name, FALSE, FALSE);
4116       if (h != NULL)
4117         h->stub_cache = stub_entry;
4118
4119       free (stub_name);
4120     }
4121
4122   return stub_entry;
4123 }
4124
4125 /* Find or create a stub section.  Returns a pointer to the stub section, and
4126    the section to which the stub section will be attached (in *LINK_SEC_P).
4127    LINK_SEC_P may be NULL.  */
4128
4129 static asection *
4130 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4131                                    struct elf32_arm_link_hash_table *htab)
4132 {
4133   asection *link_sec;
4134   asection *stub_sec;
4135
4136   link_sec = htab->stub_group[section->id].link_sec;
4137   BFD_ASSERT (link_sec != NULL);
4138   stub_sec = htab->stub_group[section->id].stub_sec;
4139
4140   if (stub_sec == NULL)
4141     {
4142       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4143       if (stub_sec == NULL)
4144         {
4145           size_t namelen;
4146           bfd_size_type len;
4147           char *s_name;
4148
4149           namelen = strlen (link_sec->name);
4150           len = namelen + sizeof (STUB_SUFFIX);
4151           s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4152           if (s_name == NULL)
4153             return NULL;
4154
4155           memcpy (s_name, link_sec->name, namelen);
4156           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4157           stub_sec = (*htab->add_stub_section) (s_name, link_sec,
4158                                                 htab->nacl_p ? 4 : 3);
4159           if (stub_sec == NULL)
4160             return NULL;
4161           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4162         }
4163       htab->stub_group[section->id].stub_sec = stub_sec;
4164     }
4165
4166   if (link_sec_p)
4167     *link_sec_p = link_sec;
4168
4169   return stub_sec;
4170 }
4171
4172 /* Add a new stub entry to the stub hash.  Not all fields of the new
4173    stub entry are initialised.  */
4174
4175 static struct elf32_arm_stub_hash_entry *
4176 elf32_arm_add_stub (const char *stub_name,
4177                     asection *section,
4178                     struct elf32_arm_link_hash_table *htab)
4179 {
4180   asection *link_sec;
4181   asection *stub_sec;
4182   struct elf32_arm_stub_hash_entry *stub_entry;
4183
4184   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab);
4185   if (stub_sec == NULL)
4186     return NULL;
4187
4188   /* Enter this entry into the linker stub hash table.  */
4189   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4190                                      TRUE, FALSE);
4191   if (stub_entry == NULL)
4192     {
4193       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
4194                              section->owner,
4195                              stub_name);
4196       return NULL;
4197     }
4198
4199   stub_entry->stub_sec = stub_sec;
4200   stub_entry->stub_offset = 0;
4201   stub_entry->id_sec = link_sec;
4202
4203   return stub_entry;
4204 }
4205
4206 /* Store an Arm insn into an output section not processed by
4207    elf32_arm_write_section.  */
4208
4209 static void
4210 put_arm_insn (struct elf32_arm_link_hash_table * htab,
4211               bfd * output_bfd, bfd_vma val, void * ptr)
4212 {
4213   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4214     bfd_putl32 (val, ptr);
4215   else
4216     bfd_putb32 (val, ptr);
4217 }
4218
4219 /* Store a 16-bit Thumb insn into an output section not processed by
4220    elf32_arm_write_section.  */
4221
4222 static void
4223 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4224                 bfd * output_bfd, bfd_vma val, void * ptr)
4225 {
4226   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4227     bfd_putl16 (val, ptr);
4228   else
4229     bfd_putb16 (val, ptr);
4230 }
4231
4232 /* Store a Thumb2 insn into an output section not processed by
4233    elf32_arm_write_section.  */
4234
4235 static void
4236 put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4237                  bfd * output_bfd, bfd_vma val, void * ptr)
4238 {
4239   /* T2 instructions are 16-bit streamed.  */
4240   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4241     {
4242       bfd_putl16 ((val >> 16) & 0xffff, ptr);
4243       bfd_putl16 ((val & 0xffff), ptr + 2);
4244     }
4245   else
4246     {
4247       bfd_putb16 ((val >> 16) & 0xffff, ptr);
4248       bfd_putb16 ((val & 0xffff), ptr + 2);
4249     }
4250 }
4251
4252 /* If it's possible to change R_TYPE to a more efficient access
4253    model, return the new reloc type.  */
4254
4255 static unsigned
4256 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4257                           struct elf_link_hash_entry *h)
4258 {
4259   int is_local = (h == NULL);
4260
4261   if (bfd_link_pic (info)
4262       || (h && h->root.type == bfd_link_hash_undefweak))
4263     return r_type;
4264
4265   /* We do not support relaxations for Old TLS models.  */
4266   switch (r_type)
4267     {
4268     case R_ARM_TLS_GOTDESC:
4269     case R_ARM_TLS_CALL:
4270     case R_ARM_THM_TLS_CALL:
4271     case R_ARM_TLS_DESCSEQ:
4272     case R_ARM_THM_TLS_DESCSEQ:
4273       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4274     }
4275
4276   return r_type;
4277 }
4278
4279 static bfd_reloc_status_type elf32_arm_final_link_relocate
4280   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4281    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4282    const char *, unsigned char, enum arm_st_branch_type,
4283    struct elf_link_hash_entry *, bfd_boolean *, char **);
4284
4285 static unsigned int
4286 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4287 {
4288   switch (stub_type)
4289     {
4290     case arm_stub_a8_veneer_b_cond:
4291     case arm_stub_a8_veneer_b:
4292     case arm_stub_a8_veneer_bl:
4293       return 2;
4294
4295     case arm_stub_long_branch_any_any:
4296     case arm_stub_long_branch_v4t_arm_thumb:
4297     case arm_stub_long_branch_thumb_only:
4298     case arm_stub_long_branch_v4t_thumb_thumb:
4299     case arm_stub_long_branch_v4t_thumb_arm:
4300     case arm_stub_short_branch_v4t_thumb_arm:
4301     case arm_stub_long_branch_any_arm_pic:
4302     case arm_stub_long_branch_any_thumb_pic:
4303     case arm_stub_long_branch_v4t_thumb_thumb_pic:
4304     case arm_stub_long_branch_v4t_arm_thumb_pic:
4305     case arm_stub_long_branch_v4t_thumb_arm_pic:
4306     case arm_stub_long_branch_thumb_only_pic:
4307     case arm_stub_long_branch_any_tls_pic:
4308     case arm_stub_long_branch_v4t_thumb_tls_pic:
4309     case arm_stub_a8_veneer_blx:
4310       return 4;
4311
4312     case arm_stub_long_branch_arm_nacl:
4313     case arm_stub_long_branch_arm_nacl_pic:
4314       return 16;
4315
4316     default:
4317       abort ();  /* Should be unreachable.  */
4318     }
4319 }
4320
4321 static bfd_boolean
4322 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4323                     void * in_arg)
4324 {
4325 #define MAXRELOCS 3
4326   struct elf32_arm_stub_hash_entry *stub_entry;
4327   struct elf32_arm_link_hash_table *globals;
4328   struct bfd_link_info *info;
4329   asection *stub_sec;
4330   bfd *stub_bfd;
4331   bfd_byte *loc;
4332   bfd_vma sym_value;
4333   int template_size;
4334   int size;
4335   const insn_sequence *template_sequence;
4336   int i;
4337   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4338   int stub_reloc_offset[MAXRELOCS] = {0, 0};
4339   int nrelocs = 0;
4340
4341   /* Massage our args to the form they really have.  */
4342   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4343   info = (struct bfd_link_info *) in_arg;
4344
4345   globals = elf32_arm_hash_table (info);
4346   if (globals == NULL)
4347     return FALSE;
4348
4349   stub_sec = stub_entry->stub_sec;
4350
4351   if ((globals->fix_cortex_a8 < 0)
4352       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4353     /* We have to do less-strictly-aligned fixes last.  */
4354     return TRUE;
4355
4356   /* Make a note of the offset within the stubs for this entry.  */
4357   stub_entry->stub_offset = stub_sec->size;
4358   loc = stub_sec->contents + stub_entry->stub_offset;
4359
4360   stub_bfd = stub_sec->owner;
4361
4362   /* This is the address of the stub destination.  */
4363   sym_value = (stub_entry->target_value
4364                + stub_entry->target_section->output_offset
4365                + stub_entry->target_section->output_section->vma);
4366
4367   template_sequence = stub_entry->stub_template;
4368   template_size = stub_entry->stub_template_size;
4369
4370   size = 0;
4371   for (i = 0; i < template_size; i++)
4372     {
4373       switch (template_sequence[i].type)
4374         {
4375         case THUMB16_TYPE:
4376           {
4377             bfd_vma data = (bfd_vma) template_sequence[i].data;
4378             if (template_sequence[i].reloc_addend != 0)
4379               {
4380                 /* We've borrowed the reloc_addend field to mean we should
4381                    insert a condition code into this (Thumb-1 branch)
4382                    instruction.  See THUMB16_BCOND_INSN.  */
4383                 BFD_ASSERT ((data & 0xff00) == 0xd000);
4384                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
4385               }
4386             bfd_put_16 (stub_bfd, data, loc + size);
4387             size += 2;
4388           }
4389           break;
4390
4391         case THUMB32_TYPE:
4392           bfd_put_16 (stub_bfd,
4393                       (template_sequence[i].data >> 16) & 0xffff,
4394                       loc + size);
4395           bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
4396                       loc + size + 2);
4397           if (template_sequence[i].r_type != R_ARM_NONE)
4398             {
4399               stub_reloc_idx[nrelocs] = i;
4400               stub_reloc_offset[nrelocs++] = size;
4401             }
4402           size += 4;
4403           break;
4404
4405         case ARM_TYPE:
4406           bfd_put_32 (stub_bfd, template_sequence[i].data,
4407                       loc + size);
4408           /* Handle cases where the target is encoded within the
4409              instruction.  */
4410           if (template_sequence[i].r_type == R_ARM_JUMP24)
4411             {
4412               stub_reloc_idx[nrelocs] = i;
4413               stub_reloc_offset[nrelocs++] = size;
4414             }
4415           size += 4;
4416           break;
4417
4418         case DATA_TYPE:
4419           bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
4420           stub_reloc_idx[nrelocs] = i;
4421           stub_reloc_offset[nrelocs++] = size;
4422           size += 4;
4423           break;
4424
4425         default:
4426           BFD_FAIL ();
4427           return FALSE;
4428         }
4429     }
4430
4431   stub_sec->size += size;
4432
4433   /* Stub size has already been computed in arm_size_one_stub. Check
4434      consistency.  */
4435   BFD_ASSERT (size == stub_entry->stub_size);
4436
4437   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
4438   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
4439     sym_value |= 1;
4440
4441   /* Assume there is at least one and at most MAXRELOCS entries to relocate
4442      in each stub.  */
4443   BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
4444
4445   for (i = 0; i < nrelocs; i++)
4446     if (template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP24
4447         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP19
4448         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_CALL
4449         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_XPC22)
4450       {
4451         Elf_Internal_Rela rel;
4452         bfd_boolean unresolved_reloc;
4453         char *error_message;
4454         enum arm_st_branch_type branch_type
4455           = (template_sequence[stub_reloc_idx[i]].r_type != R_ARM_THM_XPC22
4456              ? ST_BRANCH_TO_THUMB : ST_BRANCH_TO_ARM);
4457         bfd_vma points_to = sym_value + stub_entry->target_addend;
4458
4459         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4460         rel.r_info = ELF32_R_INFO (0,
4461                                    template_sequence[stub_reloc_idx[i]].r_type);
4462         rel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
4463
4464         if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
4465           /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
4466              template should refer back to the instruction after the original
4467              branch.  */
4468           points_to = sym_value;
4469
4470         /* There may be unintended consequences if this is not true.  */
4471         BFD_ASSERT (stub_entry->h == NULL);
4472
4473         /* Note: _bfd_final_link_relocate doesn't handle these relocations
4474            properly.  We should probably use this function unconditionally,
4475            rather than only for certain relocations listed in the enclosing
4476            conditional, for the sake of consistency.  */
4477         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4478             (template_sequence[stub_reloc_idx[i]].r_type),
4479           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4480           points_to, info, stub_entry->target_section, "", STT_FUNC,
4481           branch_type, (struct elf_link_hash_entry *) stub_entry->h,
4482           &unresolved_reloc, &error_message);
4483       }
4484     else
4485       {
4486         Elf_Internal_Rela rel;
4487         bfd_boolean unresolved_reloc;
4488         char *error_message;
4489         bfd_vma points_to = sym_value + stub_entry->target_addend
4490           + template_sequence[stub_reloc_idx[i]].reloc_addend;
4491
4492         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4493         rel.r_info = ELF32_R_INFO (0,
4494                                    template_sequence[stub_reloc_idx[i]].r_type);
4495         rel.r_addend = 0;
4496
4497         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4498             (template_sequence[stub_reloc_idx[i]].r_type),
4499           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4500           points_to, info, stub_entry->target_section, "", STT_FUNC,
4501           stub_entry->branch_type,
4502           (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
4503           &error_message);
4504       }
4505
4506   return TRUE;
4507 #undef MAXRELOCS
4508 }
4509
4510 /* Calculate the template, template size and instruction size for a stub.
4511    Return value is the instruction size.  */
4512
4513 static unsigned int
4514 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
4515                              const insn_sequence **stub_template,
4516                              int *stub_template_size)
4517 {
4518   const insn_sequence *template_sequence = NULL;
4519   int template_size = 0, i;
4520   unsigned int size;
4521
4522   template_sequence = stub_definitions[stub_type].template_sequence;
4523   if (stub_template)
4524     *stub_template = template_sequence;
4525
4526   template_size = stub_definitions[stub_type].template_size;
4527   if (stub_template_size)
4528     *stub_template_size = template_size;
4529
4530   size = 0;
4531   for (i = 0; i < template_size; i++)
4532     {
4533       switch (template_sequence[i].type)
4534         {
4535         case THUMB16_TYPE:
4536           size += 2;
4537           break;
4538
4539         case ARM_TYPE:
4540         case THUMB32_TYPE:
4541         case DATA_TYPE:
4542           size += 4;
4543           break;
4544
4545         default:
4546           BFD_FAIL ();
4547           return 0;
4548         }
4549     }
4550
4551   return size;
4552 }
4553
4554 /* As above, but don't actually build the stub.  Just bump offset so
4555    we know stub section sizes.  */
4556
4557 static bfd_boolean
4558 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
4559                    void *in_arg ATTRIBUTE_UNUSED)
4560 {
4561   struct elf32_arm_stub_hash_entry *stub_entry;
4562   const insn_sequence *template_sequence;
4563   int template_size, size;
4564
4565   /* Massage our args to the form they really have.  */
4566   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4567
4568   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
4569              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
4570
4571   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
4572                                       &template_size);
4573
4574   stub_entry->stub_size = size;
4575   stub_entry->stub_template = template_sequence;
4576   stub_entry->stub_template_size = template_size;
4577
4578   size = (size + 7) & ~7;
4579   stub_entry->stub_sec->size += size;
4580
4581   return TRUE;
4582 }
4583
4584 /* External entry points for sizing and building linker stubs.  */
4585
4586 /* Set up various things so that we can make a list of input sections
4587    for each output section included in the link.  Returns -1 on error,
4588    0 when no stubs will be needed, and 1 on success.  */
4589
4590 int
4591 elf32_arm_setup_section_lists (bfd *output_bfd,
4592                                struct bfd_link_info *info)
4593 {
4594   bfd *input_bfd;
4595   unsigned int bfd_count;
4596   unsigned int top_id, top_index;
4597   asection *section;
4598   asection **input_list, **list;
4599   bfd_size_type amt;
4600   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4601
4602   if (htab == NULL)
4603     return 0;
4604   if (! is_elf_hash_table (htab))
4605     return 0;
4606
4607   /* Count the number of input BFDs and find the top input section id.  */
4608   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
4609        input_bfd != NULL;
4610        input_bfd = input_bfd->link.next)
4611     {
4612       bfd_count += 1;
4613       for (section = input_bfd->sections;
4614            section != NULL;
4615            section = section->next)
4616         {
4617           if (top_id < section->id)
4618             top_id = section->id;
4619         }
4620     }
4621   htab->bfd_count = bfd_count;
4622
4623   amt = sizeof (struct map_stub) * (top_id + 1);
4624   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4625   if (htab->stub_group == NULL)
4626     return -1;
4627   htab->top_id = top_id;
4628
4629   /* We can't use output_bfd->section_count here to find the top output
4630      section index as some sections may have been removed, and
4631      _bfd_strip_section_from_output doesn't renumber the indices.  */
4632   for (section = output_bfd->sections, top_index = 0;
4633        section != NULL;
4634        section = section->next)
4635     {
4636       if (top_index < section->index)
4637         top_index = section->index;
4638     }
4639
4640   htab->top_index = top_index;
4641   amt = sizeof (asection *) * (top_index + 1);
4642   input_list = (asection **) bfd_malloc (amt);
4643   htab->input_list = input_list;
4644   if (input_list == NULL)
4645     return -1;
4646
4647   /* For sections we aren't interested in, mark their entries with a
4648      value we can check later.  */
4649   list = input_list + top_index;
4650   do
4651     *list = bfd_abs_section_ptr;
4652   while (list-- != input_list);
4653
4654   for (section = output_bfd->sections;
4655        section != NULL;
4656        section = section->next)
4657     {
4658       if ((section->flags & SEC_CODE) != 0)
4659         input_list[section->index] = NULL;
4660     }
4661
4662   return 1;
4663 }
4664
4665 /* The linker repeatedly calls this function for each input section,
4666    in the order that input sections are linked into output sections.
4667    Build lists of input sections to determine groupings between which
4668    we may insert linker stubs.  */
4669
4670 void
4671 elf32_arm_next_input_section (struct bfd_link_info *info,
4672                               asection *isec)
4673 {
4674   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4675
4676   if (htab == NULL)
4677     return;
4678
4679   if (isec->output_section->index <= htab->top_index)
4680     {
4681       asection **list = htab->input_list + isec->output_section->index;
4682
4683       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
4684         {
4685           /* Steal the link_sec pointer for our list.  */
4686 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4687           /* This happens to make the list in reverse order,
4688              which we reverse later.  */
4689           PREV_SEC (isec) = *list;
4690           *list = isec;
4691         }
4692     }
4693 }
4694
4695 /* See whether we can group stub sections together.  Grouping stub
4696    sections may result in fewer stubs.  More importantly, we need to
4697    put all .init* and .fini* stubs at the end of the .init or
4698    .fini output sections respectively, because glibc splits the
4699    _init and _fini functions into multiple parts.  Putting a stub in
4700    the middle of a function is not a good idea.  */
4701
4702 static void
4703 group_sections (struct elf32_arm_link_hash_table *htab,
4704                 bfd_size_type stub_group_size,
4705                 bfd_boolean stubs_always_after_branch)
4706 {
4707   asection **list = htab->input_list;
4708
4709   do
4710     {
4711       asection *tail = *list;
4712       asection *head;
4713
4714       if (tail == bfd_abs_section_ptr)
4715         continue;
4716
4717       /* Reverse the list: we must avoid placing stubs at the
4718          beginning of the section because the beginning of the text
4719          section may be required for an interrupt vector in bare metal
4720          code.  */
4721 #define NEXT_SEC PREV_SEC
4722       head = NULL;
4723       while (tail != NULL)
4724         {
4725           /* Pop from tail.  */
4726           asection *item = tail;
4727           tail = PREV_SEC (item);
4728
4729           /* Push on head.  */
4730           NEXT_SEC (item) = head;
4731           head = item;
4732         }
4733
4734       while (head != NULL)
4735         {
4736           asection *curr;
4737           asection *next;
4738           bfd_vma stub_group_start = head->output_offset;
4739           bfd_vma end_of_next;
4740
4741           curr = head;
4742           while (NEXT_SEC (curr) != NULL)
4743             {
4744               next = NEXT_SEC (curr);
4745               end_of_next = next->output_offset + next->size;
4746               if (end_of_next - stub_group_start >= stub_group_size)
4747                 /* End of NEXT is too far from start, so stop.  */
4748                 break;
4749               /* Add NEXT to the group.  */
4750               curr = next;
4751             }
4752
4753           /* OK, the size from the start to the start of CURR is less
4754              than stub_group_size and thus can be handled by one stub
4755              section.  (Or the head section is itself larger than
4756              stub_group_size, in which case we may be toast.)
4757              We should really be keeping track of the total size of
4758              stubs added here, as stubs contribute to the final output
4759              section size.  */
4760           do
4761             {
4762               next = NEXT_SEC (head);
4763               /* Set up this stub group.  */
4764               htab->stub_group[head->id].link_sec = curr;
4765             }
4766           while (head != curr && (head = next) != NULL);
4767
4768           /* But wait, there's more!  Input sections up to stub_group_size
4769              bytes after the stub section can be handled by it too.  */
4770           if (!stubs_always_after_branch)
4771             {
4772               stub_group_start = curr->output_offset + curr->size;
4773
4774               while (next != NULL)
4775                 {
4776                   end_of_next = next->output_offset + next->size;
4777                   if (end_of_next - stub_group_start >= stub_group_size)
4778                     /* End of NEXT is too far from stubs, so stop.  */
4779                     break;
4780                   /* Add NEXT to the stub group.  */
4781                   head = next;
4782                   next = NEXT_SEC (head);
4783                   htab->stub_group[head->id].link_sec = curr;
4784                 }
4785             }
4786           head = next;
4787         }
4788     }
4789   while (list++ != htab->input_list + htab->top_index);
4790
4791   free (htab->input_list);
4792 #undef PREV_SEC
4793 #undef NEXT_SEC
4794 }
4795
4796 /* Comparison function for sorting/searching relocations relating to Cortex-A8
4797    erratum fix.  */
4798
4799 static int
4800 a8_reloc_compare (const void *a, const void *b)
4801 {
4802   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
4803   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
4804
4805   if (ra->from < rb->from)
4806     return -1;
4807   else if (ra->from > rb->from)
4808     return 1;
4809   else
4810     return 0;
4811 }
4812
4813 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
4814                                                     const char *, char **);
4815
4816 /* Helper function to scan code for sequences which might trigger the Cortex-A8
4817    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
4818    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
4819    otherwise.  */
4820
4821 static bfd_boolean
4822 cortex_a8_erratum_scan (bfd *input_bfd,
4823                         struct bfd_link_info *info,
4824                         struct a8_erratum_fix **a8_fixes_p,
4825                         unsigned int *num_a8_fixes_p,
4826                         unsigned int *a8_fix_table_size_p,
4827                         struct a8_erratum_reloc *a8_relocs,
4828                         unsigned int num_a8_relocs,
4829                         unsigned prev_num_a8_fixes,
4830                         bfd_boolean *stub_changed_p)
4831 {
4832   asection *section;
4833   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4834   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
4835   unsigned int num_a8_fixes = *num_a8_fixes_p;
4836   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
4837
4838   if (htab == NULL)
4839     return FALSE;
4840
4841   for (section = input_bfd->sections;
4842        section != NULL;
4843        section = section->next)
4844     {
4845       bfd_byte *contents = NULL;
4846       struct _arm_elf_section_data *sec_data;
4847       unsigned int span;
4848       bfd_vma base_vma;
4849
4850       if (elf_section_type (section) != SHT_PROGBITS
4851           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4852           || (section->flags & SEC_EXCLUDE) != 0
4853           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4854           || (section->output_section == bfd_abs_section_ptr))
4855         continue;
4856
4857       base_vma = section->output_section->vma + section->output_offset;
4858
4859       if (elf_section_data (section)->this_hdr.contents != NULL)
4860         contents = elf_section_data (section)->this_hdr.contents;
4861       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4862         return TRUE;
4863
4864       sec_data = elf32_arm_section_data (section);
4865
4866       for (span = 0; span < sec_data->mapcount; span++)
4867         {
4868           unsigned int span_start = sec_data->map[span].vma;
4869           unsigned int span_end = (span == sec_data->mapcount - 1)
4870             ? section->size : sec_data->map[span + 1].vma;
4871           unsigned int i;
4872           char span_type = sec_data->map[span].type;
4873           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
4874
4875           if (span_type != 't')
4876             continue;
4877
4878           /* Span is entirely within a single 4KB region: skip scanning.  */
4879           if (((base_vma + span_start) & ~0xfff)
4880               == ((base_vma + span_end) & ~0xfff))
4881             continue;
4882
4883           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
4884
4885                * The opcode is BLX.W, BL.W, B.W, Bcc.W
4886                * The branch target is in the same 4KB region as the
4887                  first half of the branch.
4888                * The instruction before the branch is a 32-bit
4889                  length non-branch instruction.  */
4890           for (i = span_start; i < span_end;)
4891             {
4892               unsigned int insn = bfd_getl16 (&contents[i]);
4893               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
4894               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
4895
4896               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
4897                 insn_32bit = TRUE;
4898
4899               if (insn_32bit)
4900                 {
4901                   /* Load the rest of the insn (in manual-friendly order).  */
4902                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
4903
4904                   /* Encoding T4: B<c>.W.  */
4905                   is_b = (insn & 0xf800d000) == 0xf0009000;
4906                   /* Encoding T1: BL<c>.W.  */
4907                   is_bl = (insn & 0xf800d000) == 0xf000d000;
4908                   /* Encoding T2: BLX<c>.W.  */
4909                   is_blx = (insn & 0xf800d000) == 0xf000c000;
4910                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
4911                   is_bcc = (insn & 0xf800d000) == 0xf0008000
4912                            && (insn & 0x07f00000) != 0x03800000;
4913                 }
4914
4915               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
4916
4917               if (((base_vma + i) & 0xfff) == 0xffe
4918                   && insn_32bit
4919                   && is_32bit_branch
4920                   && last_was_32bit
4921                   && ! last_was_branch)
4922                 {
4923                   bfd_signed_vma offset = 0;
4924                   bfd_boolean force_target_arm = FALSE;
4925                   bfd_boolean force_target_thumb = FALSE;
4926                   bfd_vma target;
4927                   enum elf32_arm_stub_type stub_type = arm_stub_none;
4928                   struct a8_erratum_reloc key, *found;
4929                   bfd_boolean use_plt = FALSE;
4930
4931                   key.from = base_vma + i;
4932                   found = (struct a8_erratum_reloc *)
4933                       bsearch (&key, a8_relocs, num_a8_relocs,
4934                                sizeof (struct a8_erratum_reloc),
4935                                &a8_reloc_compare);
4936
4937                   if (found)
4938                     {
4939                       char *error_message = NULL;
4940                       struct elf_link_hash_entry *entry;
4941
4942                       /* We don't care about the error returned from this
4943                          function, only if there is glue or not.  */
4944                       entry = find_thumb_glue (info, found->sym_name,
4945                                                &error_message);
4946
4947                       if (entry)
4948                         found->non_a8_stub = TRUE;
4949
4950                       /* Keep a simpler condition, for the sake of clarity.  */
4951                       if (htab->root.splt != NULL && found->hash != NULL
4952                           && found->hash->root.plt.offset != (bfd_vma) -1)
4953                         use_plt = TRUE;
4954
4955                       if (found->r_type == R_ARM_THM_CALL)
4956                         {
4957                           if (found->branch_type == ST_BRANCH_TO_ARM
4958                               || use_plt)
4959                             force_target_arm = TRUE;
4960                           else
4961                             force_target_thumb = TRUE;
4962                         }
4963                     }
4964
4965                   /* Check if we have an offending branch instruction.  */
4966
4967                   if (found && found->non_a8_stub)
4968                     /* We've already made a stub for this instruction, e.g.
4969                        it's a long branch or a Thumb->ARM stub.  Assume that
4970                        stub will suffice to work around the A8 erratum (see
4971                        setting of always_after_branch above).  */
4972                     ;
4973                   else if (is_bcc)
4974                     {
4975                       offset = (insn & 0x7ff) << 1;
4976                       offset |= (insn & 0x3f0000) >> 4;
4977                       offset |= (insn & 0x2000) ? 0x40000 : 0;
4978                       offset |= (insn & 0x800) ? 0x80000 : 0;
4979                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
4980                       if (offset & 0x100000)
4981                         offset |= ~ ((bfd_signed_vma) 0xfffff);
4982                       stub_type = arm_stub_a8_veneer_b_cond;
4983                     }
4984                   else if (is_b || is_bl || is_blx)
4985                     {
4986                       int s = (insn & 0x4000000) != 0;
4987                       int j1 = (insn & 0x2000) != 0;
4988                       int j2 = (insn & 0x800) != 0;
4989                       int i1 = !(j1 ^ s);
4990                       int i2 = !(j2 ^ s);
4991
4992                       offset = (insn & 0x7ff) << 1;
4993                       offset |= (insn & 0x3ff0000) >> 4;
4994                       offset |= i2 << 22;
4995                       offset |= i1 << 23;
4996                       offset |= s << 24;
4997                       if (offset & 0x1000000)
4998                         offset |= ~ ((bfd_signed_vma) 0xffffff);
4999
5000                       if (is_blx)
5001                         offset &= ~ ((bfd_signed_vma) 3);
5002
5003                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
5004                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5005                     }
5006
5007                   if (stub_type != arm_stub_none)
5008                     {
5009                       bfd_vma pc_for_insn = base_vma + i + 4;
5010
5011                       /* The original instruction is a BL, but the target is
5012                          an ARM instruction.  If we were not making a stub,
5013                          the BL would have been converted to a BLX.  Use the
5014                          BLX stub instead in that case.  */
5015                       if (htab->use_blx && force_target_arm
5016                           && stub_type == arm_stub_a8_veneer_bl)
5017                         {
5018                           stub_type = arm_stub_a8_veneer_blx;
5019                           is_blx = TRUE;
5020                           is_bl = FALSE;
5021                         }
5022                       /* Conversely, if the original instruction was
5023                          BLX but the target is Thumb mode, use the BL
5024                          stub.  */
5025                       else if (force_target_thumb
5026                                && stub_type == arm_stub_a8_veneer_blx)
5027                         {
5028                           stub_type = arm_stub_a8_veneer_bl;
5029                           is_blx = FALSE;
5030                           is_bl = TRUE;
5031                         }
5032
5033                       if (is_blx)
5034                         pc_for_insn &= ~ ((bfd_vma) 3);
5035
5036                       /* If we found a relocation, use the proper destination,
5037                          not the offset in the (unrelocated) instruction.
5038                          Note this is always done if we switched the stub type
5039                          above.  */
5040                       if (found)
5041                         offset =
5042                           (bfd_signed_vma) (found->destination - pc_for_insn);
5043
5044                       /* If the stub will use a Thumb-mode branch to a
5045                          PLT target, redirect it to the preceding Thumb
5046                          entry point.  */
5047                       if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5048                         offset -= PLT_THUMB_STUB_SIZE;
5049
5050                       target = pc_for_insn + offset;
5051
5052                       /* The BLX stub is ARM-mode code.  Adjust the offset to
5053                          take the different PC value (+8 instead of +4) into
5054                          account.  */
5055                       if (stub_type == arm_stub_a8_veneer_blx)
5056                         offset += 4;
5057
5058                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5059                         {
5060                           char *stub_name = NULL;
5061
5062                           if (num_a8_fixes == a8_fix_table_size)
5063                             {
5064                               a8_fix_table_size *= 2;
5065                               a8_fixes = (struct a8_erratum_fix *)
5066                                   bfd_realloc (a8_fixes,
5067                                                sizeof (struct a8_erratum_fix)
5068                                                * a8_fix_table_size);
5069                             }
5070
5071                           if (num_a8_fixes < prev_num_a8_fixes)
5072                             {
5073                               /* If we're doing a subsequent scan,
5074                                  check if we've found the same fix as
5075                                  before, and try and reuse the stub
5076                                  name.  */
5077                               stub_name = a8_fixes[num_a8_fixes].stub_name;
5078                               if ((a8_fixes[num_a8_fixes].section != section)
5079                                   || (a8_fixes[num_a8_fixes].offset != i))
5080                                 {
5081                                   free (stub_name);
5082                                   stub_name = NULL;
5083                                   *stub_changed_p = TRUE;
5084                                 }
5085                             }
5086
5087                           if (!stub_name)
5088                             {
5089                               stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5090                               if (stub_name != NULL)
5091                                 sprintf (stub_name, "%x:%x", section->id, i);
5092                             }
5093
5094                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5095                           a8_fixes[num_a8_fixes].section = section;
5096                           a8_fixes[num_a8_fixes].offset = i;
5097                           a8_fixes[num_a8_fixes].addend = offset;
5098                           a8_fixes[num_a8_fixes].orig_insn = insn;
5099                           a8_fixes[num_a8_fixes].stub_name = stub_name;
5100                           a8_fixes[num_a8_fixes].stub_type = stub_type;
5101                           a8_fixes[num_a8_fixes].branch_type =
5102                             is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5103
5104                           num_a8_fixes++;
5105                         }
5106                     }
5107                 }
5108
5109               i += insn_32bit ? 4 : 2;
5110               last_was_32bit = insn_32bit;
5111               last_was_branch = is_32bit_branch;
5112             }
5113         }
5114
5115       if (elf_section_data (section)->this_hdr.contents == NULL)
5116         free (contents);
5117     }
5118
5119   *a8_fixes_p = a8_fixes;
5120   *num_a8_fixes_p = num_a8_fixes;
5121   *a8_fix_table_size_p = a8_fix_table_size;
5122
5123   return FALSE;
5124 }
5125
5126 /* Determine and set the size of the stub section for a final link.
5127
5128    The basic idea here is to examine all the relocations looking for
5129    PC-relative calls to a target that is unreachable with a "bl"
5130    instruction.  */
5131
5132 bfd_boolean
5133 elf32_arm_size_stubs (bfd *output_bfd,
5134                       bfd *stub_bfd,
5135                       struct bfd_link_info *info,
5136                       bfd_signed_vma group_size,
5137                       asection * (*add_stub_section) (const char *, asection *,
5138                                                       unsigned int),
5139                       void (*layout_sections_again) (void))
5140 {
5141   bfd_size_type stub_group_size;
5142   bfd_boolean stubs_always_after_branch;
5143   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5144   struct a8_erratum_fix *a8_fixes = NULL;
5145   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
5146   struct a8_erratum_reloc *a8_relocs = NULL;
5147   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
5148
5149   if (htab == NULL)
5150     return FALSE;
5151
5152   if (htab->fix_cortex_a8)
5153     {
5154       a8_fixes = (struct a8_erratum_fix *)
5155           bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
5156       a8_relocs = (struct a8_erratum_reloc *)
5157           bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
5158     }
5159
5160   /* Propagate mach to stub bfd, because it may not have been
5161      finalized when we created stub_bfd.  */
5162   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
5163                      bfd_get_mach (output_bfd));
5164
5165   /* Stash our params away.  */
5166   htab->stub_bfd = stub_bfd;
5167   htab->add_stub_section = add_stub_section;
5168   htab->layout_sections_again = layout_sections_again;
5169   stubs_always_after_branch = group_size < 0;
5170
5171   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
5172      as the first half of a 32-bit branch straddling two 4K pages.  This is a
5173      crude way of enforcing that.  */
5174   if (htab->fix_cortex_a8)
5175     stubs_always_after_branch = 1;
5176
5177   if (group_size < 0)
5178     stub_group_size = -group_size;
5179   else
5180     stub_group_size = group_size;
5181
5182   if (stub_group_size == 1)
5183     {
5184       /* Default values.  */
5185       /* Thumb branch range is +-4MB has to be used as the default
5186          maximum size (a given section can contain both ARM and Thumb
5187          code, so the worst case has to be taken into account).
5188
5189          This value is 24K less than that, which allows for 2025
5190          12-byte stubs.  If we exceed that, then we will fail to link.
5191          The user will have to relink with an explicit group size
5192          option.  */
5193       stub_group_size = 4170000;
5194     }
5195
5196   group_sections (htab, stub_group_size, stubs_always_after_branch);
5197
5198   /* If we're applying the cortex A8 fix, we need to determine the
5199      program header size now, because we cannot change it later --
5200      that could alter section placements.  Notice the A8 erratum fix
5201      ends up requiring the section addresses to remain unchanged
5202      modulo the page size.  That's something we cannot represent
5203      inside BFD, and we don't want to force the section alignment to
5204      be the page size.  */
5205   if (htab->fix_cortex_a8)
5206     (*htab->layout_sections_again) ();
5207
5208   while (1)
5209     {
5210       bfd *input_bfd;
5211       unsigned int bfd_indx;
5212       asection *stub_sec;
5213       bfd_boolean stub_changed = FALSE;
5214       unsigned prev_num_a8_fixes = num_a8_fixes;
5215
5216       num_a8_fixes = 0;
5217       for (input_bfd = info->input_bfds, bfd_indx = 0;
5218            input_bfd != NULL;
5219            input_bfd = input_bfd->link.next, bfd_indx++)
5220         {
5221           Elf_Internal_Shdr *symtab_hdr;
5222           asection *section;
5223           Elf_Internal_Sym *local_syms = NULL;
5224
5225           if (!is_arm_elf (input_bfd))
5226             continue;
5227
5228           num_a8_relocs = 0;
5229
5230           /* We'll need the symbol table in a second.  */
5231           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5232           if (symtab_hdr->sh_info == 0)
5233             continue;
5234
5235           /* Walk over each section attached to the input bfd.  */
5236           for (section = input_bfd->sections;
5237                section != NULL;
5238                section = section->next)
5239             {
5240               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5241
5242               /* If there aren't any relocs, then there's nothing more
5243                  to do.  */
5244               if ((section->flags & SEC_RELOC) == 0
5245                   || section->reloc_count == 0
5246                   || (section->flags & SEC_CODE) == 0)
5247                 continue;
5248
5249               /* If this section is a link-once section that will be
5250                  discarded, then don't create any stubs.  */
5251               if (section->output_section == NULL
5252                   || section->output_section->owner != output_bfd)
5253                 continue;
5254
5255               /* Get the relocs.  */
5256               internal_relocs
5257                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
5258                                              NULL, info->keep_memory);
5259               if (internal_relocs == NULL)
5260                 goto error_ret_free_local;
5261
5262               /* Now examine each relocation.  */
5263               irela = internal_relocs;
5264               irelaend = irela + section->reloc_count;
5265               for (; irela < irelaend; irela++)
5266                 {
5267                   unsigned int r_type, r_indx;
5268                   enum elf32_arm_stub_type stub_type;
5269                   struct elf32_arm_stub_hash_entry *stub_entry;
5270                   asection *sym_sec;
5271                   bfd_vma sym_value;
5272                   bfd_vma destination;
5273                   struct elf32_arm_link_hash_entry *hash;
5274                   const char *sym_name;
5275                   char *stub_name;
5276                   const asection *id_sec;
5277                   unsigned char st_type;
5278                   enum arm_st_branch_type branch_type;
5279                   bfd_boolean created_stub = FALSE;
5280
5281                   r_type = ELF32_R_TYPE (irela->r_info);
5282                   r_indx = ELF32_R_SYM (irela->r_info);
5283
5284                   if (r_type >= (unsigned int) R_ARM_max)
5285                     {
5286                       bfd_set_error (bfd_error_bad_value);
5287                     error_ret_free_internal:
5288                       if (elf_section_data (section)->relocs == NULL)
5289                         free (internal_relocs);
5290                       goto error_ret_free_local;
5291                     }
5292
5293                   hash = NULL;
5294                   if (r_indx >= symtab_hdr->sh_info)
5295                     hash = elf32_arm_hash_entry
5296                       (elf_sym_hashes (input_bfd)
5297                        [r_indx - symtab_hdr->sh_info]);
5298
5299                   /* Only look for stubs on branch instructions, or
5300                      non-relaxed TLSCALL  */
5301                   if ((r_type != (unsigned int) R_ARM_CALL)
5302                       && (r_type != (unsigned int) R_ARM_THM_CALL)
5303                       && (r_type != (unsigned int) R_ARM_JUMP24)
5304                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
5305                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
5306                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
5307                       && (r_type != (unsigned int) R_ARM_PLT32)
5308                       && !((r_type == (unsigned int) R_ARM_TLS_CALL
5309                             || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5310                            && r_type == elf32_arm_tls_transition
5311                                (info, r_type, &hash->root)
5312                            && ((hash ? hash->tls_type
5313                                 : (elf32_arm_local_got_tls_type
5314                                    (input_bfd)[r_indx]))
5315                                & GOT_TLS_GDESC) != 0))
5316                     continue;
5317
5318                   /* Now determine the call target, its name, value,
5319                      section.  */
5320                   sym_sec = NULL;
5321                   sym_value = 0;
5322                   destination = 0;
5323                   sym_name = NULL;
5324
5325                   if (r_type == (unsigned int) R_ARM_TLS_CALL
5326                       || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5327                     {
5328                       /* A non-relaxed TLS call.  The target is the
5329                          plt-resident trampoline and nothing to do
5330                          with the symbol.  */
5331                       BFD_ASSERT (htab->tls_trampoline > 0);
5332                       sym_sec = htab->root.splt;
5333                       sym_value = htab->tls_trampoline;
5334                       hash = 0;
5335                       st_type = STT_FUNC;
5336                       branch_type = ST_BRANCH_TO_ARM;
5337                     }
5338                   else if (!hash)
5339                     {
5340                       /* It's a local symbol.  */
5341                       Elf_Internal_Sym *sym;
5342
5343                       if (local_syms == NULL)
5344                         {
5345                           local_syms
5346                             = (Elf_Internal_Sym *) symtab_hdr->contents;
5347                           if (local_syms == NULL)
5348                             local_syms
5349                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5350                                                       symtab_hdr->sh_info, 0,
5351                                                       NULL, NULL, NULL);
5352                           if (local_syms == NULL)
5353                             goto error_ret_free_internal;
5354                         }
5355
5356                       sym = local_syms + r_indx;
5357                       if (sym->st_shndx == SHN_UNDEF)
5358                         sym_sec = bfd_und_section_ptr;
5359                       else if (sym->st_shndx == SHN_ABS)
5360                         sym_sec = bfd_abs_section_ptr;
5361                       else if (sym->st_shndx == SHN_COMMON)
5362                         sym_sec = bfd_com_section_ptr;
5363                       else
5364                         sym_sec =
5365                           bfd_section_from_elf_index (input_bfd, sym->st_shndx);
5366
5367                       if (!sym_sec)
5368                         /* This is an undefined symbol.  It can never
5369                            be resolved.  */
5370                         continue;
5371
5372                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5373                         sym_value = sym->st_value;
5374                       destination = (sym_value + irela->r_addend
5375                                      + sym_sec->output_offset
5376                                      + sym_sec->output_section->vma);
5377                       st_type = ELF_ST_TYPE (sym->st_info);
5378                       branch_type = ARM_SYM_BRANCH_TYPE (sym);
5379                       sym_name
5380                         = bfd_elf_string_from_elf_section (input_bfd,
5381                                                            symtab_hdr->sh_link,
5382                                                            sym->st_name);
5383                     }
5384                   else
5385                     {
5386                       /* It's an external symbol.  */
5387                       while (hash->root.root.type == bfd_link_hash_indirect
5388                              || hash->root.root.type == bfd_link_hash_warning)
5389                         hash = ((struct elf32_arm_link_hash_entry *)
5390                                 hash->root.root.u.i.link);
5391
5392                       if (hash->root.root.type == bfd_link_hash_defined
5393                           || hash->root.root.type == bfd_link_hash_defweak)
5394                         {
5395                           sym_sec = hash->root.root.u.def.section;
5396                           sym_value = hash->root.root.u.def.value;
5397
5398                           struct elf32_arm_link_hash_table *globals =
5399                                                   elf32_arm_hash_table (info);
5400
5401                           /* For a destination in a shared library,
5402                              use the PLT stub as target address to
5403                              decide whether a branch stub is
5404                              needed.  */
5405                           if (globals != NULL
5406                               && globals->root.splt != NULL
5407                               && hash != NULL
5408                               && hash->root.plt.offset != (bfd_vma) -1)
5409                             {
5410                               sym_sec = globals->root.splt;
5411                               sym_value = hash->root.plt.offset;
5412                               if (sym_sec->output_section != NULL)
5413                                 destination = (sym_value
5414                                                + sym_sec->output_offset
5415                                                + sym_sec->output_section->vma);
5416                             }
5417                           else if (sym_sec->output_section != NULL)
5418                             destination = (sym_value + irela->r_addend
5419                                            + sym_sec->output_offset
5420                                            + sym_sec->output_section->vma);
5421                         }
5422                       else if ((hash->root.root.type == bfd_link_hash_undefined)
5423                                || (hash->root.root.type == bfd_link_hash_undefweak))
5424                         {
5425                           /* For a shared library, use the PLT stub as
5426                              target address to decide whether a long
5427                              branch stub is needed.
5428                              For absolute code, they cannot be handled.  */
5429                           struct elf32_arm_link_hash_table *globals =
5430                             elf32_arm_hash_table (info);
5431
5432                           if (globals != NULL
5433                               && globals->root.splt != NULL
5434                               && hash != NULL
5435                               && hash->root.plt.offset != (bfd_vma) -1)
5436                             {
5437                               sym_sec = globals->root.splt;
5438                               sym_value = hash->root.plt.offset;
5439                               if (sym_sec->output_section != NULL)
5440                                 destination = (sym_value
5441                                                + sym_sec->output_offset
5442                                                + sym_sec->output_section->vma);
5443                             }
5444                           else
5445                             continue;
5446                         }
5447                       else
5448                         {
5449                           bfd_set_error (bfd_error_bad_value);
5450                           goto error_ret_free_internal;
5451                         }
5452                       st_type = hash->root.type;
5453                       branch_type = hash->root.target_internal;
5454                       sym_name = hash->root.root.root.string;
5455                     }
5456
5457                   do
5458                     {
5459                       /* Determine what (if any) linker stub is needed.  */
5460                       stub_type = arm_type_of_stub (info, section, irela,
5461                                                     st_type, &branch_type,
5462                                                     hash, destination, sym_sec,
5463                                                     input_bfd, sym_name);
5464                       if (stub_type == arm_stub_none)
5465                         break;
5466
5467                       /* Support for grouping stub sections.  */
5468                       id_sec = htab->stub_group[section->id].link_sec;
5469
5470                       /* Get the name of this stub.  */
5471                       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash,
5472                                                        irela, stub_type);
5473                       if (!stub_name)
5474                         goto error_ret_free_internal;
5475
5476                       /* We've either created a stub for this reloc already,
5477                          or we are about to.  */
5478                       created_stub = TRUE;
5479
5480                       stub_entry = arm_stub_hash_lookup
5481                                      (&htab->stub_hash_table, stub_name,
5482                                       FALSE, FALSE);
5483                       if (stub_entry != NULL)
5484                         {
5485                           /* The proper stub has already been created.  */
5486                           free (stub_name);
5487                           stub_entry->target_value = sym_value;
5488                           break;
5489                         }
5490
5491                       stub_entry = elf32_arm_add_stub (stub_name, section,
5492                                                        htab);
5493                       if (stub_entry == NULL)
5494                         {
5495                           free (stub_name);
5496                           goto error_ret_free_internal;
5497                         }
5498
5499                       stub_entry->target_value = sym_value;
5500                       stub_entry->target_section = sym_sec;
5501                       stub_entry->stub_type = stub_type;
5502                       stub_entry->h = hash;
5503                       stub_entry->branch_type = branch_type;
5504
5505                       if (sym_name == NULL)
5506                         sym_name = "unnamed";
5507                       stub_entry->output_name = (char *)
5508                           bfd_alloc (htab->stub_bfd,
5509                                      sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5510                                      + strlen (sym_name));
5511                       if (stub_entry->output_name == NULL)
5512                         {
5513                           free (stub_name);
5514                           goto error_ret_free_internal;
5515                         }
5516
5517                       /* For historical reasons, use the existing names for
5518                          ARM-to-Thumb and Thumb-to-ARM stubs.  */
5519                       if ((r_type == (unsigned int) R_ARM_THM_CALL
5520                            || r_type == (unsigned int) R_ARM_THM_JUMP24
5521                            || r_type == (unsigned int) R_ARM_THM_JUMP19)
5522                           && branch_type == ST_BRANCH_TO_ARM)
5523                         sprintf (stub_entry->output_name,
5524                                  THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5525                       else if ((r_type == (unsigned int) R_ARM_CALL
5526                                || r_type == (unsigned int) R_ARM_JUMP24)
5527                                && branch_type == ST_BRANCH_TO_THUMB)
5528                         sprintf (stub_entry->output_name,
5529                                  ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5530                       else
5531                         sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
5532                                  sym_name);
5533
5534                       stub_changed = TRUE;
5535                     }
5536                   while (0);
5537
5538                   /* Look for relocations which might trigger Cortex-A8
5539                      erratum.  */
5540                   if (htab->fix_cortex_a8
5541                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
5542                           || r_type == (unsigned int) R_ARM_THM_JUMP19
5543                           || r_type == (unsigned int) R_ARM_THM_CALL
5544                           || r_type == (unsigned int) R_ARM_THM_XPC22))
5545                     {
5546                       bfd_vma from = section->output_section->vma
5547                                      + section->output_offset
5548                                      + irela->r_offset;
5549
5550                       if ((from & 0xfff) == 0xffe)
5551                         {
5552                           /* Found a candidate.  Note we haven't checked the
5553                              destination is within 4K here: if we do so (and
5554                              don't create an entry in a8_relocs) we can't tell
5555                              that a branch should have been relocated when
5556                              scanning later.  */
5557                           if (num_a8_relocs == a8_reloc_table_size)
5558                             {
5559                               a8_reloc_table_size *= 2;
5560                               a8_relocs = (struct a8_erratum_reloc *)
5561                                   bfd_realloc (a8_relocs,
5562                                                sizeof (struct a8_erratum_reloc)
5563                                                * a8_reloc_table_size);
5564                             }
5565
5566                           a8_relocs[num_a8_relocs].from = from;
5567                           a8_relocs[num_a8_relocs].destination = destination;
5568                           a8_relocs[num_a8_relocs].r_type = r_type;
5569                           a8_relocs[num_a8_relocs].branch_type = branch_type;
5570                           a8_relocs[num_a8_relocs].sym_name = sym_name;
5571                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
5572                           a8_relocs[num_a8_relocs].hash = hash;
5573
5574                           num_a8_relocs++;
5575                         }
5576                     }
5577                 }
5578
5579               /* We're done with the internal relocs, free them.  */
5580               if (elf_section_data (section)->relocs == NULL)
5581                 free (internal_relocs);
5582             }
5583
5584           if (htab->fix_cortex_a8)
5585             {
5586               /* Sort relocs which might apply to Cortex-A8 erratum.  */
5587               qsort (a8_relocs, num_a8_relocs,
5588                      sizeof (struct a8_erratum_reloc),
5589                      &a8_reloc_compare);
5590
5591               /* Scan for branches which might trigger Cortex-A8 erratum.  */
5592               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
5593                                           &num_a8_fixes, &a8_fix_table_size,
5594                                           a8_relocs, num_a8_relocs,
5595                                           prev_num_a8_fixes, &stub_changed)
5596                   != 0)
5597                 goto error_ret_free_local;
5598             }
5599         }
5600
5601       if (prev_num_a8_fixes != num_a8_fixes)
5602         stub_changed = TRUE;
5603
5604       if (!stub_changed)
5605         break;
5606
5607       /* OK, we've added some stubs.  Find out the new size of the
5608          stub sections.  */
5609       for (stub_sec = htab->stub_bfd->sections;
5610            stub_sec != NULL;
5611            stub_sec = stub_sec->next)
5612         {
5613           /* Ignore non-stub sections.  */
5614           if (!strstr (stub_sec->name, STUB_SUFFIX))
5615             continue;
5616
5617           stub_sec->size = 0;
5618         }
5619
5620       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
5621
5622       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
5623       if (htab->fix_cortex_a8)
5624         for (i = 0; i < num_a8_fixes; i++)
5625           {
5626             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
5627                          a8_fixes[i].section, htab);
5628
5629             if (stub_sec == NULL)
5630               goto error_ret_free_local;
5631
5632             stub_sec->size
5633               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
5634                                               NULL);
5635           }
5636
5637
5638       /* Ask the linker to do its stuff.  */
5639       (*htab->layout_sections_again) ();
5640     }
5641
5642   /* Add stubs for Cortex-A8 erratum fixes now.  */
5643   if (htab->fix_cortex_a8)
5644     {
5645       for (i = 0; i < num_a8_fixes; i++)
5646         {
5647           struct elf32_arm_stub_hash_entry *stub_entry;
5648           char *stub_name = a8_fixes[i].stub_name;
5649           asection *section = a8_fixes[i].section;
5650           unsigned int section_id = a8_fixes[i].section->id;
5651           asection *link_sec = htab->stub_group[section_id].link_sec;
5652           asection *stub_sec = htab->stub_group[section_id].stub_sec;
5653           const insn_sequence *template_sequence;
5654           int template_size, size = 0;
5655
5656           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
5657                                              TRUE, FALSE);
5658           if (stub_entry == NULL)
5659             {
5660               (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
5661                                      section->owner,
5662                                      stub_name);
5663               return FALSE;
5664             }
5665
5666           stub_entry->stub_sec = stub_sec;
5667           stub_entry->stub_offset = 0;
5668           stub_entry->id_sec = link_sec;
5669           stub_entry->stub_type = a8_fixes[i].stub_type;
5670           stub_entry->target_section = a8_fixes[i].section;
5671           stub_entry->target_value = a8_fixes[i].offset;
5672           stub_entry->target_addend = a8_fixes[i].addend;
5673           stub_entry->orig_insn = a8_fixes[i].orig_insn;
5674           stub_entry->branch_type = a8_fixes[i].branch_type;
5675
5676           size = find_stub_size_and_template (a8_fixes[i].stub_type,
5677                                               &template_sequence,
5678                                               &template_size);
5679
5680           stub_entry->stub_size = size;
5681           stub_entry->stub_template = template_sequence;
5682           stub_entry->stub_template_size = template_size;
5683         }
5684
5685       /* Stash the Cortex-A8 erratum fix array for use later in
5686          elf32_arm_write_section().  */
5687       htab->a8_erratum_fixes = a8_fixes;
5688       htab->num_a8_erratum_fixes = num_a8_fixes;
5689     }
5690   else
5691     {
5692       htab->a8_erratum_fixes = NULL;
5693       htab->num_a8_erratum_fixes = 0;
5694     }
5695   return TRUE;
5696
5697  error_ret_free_local:
5698   return FALSE;
5699 }
5700
5701 /* Build all the stubs associated with the current output file.  The
5702    stubs are kept in a hash table attached to the main linker hash
5703    table.  We also set up the .plt entries for statically linked PIC
5704    functions here.  This function is called via arm_elf_finish in the
5705    linker.  */
5706
5707 bfd_boolean
5708 elf32_arm_build_stubs (struct bfd_link_info *info)
5709 {
5710   asection *stub_sec;
5711   struct bfd_hash_table *table;
5712   struct elf32_arm_link_hash_table *htab;
5713
5714   htab = elf32_arm_hash_table (info);
5715   if (htab == NULL)
5716     return FALSE;
5717
5718   for (stub_sec = htab->stub_bfd->sections;
5719        stub_sec != NULL;
5720        stub_sec = stub_sec->next)
5721     {
5722       bfd_size_type size;
5723
5724       /* Ignore non-stub sections.  */
5725       if (!strstr (stub_sec->name, STUB_SUFFIX))
5726         continue;
5727
5728       /* Allocate memory to hold the linker stubs.  */
5729       size = stub_sec->size;
5730       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
5731       if (stub_sec->contents == NULL && size != 0)
5732         return FALSE;
5733       stub_sec->size = 0;
5734     }
5735
5736   /* Build the stubs as directed by the stub hash table.  */
5737   table = &htab->stub_hash_table;
5738   bfd_hash_traverse (table, arm_build_one_stub, info);
5739   if (htab->fix_cortex_a8)
5740     {
5741       /* Place the cortex a8 stubs last.  */
5742       htab->fix_cortex_a8 = -1;
5743       bfd_hash_traverse (table, arm_build_one_stub, info);
5744     }
5745
5746   return TRUE;
5747 }
5748
5749 /* Locate the Thumb encoded calling stub for NAME.  */
5750
5751 static struct elf_link_hash_entry *
5752 find_thumb_glue (struct bfd_link_info *link_info,
5753                  const char *name,
5754                  char **error_message)
5755 {
5756   char *tmp_name;
5757   struct elf_link_hash_entry *hash;
5758   struct elf32_arm_link_hash_table *hash_table;
5759
5760   /* We need a pointer to the armelf specific hash table.  */
5761   hash_table = elf32_arm_hash_table (link_info);
5762   if (hash_table == NULL)
5763     return NULL;
5764
5765   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5766                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
5767
5768   BFD_ASSERT (tmp_name);
5769
5770   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
5771
5772   hash = elf_link_hash_lookup
5773     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5774
5775   if (hash == NULL
5776       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
5777                    tmp_name, name) == -1)
5778     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5779
5780   free (tmp_name);
5781
5782   return hash;
5783 }
5784
5785 /* Locate the ARM encoded calling stub for NAME.  */
5786
5787 static struct elf_link_hash_entry *
5788 find_arm_glue (struct bfd_link_info *link_info,
5789                const char *name,
5790                char **error_message)
5791 {
5792   char *tmp_name;
5793   struct elf_link_hash_entry *myh;
5794   struct elf32_arm_link_hash_table *hash_table;
5795
5796   /* We need a pointer to the elfarm specific hash table.  */
5797   hash_table = elf32_arm_hash_table (link_info);
5798   if (hash_table == NULL)
5799     return NULL;
5800
5801   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5802                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5803
5804   BFD_ASSERT (tmp_name);
5805
5806   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5807
5808   myh = elf_link_hash_lookup
5809     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5810
5811   if (myh == NULL
5812       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
5813                    tmp_name, name) == -1)
5814     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5815
5816   free (tmp_name);
5817
5818   return myh;
5819 }
5820
5821 /* ARM->Thumb glue (static images):
5822
5823    .arm
5824    __func_from_arm:
5825    ldr r12, __func_addr
5826    bx  r12
5827    __func_addr:
5828    .word func    @ behave as if you saw a ARM_32 reloc.
5829
5830    (v5t static images)
5831    .arm
5832    __func_from_arm:
5833    ldr pc, __func_addr
5834    __func_addr:
5835    .word func    @ behave as if you saw a ARM_32 reloc.
5836
5837    (relocatable images)
5838    .arm
5839    __func_from_arm:
5840    ldr r12, __func_offset
5841    add r12, r12, pc
5842    bx  r12
5843    __func_offset:
5844    .word func - .   */
5845
5846 #define ARM2THUMB_STATIC_GLUE_SIZE 12
5847 static const insn32 a2t1_ldr_insn = 0xe59fc000;
5848 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
5849 static const insn32 a2t3_func_addr_insn = 0x00000001;
5850
5851 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
5852 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
5853 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
5854
5855 #define ARM2THUMB_PIC_GLUE_SIZE 16
5856 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
5857 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
5858 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
5859
5860 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
5861
5862      .thumb                             .thumb
5863      .align 2                           .align 2
5864  __func_from_thumb:                 __func_from_thumb:
5865      bx pc                              push {r6, lr}
5866      nop                                ldr  r6, __func_addr
5867      .arm                               mov  lr, pc
5868      b func                             bx   r6
5869                                         .arm
5870                                     ;; back_to_thumb
5871                                         ldmia r13! {r6, lr}
5872                                         bx    lr
5873                                     __func_addr:
5874                                         .word        func  */
5875
5876 #define THUMB2ARM_GLUE_SIZE 8
5877 static const insn16 t2a1_bx_pc_insn = 0x4778;
5878 static const insn16 t2a2_noop_insn = 0x46c0;
5879 static const insn32 t2a3_b_insn = 0xea000000;
5880
5881 #define VFP11_ERRATUM_VENEER_SIZE 8
5882 #define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
5883 #define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
5884
5885 #define ARM_BX_VENEER_SIZE 12
5886 static const insn32 armbx1_tst_insn = 0xe3100001;
5887 static const insn32 armbx2_moveq_insn = 0x01a0f000;
5888 static const insn32 armbx3_bx_insn = 0xe12fff10;
5889
5890 #ifndef ELFARM_NABI_C_INCLUDED
5891 static void
5892 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
5893 {
5894   asection * s;
5895   bfd_byte * contents;
5896
5897   if (size == 0)
5898     {
5899       /* Do not include empty glue sections in the output.  */
5900       if (abfd != NULL)
5901         {
5902           s = bfd_get_linker_section (abfd, name);
5903           if (s != NULL)
5904             s->flags |= SEC_EXCLUDE;
5905         }
5906       return;
5907     }
5908
5909   BFD_ASSERT (abfd != NULL);
5910
5911   s = bfd_get_linker_section (abfd, name);
5912   BFD_ASSERT (s != NULL);
5913
5914   contents = (bfd_byte *) bfd_alloc (abfd, size);
5915
5916   BFD_ASSERT (s->size == size);
5917   s->contents = contents;
5918 }
5919
5920 bfd_boolean
5921 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
5922 {
5923   struct elf32_arm_link_hash_table * globals;
5924
5925   globals = elf32_arm_hash_table (info);
5926   BFD_ASSERT (globals != NULL);
5927
5928   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5929                                    globals->arm_glue_size,
5930                                    ARM2THUMB_GLUE_SECTION_NAME);
5931
5932   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5933                                    globals->thumb_glue_size,
5934                                    THUMB2ARM_GLUE_SECTION_NAME);
5935
5936   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5937                                    globals->vfp11_erratum_glue_size,
5938                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
5939
5940   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5941                                    globals->stm32l4xx_erratum_glue_size,
5942                                    STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
5943
5944   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5945                                    globals->bx_glue_size,
5946                                    ARM_BX_GLUE_SECTION_NAME);
5947
5948   return TRUE;
5949 }
5950
5951 /* Allocate space and symbols for calling a Thumb function from Arm mode.
5952    returns the symbol identifying the stub.  */
5953
5954 static struct elf_link_hash_entry *
5955 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
5956                           struct elf_link_hash_entry * h)
5957 {
5958   const char * name = h->root.root.string;
5959   asection * s;
5960   char * tmp_name;
5961   struct elf_link_hash_entry * myh;
5962   struct bfd_link_hash_entry * bh;
5963   struct elf32_arm_link_hash_table * globals;
5964   bfd_vma val;
5965   bfd_size_type size;
5966
5967   globals = elf32_arm_hash_table (link_info);
5968   BFD_ASSERT (globals != NULL);
5969   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5970
5971   s = bfd_get_linker_section
5972     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
5973
5974   BFD_ASSERT (s != NULL);
5975
5976   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5977                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5978
5979   BFD_ASSERT (tmp_name);
5980
5981   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5982
5983   myh = elf_link_hash_lookup
5984     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
5985
5986   if (myh != NULL)
5987     {
5988       /* We've already seen this guy.  */
5989       free (tmp_name);
5990       return myh;
5991     }
5992
5993   /* The only trick here is using hash_table->arm_glue_size as the value.
5994      Even though the section isn't allocated yet, this is where we will be
5995      putting it.  The +1 on the value marks that the stub has not been
5996      output yet - not that it is a Thumb function.  */
5997   bh = NULL;
5998   val = globals->arm_glue_size + 1;
5999   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
6000                                     tmp_name, BSF_GLOBAL, s, val,
6001                                     NULL, TRUE, FALSE, &bh);
6002
6003   myh = (struct elf_link_hash_entry *) bh;
6004   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6005   myh->forced_local = 1;
6006
6007   free (tmp_name);
6008
6009   if (bfd_link_pic (link_info)
6010       || globals->root.is_relocatable_executable
6011       || globals->pic_veneer)
6012     size = ARM2THUMB_PIC_GLUE_SIZE;
6013   else if (globals->use_blx)
6014     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
6015   else
6016     size = ARM2THUMB_STATIC_GLUE_SIZE;
6017
6018   s->size += size;
6019   globals->arm_glue_size += size;
6020
6021   return myh;
6022 }
6023
6024 /* Allocate space for ARMv4 BX veneers.  */
6025
6026 static void
6027 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
6028 {
6029   asection * s;
6030   struct elf32_arm_link_hash_table *globals;
6031   char *tmp_name;
6032   struct elf_link_hash_entry *myh;
6033   struct bfd_link_hash_entry *bh;
6034   bfd_vma val;
6035
6036   /* BX PC does not need a veneer.  */
6037   if (reg == 15)
6038     return;
6039
6040   globals = elf32_arm_hash_table (link_info);
6041   BFD_ASSERT (globals != NULL);
6042   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6043
6044   /* Check if this veneer has already been allocated.  */
6045   if (globals->bx_glue_offset[reg])
6046     return;
6047
6048   s = bfd_get_linker_section
6049     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
6050
6051   BFD_ASSERT (s != NULL);
6052
6053   /* Add symbol for veneer.  */
6054   tmp_name = (char *)
6055       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
6056
6057   BFD_ASSERT (tmp_name);
6058
6059   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
6060
6061   myh = elf_link_hash_lookup
6062     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
6063
6064   BFD_ASSERT (myh == NULL);
6065
6066   bh = NULL;
6067   val = globals->bx_glue_size;
6068   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
6069                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6070                                     NULL, TRUE, FALSE, &bh);
6071
6072   myh = (struct elf_link_hash_entry *) bh;
6073   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6074   myh->forced_local = 1;
6075
6076   s->size += ARM_BX_VENEER_SIZE;
6077   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
6078   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
6079 }
6080
6081
6082 /* Add an entry to the code/data map for section SEC.  */
6083
6084 static void
6085 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
6086 {
6087   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
6088   unsigned int newidx;
6089
6090   if (sec_data->map == NULL)
6091     {
6092       sec_data->map = (elf32_arm_section_map *)
6093           bfd_malloc (sizeof (elf32_arm_section_map));
6094       sec_data->mapcount = 0;
6095       sec_data->mapsize = 1;
6096     }
6097
6098   newidx = sec_data->mapcount++;
6099
6100   if (sec_data->mapcount > sec_data->mapsize)
6101     {
6102       sec_data->mapsize *= 2;
6103       sec_data->map = (elf32_arm_section_map *)
6104           bfd_realloc_or_free (sec_data->map, sec_data->mapsize
6105                                * sizeof (elf32_arm_section_map));
6106     }
6107
6108   if (sec_data->map)
6109     {
6110       sec_data->map[newidx].vma = vma;
6111       sec_data->map[newidx].type = type;
6112     }
6113 }
6114
6115
6116 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
6117    veneers are handled for now.  */
6118
6119 static bfd_vma
6120 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
6121                              elf32_vfp11_erratum_list *branch,
6122                              bfd *branch_bfd,
6123                              asection *branch_sec,
6124                              unsigned int offset)
6125 {
6126   asection *s;
6127   struct elf32_arm_link_hash_table *hash_table;
6128   char *tmp_name;
6129   struct elf_link_hash_entry *myh;
6130   struct bfd_link_hash_entry *bh;
6131   bfd_vma val;
6132   struct _arm_elf_section_data *sec_data;
6133   elf32_vfp11_erratum_list *newerr;
6134
6135   hash_table = elf32_arm_hash_table (link_info);
6136   BFD_ASSERT (hash_table != NULL);
6137   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
6138
6139   s = bfd_get_linker_section
6140     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
6141
6142   sec_data = elf32_arm_section_data (s);
6143
6144   BFD_ASSERT (s != NULL);
6145
6146   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
6147                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
6148
6149   BFD_ASSERT (tmp_name);
6150
6151   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
6152            hash_table->num_vfp11_fixes);
6153
6154   myh = elf_link_hash_lookup
6155     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6156
6157   BFD_ASSERT (myh == NULL);
6158
6159   bh = NULL;
6160   val = hash_table->vfp11_erratum_glue_size;
6161   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
6162                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6163                                     NULL, TRUE, FALSE, &bh);
6164
6165   myh = (struct elf_link_hash_entry *) bh;
6166   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6167   myh->forced_local = 1;
6168
6169   /* Link veneer back to calling location.  */
6170   sec_data->erratumcount += 1;
6171   newerr = (elf32_vfp11_erratum_list *)
6172       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
6173
6174   newerr->type = VFP11_ERRATUM_ARM_VENEER;
6175   newerr->vma = -1;
6176   newerr->u.v.branch = branch;
6177   newerr->u.v.id = hash_table->num_vfp11_fixes;
6178   branch->u.b.veneer = newerr;
6179
6180   newerr->next = sec_data->erratumlist;
6181   sec_data->erratumlist = newerr;
6182
6183   /* A symbol for the return from the veneer.  */
6184   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
6185            hash_table->num_vfp11_fixes);
6186
6187   myh = elf_link_hash_lookup
6188     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6189
6190   if (myh != NULL)
6191     abort ();
6192
6193   bh = NULL;
6194   val = offset + 4;
6195   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
6196                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
6197
6198   myh = (struct elf_link_hash_entry *) bh;
6199   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6200   myh->forced_local = 1;
6201
6202   free (tmp_name);
6203
6204   /* Generate a mapping symbol for the veneer section, and explicitly add an
6205      entry for that symbol to the code/data map for the section.  */
6206   if (hash_table->vfp11_erratum_glue_size == 0)
6207     {
6208       bh = NULL;
6209       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
6210          ever requires this erratum fix.  */
6211       _bfd_generic_link_add_one_symbol (link_info,
6212                                         hash_table->bfd_of_glue_owner, "$a",
6213                                         BSF_LOCAL, s, 0, NULL,
6214                                         TRUE, FALSE, &bh);
6215
6216       myh = (struct elf_link_hash_entry *) bh;
6217       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6218       myh->forced_local = 1;
6219
6220       /* The elf32_arm_init_maps function only cares about symbols from input
6221          BFDs.  We must make a note of this generated mapping symbol
6222          ourselves so that code byteswapping works properly in
6223          elf32_arm_write_section.  */
6224       elf32_arm_section_map_add (s, 'a', 0);
6225     }
6226
6227   s->size += VFP11_ERRATUM_VENEER_SIZE;
6228   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
6229   hash_table->num_vfp11_fixes++;
6230
6231   /* The offset of the veneer.  */
6232   return val;
6233 }
6234
6235 /* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
6236    veneers need to be handled because used only in Cortex-M.  */
6237
6238 static bfd_vma
6239 record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
6240                                  elf32_stm32l4xx_erratum_list *branch,
6241                                  bfd *branch_bfd,
6242                                  asection *branch_sec,
6243                                  unsigned int offset,
6244                                  bfd_size_type veneer_size)
6245 {
6246   asection *s;
6247   struct elf32_arm_link_hash_table *hash_table;
6248   char *tmp_name;
6249   struct elf_link_hash_entry *myh;
6250   struct bfd_link_hash_entry *bh;
6251   bfd_vma val;
6252   struct _arm_elf_section_data *sec_data;
6253   elf32_stm32l4xx_erratum_list *newerr;
6254
6255   hash_table = elf32_arm_hash_table (link_info);
6256   BFD_ASSERT (hash_table != NULL);
6257   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
6258
6259   s = bfd_get_linker_section
6260     (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
6261
6262   BFD_ASSERT (s != NULL);
6263
6264   sec_data = elf32_arm_section_data (s);
6265
6266   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
6267                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
6268
6269   BFD_ASSERT (tmp_name);
6270
6271   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
6272            hash_table->num_stm32l4xx_fixes);
6273
6274   myh = elf_link_hash_lookup
6275     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6276
6277   BFD_ASSERT (myh == NULL);
6278
6279   bh = NULL;
6280   val = hash_table->stm32l4xx_erratum_glue_size;
6281   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
6282                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6283                                     NULL, TRUE, FALSE, &bh);
6284
6285   myh = (struct elf_link_hash_entry *) bh;
6286   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6287   myh->forced_local = 1;
6288
6289   /* Link veneer back to calling location.  */
6290   sec_data->stm32l4xx_erratumcount += 1;
6291   newerr = (elf32_stm32l4xx_erratum_list *)
6292       bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
6293
6294   newerr->type = STM32L4XX_ERRATUM_VENEER;
6295   newerr->vma = -1;
6296   newerr->u.v.branch = branch;
6297   newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
6298   branch->u.b.veneer = newerr;
6299
6300   newerr->next = sec_data->stm32l4xx_erratumlist;
6301   sec_data->stm32l4xx_erratumlist = newerr;
6302
6303   /* A symbol for the return from the veneer.  */
6304   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
6305            hash_table->num_stm32l4xx_fixes);
6306
6307   myh = elf_link_hash_lookup
6308     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6309
6310   if (myh != NULL)
6311     abort ();
6312
6313   bh = NULL;
6314   val = offset + 4;
6315   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
6316                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
6317
6318   myh = (struct elf_link_hash_entry *) bh;
6319   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6320   myh->forced_local = 1;
6321
6322   free (tmp_name);
6323
6324   /* Generate a mapping symbol for the veneer section, and explicitly add an
6325      entry for that symbol to the code/data map for the section.  */
6326   if (hash_table->stm32l4xx_erratum_glue_size == 0)
6327     {
6328       bh = NULL;
6329       /* Creates a THUMB symbol since there is no other choice.  */
6330       _bfd_generic_link_add_one_symbol (link_info,
6331                                         hash_table->bfd_of_glue_owner, "$t",
6332                                         BSF_LOCAL, s, 0, NULL,
6333                                         TRUE, FALSE, &bh);
6334
6335       myh = (struct elf_link_hash_entry *) bh;
6336       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6337       myh->forced_local = 1;
6338
6339       /* The elf32_arm_init_maps function only cares about symbols from input
6340          BFDs.  We must make a note of this generated mapping symbol
6341          ourselves so that code byteswapping works properly in
6342          elf32_arm_write_section.  */
6343       elf32_arm_section_map_add (s, 't', 0);
6344     }
6345
6346   s->size += veneer_size;
6347   hash_table->stm32l4xx_erratum_glue_size += veneer_size;
6348   hash_table->num_stm32l4xx_fixes++;
6349
6350   /* The offset of the veneer.  */
6351   return val;
6352 }
6353
6354 #define ARM_GLUE_SECTION_FLAGS \
6355   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
6356    | SEC_READONLY | SEC_LINKER_CREATED)
6357
6358 /* Create a fake section for use by the ARM backend of the linker.  */
6359
6360 static bfd_boolean
6361 arm_make_glue_section (bfd * abfd, const char * name)
6362 {
6363   asection * sec;
6364
6365   sec = bfd_get_linker_section (abfd, name);
6366   if (sec != NULL)
6367     /* Already made.  */
6368     return TRUE;
6369
6370   sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
6371
6372   if (sec == NULL
6373       || !bfd_set_section_alignment (abfd, sec, 2))
6374     return FALSE;
6375
6376   /* Set the gc mark to prevent the section from being removed by garbage
6377      collection, despite the fact that no relocs refer to this section.  */
6378   sec->gc_mark = 1;
6379
6380   return TRUE;
6381 }
6382
6383 /* Set size of .plt entries.  This function is called from the
6384    linker scripts in ld/emultempl/{armelf}.em.  */
6385
6386 void
6387 bfd_elf32_arm_use_long_plt (void)
6388 {
6389   elf32_arm_use_long_plt_entry = TRUE;
6390 }
6391
6392 /* Add the glue sections to ABFD.  This function is called from the
6393    linker scripts in ld/emultempl/{armelf}.em.  */
6394
6395 bfd_boolean
6396 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
6397                                         struct bfd_link_info *info)
6398 {
6399   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
6400   bfd_boolean dostm32l4xx = globals
6401     && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
6402   bfd_boolean addglue;
6403
6404   /* If we are only performing a partial
6405      link do not bother adding the glue.  */
6406   if (bfd_link_relocatable (info))
6407     return TRUE;
6408
6409   addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
6410     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
6411     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
6412     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
6413
6414   if (!dostm32l4xx)
6415     return addglue;
6416
6417   return addglue
6418     && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
6419 }
6420
6421 /* Select a BFD to be used to hold the sections used by the glue code.
6422    This function is called from the linker scripts in ld/emultempl/
6423    {armelf/pe}.em.  */
6424
6425 bfd_boolean
6426 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
6427 {
6428   struct elf32_arm_link_hash_table *globals;
6429
6430   /* If we are only performing a partial link
6431      do not bother getting a bfd to hold the glue.  */
6432   if (bfd_link_relocatable (info))
6433     return TRUE;
6434
6435   /* Make sure we don't attach the glue sections to a dynamic object.  */
6436   BFD_ASSERT (!(abfd->flags & DYNAMIC));
6437
6438   globals = elf32_arm_hash_table (info);
6439   BFD_ASSERT (globals != NULL);
6440
6441   if (globals->bfd_of_glue_owner != NULL)
6442     return TRUE;
6443
6444   /* Save the bfd for later use.  */
6445   globals->bfd_of_glue_owner = abfd;
6446
6447   return TRUE;
6448 }
6449
6450 static void
6451 check_use_blx (struct elf32_arm_link_hash_table *globals)
6452 {
6453   int cpu_arch;
6454
6455   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
6456                                        Tag_CPU_arch);
6457
6458   if (globals->fix_arm1176)
6459     {
6460       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
6461         globals->use_blx = 1;
6462     }
6463   else
6464     {
6465       if (cpu_arch > TAG_CPU_ARCH_V4T)
6466         globals->use_blx = 1;
6467     }
6468 }
6469
6470 bfd_boolean
6471 bfd_elf32_arm_process_before_allocation (bfd *abfd,
6472                                          struct bfd_link_info *link_info)
6473 {
6474   Elf_Internal_Shdr *symtab_hdr;
6475   Elf_Internal_Rela *internal_relocs = NULL;
6476   Elf_Internal_Rela *irel, *irelend;
6477   bfd_byte *contents = NULL;
6478
6479   asection *sec;
6480   struct elf32_arm_link_hash_table *globals;
6481
6482   /* If we are only performing a partial link do not bother
6483      to construct any glue.  */
6484   if (bfd_link_relocatable (link_info))
6485     return TRUE;
6486
6487   /* Here we have a bfd that is to be included on the link.  We have a
6488      hook to do reloc rummaging, before section sizes are nailed down.  */
6489   globals = elf32_arm_hash_table (link_info);
6490   BFD_ASSERT (globals != NULL);
6491
6492   check_use_blx (globals);
6493
6494   if (globals->byteswap_code && !bfd_big_endian (abfd))
6495     {
6496       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
6497                           abfd);
6498       return FALSE;
6499     }
6500
6501   /* PR 5398: If we have not decided to include any loadable sections in
6502      the output then we will not have a glue owner bfd.  This is OK, it
6503      just means that there is nothing else for us to do here.  */
6504   if (globals->bfd_of_glue_owner == NULL)
6505     return TRUE;
6506
6507   /* Rummage around all the relocs and map the glue vectors.  */
6508   sec = abfd->sections;
6509
6510   if (sec == NULL)
6511     return TRUE;
6512
6513   for (; sec != NULL; sec = sec->next)
6514     {
6515       if (sec->reloc_count == 0)
6516         continue;
6517
6518       if ((sec->flags & SEC_EXCLUDE) != 0)
6519         continue;
6520
6521       symtab_hdr = & elf_symtab_hdr (abfd);
6522
6523       /* Load the relocs.  */
6524       internal_relocs
6525         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
6526
6527       if (internal_relocs == NULL)
6528         goto error_return;
6529
6530       irelend = internal_relocs + sec->reloc_count;
6531       for (irel = internal_relocs; irel < irelend; irel++)
6532         {
6533           long r_type;
6534           unsigned long r_index;
6535
6536           struct elf_link_hash_entry *h;
6537
6538           r_type = ELF32_R_TYPE (irel->r_info);
6539           r_index = ELF32_R_SYM (irel->r_info);
6540
6541           /* These are the only relocation types we care about.  */
6542           if (   r_type != R_ARM_PC24
6543               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
6544             continue;
6545
6546           /* Get the section contents if we haven't done so already.  */
6547           if (contents == NULL)
6548             {
6549               /* Get cached copy if it exists.  */
6550               if (elf_section_data (sec)->this_hdr.contents != NULL)
6551                 contents = elf_section_data (sec)->this_hdr.contents;
6552               else
6553                 {
6554                   /* Go get them off disk.  */
6555                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
6556                     goto error_return;
6557                 }
6558             }
6559
6560           if (r_type == R_ARM_V4BX)
6561             {
6562               int reg;
6563
6564               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
6565               record_arm_bx_glue (link_info, reg);
6566               continue;
6567             }
6568
6569           /* If the relocation is not against a symbol it cannot concern us.  */
6570           h = NULL;
6571
6572           /* We don't care about local symbols.  */
6573           if (r_index < symtab_hdr->sh_info)
6574             continue;
6575
6576           /* This is an external symbol.  */
6577           r_index -= symtab_hdr->sh_info;
6578           h = (struct elf_link_hash_entry *)
6579             elf_sym_hashes (abfd)[r_index];
6580
6581           /* If the relocation is against a static symbol it must be within
6582              the current section and so cannot be a cross ARM/Thumb relocation.  */
6583           if (h == NULL)
6584             continue;
6585
6586           /* If the call will go through a PLT entry then we do not need
6587              glue.  */
6588           if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
6589             continue;
6590
6591           switch (r_type)
6592             {
6593             case R_ARM_PC24:
6594               /* This one is a call from arm code.  We need to look up
6595                  the target of the call.  If it is a thumb target, we
6596                  insert glue.  */
6597               if (h->target_internal == ST_BRANCH_TO_THUMB)
6598                 record_arm_to_thumb_glue (link_info, h);
6599               break;
6600
6601             default:
6602               abort ();
6603             }
6604         }
6605
6606       if (contents != NULL
6607           && elf_section_data (sec)->this_hdr.contents != contents)
6608         free (contents);
6609       contents = NULL;
6610
6611       if (internal_relocs != NULL
6612           && elf_section_data (sec)->relocs != internal_relocs)
6613         free (internal_relocs);
6614       internal_relocs = NULL;
6615     }
6616
6617   return TRUE;
6618
6619 error_return:
6620   if (contents != NULL
6621       && elf_section_data (sec)->this_hdr.contents != contents)
6622     free (contents);
6623   if (internal_relocs != NULL
6624       && elf_section_data (sec)->relocs != internal_relocs)
6625     free (internal_relocs);
6626
6627   return FALSE;
6628 }
6629 #endif
6630
6631
6632 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
6633
6634 void
6635 bfd_elf32_arm_init_maps (bfd *abfd)
6636 {
6637   Elf_Internal_Sym *isymbuf;
6638   Elf_Internal_Shdr *hdr;
6639   unsigned int i, localsyms;
6640
6641   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
6642   if (! is_arm_elf (abfd))
6643     return;
6644
6645   if ((abfd->flags & DYNAMIC) != 0)
6646     return;
6647
6648   hdr = & elf_symtab_hdr (abfd);
6649   localsyms = hdr->sh_info;
6650
6651   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
6652      should contain the number of local symbols, which should come before any
6653      global symbols.  Mapping symbols are always local.  */
6654   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
6655                                   NULL);
6656
6657   /* No internal symbols read?  Skip this BFD.  */
6658   if (isymbuf == NULL)
6659     return;
6660
6661   for (i = 0; i < localsyms; i++)
6662     {
6663       Elf_Internal_Sym *isym = &isymbuf[i];
6664       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6665       const char *name;
6666
6667       if (sec != NULL
6668           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
6669         {
6670           name = bfd_elf_string_from_elf_section (abfd,
6671             hdr->sh_link, isym->st_name);
6672
6673           if (bfd_is_arm_special_symbol_name (name,
6674                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
6675             elf32_arm_section_map_add (sec, name[1], isym->st_value);
6676         }
6677     }
6678 }
6679
6680
6681 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
6682    say what they wanted.  */
6683
6684 void
6685 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
6686 {
6687   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6688   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6689
6690   if (globals == NULL)
6691     return;
6692
6693   if (globals->fix_cortex_a8 == -1)
6694     {
6695       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
6696       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
6697           && (out_attr[Tag_CPU_arch_profile].i == 'A'
6698               || out_attr[Tag_CPU_arch_profile].i == 0))
6699         globals->fix_cortex_a8 = 1;
6700       else
6701         globals->fix_cortex_a8 = 0;
6702     }
6703 }
6704
6705
6706 void
6707 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
6708 {
6709   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6710   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6711
6712   if (globals == NULL)
6713     return;
6714   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
6715   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
6716     {
6717       switch (globals->vfp11_fix)
6718         {
6719         case BFD_ARM_VFP11_FIX_DEFAULT:
6720         case BFD_ARM_VFP11_FIX_NONE:
6721           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6722           break;
6723
6724         default:
6725           /* Give a warning, but do as the user requests anyway.  */
6726           (*_bfd_error_handler) (_("%B: warning: selected VFP11 erratum "
6727             "workaround is not necessary for target architecture"), obfd);
6728         }
6729     }
6730   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
6731     /* For earlier architectures, we might need the workaround, but do not
6732        enable it by default.  If users is running with broken hardware, they
6733        must enable the erratum fix explicitly.  */
6734     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6735 }
6736
6737 void
6738 bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
6739 {
6740   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6741   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6742
6743   if (globals == NULL)
6744     return;
6745
6746   /* We assume only Cortex-M4 may require the fix.  */
6747   if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
6748       || out_attr[Tag_CPU_arch_profile].i != 'M')
6749     {
6750       if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
6751         /* Give a warning, but do as the user requests anyway.  */
6752         (*_bfd_error_handler)
6753           (_("%B: warning: selected STM32L4XX erratum "
6754              "workaround is not necessary for target architecture"), obfd);
6755     }
6756 }
6757
6758 enum bfd_arm_vfp11_pipe
6759 {
6760   VFP11_FMAC,
6761   VFP11_LS,
6762   VFP11_DS,
6763   VFP11_BAD
6764 };
6765
6766 /* Return a VFP register number.  This is encoded as RX:X for single-precision
6767    registers, or X:RX for double-precision registers, where RX is the group of
6768    four bits in the instruction encoding and X is the single extension bit.
6769    RX and X fields are specified using their lowest (starting) bit.  The return
6770    value is:
6771
6772      0...31: single-precision registers s0...s31
6773      32...63: double-precision registers d0...d31.
6774
6775    Although X should be zero for VFP11 (encoding d0...d15 only), we might
6776    encounter VFP3 instructions, so we allow the full range for DP registers.  */
6777
6778 static unsigned int
6779 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
6780                      unsigned int x)
6781 {
6782   if (is_double)
6783     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
6784   else
6785     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
6786 }
6787
6788 /* Set bits in *WMASK according to a register number REG as encoded by
6789    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
6790
6791 static void
6792 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
6793 {
6794   if (reg < 32)
6795     *wmask |= 1 << reg;
6796   else if (reg < 48)
6797     *wmask |= 3 << ((reg - 32) * 2);
6798 }
6799
6800 /* Return TRUE if WMASK overwrites anything in REGS.  */
6801
6802 static bfd_boolean
6803 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
6804 {
6805   int i;
6806
6807   for (i = 0; i < numregs; i++)
6808     {
6809       unsigned int reg = regs[i];
6810
6811       if (reg < 32 && (wmask & (1 << reg)) != 0)
6812         return TRUE;
6813
6814       reg -= 32;
6815
6816       if (reg >= 16)
6817         continue;
6818
6819       if ((wmask & (3 << (reg * 2))) != 0)
6820         return TRUE;
6821     }
6822
6823   return FALSE;
6824 }
6825
6826 /* In this function, we're interested in two things: finding input registers
6827    for VFP data-processing instructions, and finding the set of registers which
6828    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
6829    hold the written set, so FLDM etc. are easy to deal with (we're only
6830    interested in 32 SP registers or 16 dp registers, due to the VFP version
6831    implemented by the chip in question).  DP registers are marked by setting
6832    both SP registers in the write mask).  */
6833
6834 static enum bfd_arm_vfp11_pipe
6835 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
6836                            int *numregs)
6837 {
6838   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
6839   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
6840
6841   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
6842     {
6843       unsigned int pqrs;
6844       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6845       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6846
6847       pqrs = ((insn & 0x00800000) >> 20)
6848            | ((insn & 0x00300000) >> 19)
6849            | ((insn & 0x00000040) >> 6);
6850
6851       switch (pqrs)
6852         {
6853         case 0: /* fmac[sd].  */
6854         case 1: /* fnmac[sd].  */
6855         case 2: /* fmsc[sd].  */
6856         case 3: /* fnmsc[sd].  */
6857           vpipe = VFP11_FMAC;
6858           bfd_arm_vfp11_write_mask (destmask, fd);
6859           regs[0] = fd;
6860           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
6861           regs[2] = fm;
6862           *numregs = 3;
6863           break;
6864
6865         case 4: /* fmul[sd].  */
6866         case 5: /* fnmul[sd].  */
6867         case 6: /* fadd[sd].  */
6868         case 7: /* fsub[sd].  */
6869           vpipe = VFP11_FMAC;
6870           goto vfp_binop;
6871
6872         case 8: /* fdiv[sd].  */
6873           vpipe = VFP11_DS;
6874           vfp_binop:
6875           bfd_arm_vfp11_write_mask (destmask, fd);
6876           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
6877           regs[1] = fm;
6878           *numregs = 2;
6879           break;
6880
6881         case 15: /* extended opcode.  */
6882           {
6883             unsigned int extn = ((insn >> 15) & 0x1e)
6884                               | ((insn >> 7) & 1);
6885
6886             switch (extn)
6887               {
6888               case 0: /* fcpy[sd].  */
6889               case 1: /* fabs[sd].  */
6890               case 2: /* fneg[sd].  */
6891               case 8: /* fcmp[sd].  */
6892               case 9: /* fcmpe[sd].  */
6893               case 10: /* fcmpz[sd].  */
6894               case 11: /* fcmpez[sd].  */
6895               case 16: /* fuito[sd].  */
6896               case 17: /* fsito[sd].  */
6897               case 24: /* ftoui[sd].  */
6898               case 25: /* ftouiz[sd].  */
6899               case 26: /* ftosi[sd].  */
6900               case 27: /* ftosiz[sd].  */
6901                 /* These instructions will not bounce due to underflow.  */
6902                 *numregs = 0;
6903                 vpipe = VFP11_FMAC;
6904                 break;
6905
6906               case 3: /* fsqrt[sd].  */
6907                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
6908                    registers to cause the erratum in previous instructions.  */
6909                 bfd_arm_vfp11_write_mask (destmask, fd);
6910                 vpipe = VFP11_DS;
6911                 break;
6912
6913               case 15: /* fcvt{ds,sd}.  */
6914                 {
6915                   int rnum = 0;
6916
6917                   bfd_arm_vfp11_write_mask (destmask, fd);
6918
6919                   /* Only FCVTSD can underflow.  */
6920                   if ((insn & 0x100) != 0)
6921                     regs[rnum++] = fm;
6922
6923                   *numregs = rnum;
6924
6925                   vpipe = VFP11_FMAC;
6926                 }
6927                 break;
6928
6929               default:
6930                 return VFP11_BAD;
6931               }
6932           }
6933           break;
6934
6935         default:
6936           return VFP11_BAD;
6937         }
6938     }
6939   /* Two-register transfer.  */
6940   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
6941     {
6942       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6943
6944       if ((insn & 0x100000) == 0)
6945         {
6946           if (is_double)
6947             bfd_arm_vfp11_write_mask (destmask, fm);
6948           else
6949             {
6950               bfd_arm_vfp11_write_mask (destmask, fm);
6951               bfd_arm_vfp11_write_mask (destmask, fm + 1);
6952             }
6953         }
6954
6955       vpipe = VFP11_LS;
6956     }
6957   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
6958     {
6959       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6960       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
6961
6962       switch (puw)
6963         {
6964         case 0: /* Two-reg transfer.  We should catch these above.  */
6965           abort ();
6966
6967         case 2: /* fldm[sdx].  */
6968         case 3:
6969         case 5:
6970           {
6971             unsigned int i, offset = insn & 0xff;
6972
6973             if (is_double)
6974               offset >>= 1;
6975
6976             for (i = fd; i < fd + offset; i++)
6977               bfd_arm_vfp11_write_mask (destmask, i);
6978           }
6979           break;
6980
6981         case 4: /* fld[sd].  */
6982         case 6:
6983           bfd_arm_vfp11_write_mask (destmask, fd);
6984           break;
6985
6986         default:
6987           return VFP11_BAD;
6988         }
6989
6990       vpipe = VFP11_LS;
6991     }
6992   /* Single-register transfer. Note L==0.  */
6993   else if ((insn & 0x0f100e10) == 0x0e000a10)
6994     {
6995       unsigned int opcode = (insn >> 21) & 7;
6996       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
6997
6998       switch (opcode)
6999         {
7000         case 0: /* fmsr/fmdlr.  */
7001         case 1: /* fmdhr.  */
7002           /* Mark fmdhr and fmdlr as writing to the whole of the DP
7003              destination register.  I don't know if this is exactly right,
7004              but it is the conservative choice.  */
7005           bfd_arm_vfp11_write_mask (destmask, fn);
7006           break;
7007
7008         case 7: /* fmxr.  */
7009           break;
7010         }
7011
7012       vpipe = VFP11_LS;
7013     }
7014
7015   return vpipe;
7016 }
7017
7018
7019 static int elf32_arm_compare_mapping (const void * a, const void * b);
7020
7021
7022 /* Look for potentially-troublesome code sequences which might trigger the
7023    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
7024    (available from ARM) for details of the erratum.  A short version is
7025    described in ld.texinfo.  */
7026
7027 bfd_boolean
7028 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
7029 {
7030   asection *sec;
7031   bfd_byte *contents = NULL;
7032   int state = 0;
7033   int regs[3], numregs = 0;
7034   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7035   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
7036
7037   if (globals == NULL)
7038     return FALSE;
7039
7040   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
7041      The states transition as follows:
7042
7043        0 -> 1 (vector) or 0 -> 2 (scalar)
7044            A VFP FMAC-pipeline instruction has been seen. Fill
7045            regs[0]..regs[numregs-1] with its input operands. Remember this
7046            instruction in 'first_fmac'.
7047
7048        1 -> 2
7049            Any instruction, except for a VFP instruction which overwrites
7050            regs[*].
7051
7052        1 -> 3 [ -> 0 ]  or
7053        2 -> 3 [ -> 0 ]
7054            A VFP instruction has been seen which overwrites any of regs[*].
7055            We must make a veneer!  Reset state to 0 before examining next
7056            instruction.
7057
7058        2 -> 0
7059            If we fail to match anything in state 2, reset to state 0 and reset
7060            the instruction pointer to the instruction after 'first_fmac'.
7061
7062      If the VFP11 vector mode is in use, there must be at least two unrelated
7063      instructions between anti-dependent VFP11 instructions to properly avoid
7064      triggering the erratum, hence the use of the extra state 1.  */
7065
7066   /* If we are only performing a partial link do not bother
7067      to construct any glue.  */
7068   if (bfd_link_relocatable (link_info))
7069     return TRUE;
7070
7071   /* Skip if this bfd does not correspond to an ELF image.  */
7072   if (! is_arm_elf (abfd))
7073     return TRUE;
7074
7075   /* We should have chosen a fix type by the time we get here.  */
7076   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
7077
7078   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
7079     return TRUE;
7080
7081   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
7082   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
7083     return TRUE;
7084
7085   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7086     {
7087       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
7088       struct _arm_elf_section_data *sec_data;
7089
7090       /* If we don't have executable progbits, we're not interested in this
7091          section.  Also skip if section is to be excluded.  */
7092       if (elf_section_type (sec) != SHT_PROGBITS
7093           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
7094           || (sec->flags & SEC_EXCLUDE) != 0
7095           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7096           || sec->output_section == bfd_abs_section_ptr
7097           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
7098         continue;
7099
7100       sec_data = elf32_arm_section_data (sec);
7101
7102       if (sec_data->mapcount == 0)
7103         continue;
7104
7105       if (elf_section_data (sec)->this_hdr.contents != NULL)
7106         contents = elf_section_data (sec)->this_hdr.contents;
7107       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7108         goto error_return;
7109
7110       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
7111              elf32_arm_compare_mapping);
7112
7113       for (span = 0; span < sec_data->mapcount; span++)
7114         {
7115           unsigned int span_start = sec_data->map[span].vma;
7116           unsigned int span_end = (span == sec_data->mapcount - 1)
7117                                   ? sec->size : sec_data->map[span + 1].vma;
7118           char span_type = sec_data->map[span].type;
7119
7120           /* FIXME: Only ARM mode is supported at present.  We may need to
7121              support Thumb-2 mode also at some point.  */
7122           if (span_type != 'a')
7123             continue;
7124
7125           for (i = span_start; i < span_end;)
7126             {
7127               unsigned int next_i = i + 4;
7128               unsigned int insn = bfd_big_endian (abfd)
7129                 ? (contents[i] << 24)
7130                   | (contents[i + 1] << 16)
7131                   | (contents[i + 2] << 8)
7132                   | contents[i + 3]
7133                 : (contents[i + 3] << 24)
7134                   | (contents[i + 2] << 16)
7135                   | (contents[i + 1] << 8)
7136                   | contents[i];
7137               unsigned int writemask = 0;
7138               enum bfd_arm_vfp11_pipe vpipe;
7139
7140               switch (state)
7141                 {
7142                 case 0:
7143                   vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
7144                                                     &numregs);
7145                   /* I'm assuming the VFP11 erratum can trigger with denorm
7146                      operands on either the FMAC or the DS pipeline. This might
7147                      lead to slightly overenthusiastic veneer insertion.  */
7148                   if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
7149                     {
7150                       state = use_vector ? 1 : 2;
7151                       first_fmac = i;
7152                       veneer_of_insn = insn;
7153                     }
7154                   break;
7155
7156                 case 1:
7157                   {
7158                     int other_regs[3], other_numregs;
7159                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
7160                                                       other_regs,
7161                                                       &other_numregs);
7162                     if (vpipe != VFP11_BAD
7163                         && bfd_arm_vfp11_antidependency (writemask, regs,
7164                                                          numregs))
7165                       state = 3;
7166                     else
7167                       state = 2;
7168                   }
7169                   break;
7170
7171                 case 2:
7172                   {
7173                     int other_regs[3], other_numregs;
7174                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
7175                                                       other_regs,
7176                                                       &other_numregs);
7177                     if (vpipe != VFP11_BAD
7178                         && bfd_arm_vfp11_antidependency (writemask, regs,
7179                                                          numregs))
7180                       state = 3;
7181                     else
7182                       {
7183                         state = 0;
7184                         next_i = first_fmac + 4;
7185                       }
7186                   }
7187                   break;
7188
7189                 case 3:
7190                   abort ();  /* Should be unreachable.  */
7191                 }
7192
7193               if (state == 3)
7194                 {
7195                   elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
7196                       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7197
7198                   elf32_arm_section_data (sec)->erratumcount += 1;
7199
7200                   newerr->u.b.vfp_insn = veneer_of_insn;
7201
7202                   switch (span_type)
7203                     {
7204                     case 'a':
7205                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
7206                       break;
7207
7208                     default:
7209                       abort ();
7210                     }
7211
7212                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
7213                                                first_fmac);
7214
7215                   newerr->vma = -1;
7216
7217                   newerr->next = sec_data->erratumlist;
7218                   sec_data->erratumlist = newerr;
7219
7220                   state = 0;
7221                 }
7222
7223               i = next_i;
7224             }
7225         }
7226
7227       if (contents != NULL
7228           && elf_section_data (sec)->this_hdr.contents != contents)
7229         free (contents);
7230       contents = NULL;
7231     }
7232
7233   return TRUE;
7234
7235 error_return:
7236   if (contents != NULL
7237       && elf_section_data (sec)->this_hdr.contents != contents)
7238     free (contents);
7239
7240   return FALSE;
7241 }
7242
7243 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
7244    after sections have been laid out, using specially-named symbols.  */
7245
7246 void
7247 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
7248                                           struct bfd_link_info *link_info)
7249 {
7250   asection *sec;
7251   struct elf32_arm_link_hash_table *globals;
7252   char *tmp_name;
7253
7254   if (bfd_link_relocatable (link_info))
7255     return;
7256
7257   /* Skip if this bfd does not correspond to an ELF image.  */
7258   if (! is_arm_elf (abfd))
7259     return;
7260
7261   globals = elf32_arm_hash_table (link_info);
7262   if (globals == NULL)
7263     return;
7264
7265   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7266                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7267
7268   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7269     {
7270       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7271       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
7272
7273       for (; errnode != NULL; errnode = errnode->next)
7274         {
7275           struct elf_link_hash_entry *myh;
7276           bfd_vma vma;
7277
7278           switch (errnode->type)
7279             {
7280             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
7281             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
7282               /* Find veneer symbol.  */
7283               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7284                        errnode->u.b.veneer->u.v.id);
7285
7286               myh = elf_link_hash_lookup
7287                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7288
7289               if (myh == NULL)
7290                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
7291                                          "`%s'"), abfd, tmp_name);
7292
7293               vma = myh->root.u.def.section->output_section->vma
7294                     + myh->root.u.def.section->output_offset
7295                     + myh->root.u.def.value;
7296
7297               errnode->u.b.veneer->vma = vma;
7298               break;
7299
7300             case VFP11_ERRATUM_ARM_VENEER:
7301             case VFP11_ERRATUM_THUMB_VENEER:
7302               /* Find return location.  */
7303               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7304                        errnode->u.v.id);
7305
7306               myh = elf_link_hash_lookup
7307                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7308
7309               if (myh == NULL)
7310                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
7311                                          "`%s'"), abfd, tmp_name);
7312
7313               vma = myh->root.u.def.section->output_section->vma
7314                     + myh->root.u.def.section->output_offset
7315                     + myh->root.u.def.value;
7316
7317               errnode->u.v.branch->vma = vma;
7318               break;
7319
7320             default:
7321               abort ();
7322             }
7323         }
7324     }
7325
7326   free (tmp_name);
7327 }
7328
7329 /* Find virtual-memory addresses for STM32L4XX erratum veneers and
7330    return locations after sections have been laid out, using
7331    specially-named symbols.  */
7332
7333 void
7334 bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
7335                                               struct bfd_link_info *link_info)
7336 {
7337   asection *sec;
7338   struct elf32_arm_link_hash_table *globals;
7339   char *tmp_name;
7340
7341   if (bfd_link_relocatable (link_info))
7342     return;
7343
7344   /* Skip if this bfd does not correspond to an ELF image.  */
7345   if (! is_arm_elf (abfd))
7346     return;
7347
7348   globals = elf32_arm_hash_table (link_info);
7349   if (globals == NULL)
7350     return;
7351
7352   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7353                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7354
7355   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7356     {
7357       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7358       elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
7359
7360       for (; errnode != NULL; errnode = errnode->next)
7361         {
7362           struct elf_link_hash_entry *myh;
7363           bfd_vma vma;
7364
7365           switch (errnode->type)
7366             {
7367             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
7368               /* Find veneer symbol.  */
7369               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7370                        errnode->u.b.veneer->u.v.id);
7371
7372               myh = elf_link_hash_lookup
7373                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7374
7375               if (myh == NULL)
7376                 (*_bfd_error_handler) (_("%B: unable to find STM32L4XX veneer "
7377                                          "`%s'"), abfd, tmp_name);
7378
7379               vma = myh->root.u.def.section->output_section->vma
7380                 + myh->root.u.def.section->output_offset
7381                 + myh->root.u.def.value;
7382
7383               errnode->u.b.veneer->vma = vma;
7384               break;
7385
7386             case STM32L4XX_ERRATUM_VENEER:
7387               /* Find return location.  */
7388               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7389                        errnode->u.v.id);
7390
7391               myh = elf_link_hash_lookup
7392                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7393
7394               if (myh == NULL)
7395                 (*_bfd_error_handler) (_("%B: unable to find STM32L4XX veneer "
7396                                          "`%s'"), abfd, tmp_name);
7397
7398               vma = myh->root.u.def.section->output_section->vma
7399                 + myh->root.u.def.section->output_offset
7400                 + myh->root.u.def.value;
7401
7402               errnode->u.v.branch->vma = vma;
7403               break;
7404
7405             default:
7406               abort ();
7407             }
7408         }
7409     }
7410
7411   free (tmp_name);
7412 }
7413
7414 static inline bfd_boolean
7415 is_thumb2_ldmia (const insn32 insn)
7416 {
7417   /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
7418      1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
7419   return (insn & 0xffd02000) == 0xe8900000;
7420 }
7421
7422 static inline bfd_boolean
7423 is_thumb2_ldmdb (const insn32 insn)
7424 {
7425   /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
7426      1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
7427   return (insn & 0xffd02000) == 0xe9100000;
7428 }
7429
7430 static inline bfd_boolean
7431 is_thumb2_vldm (const insn32 insn)
7432 {
7433   /* A6.5 Extension register load or store instruction
7434      A7.7.229
7435      We look only for the 32-bit registers case since the DP (64-bit
7436      registers) are not supported for STM32L4XX
7437      Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
7438      <list> is consecutive 32-bit registers
7439      1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
7440      if P==0 && U==1 && W==1 && Rn=1101 VPOP
7441      if PUW=010 || PUW=011 || PUW=101 VLDM.  */
7442   return
7443     ((insn & 0xfe100f00) == 0xec100a00)
7444     && /* (IA without !).  */
7445     (((((insn << 7) >> 28) & 0xd) == 0x4)
7446      /* (IA with !), includes VPOP (when reg number is SP).  */     
7447      || ((((insn << 7) >> 28) & 0xd) == 0x5)
7448      /* (DB with !).  */
7449      || ((((insn << 7) >> 28) & 0xd) == 0x9));
7450 }
7451
7452 /* STM STM32L4XX erratum : This function assumes that it receives an LDM or
7453    VLDM opcode and:
7454  - computes the number and the mode of memory accesses
7455  - decides if the replacement should be done:
7456    . replaces only if > 8-word accesses
7457    . or (testing purposes only) replaces all accesses.  */
7458
7459 static bfd_boolean
7460 stm32l4xx_need_create_replacing_stub (const insn32 insn,
7461                                       bfd_arm_stm32l4xx_fix stm32l4xx_fix)
7462 {
7463   int nb_regs = 0;
7464
7465   /* The field encoding the register list is the same for both LDMIA
7466      and LDMDB encodings.  */
7467   if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
7468     nb_regs = popcount (insn & 0x0000ffff);
7469   else if (is_thumb2_vldm (insn))
7470    nb_regs = (insn & 0xff);
7471
7472   /* DEFAULT mode accounts for the real bug condition situation,
7473      ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
7474   return
7475     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT) ? nb_regs > 8 :
7476     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL) ? TRUE : FALSE;
7477 }
7478
7479 /* Look for potentially-troublesome code sequences which might trigger
7480    the STM STM32L4XX erratum.  */
7481
7482 bfd_boolean
7483 bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
7484                                       struct bfd_link_info *link_info)
7485 {
7486   asection *sec;
7487   bfd_byte *contents = NULL;
7488   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7489
7490   if (globals == NULL)
7491     return FALSE;
7492
7493   /* If we are only performing a partial link do not bother
7494      to construct any glue.  */
7495   if (bfd_link_relocatable (link_info))
7496     return TRUE;
7497
7498   /* Skip if this bfd does not correspond to an ELF image.  */
7499   if (! is_arm_elf (abfd))
7500     return TRUE;
7501
7502   if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
7503     return TRUE;
7504
7505   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
7506   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
7507     return TRUE;
7508
7509   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7510     {
7511       unsigned int i, span;
7512       struct _arm_elf_section_data *sec_data;
7513
7514       /* If we don't have executable progbits, we're not interested in this
7515          section.  Also skip if section is to be excluded.  */
7516       if (elf_section_type (sec) != SHT_PROGBITS
7517           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
7518           || (sec->flags & SEC_EXCLUDE) != 0
7519           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7520           || sec->output_section == bfd_abs_section_ptr
7521           || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
7522         continue;
7523
7524       sec_data = elf32_arm_section_data (sec);
7525
7526       if (sec_data->mapcount == 0)
7527         continue;
7528
7529       if (elf_section_data (sec)->this_hdr.contents != NULL)
7530         contents = elf_section_data (sec)->this_hdr.contents;
7531       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7532         goto error_return;
7533
7534       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
7535              elf32_arm_compare_mapping);
7536
7537       for (span = 0; span < sec_data->mapcount; span++)
7538         {
7539           unsigned int span_start = sec_data->map[span].vma;
7540           unsigned int span_end = (span == sec_data->mapcount - 1)
7541             ? sec->size : sec_data->map[span + 1].vma;
7542           char span_type = sec_data->map[span].type;
7543           int itblock_current_pos = 0;
7544
7545           /* Only Thumb2 mode need be supported with this CM4 specific
7546              code, we should not encounter any arm mode eg span_type
7547              != 'a'.  */
7548           if (span_type != 't')
7549             continue;
7550
7551           for (i = span_start; i < span_end;)
7552             {
7553               unsigned int insn = bfd_get_16 (abfd, &contents[i]);
7554               bfd_boolean insn_32bit = FALSE;
7555               bfd_boolean is_ldm = FALSE;
7556               bfd_boolean is_vldm = FALSE;
7557               bfd_boolean is_not_last_in_it_block = FALSE;
7558
7559               /* The first 16-bits of all 32-bit thumb2 instructions start
7560                  with opcode[15..13]=0b111 and the encoded op1 can be anything
7561                  except opcode[12..11]!=0b00.
7562                  See 32-bit Thumb instruction encoding.  */
7563               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
7564                 insn_32bit = TRUE;
7565
7566               /* Compute the predicate that tells if the instruction
7567                  is concerned by the IT block
7568                  - Creates an error if there is a ldm that is not
7569                    last in the IT block thus cannot be replaced
7570                  - Otherwise we can create a branch at the end of the
7571                    IT block, it will be controlled naturally by IT
7572                    with the proper pseudo-predicate
7573                  - So the only interesting predicate is the one that
7574                    tells that we are not on the last item of an IT
7575                    block.  */
7576               if (itblock_current_pos != 0)
7577                   is_not_last_in_it_block = !!--itblock_current_pos;
7578
7579               if (insn_32bit)
7580                 {
7581                   /* Load the rest of the insn (in manual-friendly order).  */
7582                   insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
7583                   is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
7584                   is_vldm = is_thumb2_vldm (insn);
7585
7586                   /* Veneers are created for (v)ldm depending on
7587                      option flags and memory accesses conditions; but
7588                      if the instruction is not the last instruction of
7589                      an IT block, we cannot create a jump there, so we
7590                      bail out.  */
7591                     if ((is_ldm || is_vldm) &&
7592                         stm32l4xx_need_create_replacing_stub
7593                         (insn, globals->stm32l4xx_fix))
7594                       {
7595                         if (is_not_last_in_it_block)
7596                           {
7597                             (*_bfd_error_handler)
7598                               /* Note - overlong line used here to allow for translation.  */
7599                               (_("\
7600 %B(%A+0x%lx): error: multiple load detected in non-last IT block instruction : STM32L4XX veneer cannot be generated.\n"
7601                                  "Use gcc option -mrestrict-it to generate only one instruction per IT block.\n"),
7602                                abfd, sec, (long)i);
7603                           }
7604                         else
7605                           {
7606                             elf32_stm32l4xx_erratum_list *newerr =
7607                               (elf32_stm32l4xx_erratum_list *)
7608                               bfd_zmalloc
7609                               (sizeof (elf32_stm32l4xx_erratum_list));
7610
7611                             elf32_arm_section_data (sec)
7612                               ->stm32l4xx_erratumcount += 1;
7613                             newerr->u.b.insn = insn;
7614                             /* We create only thumb branches.  */
7615                             newerr->type =
7616                               STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
7617                             record_stm32l4xx_erratum_veneer
7618                               (link_info, newerr, abfd, sec,
7619                                i,
7620                                is_ldm ?
7621                                STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
7622                                STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
7623                             newerr->vma = -1;
7624                             newerr->next = sec_data->stm32l4xx_erratumlist;
7625                             sec_data->stm32l4xx_erratumlist = newerr;
7626                           }
7627                       }
7628                 }
7629               else
7630                 {
7631                   /* A7.7.37 IT p208
7632                      IT blocks are only encoded in T1
7633                      Encoding T1: IT{x{y{z}}} <firstcond>
7634                      1 0 1 1 - 1 1 1 1 - firstcond - mask
7635                      if mask = '0000' then see 'related encodings'
7636                      We don't deal with UNPREDICTABLE, just ignore these.
7637                      There can be no nested IT blocks so an IT block
7638                      is naturally a new one for which it is worth
7639                      computing its size.  */
7640                   bfd_boolean is_newitblock = ((insn & 0xff00) == 0xbf00) &&
7641                     ((insn & 0x000f) != 0x0000);
7642                   /* If we have a new IT block we compute its size.  */
7643                   if (is_newitblock)
7644                     {
7645                       /* Compute the number of instructions controlled
7646                          by the IT block, it will be used to decide
7647                          whether we are inside an IT block or not.  */
7648                       unsigned int mask = insn & 0x000f;
7649                       itblock_current_pos = 4 - ctz (mask);
7650                     }
7651                 }
7652
7653               i += insn_32bit ? 4 : 2;
7654             }
7655         }
7656
7657       if (contents != NULL
7658           && elf_section_data (sec)->this_hdr.contents != contents)
7659         free (contents);
7660       contents = NULL;
7661     }
7662
7663   return TRUE;
7664
7665 error_return:
7666   if (contents != NULL
7667       && elf_section_data (sec)->this_hdr.contents != contents)
7668     free (contents);
7669
7670   return FALSE;
7671 }
7672
7673 /* Set target relocation values needed during linking.  */
7674
7675 void
7676 bfd_elf32_arm_set_target_relocs (struct bfd *output_bfd,
7677                                  struct bfd_link_info *link_info,
7678                                  int target1_is_rel,
7679                                  char * target2_type,
7680                                  int fix_v4bx,
7681                                  int use_blx,
7682                                  bfd_arm_vfp11_fix vfp11_fix,
7683                                  bfd_arm_stm32l4xx_fix stm32l4xx_fix,
7684                                  int no_enum_warn, int no_wchar_warn,
7685                                  int pic_veneer, int fix_cortex_a8,
7686                                  int fix_arm1176)
7687 {
7688   struct elf32_arm_link_hash_table *globals;
7689
7690   globals = elf32_arm_hash_table (link_info);
7691   if (globals == NULL)
7692     return;
7693
7694   globals->target1_is_rel = target1_is_rel;
7695   if (strcmp (target2_type, "rel") == 0)
7696     globals->target2_reloc = R_ARM_REL32;
7697   else if (strcmp (target2_type, "abs") == 0)
7698     globals->target2_reloc = R_ARM_ABS32;
7699   else if (strcmp (target2_type, "got-rel") == 0)
7700     globals->target2_reloc = R_ARM_GOT_PREL;
7701   else
7702     {
7703       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
7704                           target2_type);
7705     }
7706   globals->fix_v4bx = fix_v4bx;
7707   globals->use_blx |= use_blx;
7708   globals->vfp11_fix = vfp11_fix;
7709   globals->stm32l4xx_fix = stm32l4xx_fix;
7710   globals->pic_veneer = pic_veneer;
7711   globals->fix_cortex_a8 = fix_cortex_a8;
7712   globals->fix_arm1176 = fix_arm1176;
7713
7714   BFD_ASSERT (is_arm_elf (output_bfd));
7715   elf_arm_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
7716   elf_arm_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
7717 }
7718
7719 /* Replace the target offset of a Thumb bl or b.w instruction.  */
7720
7721 static void
7722 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
7723 {
7724   bfd_vma upper;
7725   bfd_vma lower;
7726   int reloc_sign;
7727
7728   BFD_ASSERT ((offset & 1) == 0);
7729
7730   upper = bfd_get_16 (abfd, insn);
7731   lower = bfd_get_16 (abfd, insn + 2);
7732   reloc_sign = (offset < 0) ? 1 : 0;
7733   upper = (upper & ~(bfd_vma) 0x7ff)
7734           | ((offset >> 12) & 0x3ff)
7735           | (reloc_sign << 10);
7736   lower = (lower & ~(bfd_vma) 0x2fff)
7737           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
7738           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
7739           | ((offset >> 1) & 0x7ff);
7740   bfd_put_16 (abfd, upper, insn);
7741   bfd_put_16 (abfd, lower, insn + 2);
7742 }
7743
7744 /* Thumb code calling an ARM function.  */
7745
7746 static int
7747 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
7748                          const char *           name,
7749                          bfd *                  input_bfd,
7750                          bfd *                  output_bfd,
7751                          asection *             input_section,
7752                          bfd_byte *             hit_data,
7753                          asection *             sym_sec,
7754                          bfd_vma                offset,
7755                          bfd_signed_vma         addend,
7756                          bfd_vma                val,
7757                          char **error_message)
7758 {
7759   asection * s = 0;
7760   bfd_vma my_offset;
7761   long int ret_offset;
7762   struct elf_link_hash_entry * myh;
7763   struct elf32_arm_link_hash_table * globals;
7764
7765   myh = find_thumb_glue (info, name, error_message);
7766   if (myh == NULL)
7767     return FALSE;
7768
7769   globals = elf32_arm_hash_table (info);
7770   BFD_ASSERT (globals != NULL);
7771   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7772
7773   my_offset = myh->root.u.def.value;
7774
7775   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
7776                               THUMB2ARM_GLUE_SECTION_NAME);
7777
7778   BFD_ASSERT (s != NULL);
7779   BFD_ASSERT (s->contents != NULL);
7780   BFD_ASSERT (s->output_section != NULL);
7781
7782   if ((my_offset & 0x01) == 0x01)
7783     {
7784       if (sym_sec != NULL
7785           && sym_sec->owner != NULL
7786           && !INTERWORK_FLAG (sym_sec->owner))
7787         {
7788           (*_bfd_error_handler)
7789             (_("%B(%s): warning: interworking not enabled.\n"
7790                "  first occurrence: %B: Thumb call to ARM"),
7791              sym_sec->owner, input_bfd, name);
7792
7793           return FALSE;
7794         }
7795
7796       --my_offset;
7797       myh->root.u.def.value = my_offset;
7798
7799       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
7800                       s->contents + my_offset);
7801
7802       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
7803                       s->contents + my_offset + 2);
7804
7805       ret_offset =
7806         /* Address of destination of the stub.  */
7807         ((bfd_signed_vma) val)
7808         - ((bfd_signed_vma)
7809            /* Offset from the start of the current section
7810               to the start of the stubs.  */
7811            (s->output_offset
7812             /* Offset of the start of this stub from the start of the stubs.  */
7813             + my_offset
7814             /* Address of the start of the current section.  */
7815             + s->output_section->vma)
7816            /* The branch instruction is 4 bytes into the stub.  */
7817            + 4
7818            /* ARM branches work from the pc of the instruction + 8.  */
7819            + 8);
7820
7821       put_arm_insn (globals, output_bfd,
7822                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
7823                     s->contents + my_offset + 4);
7824     }
7825
7826   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
7827
7828   /* Now go back and fix up the original BL insn to point to here.  */
7829   ret_offset =
7830     /* Address of where the stub is located.  */
7831     (s->output_section->vma + s->output_offset + my_offset)
7832      /* Address of where the BL is located.  */
7833     - (input_section->output_section->vma + input_section->output_offset
7834        + offset)
7835     /* Addend in the relocation.  */
7836     - addend
7837     /* Biassing for PC-relative addressing.  */
7838     - 8;
7839
7840   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
7841
7842   return TRUE;
7843 }
7844
7845 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
7846
7847 static struct elf_link_hash_entry *
7848 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
7849                              const char *           name,
7850                              bfd *                  input_bfd,
7851                              bfd *                  output_bfd,
7852                              asection *             sym_sec,
7853                              bfd_vma                val,
7854                              asection *             s,
7855                              char **                error_message)
7856 {
7857   bfd_vma my_offset;
7858   long int ret_offset;
7859   struct elf_link_hash_entry * myh;
7860   struct elf32_arm_link_hash_table * globals;
7861
7862   myh = find_arm_glue (info, name, error_message);
7863   if (myh == NULL)
7864     return NULL;
7865
7866   globals = elf32_arm_hash_table (info);
7867   BFD_ASSERT (globals != NULL);
7868   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7869
7870   my_offset = myh->root.u.def.value;
7871
7872   if ((my_offset & 0x01) == 0x01)
7873     {
7874       if (sym_sec != NULL
7875           && sym_sec->owner != NULL
7876           && !INTERWORK_FLAG (sym_sec->owner))
7877         {
7878           (*_bfd_error_handler)
7879             (_("%B(%s): warning: interworking not enabled.\n"
7880                "  first occurrence: %B: arm call to thumb"),
7881              sym_sec->owner, input_bfd, name);
7882         }
7883
7884       --my_offset;
7885       myh->root.u.def.value = my_offset;
7886
7887       if (bfd_link_pic (info)
7888           || globals->root.is_relocatable_executable
7889           || globals->pic_veneer)
7890         {
7891           /* For relocatable objects we can't use absolute addresses,
7892              so construct the address from a relative offset.  */
7893           /* TODO: If the offset is small it's probably worth
7894              constructing the address with adds.  */
7895           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
7896                         s->contents + my_offset);
7897           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
7898                         s->contents + my_offset + 4);
7899           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
7900                         s->contents + my_offset + 8);
7901           /* Adjust the offset by 4 for the position of the add,
7902              and 8 for the pipeline offset.  */
7903           ret_offset = (val - (s->output_offset
7904                                + s->output_section->vma
7905                                + my_offset + 12))
7906                        | 1;
7907           bfd_put_32 (output_bfd, ret_offset,
7908                       s->contents + my_offset + 12);
7909         }
7910       else if (globals->use_blx)
7911         {
7912           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
7913                         s->contents + my_offset);
7914
7915           /* It's a thumb address.  Add the low order bit.  */
7916           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
7917                       s->contents + my_offset + 4);
7918         }
7919       else
7920         {
7921           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
7922                         s->contents + my_offset);
7923
7924           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
7925                         s->contents + my_offset + 4);
7926
7927           /* It's a thumb address.  Add the low order bit.  */
7928           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
7929                       s->contents + my_offset + 8);
7930
7931           my_offset += 12;
7932         }
7933     }
7934
7935   BFD_ASSERT (my_offset <= globals->arm_glue_size);
7936
7937   return myh;
7938 }
7939
7940 /* Arm code calling a Thumb function.  */
7941
7942 static int
7943 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
7944                          const char *           name,
7945                          bfd *                  input_bfd,
7946                          bfd *                  output_bfd,
7947                          asection *             input_section,
7948                          bfd_byte *             hit_data,
7949                          asection *             sym_sec,
7950                          bfd_vma                offset,
7951                          bfd_signed_vma         addend,
7952                          bfd_vma                val,
7953                          char **error_message)
7954 {
7955   unsigned long int tmp;
7956   bfd_vma my_offset;
7957   asection * s;
7958   long int ret_offset;
7959   struct elf_link_hash_entry * myh;
7960   struct elf32_arm_link_hash_table * globals;
7961
7962   globals = elf32_arm_hash_table (info);
7963   BFD_ASSERT (globals != NULL);
7964   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7965
7966   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
7967                               ARM2THUMB_GLUE_SECTION_NAME);
7968   BFD_ASSERT (s != NULL);
7969   BFD_ASSERT (s->contents != NULL);
7970   BFD_ASSERT (s->output_section != NULL);
7971
7972   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
7973                                      sym_sec, val, s, error_message);
7974   if (!myh)
7975     return FALSE;
7976
7977   my_offset = myh->root.u.def.value;
7978   tmp = bfd_get_32 (input_bfd, hit_data);
7979   tmp = tmp & 0xFF000000;
7980
7981   /* Somehow these are both 4 too far, so subtract 8.  */
7982   ret_offset = (s->output_offset
7983                 + my_offset
7984                 + s->output_section->vma
7985                 - (input_section->output_offset
7986                    + input_section->output_section->vma
7987                    + offset + addend)
7988                 - 8);
7989
7990   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
7991
7992   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
7993
7994   return TRUE;
7995 }
7996
7997 /* Populate Arm stub for an exported Thumb function.  */
7998
7999 static bfd_boolean
8000 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
8001 {
8002   struct bfd_link_info * info = (struct bfd_link_info *) inf;
8003   asection * s;
8004   struct elf_link_hash_entry * myh;
8005   struct elf32_arm_link_hash_entry *eh;
8006   struct elf32_arm_link_hash_table * globals;
8007   asection *sec;
8008   bfd_vma val;
8009   char *error_message;
8010
8011   eh = elf32_arm_hash_entry (h);
8012   /* Allocate stubs for exported Thumb functions on v4t.  */
8013   if (eh->export_glue == NULL)
8014     return TRUE;
8015
8016   globals = elf32_arm_hash_table (info);
8017   BFD_ASSERT (globals != NULL);
8018   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
8019
8020   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
8021                               ARM2THUMB_GLUE_SECTION_NAME);
8022   BFD_ASSERT (s != NULL);
8023   BFD_ASSERT (s->contents != NULL);
8024   BFD_ASSERT (s->output_section != NULL);
8025
8026   sec = eh->export_glue->root.u.def.section;
8027
8028   BFD_ASSERT (sec->output_section != NULL);
8029
8030   val = eh->export_glue->root.u.def.value + sec->output_offset
8031         + sec->output_section->vma;
8032
8033   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
8034                                      h->root.u.def.section->owner,
8035                                      globals->obfd, sec, val, s,
8036                                      &error_message);
8037   BFD_ASSERT (myh);
8038   return TRUE;
8039 }
8040
8041 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
8042
8043 static bfd_vma
8044 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
8045 {
8046   bfd_byte *p;
8047   bfd_vma glue_addr;
8048   asection *s;
8049   struct elf32_arm_link_hash_table *globals;
8050
8051   globals = elf32_arm_hash_table (info);
8052   BFD_ASSERT (globals != NULL);
8053   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
8054
8055   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
8056                               ARM_BX_GLUE_SECTION_NAME);
8057   BFD_ASSERT (s != NULL);
8058   BFD_ASSERT (s->contents != NULL);
8059   BFD_ASSERT (s->output_section != NULL);
8060
8061   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
8062
8063   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
8064
8065   if ((globals->bx_glue_offset[reg] & 1) == 0)
8066     {
8067       p = s->contents + glue_addr;
8068       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
8069       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
8070       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
8071       globals->bx_glue_offset[reg] |= 1;
8072     }
8073
8074   return glue_addr + s->output_section->vma + s->output_offset;
8075 }
8076
8077 /* Generate Arm stubs for exported Thumb symbols.  */
8078 static void
8079 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
8080                                   struct bfd_link_info *link_info)
8081 {
8082   struct elf32_arm_link_hash_table * globals;
8083
8084   if (link_info == NULL)
8085     /* Ignore this if we are not called by the ELF backend linker.  */
8086     return;
8087
8088   globals = elf32_arm_hash_table (link_info);
8089   if (globals == NULL)
8090     return;
8091
8092   /* If blx is available then exported Thumb symbols are OK and there is
8093      nothing to do.  */
8094   if (globals->use_blx)
8095     return;
8096
8097   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
8098                           link_info);
8099 }
8100
8101 /* Reserve space for COUNT dynamic relocations in relocation selection
8102    SRELOC.  */
8103
8104 static void
8105 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
8106                               bfd_size_type count)
8107 {
8108   struct elf32_arm_link_hash_table *htab;
8109
8110   htab = elf32_arm_hash_table (info);
8111   BFD_ASSERT (htab->root.dynamic_sections_created);
8112   if (sreloc == NULL)
8113     abort ();
8114   sreloc->size += RELOC_SIZE (htab) * count;
8115 }
8116
8117 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
8118    dynamic, the relocations should go in SRELOC, otherwise they should
8119    go in the special .rel.iplt section.  */
8120
8121 static void
8122 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
8123                             bfd_size_type count)
8124 {
8125   struct elf32_arm_link_hash_table *htab;
8126
8127   htab = elf32_arm_hash_table (info);
8128   if (!htab->root.dynamic_sections_created)
8129     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
8130   else
8131     {
8132       BFD_ASSERT (sreloc != NULL);
8133       sreloc->size += RELOC_SIZE (htab) * count;
8134     }
8135 }
8136
8137 /* Add relocation REL to the end of relocation section SRELOC.  */
8138
8139 static void
8140 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
8141                         asection *sreloc, Elf_Internal_Rela *rel)
8142 {
8143   bfd_byte *loc;
8144   struct elf32_arm_link_hash_table *htab;
8145
8146   htab = elf32_arm_hash_table (info);
8147   if (!htab->root.dynamic_sections_created
8148       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
8149     sreloc = htab->root.irelplt;
8150   if (sreloc == NULL)
8151     abort ();
8152   loc = sreloc->contents;
8153   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
8154   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
8155     abort ();
8156   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
8157 }
8158
8159 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
8160    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
8161    to .plt.  */
8162
8163 static void
8164 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
8165                               bfd_boolean is_iplt_entry,
8166                               union gotplt_union *root_plt,
8167                               struct arm_plt_info *arm_plt)
8168 {
8169   struct elf32_arm_link_hash_table *htab;
8170   asection *splt;
8171   asection *sgotplt;
8172
8173   htab = elf32_arm_hash_table (info);
8174
8175   if (is_iplt_entry)
8176     {
8177       splt = htab->root.iplt;
8178       sgotplt = htab->root.igotplt;
8179
8180       /* NaCl uses a special first entry in .iplt too.  */
8181       if (htab->nacl_p && splt->size == 0)
8182         splt->size += htab->plt_header_size;
8183
8184       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
8185       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
8186     }
8187   else
8188     {
8189       splt = htab->root.splt;
8190       sgotplt = htab->root.sgotplt;
8191
8192       /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
8193       elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
8194
8195       /* If this is the first .plt entry, make room for the special
8196          first entry.  */
8197       if (splt->size == 0)
8198         splt->size += htab->plt_header_size;
8199
8200       htab->next_tls_desc_index++;
8201     }
8202
8203   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
8204   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
8205     splt->size += PLT_THUMB_STUB_SIZE;
8206   root_plt->offset = splt->size;
8207   splt->size += htab->plt_entry_size;
8208
8209   if (!htab->symbian_p)
8210     {
8211       /* We also need to make an entry in the .got.plt section, which
8212          will be placed in the .got section by the linker script.  */
8213       if (is_iplt_entry)
8214         arm_plt->got_offset = sgotplt->size;
8215       else
8216         arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
8217       sgotplt->size += 4;
8218     }
8219 }
8220
8221 static bfd_vma
8222 arm_movw_immediate (bfd_vma value)
8223 {
8224   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
8225 }
8226
8227 static bfd_vma
8228 arm_movt_immediate (bfd_vma value)
8229 {
8230   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
8231 }
8232
8233 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
8234    the entry lives in .iplt and resolves to (*SYM_VALUE)().
8235    Otherwise, DYNINDX is the index of the symbol in the dynamic
8236    symbol table and SYM_VALUE is undefined.
8237
8238    ROOT_PLT points to the offset of the PLT entry from the start of its
8239    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
8240    bookkeeping information.
8241
8242    Returns FALSE if there was a problem.  */
8243
8244 static bfd_boolean
8245 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
8246                               union gotplt_union *root_plt,
8247                               struct arm_plt_info *arm_plt,
8248                               int dynindx, bfd_vma sym_value)
8249 {
8250   struct elf32_arm_link_hash_table *htab;
8251   asection *sgot;
8252   asection *splt;
8253   asection *srel;
8254   bfd_byte *loc;
8255   bfd_vma plt_index;
8256   Elf_Internal_Rela rel;
8257   bfd_vma plt_header_size;
8258   bfd_vma got_header_size;
8259
8260   htab = elf32_arm_hash_table (info);
8261
8262   /* Pick the appropriate sections and sizes.  */
8263   if (dynindx == -1)
8264     {
8265       splt = htab->root.iplt;
8266       sgot = htab->root.igotplt;
8267       srel = htab->root.irelplt;
8268
8269       /* There are no reserved entries in .igot.plt, and no special
8270          first entry in .iplt.  */
8271       got_header_size = 0;
8272       plt_header_size = 0;
8273     }
8274   else
8275     {
8276       splt = htab->root.splt;
8277       sgot = htab->root.sgotplt;
8278       srel = htab->root.srelplt;
8279
8280       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
8281       plt_header_size = htab->plt_header_size;
8282     }
8283   BFD_ASSERT (splt != NULL && srel != NULL);
8284
8285   /* Fill in the entry in the procedure linkage table.  */
8286   if (htab->symbian_p)
8287     {
8288       BFD_ASSERT (dynindx >= 0);
8289       put_arm_insn (htab, output_bfd,
8290                     elf32_arm_symbian_plt_entry[0],
8291                     splt->contents + root_plt->offset);
8292       bfd_put_32 (output_bfd,
8293                   elf32_arm_symbian_plt_entry[1],
8294                   splt->contents + root_plt->offset + 4);
8295
8296       /* Fill in the entry in the .rel.plt section.  */
8297       rel.r_offset = (splt->output_section->vma
8298                       + splt->output_offset
8299                       + root_plt->offset + 4);
8300       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
8301
8302       /* Get the index in the procedure linkage table which
8303          corresponds to this symbol.  This is the index of this symbol
8304          in all the symbols for which we are making plt entries.  The
8305          first entry in the procedure linkage table is reserved.  */
8306       plt_index = ((root_plt->offset - plt_header_size)
8307                    / htab->plt_entry_size);
8308     }
8309   else
8310     {
8311       bfd_vma got_offset, got_address, plt_address;
8312       bfd_vma got_displacement, initial_got_entry;
8313       bfd_byte * ptr;
8314
8315       BFD_ASSERT (sgot != NULL);
8316
8317       /* Get the offset into the .(i)got.plt table of the entry that
8318          corresponds to this function.  */
8319       got_offset = (arm_plt->got_offset & -2);
8320
8321       /* Get the index in the procedure linkage table which
8322          corresponds to this symbol.  This is the index of this symbol
8323          in all the symbols for which we are making plt entries.
8324          After the reserved .got.plt entries, all symbols appear in
8325          the same order as in .plt.  */
8326       plt_index = (got_offset - got_header_size) / 4;
8327
8328       /* Calculate the address of the GOT entry.  */
8329       got_address = (sgot->output_section->vma
8330                      + sgot->output_offset
8331                      + got_offset);
8332
8333       /* ...and the address of the PLT entry.  */
8334       plt_address = (splt->output_section->vma
8335                      + splt->output_offset
8336                      + root_plt->offset);
8337
8338       ptr = splt->contents + root_plt->offset;
8339       if (htab->vxworks_p && bfd_link_pic (info))
8340         {
8341           unsigned int i;
8342           bfd_vma val;
8343
8344           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
8345             {
8346               val = elf32_arm_vxworks_shared_plt_entry[i];
8347               if (i == 2)
8348                 val |= got_address - sgot->output_section->vma;
8349               if (i == 5)
8350                 val |= plt_index * RELOC_SIZE (htab);
8351               if (i == 2 || i == 5)
8352                 bfd_put_32 (output_bfd, val, ptr);
8353               else
8354                 put_arm_insn (htab, output_bfd, val, ptr);
8355             }
8356         }
8357       else if (htab->vxworks_p)
8358         {
8359           unsigned int i;
8360           bfd_vma val;
8361
8362           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
8363             {
8364               val = elf32_arm_vxworks_exec_plt_entry[i];
8365               if (i == 2)
8366                 val |= got_address;
8367               if (i == 4)
8368                 val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
8369               if (i == 5)
8370                 val |= plt_index * RELOC_SIZE (htab);
8371               if (i == 2 || i == 5)
8372                 bfd_put_32 (output_bfd, val, ptr);
8373               else
8374                 put_arm_insn (htab, output_bfd, val, ptr);
8375             }
8376
8377           loc = (htab->srelplt2->contents
8378                  + (plt_index * 2 + 1) * RELOC_SIZE (htab));
8379
8380           /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
8381              referencing the GOT for this PLT entry.  */
8382           rel.r_offset = plt_address + 8;
8383           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
8384           rel.r_addend = got_offset;
8385           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8386           loc += RELOC_SIZE (htab);
8387
8388           /* Create the R_ARM_ABS32 relocation referencing the
8389              beginning of the PLT for this GOT entry.  */
8390           rel.r_offset = got_address;
8391           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
8392           rel.r_addend = 0;
8393           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8394         }
8395       else if (htab->nacl_p)
8396         {
8397           /* Calculate the displacement between the PLT slot and the
8398              common tail that's part of the special initial PLT slot.  */
8399           int32_t tail_displacement
8400             = ((splt->output_section->vma + splt->output_offset
8401                 + ARM_NACL_PLT_TAIL_OFFSET)
8402                - (plt_address + htab->plt_entry_size + 4));
8403           BFD_ASSERT ((tail_displacement & 3) == 0);
8404           tail_displacement >>= 2;
8405
8406           BFD_ASSERT ((tail_displacement & 0xff000000) == 0
8407                       || (-tail_displacement & 0xff000000) == 0);
8408
8409           /* Calculate the displacement between the PLT slot and the entry
8410              in the GOT.  The offset accounts for the value produced by
8411              adding to pc in the penultimate instruction of the PLT stub.  */
8412           got_displacement = (got_address
8413                               - (plt_address + htab->plt_entry_size));
8414
8415           /* NaCl does not support interworking at all.  */
8416           BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
8417
8418           put_arm_insn (htab, output_bfd,
8419                         elf32_arm_nacl_plt_entry[0]
8420                         | arm_movw_immediate (got_displacement),
8421                         ptr + 0);
8422           put_arm_insn (htab, output_bfd,
8423                         elf32_arm_nacl_plt_entry[1]
8424                         | arm_movt_immediate (got_displacement),
8425                         ptr + 4);
8426           put_arm_insn (htab, output_bfd,
8427                         elf32_arm_nacl_plt_entry[2],
8428                         ptr + 8);
8429           put_arm_insn (htab, output_bfd,
8430                         elf32_arm_nacl_plt_entry[3]
8431                         | (tail_displacement & 0x00ffffff),
8432                         ptr + 12);
8433         }
8434       else if (using_thumb_only (htab))
8435         {
8436           /* PR ld/16017: Generate thumb only PLT entries.  */
8437           if (!using_thumb2 (htab))
8438             {
8439               /* FIXME: We ought to be able to generate thumb-1 PLT
8440                  instructions...  */
8441               _bfd_error_handler (_("%B: Warning: thumb-1 mode PLT generation not currently supported"),
8442                                   output_bfd);
8443               return FALSE;
8444             }
8445
8446           /* Calculate the displacement between the PLT slot and the entry in
8447              the GOT.  The 12-byte offset accounts for the value produced by
8448              adding to pc in the 3rd instruction of the PLT stub.  */
8449           got_displacement = got_address - (plt_address + 12);
8450
8451           /* As we are using 32 bit instructions we have to use 'put_arm_insn'
8452              instead of 'put_thumb_insn'.  */
8453           put_arm_insn (htab, output_bfd,
8454                         elf32_thumb2_plt_entry[0]
8455                         | ((got_displacement & 0x000000ff) << 16)
8456                         | ((got_displacement & 0x00000700) << 20)
8457                         | ((got_displacement & 0x00000800) >>  1)
8458                         | ((got_displacement & 0x0000f000) >> 12),
8459                         ptr + 0);
8460           put_arm_insn (htab, output_bfd,
8461                         elf32_thumb2_plt_entry[1]
8462                         | ((got_displacement & 0x00ff0000)      )
8463                         | ((got_displacement & 0x07000000) <<  4)
8464                         | ((got_displacement & 0x08000000) >> 17)
8465                         | ((got_displacement & 0xf0000000) >> 28),
8466                         ptr + 4);
8467           put_arm_insn (htab, output_bfd,
8468                         elf32_thumb2_plt_entry[2],
8469                         ptr + 8);
8470           put_arm_insn (htab, output_bfd,
8471                         elf32_thumb2_plt_entry[3],
8472                         ptr + 12);
8473         }
8474       else
8475         {
8476           /* Calculate the displacement between the PLT slot and the
8477              entry in the GOT.  The eight-byte offset accounts for the
8478              value produced by adding to pc in the first instruction
8479              of the PLT stub.  */
8480           got_displacement = got_address - (plt_address + 8);
8481
8482           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
8483             {
8484               put_thumb_insn (htab, output_bfd,
8485                               elf32_arm_plt_thumb_stub[0], ptr - 4);
8486               put_thumb_insn (htab, output_bfd,
8487                               elf32_arm_plt_thumb_stub[1], ptr - 2);
8488             }
8489
8490           if (!elf32_arm_use_long_plt_entry)
8491             {
8492               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
8493
8494               put_arm_insn (htab, output_bfd,
8495                             elf32_arm_plt_entry_short[0]
8496                             | ((got_displacement & 0x0ff00000) >> 20),
8497                             ptr + 0);
8498               put_arm_insn (htab, output_bfd,
8499                             elf32_arm_plt_entry_short[1]
8500                             | ((got_displacement & 0x000ff000) >> 12),
8501                             ptr+ 4);
8502               put_arm_insn (htab, output_bfd,
8503                             elf32_arm_plt_entry_short[2]
8504                             | (got_displacement & 0x00000fff),
8505                             ptr + 8);
8506 #ifdef FOUR_WORD_PLT
8507               bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
8508 #endif
8509             }
8510           else
8511             {
8512               put_arm_insn (htab, output_bfd,
8513                             elf32_arm_plt_entry_long[0]
8514                             | ((got_displacement & 0xf0000000) >> 28),
8515                             ptr + 0);
8516               put_arm_insn (htab, output_bfd,
8517                             elf32_arm_plt_entry_long[1]
8518                             | ((got_displacement & 0x0ff00000) >> 20),
8519                             ptr + 4);
8520               put_arm_insn (htab, output_bfd,
8521                             elf32_arm_plt_entry_long[2]
8522                             | ((got_displacement & 0x000ff000) >> 12),
8523                             ptr+ 8);
8524               put_arm_insn (htab, output_bfd,
8525                             elf32_arm_plt_entry_long[3]
8526                             | (got_displacement & 0x00000fff),
8527                             ptr + 12);
8528             }
8529         }
8530
8531       /* Fill in the entry in the .rel(a).(i)plt section.  */
8532       rel.r_offset = got_address;
8533       rel.r_addend = 0;
8534       if (dynindx == -1)
8535         {
8536           /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
8537              The dynamic linker or static executable then calls SYM_VALUE
8538              to determine the correct run-time value of the .igot.plt entry.  */
8539           rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
8540           initial_got_entry = sym_value;
8541         }
8542       else
8543         {
8544           rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
8545           initial_got_entry = (splt->output_section->vma
8546                                + splt->output_offset);
8547         }
8548
8549       /* Fill in the entry in the global offset table.  */
8550       bfd_put_32 (output_bfd, initial_got_entry,
8551                   sgot->contents + got_offset);
8552     }
8553
8554   if (dynindx == -1)
8555     elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
8556   else
8557     {
8558       loc = srel->contents + plt_index * RELOC_SIZE (htab);
8559       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8560     }
8561
8562   return TRUE;
8563 }
8564
8565 /* Some relocations map to different relocations depending on the
8566    target.  Return the real relocation.  */
8567
8568 static int
8569 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
8570                      int r_type)
8571 {
8572   switch (r_type)
8573     {
8574     case R_ARM_TARGET1:
8575       if (globals->target1_is_rel)
8576         return R_ARM_REL32;
8577       else
8578         return R_ARM_ABS32;
8579
8580     case R_ARM_TARGET2:
8581       return globals->target2_reloc;
8582
8583     default:
8584       return r_type;
8585     }
8586 }
8587
8588 /* Return the base VMA address which should be subtracted from real addresses
8589    when resolving @dtpoff relocation.
8590    This is PT_TLS segment p_vaddr.  */
8591
8592 static bfd_vma
8593 dtpoff_base (struct bfd_link_info *info)
8594 {
8595   /* If tls_sec is NULL, we should have signalled an error already.  */
8596   if (elf_hash_table (info)->tls_sec == NULL)
8597     return 0;
8598   return elf_hash_table (info)->tls_sec->vma;
8599 }
8600
8601 /* Return the relocation value for @tpoff relocation
8602    if STT_TLS virtual address is ADDRESS.  */
8603
8604 static bfd_vma
8605 tpoff (struct bfd_link_info *info, bfd_vma address)
8606 {
8607   struct elf_link_hash_table *htab = elf_hash_table (info);
8608   bfd_vma base;
8609
8610   /* If tls_sec is NULL, we should have signalled an error already.  */
8611   if (htab->tls_sec == NULL)
8612     return 0;
8613   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
8614   return address - htab->tls_sec->vma + base;
8615 }
8616
8617 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
8618    VALUE is the relocation value.  */
8619
8620 static bfd_reloc_status_type
8621 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
8622 {
8623   if (value > 0xfff)
8624     return bfd_reloc_overflow;
8625
8626   value |= bfd_get_32 (abfd, data) & 0xfffff000;
8627   bfd_put_32 (abfd, value, data);
8628   return bfd_reloc_ok;
8629 }
8630
8631 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
8632    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
8633    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
8634
8635    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
8636    is to then call final_link_relocate.  Return other values in the
8637    case of error.
8638
8639    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
8640    the pre-relaxed code.  It would be nice if the relocs were updated
8641    to match the optimization.   */
8642
8643 static bfd_reloc_status_type
8644 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
8645                      bfd *input_bfd, asection *input_sec, bfd_byte *contents,
8646                      Elf_Internal_Rela *rel, unsigned long is_local)
8647 {
8648   unsigned long insn;
8649
8650   switch (ELF32_R_TYPE (rel->r_info))
8651     {
8652     default:
8653       return bfd_reloc_notsupported;
8654
8655     case R_ARM_TLS_GOTDESC:
8656       if (is_local)
8657         insn = 0;
8658       else
8659         {
8660           insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
8661           if (insn & 1)
8662             insn -= 5; /* THUMB */
8663           else
8664             insn -= 8; /* ARM */
8665         }
8666       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8667       return bfd_reloc_continue;
8668
8669     case R_ARM_THM_TLS_DESCSEQ:
8670       /* Thumb insn.  */
8671       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
8672       if ((insn & 0xff78) == 0x4478)      /* add rx, pc */
8673         {
8674           if (is_local)
8675             /* nop */
8676             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8677         }
8678       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
8679         {
8680           if (is_local)
8681             /* nop */
8682             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8683           else
8684             /* ldr rx,[ry] */
8685             bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
8686         }
8687       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
8688         {
8689           if (is_local)
8690             /* nop */
8691             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8692           else
8693             /* mov r0, rx */
8694             bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
8695                         contents + rel->r_offset);
8696         }
8697       else
8698         {
8699           if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
8700             /* It's a 32 bit instruction, fetch the rest of it for
8701                error generation.  */
8702             insn = (insn << 16)
8703               | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
8704           (*_bfd_error_handler)
8705             (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' in TLS trampoline"),
8706              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
8707           return bfd_reloc_notsupported;
8708         }
8709       break;
8710
8711     case R_ARM_TLS_DESCSEQ:
8712       /* arm insn.  */
8713       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
8714       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
8715         {
8716           if (is_local)
8717             /* mov rx, ry */
8718             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
8719                         contents + rel->r_offset);
8720         }
8721       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
8722         {
8723           if (is_local)
8724             /* nop */
8725             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
8726           else
8727             /* ldr rx,[ry] */
8728             bfd_put_32 (input_bfd, insn & 0xfffff000,
8729                         contents + rel->r_offset);
8730         }
8731       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
8732         {
8733           if (is_local)
8734             /* nop */
8735             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
8736           else
8737             /* mov r0, rx */
8738             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
8739                         contents + rel->r_offset);
8740         }
8741       else
8742         {
8743           (*_bfd_error_handler)
8744             (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' in TLS trampoline"),
8745              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
8746           return bfd_reloc_notsupported;
8747         }
8748       break;
8749
8750     case R_ARM_TLS_CALL:
8751       /* GD->IE relaxation, turn the instruction into 'nop' or
8752          'ldr r0, [pc,r0]'  */
8753       insn = is_local ? 0xe1a00000 : 0xe79f0000;
8754       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8755       break;
8756
8757     case R_ARM_THM_TLS_CALL:
8758       /* GD->IE relaxation.  */
8759       if (!is_local)
8760         /* add r0,pc; ldr r0, [r0]  */
8761         insn = 0x44786800;
8762       else if (arch_has_thumb2_nop (globals))
8763         /* nop.w */
8764         insn = 0xf3af8000;
8765       else
8766         /* nop; nop */
8767         insn = 0xbf00bf00;
8768
8769       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
8770       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
8771       break;
8772     }
8773   return bfd_reloc_ok;
8774 }
8775
8776 /* For a given value of n, calculate the value of G_n as required to
8777    deal with group relocations.  We return it in the form of an
8778    encoded constant-and-rotation, together with the final residual.  If n is
8779    specified as less than zero, then final_residual is filled with the
8780    input value and no further action is performed.  */
8781
8782 static bfd_vma
8783 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
8784 {
8785   int current_n;
8786   bfd_vma g_n;
8787   bfd_vma encoded_g_n = 0;
8788   bfd_vma residual = value; /* Also known as Y_n.  */
8789
8790   for (current_n = 0; current_n <= n; current_n++)
8791     {
8792       int shift;
8793
8794       /* Calculate which part of the value to mask.  */
8795       if (residual == 0)
8796         shift = 0;
8797       else
8798         {
8799           int msb;
8800
8801           /* Determine the most significant bit in the residual and
8802              align the resulting value to a 2-bit boundary.  */
8803           for (msb = 30; msb >= 0; msb -= 2)
8804             if (residual & (3 << msb))
8805               break;
8806
8807           /* The desired shift is now (msb - 6), or zero, whichever
8808              is the greater.  */
8809           shift = msb - 6;
8810           if (shift < 0)
8811             shift = 0;
8812         }
8813
8814       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
8815       g_n = residual & (0xff << shift);
8816       encoded_g_n = (g_n >> shift)
8817                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
8818
8819       /* Calculate the residual for the next time around.  */
8820       residual &= ~g_n;
8821     }
8822
8823   *final_residual = residual;
8824
8825   return encoded_g_n;
8826 }
8827
8828 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
8829    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
8830
8831 static int
8832 identify_add_or_sub (bfd_vma insn)
8833 {
8834   int opcode = insn & 0x1e00000;
8835
8836   if (opcode == 1 << 23) /* ADD */
8837     return 1;
8838
8839   if (opcode == 1 << 22) /* SUB */
8840     return -1;
8841
8842   return 0;
8843 }
8844
8845 /* Perform a relocation as part of a final link.  */
8846
8847 static bfd_reloc_status_type
8848 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
8849                                bfd *                        input_bfd,
8850                                bfd *                        output_bfd,
8851                                asection *                   input_section,
8852                                bfd_byte *                   contents,
8853                                Elf_Internal_Rela *          rel,
8854                                bfd_vma                      value,
8855                                struct bfd_link_info *       info,
8856                                asection *                   sym_sec,
8857                                const char *                 sym_name,
8858                                unsigned char                st_type,
8859                                enum arm_st_branch_type      branch_type,
8860                                struct elf_link_hash_entry * h,
8861                                bfd_boolean *                unresolved_reloc_p,
8862                                char **                      error_message)
8863 {
8864   unsigned long                 r_type = howto->type;
8865   unsigned long                 r_symndx;
8866   bfd_byte *                    hit_data = contents + rel->r_offset;
8867   bfd_vma *                     local_got_offsets;
8868   bfd_vma *                     local_tlsdesc_gotents;
8869   asection *                    sgot;
8870   asection *                    splt;
8871   asection *                    sreloc = NULL;
8872   asection *                    srelgot;
8873   bfd_vma                       addend;
8874   bfd_signed_vma                signed_addend;
8875   unsigned char                 dynreloc_st_type;
8876   bfd_vma                       dynreloc_value;
8877   struct elf32_arm_link_hash_table * globals;
8878   struct elf32_arm_link_hash_entry *eh;
8879   union gotplt_union           *root_plt;
8880   struct arm_plt_info          *arm_plt;
8881   bfd_vma                       plt_offset;
8882   bfd_vma                       gotplt_offset;
8883   bfd_boolean                   has_iplt_entry;
8884
8885   globals = elf32_arm_hash_table (info);
8886   if (globals == NULL)
8887     return bfd_reloc_notsupported;
8888
8889   BFD_ASSERT (is_arm_elf (input_bfd));
8890
8891   /* Some relocation types map to different relocations depending on the
8892      target.  We pick the right one here.  */
8893   r_type = arm_real_reloc_type (globals, r_type);
8894
8895   /* It is possible to have linker relaxations on some TLS access
8896      models.  Update our information here.  */
8897   r_type = elf32_arm_tls_transition (info, r_type, h);
8898
8899   if (r_type != howto->type)
8900     howto = elf32_arm_howto_from_type (r_type);
8901
8902   eh = (struct elf32_arm_link_hash_entry *) h;
8903   sgot = globals->root.sgot;
8904   local_got_offsets = elf_local_got_offsets (input_bfd);
8905   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
8906
8907   if (globals->root.dynamic_sections_created)
8908     srelgot = globals->root.srelgot;
8909   else
8910     srelgot = NULL;
8911
8912   r_symndx = ELF32_R_SYM (rel->r_info);
8913
8914   if (globals->use_rel)
8915     {
8916       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
8917
8918       if (addend & ((howto->src_mask + 1) >> 1))
8919         {
8920           signed_addend = -1;
8921           signed_addend &= ~ howto->src_mask;
8922           signed_addend |= addend;
8923         }
8924       else
8925         signed_addend = addend;
8926     }
8927   else
8928     addend = signed_addend = rel->r_addend;
8929
8930   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
8931      are resolving a function call relocation.  */
8932   if (using_thumb_only (globals)
8933       && (r_type == R_ARM_THM_CALL
8934           || r_type == R_ARM_THM_JUMP24)
8935       && branch_type == ST_BRANCH_TO_ARM)
8936     branch_type = ST_BRANCH_TO_THUMB;
8937
8938   /* Record the symbol information that should be used in dynamic
8939      relocations.  */
8940   dynreloc_st_type = st_type;
8941   dynreloc_value = value;
8942   if (branch_type == ST_BRANCH_TO_THUMB)
8943     dynreloc_value |= 1;
8944
8945   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
8946      VALUE appropriately for relocations that we resolve at link time.  */
8947   has_iplt_entry = FALSE;
8948   if (elf32_arm_get_plt_info (input_bfd, eh, r_symndx, &root_plt, &arm_plt)
8949       && root_plt->offset != (bfd_vma) -1)
8950     {
8951       plt_offset = root_plt->offset;
8952       gotplt_offset = arm_plt->got_offset;
8953
8954       if (h == NULL || eh->is_iplt)
8955         {
8956           has_iplt_entry = TRUE;
8957           splt = globals->root.iplt;
8958
8959           /* Populate .iplt entries here, because not all of them will
8960              be seen by finish_dynamic_symbol.  The lower bit is set if
8961              we have already populated the entry.  */
8962           if (plt_offset & 1)
8963             plt_offset--;
8964           else
8965             {
8966               if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
8967                                                 -1, dynreloc_value))
8968                 root_plt->offset |= 1;
8969               else
8970                 return bfd_reloc_notsupported;
8971             }
8972
8973           /* Static relocations always resolve to the .iplt entry.  */
8974           st_type = STT_FUNC;
8975           value = (splt->output_section->vma
8976                    + splt->output_offset
8977                    + plt_offset);
8978           branch_type = ST_BRANCH_TO_ARM;
8979
8980           /* If there are non-call relocations that resolve to the .iplt
8981              entry, then all dynamic ones must too.  */
8982           if (arm_plt->noncall_refcount != 0)
8983             {
8984               dynreloc_st_type = st_type;
8985               dynreloc_value = value;
8986             }
8987         }
8988       else
8989         /* We populate the .plt entry in finish_dynamic_symbol.  */
8990         splt = globals->root.splt;
8991     }
8992   else
8993     {
8994       splt = NULL;
8995       plt_offset = (bfd_vma) -1;
8996       gotplt_offset = (bfd_vma) -1;
8997     }
8998
8999   switch (r_type)
9000     {
9001     case R_ARM_NONE:
9002       /* We don't need to find a value for this symbol.  It's just a
9003          marker.  */
9004       *unresolved_reloc_p = FALSE;
9005       return bfd_reloc_ok;
9006
9007     case R_ARM_ABS12:
9008       if (!globals->vxworks_p)
9009         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
9010
9011     case R_ARM_PC24:
9012     case R_ARM_ABS32:
9013     case R_ARM_ABS32_NOI:
9014     case R_ARM_REL32:
9015     case R_ARM_REL32_NOI:
9016     case R_ARM_CALL:
9017     case R_ARM_JUMP24:
9018     case R_ARM_XPC25:
9019     case R_ARM_PREL31:
9020     case R_ARM_PLT32:
9021       /* Handle relocations which should use the PLT entry.  ABS32/REL32
9022          will use the symbol's value, which may point to a PLT entry, but we
9023          don't need to handle that here.  If we created a PLT entry, all
9024          branches in this object should go to it, except if the PLT is too
9025          far away, in which case a long branch stub should be inserted.  */
9026       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
9027            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
9028            && r_type != R_ARM_CALL
9029            && r_type != R_ARM_JUMP24
9030            && r_type != R_ARM_PLT32)
9031           && plt_offset != (bfd_vma) -1)
9032         {
9033           /* If we've created a .plt section, and assigned a PLT entry
9034              to this function, it must either be a STT_GNU_IFUNC reference
9035              or not be known to bind locally.  In other cases, we should
9036              have cleared the PLT entry by now.  */
9037           BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
9038
9039           value = (splt->output_section->vma
9040                    + splt->output_offset
9041                    + plt_offset);
9042           *unresolved_reloc_p = FALSE;
9043           return _bfd_final_link_relocate (howto, input_bfd, input_section,
9044                                            contents, rel->r_offset, value,
9045                                            rel->r_addend);
9046         }
9047
9048       /* When generating a shared object or relocatable executable, these
9049          relocations are copied into the output file to be resolved at
9050          run time.  */
9051       if ((bfd_link_pic (info)
9052            || globals->root.is_relocatable_executable)
9053           && (input_section->flags & SEC_ALLOC)
9054           && !(globals->vxworks_p
9055                && strcmp (input_section->output_section->name,
9056                           ".tls_vars") == 0)
9057           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
9058               || !SYMBOL_CALLS_LOCAL (info, h))
9059           && !(input_bfd == globals->stub_bfd
9060                && strstr (input_section->name, STUB_SUFFIX))
9061           && (h == NULL
9062               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9063               || h->root.type != bfd_link_hash_undefweak)
9064           && r_type != R_ARM_PC24
9065           && r_type != R_ARM_CALL
9066           && r_type != R_ARM_JUMP24
9067           && r_type != R_ARM_PREL31
9068           && r_type != R_ARM_PLT32)
9069         {
9070           Elf_Internal_Rela outrel;
9071           bfd_boolean skip, relocate;
9072
9073           if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
9074               && !h->def_regular)
9075             {
9076               char *v = _("shared object");
9077
9078               if (bfd_link_executable (info))
9079                 v = _("PIE executable");
9080
9081               (*_bfd_error_handler)
9082                 (_("%B: relocation %s against external or undefined symbol `%s'"
9083                    " can not be used when making a %s; recompile with -fPIC"), input_bfd,
9084                  elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
9085               return bfd_reloc_notsupported;
9086             }
9087
9088           *unresolved_reloc_p = FALSE;
9089
9090           if (sreloc == NULL && globals->root.dynamic_sections_created)
9091             {
9092               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
9093                                                            ! globals->use_rel);
9094
9095               if (sreloc == NULL)
9096                 return bfd_reloc_notsupported;
9097             }
9098
9099           skip = FALSE;
9100           relocate = FALSE;
9101
9102           outrel.r_addend = addend;
9103           outrel.r_offset =
9104             _bfd_elf_section_offset (output_bfd, info, input_section,
9105                                      rel->r_offset);
9106           if (outrel.r_offset == (bfd_vma) -1)
9107             skip = TRUE;
9108           else if (outrel.r_offset == (bfd_vma) -2)
9109             skip = TRUE, relocate = TRUE;
9110           outrel.r_offset += (input_section->output_section->vma
9111                               + input_section->output_offset);
9112
9113           if (skip)
9114             memset (&outrel, 0, sizeof outrel);
9115           else if (h != NULL
9116                    && h->dynindx != -1
9117                    && (!bfd_link_pic (info)
9118                        || !SYMBOLIC_BIND (info, h)
9119                        || !h->def_regular))
9120             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
9121           else
9122             {
9123               int symbol;
9124
9125               /* This symbol is local, or marked to become local.  */
9126               BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI);
9127               if (globals->symbian_p)
9128                 {
9129                   asection *osec;
9130
9131                   /* On Symbian OS, the data segment and text segement
9132                      can be relocated independently.  Therefore, we
9133                      must indicate the segment to which this
9134                      relocation is relative.  The BPABI allows us to
9135                      use any symbol in the right segment; we just use
9136                      the section symbol as it is convenient.  (We
9137                      cannot use the symbol given by "h" directly as it
9138                      will not appear in the dynamic symbol table.)
9139
9140                      Note that the dynamic linker ignores the section
9141                      symbol value, so we don't subtract osec->vma
9142                      from the emitted reloc addend.  */
9143                   if (sym_sec)
9144                     osec = sym_sec->output_section;
9145                   else
9146                     osec = input_section->output_section;
9147                   symbol = elf_section_data (osec)->dynindx;
9148                   if (symbol == 0)
9149                     {
9150                       struct elf_link_hash_table *htab = elf_hash_table (info);
9151
9152                       if ((osec->flags & SEC_READONLY) == 0
9153                           && htab->data_index_section != NULL)
9154                         osec = htab->data_index_section;
9155                       else
9156                         osec = htab->text_index_section;
9157                       symbol = elf_section_data (osec)->dynindx;
9158                     }
9159                   BFD_ASSERT (symbol != 0);
9160                 }
9161               else
9162                 /* On SVR4-ish systems, the dynamic loader cannot
9163                    relocate the text and data segments independently,
9164                    so the symbol does not matter.  */
9165                 symbol = 0;
9166               if (dynreloc_st_type == STT_GNU_IFUNC)
9167                 /* We have an STT_GNU_IFUNC symbol that doesn't resolve
9168                    to the .iplt entry.  Instead, every non-call reference
9169                    must use an R_ARM_IRELATIVE relocation to obtain the
9170                    correct run-time address.  */
9171                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
9172               else
9173                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
9174               if (globals->use_rel)
9175                 relocate = TRUE;
9176               else
9177                 outrel.r_addend += dynreloc_value;
9178             }
9179
9180           elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
9181
9182           /* If this reloc is against an external symbol, we do not want to
9183              fiddle with the addend.  Otherwise, we need to include the symbol
9184              value so that it becomes an addend for the dynamic reloc.  */
9185           if (! relocate)
9186             return bfd_reloc_ok;
9187
9188           return _bfd_final_link_relocate (howto, input_bfd, input_section,
9189                                            contents, rel->r_offset,
9190                                            dynreloc_value, (bfd_vma) 0);
9191         }
9192       else switch (r_type)
9193         {
9194         case R_ARM_ABS12:
9195           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
9196
9197         case R_ARM_XPC25:         /* Arm BLX instruction.  */
9198         case R_ARM_CALL:
9199         case R_ARM_JUMP24:
9200         case R_ARM_PC24:          /* Arm B/BL instruction.  */
9201         case R_ARM_PLT32:
9202           {
9203           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
9204
9205           if (r_type == R_ARM_XPC25)
9206             {
9207               /* Check for Arm calling Arm function.  */
9208               /* FIXME: Should we translate the instruction into a BL
9209                  instruction instead ?  */
9210               if (branch_type != ST_BRANCH_TO_THUMB)
9211                 (*_bfd_error_handler)
9212                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
9213                    input_bfd,
9214                    h ? h->root.root.string : "(local)");
9215             }
9216           else if (r_type == R_ARM_PC24)
9217             {
9218               /* Check for Arm calling Thumb function.  */
9219               if (branch_type == ST_BRANCH_TO_THUMB)
9220                 {
9221                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
9222                                                output_bfd, input_section,
9223                                                hit_data, sym_sec, rel->r_offset,
9224                                                signed_addend, value,
9225                                                error_message))
9226                     return bfd_reloc_ok;
9227                   else
9228                     return bfd_reloc_dangerous;
9229                 }
9230             }
9231
9232           /* Check if a stub has to be inserted because the
9233              destination is too far or we are changing mode.  */
9234           if (   r_type == R_ARM_CALL
9235               || r_type == R_ARM_JUMP24
9236               || r_type == R_ARM_PLT32)
9237             {
9238               enum elf32_arm_stub_type stub_type = arm_stub_none;
9239               struct elf32_arm_link_hash_entry *hash;
9240
9241               hash = (struct elf32_arm_link_hash_entry *) h;
9242               stub_type = arm_type_of_stub (info, input_section, rel,
9243                                             st_type, &branch_type,
9244                                             hash, value, sym_sec,
9245                                             input_bfd, sym_name);
9246
9247               if (stub_type != arm_stub_none)
9248                 {
9249                   /* The target is out of reach, so redirect the
9250                      branch to the local stub for this function.  */
9251                   stub_entry = elf32_arm_get_stub_entry (input_section,
9252                                                          sym_sec, h,
9253                                                          rel, globals,
9254                                                          stub_type);
9255                   {
9256                     if (stub_entry != NULL)
9257                       value = (stub_entry->stub_offset
9258                                + stub_entry->stub_sec->output_offset
9259                                + stub_entry->stub_sec->output_section->vma);
9260
9261                     if (plt_offset != (bfd_vma) -1)
9262                       *unresolved_reloc_p = FALSE;
9263                   }
9264                 }
9265               else
9266                 {
9267                   /* If the call goes through a PLT entry, make sure to
9268                      check distance to the right destination address.  */
9269                   if (plt_offset != (bfd_vma) -1)
9270                     {
9271                       value = (splt->output_section->vma
9272                                + splt->output_offset
9273                                + plt_offset);
9274                       *unresolved_reloc_p = FALSE;
9275                       /* The PLT entry is in ARM mode, regardless of the
9276                          target function.  */
9277                       branch_type = ST_BRANCH_TO_ARM;
9278                     }
9279                 }
9280             }
9281
9282           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
9283              where:
9284               S is the address of the symbol in the relocation.
9285               P is address of the instruction being relocated.
9286               A is the addend (extracted from the instruction) in bytes.
9287
9288              S is held in 'value'.
9289              P is the base address of the section containing the
9290                instruction plus the offset of the reloc into that
9291                section, ie:
9292                  (input_section->output_section->vma +
9293                   input_section->output_offset +
9294                   rel->r_offset).
9295              A is the addend, converted into bytes, ie:
9296                  (signed_addend * 4)
9297
9298              Note: None of these operations have knowledge of the pipeline
9299              size of the processor, thus it is up to the assembler to
9300              encode this information into the addend.  */
9301           value -= (input_section->output_section->vma
9302                     + input_section->output_offset);
9303           value -= rel->r_offset;
9304           if (globals->use_rel)
9305             value += (signed_addend << howto->size);
9306           else
9307             /* RELA addends do not have to be adjusted by howto->size.  */
9308             value += signed_addend;
9309
9310           signed_addend = value;
9311           signed_addend >>= howto->rightshift;
9312
9313           /* A branch to an undefined weak symbol is turned into a jump to
9314              the next instruction unless a PLT entry will be created.
9315              Do the same for local undefined symbols (but not for STN_UNDEF).
9316              The jump to the next instruction is optimized as a NOP depending
9317              on the architecture.  */
9318           if (h ? (h->root.type == bfd_link_hash_undefweak
9319                    && plt_offset == (bfd_vma) -1)
9320               : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
9321             {
9322               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
9323
9324               if (arch_has_arm_nop (globals))
9325                 value |= 0x0320f000;
9326               else
9327                 value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
9328             }
9329           else
9330             {
9331               /* Perform a signed range check.  */
9332               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
9333                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
9334                 return bfd_reloc_overflow;
9335
9336               addend = (value & 2);
9337
9338               value = (signed_addend & howto->dst_mask)
9339                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
9340
9341               if (r_type == R_ARM_CALL)
9342                 {
9343                   /* Set the H bit in the BLX instruction.  */
9344                   if (branch_type == ST_BRANCH_TO_THUMB)
9345                     {
9346                       if (addend)
9347                         value |= (1 << 24);
9348                       else
9349                         value &= ~(bfd_vma)(1 << 24);
9350                     }
9351
9352                   /* Select the correct instruction (BL or BLX).  */
9353                   /* Only if we are not handling a BL to a stub. In this
9354                      case, mode switching is performed by the stub.  */
9355                   if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
9356                     value |= (1 << 28);
9357                   else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
9358                     {
9359                       value &= ~(bfd_vma)(1 << 28);
9360                       value |= (1 << 24);
9361                     }
9362                 }
9363             }
9364           }
9365           break;
9366
9367         case R_ARM_ABS32:
9368           value += addend;
9369           if (branch_type == ST_BRANCH_TO_THUMB)
9370             value |= 1;
9371           break;
9372
9373         case R_ARM_ABS32_NOI:
9374           value += addend;
9375           break;
9376
9377         case R_ARM_REL32:
9378           value += addend;
9379           if (branch_type == ST_BRANCH_TO_THUMB)
9380             value |= 1;
9381           value -= (input_section->output_section->vma
9382                     + input_section->output_offset + rel->r_offset);
9383           break;
9384
9385         case R_ARM_REL32_NOI:
9386           value += addend;
9387           value -= (input_section->output_section->vma
9388                     + input_section->output_offset + rel->r_offset);
9389           break;
9390
9391         case R_ARM_PREL31:
9392           value -= (input_section->output_section->vma
9393                     + input_section->output_offset + rel->r_offset);
9394           value += signed_addend;
9395           if (! h || h->root.type != bfd_link_hash_undefweak)
9396             {
9397               /* Check for overflow.  */
9398               if ((value ^ (value >> 1)) & (1 << 30))
9399                 return bfd_reloc_overflow;
9400             }
9401           value &= 0x7fffffff;
9402           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
9403           if (branch_type == ST_BRANCH_TO_THUMB)
9404             value |= 1;
9405           break;
9406         }
9407
9408       bfd_put_32 (input_bfd, value, hit_data);
9409       return bfd_reloc_ok;
9410
9411     case R_ARM_ABS8:
9412       /* PR 16202: Refectch the addend using the correct size.  */
9413       if (globals->use_rel)
9414         addend = bfd_get_8 (input_bfd, hit_data);
9415       value += addend;
9416
9417       /* There is no way to tell whether the user intended to use a signed or
9418          unsigned addend.  When checking for overflow we accept either,
9419          as specified by the AAELF.  */
9420       if ((long) value > 0xff || (long) value < -0x80)
9421         return bfd_reloc_overflow;
9422
9423       bfd_put_8 (input_bfd, value, hit_data);
9424       return bfd_reloc_ok;
9425
9426     case R_ARM_ABS16:
9427       /* PR 16202: Refectch the addend using the correct size.  */
9428       if (globals->use_rel)
9429         addend = bfd_get_16 (input_bfd, hit_data);
9430       value += addend;
9431
9432       /* See comment for R_ARM_ABS8.  */
9433       if ((long) value > 0xffff || (long) value < -0x8000)
9434         return bfd_reloc_overflow;
9435
9436       bfd_put_16 (input_bfd, value, hit_data);
9437       return bfd_reloc_ok;
9438
9439     case R_ARM_THM_ABS5:
9440       /* Support ldr and str instructions for the thumb.  */
9441       if (globals->use_rel)
9442         {
9443           /* Need to refetch addend.  */
9444           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
9445           /* ??? Need to determine shift amount from operand size.  */
9446           addend >>= howto->rightshift;
9447         }
9448       value += addend;
9449
9450       /* ??? Isn't value unsigned?  */
9451       if ((long) value > 0x1f || (long) value < -0x10)
9452         return bfd_reloc_overflow;
9453
9454       /* ??? Value needs to be properly shifted into place first.  */
9455       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
9456       bfd_put_16 (input_bfd, value, hit_data);
9457       return bfd_reloc_ok;
9458
9459     case R_ARM_THM_ALU_PREL_11_0:
9460       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
9461       {
9462         bfd_vma insn;
9463         bfd_signed_vma relocation;
9464
9465         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
9466              | bfd_get_16 (input_bfd, hit_data + 2);
9467
9468         if (globals->use_rel)
9469           {
9470             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
9471                           | ((insn & (1 << 26)) >> 15);
9472             if (insn & 0xf00000)
9473               signed_addend = -signed_addend;
9474           }
9475
9476         relocation = value + signed_addend;
9477         relocation -= Pa (input_section->output_section->vma
9478                           + input_section->output_offset
9479                           + rel->r_offset);
9480
9481         value = relocation;
9482
9483         if (value >= 0x1000)
9484           return bfd_reloc_overflow;
9485
9486         insn = (insn & 0xfb0f8f00) | (value & 0xff)
9487              | ((value & 0x700) << 4)
9488              | ((value & 0x800) << 15);
9489         if (relocation < 0)
9490           insn |= 0xa00000;
9491
9492         bfd_put_16 (input_bfd, insn >> 16, hit_data);
9493         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
9494
9495         return bfd_reloc_ok;
9496       }
9497
9498     case R_ARM_THM_PC8:
9499       /* PR 10073:  This reloc is not generated by the GNU toolchain,
9500          but it is supported for compatibility with third party libraries
9501          generated by other compilers, specifically the ARM/IAR.  */
9502       {
9503         bfd_vma insn;
9504         bfd_signed_vma relocation;
9505
9506         insn = bfd_get_16 (input_bfd, hit_data);
9507
9508         if (globals->use_rel)
9509           addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
9510
9511         relocation = value + addend;
9512         relocation -= Pa (input_section->output_section->vma
9513                           + input_section->output_offset
9514                           + rel->r_offset);
9515
9516         value = relocation;
9517
9518         /* We do not check for overflow of this reloc.  Although strictly
9519            speaking this is incorrect, it appears to be necessary in order
9520            to work with IAR generated relocs.  Since GCC and GAS do not
9521            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
9522            a problem for them.  */
9523         value &= 0x3fc;
9524
9525         insn = (insn & 0xff00) | (value >> 2);
9526
9527         bfd_put_16 (input_bfd, insn, hit_data);
9528
9529         return bfd_reloc_ok;
9530       }
9531
9532     case R_ARM_THM_PC12:
9533       /* Corresponds to: ldr.w reg, [pc, #offset].  */
9534       {
9535         bfd_vma insn;
9536         bfd_signed_vma relocation;
9537
9538         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
9539              | bfd_get_16 (input_bfd, hit_data + 2);
9540
9541         if (globals->use_rel)
9542           {
9543             signed_addend = insn & 0xfff;
9544             if (!(insn & (1 << 23)))
9545               signed_addend = -signed_addend;
9546           }
9547
9548         relocation = value + signed_addend;
9549         relocation -= Pa (input_section->output_section->vma
9550                           + input_section->output_offset
9551                           + rel->r_offset);
9552
9553         value = relocation;
9554
9555         if (value >= 0x1000)
9556           return bfd_reloc_overflow;
9557
9558         insn = (insn & 0xff7ff000) | value;
9559         if (relocation >= 0)
9560           insn |= (1 << 23);
9561
9562         bfd_put_16 (input_bfd, insn >> 16, hit_data);
9563         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
9564
9565         return bfd_reloc_ok;
9566       }
9567
9568     case R_ARM_THM_XPC22:
9569     case R_ARM_THM_CALL:
9570     case R_ARM_THM_JUMP24:
9571       /* Thumb BL (branch long instruction).  */
9572       {
9573         bfd_vma relocation;
9574         bfd_vma reloc_sign;
9575         bfd_boolean overflow = FALSE;
9576         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
9577         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
9578         bfd_signed_vma reloc_signed_max;
9579         bfd_signed_vma reloc_signed_min;
9580         bfd_vma check;
9581         bfd_signed_vma signed_check;
9582         int bitsize;
9583         const int thumb2 = using_thumb2 (globals);
9584
9585         /* A branch to an undefined weak symbol is turned into a jump to
9586            the next instruction unless a PLT entry will be created.
9587            The jump to the next instruction is optimized as a NOP.W for
9588            Thumb-2 enabled architectures.  */
9589         if (h && h->root.type == bfd_link_hash_undefweak
9590             && plt_offset == (bfd_vma) -1)
9591           {
9592             if (arch_has_thumb2_nop (globals))
9593               {
9594                 bfd_put_16 (input_bfd, 0xf3af, hit_data);
9595                 bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
9596               }
9597             else
9598               {
9599                 bfd_put_16 (input_bfd, 0xe000, hit_data);
9600                 bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
9601               }
9602             return bfd_reloc_ok;
9603           }
9604
9605         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
9606            with Thumb-1) involving the J1 and J2 bits.  */
9607         if (globals->use_rel)
9608           {
9609             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
9610             bfd_vma upper = upper_insn & 0x3ff;
9611             bfd_vma lower = lower_insn & 0x7ff;
9612             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
9613             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
9614             bfd_vma i1 = j1 ^ s ? 0 : 1;
9615             bfd_vma i2 = j2 ^ s ? 0 : 1;
9616
9617             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
9618             /* Sign extend.  */
9619             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
9620
9621             signed_addend = addend;
9622           }
9623
9624         if (r_type == R_ARM_THM_XPC22)
9625           {
9626             /* Check for Thumb to Thumb call.  */
9627             /* FIXME: Should we translate the instruction into a BL
9628                instruction instead ?  */
9629             if (branch_type == ST_BRANCH_TO_THUMB)
9630               (*_bfd_error_handler)
9631                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
9632                  input_bfd,
9633                  h ? h->root.root.string : "(local)");
9634           }
9635         else
9636           {
9637             /* If it is not a call to Thumb, assume call to Arm.
9638                If it is a call relative to a section name, then it is not a
9639                function call at all, but rather a long jump.  Calls through
9640                the PLT do not require stubs.  */
9641             if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
9642               {
9643                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
9644                   {
9645                     /* Convert BL to BLX.  */
9646                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
9647                   }
9648                 else if ((   r_type != R_ARM_THM_CALL)
9649                          && (r_type != R_ARM_THM_JUMP24))
9650                   {
9651                     if (elf32_thumb_to_arm_stub
9652                         (info, sym_name, input_bfd, output_bfd, input_section,
9653                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
9654                          error_message))
9655                       return bfd_reloc_ok;
9656                     else
9657                       return bfd_reloc_dangerous;
9658                   }
9659               }
9660             else if (branch_type == ST_BRANCH_TO_THUMB
9661                      && globals->use_blx
9662                      && r_type == R_ARM_THM_CALL)
9663               {
9664                 /* Make sure this is a BL.  */
9665                 lower_insn |= 0x1800;
9666               }
9667           }
9668
9669         enum elf32_arm_stub_type stub_type = arm_stub_none;
9670         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
9671           {
9672             /* Check if a stub has to be inserted because the destination
9673                is too far.  */
9674             struct elf32_arm_stub_hash_entry *stub_entry;
9675             struct elf32_arm_link_hash_entry *hash;
9676
9677             hash = (struct elf32_arm_link_hash_entry *) h;
9678
9679             stub_type = arm_type_of_stub (info, input_section, rel,
9680                                           st_type, &branch_type,
9681                                           hash, value, sym_sec,
9682                                           input_bfd, sym_name);
9683
9684             if (stub_type != arm_stub_none)
9685               {
9686                 /* The target is out of reach or we are changing modes, so
9687                    redirect the branch to the local stub for this
9688                    function.  */
9689                 stub_entry = elf32_arm_get_stub_entry (input_section,
9690                                                        sym_sec, h,
9691                                                        rel, globals,
9692                                                        stub_type);
9693                 if (stub_entry != NULL)
9694                   {
9695                     value = (stub_entry->stub_offset
9696                              + stub_entry->stub_sec->output_offset
9697                              + stub_entry->stub_sec->output_section->vma);
9698
9699                     if (plt_offset != (bfd_vma) -1)
9700                       *unresolved_reloc_p = FALSE;
9701                   }
9702
9703                 /* If this call becomes a call to Arm, force BLX.  */
9704                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
9705                   {
9706                     if ((stub_entry
9707                          && !arm_stub_is_thumb (stub_entry->stub_type))
9708                         || branch_type != ST_BRANCH_TO_THUMB)
9709                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
9710                   }
9711               }
9712           }
9713
9714         /* Handle calls via the PLT.  */
9715         if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
9716           {
9717             value = (splt->output_section->vma
9718                      + splt->output_offset
9719                      + plt_offset);
9720
9721             if (globals->use_blx
9722                 && r_type == R_ARM_THM_CALL
9723                 && ! using_thumb_only (globals))
9724               {
9725                 /* If the Thumb BLX instruction is available, convert
9726                    the BL to a BLX instruction to call the ARM-mode
9727                    PLT entry.  */
9728                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
9729                 branch_type = ST_BRANCH_TO_ARM;
9730               }
9731             else
9732               {
9733                 if (! using_thumb_only (globals))
9734                   /* Target the Thumb stub before the ARM PLT entry.  */
9735                   value -= PLT_THUMB_STUB_SIZE;
9736                 branch_type = ST_BRANCH_TO_THUMB;
9737               }
9738             *unresolved_reloc_p = FALSE;
9739           }
9740
9741         relocation = value + signed_addend;
9742
9743         relocation -= (input_section->output_section->vma
9744                        + input_section->output_offset
9745                        + rel->r_offset);
9746
9747         check = relocation >> howto->rightshift;
9748
9749         /* If this is a signed value, the rightshift just dropped
9750            leading 1 bits (assuming twos complement).  */
9751         if ((bfd_signed_vma) relocation >= 0)
9752           signed_check = check;
9753         else
9754           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
9755
9756         /* Calculate the permissable maximum and minimum values for
9757            this relocation according to whether we're relocating for
9758            Thumb-2 or not.  */
9759         bitsize = howto->bitsize;
9760         if (!thumb2)
9761           bitsize -= 2;
9762         reloc_signed_max = (1 << (bitsize - 1)) - 1;
9763         reloc_signed_min = ~reloc_signed_max;
9764
9765         /* Assumes two's complement.  */
9766         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9767           overflow = TRUE;
9768
9769         if ((lower_insn & 0x5000) == 0x4000)
9770           /* For a BLX instruction, make sure that the relocation is rounded up
9771              to a word boundary.  This follows the semantics of the instruction
9772              which specifies that bit 1 of the target address will come from bit
9773              1 of the base address.  */
9774           relocation = (relocation + 2) & ~ 3;
9775
9776         /* Put RELOCATION back into the insn.  Assumes two's complement.
9777            We use the Thumb-2 encoding, which is safe even if dealing with
9778            a Thumb-1 instruction by virtue of our overflow check above.  */
9779         reloc_sign = (signed_check < 0) ? 1 : 0;
9780         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
9781                      | ((relocation >> 12) & 0x3ff)
9782                      | (reloc_sign << 10);
9783         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
9784                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
9785                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
9786                      | ((relocation >> 1) & 0x7ff);
9787
9788         /* Put the relocated value back in the object file:  */
9789         bfd_put_16 (input_bfd, upper_insn, hit_data);
9790         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9791
9792         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
9793       }
9794       break;
9795
9796     case R_ARM_THM_JUMP19:
9797       /* Thumb32 conditional branch instruction.  */
9798       {
9799         bfd_vma relocation;
9800         bfd_boolean overflow = FALSE;
9801         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
9802         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
9803         bfd_signed_vma reloc_signed_max = 0xffffe;
9804         bfd_signed_vma reloc_signed_min = -0x100000;
9805         bfd_signed_vma signed_check;
9806         enum elf32_arm_stub_type stub_type = arm_stub_none;
9807         struct elf32_arm_stub_hash_entry *stub_entry;
9808         struct elf32_arm_link_hash_entry *hash;
9809
9810         /* Need to refetch the addend, reconstruct the top three bits,
9811            and squish the two 11 bit pieces together.  */
9812         if (globals->use_rel)
9813           {
9814             bfd_vma S     = (upper_insn & 0x0400) >> 10;
9815             bfd_vma upper = (upper_insn & 0x003f);
9816             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
9817             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
9818             bfd_vma lower = (lower_insn & 0x07ff);
9819
9820             upper |= J1 << 6;
9821             upper |= J2 << 7;
9822             upper |= (!S) << 8;
9823             upper -= 0x0100; /* Sign extend.  */
9824
9825             addend = (upper << 12) | (lower << 1);
9826             signed_addend = addend;
9827           }
9828
9829         /* Handle calls via the PLT.  */
9830         if (plt_offset != (bfd_vma) -1)
9831           {
9832             value = (splt->output_section->vma
9833                      + splt->output_offset
9834                      + plt_offset);
9835             /* Target the Thumb stub before the ARM PLT entry.  */
9836             value -= PLT_THUMB_STUB_SIZE;
9837             *unresolved_reloc_p = FALSE;
9838           }
9839
9840         hash = (struct elf32_arm_link_hash_entry *)h;
9841
9842         stub_type = arm_type_of_stub (info, input_section, rel,
9843                                       st_type, &branch_type,
9844                                       hash, value, sym_sec,
9845                                       input_bfd, sym_name);
9846         if (stub_type != arm_stub_none)
9847           {
9848             stub_entry = elf32_arm_get_stub_entry (input_section,
9849                                                    sym_sec, h,
9850                                                    rel, globals,
9851                                                    stub_type);
9852             if (stub_entry != NULL)
9853               {
9854                 value = (stub_entry->stub_offset
9855                         + stub_entry->stub_sec->output_offset
9856                         + stub_entry->stub_sec->output_section->vma);
9857               }
9858           }
9859
9860         relocation = value + signed_addend;
9861         relocation -= (input_section->output_section->vma
9862                        + input_section->output_offset
9863                        + rel->r_offset);
9864         signed_check = (bfd_signed_vma) relocation;
9865
9866         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9867           overflow = TRUE;
9868
9869         /* Put RELOCATION back into the insn.  */
9870         {
9871           bfd_vma S  = (relocation & 0x00100000) >> 20;
9872           bfd_vma J2 = (relocation & 0x00080000) >> 19;
9873           bfd_vma J1 = (relocation & 0x00040000) >> 18;
9874           bfd_vma hi = (relocation & 0x0003f000) >> 12;
9875           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
9876
9877           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
9878           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
9879         }
9880
9881         /* Put the relocated value back in the object file:  */
9882         bfd_put_16 (input_bfd, upper_insn, hit_data);
9883         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9884
9885         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
9886       }
9887
9888     case R_ARM_THM_JUMP11:
9889     case R_ARM_THM_JUMP8:
9890     case R_ARM_THM_JUMP6:
9891       /* Thumb B (branch) instruction).  */
9892       {
9893         bfd_signed_vma relocation;
9894         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
9895         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
9896         bfd_signed_vma signed_check;
9897
9898         /* CZB cannot jump backward.  */
9899         if (r_type == R_ARM_THM_JUMP6)
9900           reloc_signed_min = 0;
9901
9902         if (globals->use_rel)
9903           {
9904             /* Need to refetch addend.  */
9905             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
9906             if (addend & ((howto->src_mask + 1) >> 1))
9907               {
9908                 signed_addend = -1;
9909                 signed_addend &= ~ howto->src_mask;
9910                 signed_addend |= addend;
9911               }
9912             else
9913               signed_addend = addend;
9914             /* The value in the insn has been right shifted.  We need to
9915                undo this, so that we can perform the address calculation
9916                in terms of bytes.  */
9917             signed_addend <<= howto->rightshift;
9918           }
9919         relocation = value + signed_addend;
9920
9921         relocation -= (input_section->output_section->vma
9922                        + input_section->output_offset
9923                        + rel->r_offset);
9924
9925         relocation >>= howto->rightshift;
9926         signed_check = relocation;
9927
9928         if (r_type == R_ARM_THM_JUMP6)
9929           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
9930         else
9931           relocation &= howto->dst_mask;
9932         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
9933
9934         bfd_put_16 (input_bfd, relocation, hit_data);
9935
9936         /* Assumes two's complement.  */
9937         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9938           return bfd_reloc_overflow;
9939
9940         return bfd_reloc_ok;
9941       }
9942
9943     case R_ARM_ALU_PCREL7_0:
9944     case R_ARM_ALU_PCREL15_8:
9945     case R_ARM_ALU_PCREL23_15:
9946       {
9947         bfd_vma insn;
9948         bfd_vma relocation;
9949
9950         insn = bfd_get_32 (input_bfd, hit_data);
9951         if (globals->use_rel)
9952           {
9953             /* Extract the addend.  */
9954             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
9955             signed_addend = addend;
9956           }
9957         relocation = value + signed_addend;
9958
9959         relocation -= (input_section->output_section->vma
9960                        + input_section->output_offset
9961                        + rel->r_offset);
9962         insn = (insn & ~0xfff)
9963                | ((howto->bitpos << 7) & 0xf00)
9964                | ((relocation >> howto->bitpos) & 0xff);
9965         bfd_put_32 (input_bfd, value, hit_data);
9966       }
9967       return bfd_reloc_ok;
9968
9969     case R_ARM_GNU_VTINHERIT:
9970     case R_ARM_GNU_VTENTRY:
9971       return bfd_reloc_ok;
9972
9973     case R_ARM_GOTOFF32:
9974       /* Relocation is relative to the start of the
9975          global offset table.  */
9976
9977       BFD_ASSERT (sgot != NULL);
9978       if (sgot == NULL)
9979         return bfd_reloc_notsupported;
9980
9981       /* If we are addressing a Thumb function, we need to adjust the
9982          address by one, so that attempts to call the function pointer will
9983          correctly interpret it as Thumb code.  */
9984       if (branch_type == ST_BRANCH_TO_THUMB)
9985         value += 1;
9986
9987       /* Note that sgot->output_offset is not involved in this
9988          calculation.  We always want the start of .got.  If we
9989          define _GLOBAL_OFFSET_TABLE in a different way, as is
9990          permitted by the ABI, we might have to change this
9991          calculation.  */
9992       value -= sgot->output_section->vma;
9993       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9994                                        contents, rel->r_offset, value,
9995                                        rel->r_addend);
9996
9997     case R_ARM_GOTPC:
9998       /* Use global offset table as symbol value.  */
9999       BFD_ASSERT (sgot != NULL);
10000
10001       if (sgot == NULL)
10002         return bfd_reloc_notsupported;
10003
10004       *unresolved_reloc_p = FALSE;
10005       value = sgot->output_section->vma;
10006       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10007                                        contents, rel->r_offset, value,
10008                                        rel->r_addend);
10009
10010     case R_ARM_GOT32:
10011     case R_ARM_GOT_PREL:
10012       /* Relocation is to the entry for this symbol in the
10013          global offset table.  */
10014       if (sgot == NULL)
10015         return bfd_reloc_notsupported;
10016
10017       if (dynreloc_st_type == STT_GNU_IFUNC
10018           && plt_offset != (bfd_vma) -1
10019           && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
10020         {
10021           /* We have a relocation against a locally-binding STT_GNU_IFUNC
10022              symbol, and the relocation resolves directly to the runtime
10023              target rather than to the .iplt entry.  This means that any
10024              .got entry would be the same value as the .igot.plt entry,
10025              so there's no point creating both.  */
10026           sgot = globals->root.igotplt;
10027           value = sgot->output_offset + gotplt_offset;
10028         }
10029       else if (h != NULL)
10030         {
10031           bfd_vma off;
10032
10033           off = h->got.offset;
10034           BFD_ASSERT (off != (bfd_vma) -1);
10035           if ((off & 1) != 0)
10036             {
10037               /* We have already processsed one GOT relocation against
10038                  this symbol.  */
10039               off &= ~1;
10040               if (globals->root.dynamic_sections_created
10041                   && !SYMBOL_REFERENCES_LOCAL (info, h))
10042                 *unresolved_reloc_p = FALSE;
10043             }
10044           else
10045             {
10046               Elf_Internal_Rela outrel;
10047
10048               if (h->dynindx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
10049                 {
10050                   /* If the symbol doesn't resolve locally in a static
10051                      object, we have an undefined reference.  If the
10052                      symbol doesn't resolve locally in a dynamic object,
10053                      it should be resolved by the dynamic linker.  */
10054                   if (globals->root.dynamic_sections_created)
10055                     {
10056                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
10057                       *unresolved_reloc_p = FALSE;
10058                     }
10059                   else
10060                     outrel.r_info = 0;
10061                   outrel.r_addend = 0;
10062                 }
10063               else
10064                 {
10065                   if (dynreloc_st_type == STT_GNU_IFUNC)
10066                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
10067                   else if (bfd_link_pic (info) &&
10068                            (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10069                             || h->root.type != bfd_link_hash_undefweak))
10070                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
10071                   else
10072                     outrel.r_info = 0;
10073                   outrel.r_addend = dynreloc_value;
10074                 }
10075
10076               /* The GOT entry is initialized to zero by default.
10077                  See if we should install a different value.  */
10078               if (outrel.r_addend != 0
10079                   && (outrel.r_info == 0 || globals->use_rel))
10080                 {
10081                   bfd_put_32 (output_bfd, outrel.r_addend,
10082                               sgot->contents + off);
10083                   outrel.r_addend = 0;
10084                 }
10085
10086               if (outrel.r_info != 0)
10087                 {
10088                   outrel.r_offset = (sgot->output_section->vma
10089                                      + sgot->output_offset
10090                                      + off);
10091                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10092                 }
10093               h->got.offset |= 1;
10094             }
10095           value = sgot->output_offset + off;
10096         }
10097       else
10098         {
10099           bfd_vma off;
10100
10101           BFD_ASSERT (local_got_offsets != NULL &&
10102                       local_got_offsets[r_symndx] != (bfd_vma) -1);
10103
10104           off = local_got_offsets[r_symndx];
10105
10106           /* The offset must always be a multiple of 4.  We use the
10107              least significant bit to record whether we have already
10108              generated the necessary reloc.  */
10109           if ((off & 1) != 0)
10110             off &= ~1;
10111           else
10112             {
10113               if (globals->use_rel)
10114                 bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
10115
10116               if (bfd_link_pic (info) || dynreloc_st_type == STT_GNU_IFUNC)
10117                 {
10118                   Elf_Internal_Rela outrel;
10119
10120                   outrel.r_addend = addend + dynreloc_value;
10121                   outrel.r_offset = (sgot->output_section->vma
10122                                      + sgot->output_offset
10123                                      + off);
10124                   if (dynreloc_st_type == STT_GNU_IFUNC)
10125                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
10126                   else
10127                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
10128                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10129                 }
10130
10131               local_got_offsets[r_symndx] |= 1;
10132             }
10133
10134           value = sgot->output_offset + off;
10135         }
10136       if (r_type != R_ARM_GOT32)
10137         value += sgot->output_section->vma;
10138
10139       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10140                                        contents, rel->r_offset, value,
10141                                        rel->r_addend);
10142
10143     case R_ARM_TLS_LDO32:
10144       value = value - dtpoff_base (info);
10145
10146       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10147                                        contents, rel->r_offset, value,
10148                                        rel->r_addend);
10149
10150     case R_ARM_TLS_LDM32:
10151       {
10152         bfd_vma off;
10153
10154         if (sgot == NULL)
10155           abort ();
10156
10157         off = globals->tls_ldm_got.offset;
10158
10159         if ((off & 1) != 0)
10160           off &= ~1;
10161         else
10162           {
10163             /* If we don't know the module number, create a relocation
10164                for it.  */
10165             if (bfd_link_pic (info))
10166               {
10167                 Elf_Internal_Rela outrel;
10168
10169                 if (srelgot == NULL)
10170                   abort ();
10171
10172                 outrel.r_addend = 0;
10173                 outrel.r_offset = (sgot->output_section->vma
10174                                    + sgot->output_offset + off);
10175                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
10176
10177                 if (globals->use_rel)
10178                   bfd_put_32 (output_bfd, outrel.r_addend,
10179                               sgot->contents + off);
10180
10181                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10182               }
10183             else
10184               bfd_put_32 (output_bfd, 1, sgot->contents + off);
10185
10186             globals->tls_ldm_got.offset |= 1;
10187           }
10188
10189         value = sgot->output_section->vma + sgot->output_offset + off
10190           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
10191
10192         return _bfd_final_link_relocate (howto, input_bfd, input_section,
10193                                          contents, rel->r_offset, value,
10194                                          rel->r_addend);
10195       }
10196
10197     case R_ARM_TLS_CALL:
10198     case R_ARM_THM_TLS_CALL:
10199     case R_ARM_TLS_GD32:
10200     case R_ARM_TLS_IE32:
10201     case R_ARM_TLS_GOTDESC:
10202     case R_ARM_TLS_DESCSEQ:
10203     case R_ARM_THM_TLS_DESCSEQ:
10204       {
10205         bfd_vma off, offplt;
10206         int indx = 0;
10207         char tls_type;
10208
10209         BFD_ASSERT (sgot != NULL);
10210
10211         if (h != NULL)
10212           {
10213             bfd_boolean dyn;
10214             dyn = globals->root.dynamic_sections_created;
10215             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
10216                                                  bfd_link_pic (info),
10217                                                  h)
10218                 && (!bfd_link_pic (info)
10219                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
10220               {
10221                 *unresolved_reloc_p = FALSE;
10222                 indx = h->dynindx;
10223               }
10224             off = h->got.offset;
10225             offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
10226             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
10227           }
10228         else
10229           {
10230             BFD_ASSERT (local_got_offsets != NULL);
10231             off = local_got_offsets[r_symndx];
10232             offplt = local_tlsdesc_gotents[r_symndx];
10233             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
10234           }
10235
10236         /* Linker relaxations happens from one of the
10237            R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
10238         if (ELF32_R_TYPE(rel->r_info) != r_type)
10239           tls_type = GOT_TLS_IE;
10240
10241         BFD_ASSERT (tls_type != GOT_UNKNOWN);
10242
10243         if ((off & 1) != 0)
10244           off &= ~1;
10245         else
10246           {
10247             bfd_boolean need_relocs = FALSE;
10248             Elf_Internal_Rela outrel;
10249             int cur_off = off;
10250
10251             /* The GOT entries have not been initialized yet.  Do it
10252                now, and emit any relocations.  If both an IE GOT and a
10253                GD GOT are necessary, we emit the GD first.  */
10254
10255             if ((bfd_link_pic (info) || indx != 0)
10256                 && (h == NULL
10257                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10258                     || h->root.type != bfd_link_hash_undefweak))
10259               {
10260                 need_relocs = TRUE;
10261                 BFD_ASSERT (srelgot != NULL);
10262               }
10263
10264             if (tls_type & GOT_TLS_GDESC)
10265               {
10266                 bfd_byte *loc;
10267
10268                 /* We should have relaxed, unless this is an undefined
10269                    weak symbol.  */
10270                 BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
10271                             || bfd_link_pic (info));
10272                 BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
10273                             <= globals->root.sgotplt->size);
10274
10275                 outrel.r_addend = 0;
10276                 outrel.r_offset = (globals->root.sgotplt->output_section->vma
10277                                    + globals->root.sgotplt->output_offset
10278                                    + offplt
10279                                    + globals->sgotplt_jump_table_size);
10280
10281                 outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
10282                 sreloc = globals->root.srelplt;
10283                 loc = sreloc->contents;
10284                 loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
10285                 BFD_ASSERT (loc + RELOC_SIZE (globals)
10286                            <= sreloc->contents + sreloc->size);
10287
10288                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
10289
10290                 /* For globals, the first word in the relocation gets
10291                    the relocation index and the top bit set, or zero,
10292                    if we're binding now.  For locals, it gets the
10293                    symbol's offset in the tls section.  */
10294                 bfd_put_32 (output_bfd,
10295                             !h ? value - elf_hash_table (info)->tls_sec->vma
10296                             : info->flags & DF_BIND_NOW ? 0
10297                             : 0x80000000 | ELF32_R_SYM (outrel.r_info),
10298                             globals->root.sgotplt->contents + offplt
10299                             + globals->sgotplt_jump_table_size);
10300
10301                 /* Second word in the relocation is always zero.  */
10302                 bfd_put_32 (output_bfd, 0,
10303                             globals->root.sgotplt->contents + offplt
10304                             + globals->sgotplt_jump_table_size + 4);
10305               }
10306             if (tls_type & GOT_TLS_GD)
10307               {
10308                 if (need_relocs)
10309                   {
10310                     outrel.r_addend = 0;
10311                     outrel.r_offset = (sgot->output_section->vma
10312                                        + sgot->output_offset
10313                                        + cur_off);
10314                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
10315
10316                     if (globals->use_rel)
10317                       bfd_put_32 (output_bfd, outrel.r_addend,
10318                                   sgot->contents + cur_off);
10319
10320                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10321
10322                     if (indx == 0)
10323                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
10324                                   sgot->contents + cur_off + 4);
10325                     else
10326                       {
10327                         outrel.r_addend = 0;
10328                         outrel.r_info = ELF32_R_INFO (indx,
10329                                                       R_ARM_TLS_DTPOFF32);
10330                         outrel.r_offset += 4;
10331
10332                         if (globals->use_rel)
10333                           bfd_put_32 (output_bfd, outrel.r_addend,
10334                                       sgot->contents + cur_off + 4);
10335
10336                         elf32_arm_add_dynreloc (output_bfd, info,
10337                                                 srelgot, &outrel);
10338                       }
10339                   }
10340                 else
10341                   {
10342                     /* If we are not emitting relocations for a
10343                        general dynamic reference, then we must be in a
10344                        static link or an executable link with the
10345                        symbol binding locally.  Mark it as belonging
10346                        to module 1, the executable.  */
10347                     bfd_put_32 (output_bfd, 1,
10348                                 sgot->contents + cur_off);
10349                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
10350                                 sgot->contents + cur_off + 4);
10351                   }
10352
10353                 cur_off += 8;
10354               }
10355
10356             if (tls_type & GOT_TLS_IE)
10357               {
10358                 if (need_relocs)
10359                   {
10360                     if (indx == 0)
10361                       outrel.r_addend = value - dtpoff_base (info);
10362                     else
10363                       outrel.r_addend = 0;
10364                     outrel.r_offset = (sgot->output_section->vma
10365                                        + sgot->output_offset
10366                                        + cur_off);
10367                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
10368
10369                     if (globals->use_rel)
10370                       bfd_put_32 (output_bfd, outrel.r_addend,
10371                                   sgot->contents + cur_off);
10372
10373                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10374                   }
10375                 else
10376                   bfd_put_32 (output_bfd, tpoff (info, value),
10377                               sgot->contents + cur_off);
10378                 cur_off += 4;
10379               }
10380
10381             if (h != NULL)
10382               h->got.offset |= 1;
10383             else
10384               local_got_offsets[r_symndx] |= 1;
10385           }
10386
10387         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
10388           off += 8;
10389         else if (tls_type & GOT_TLS_GDESC)
10390           off = offplt;
10391
10392         if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
10393             || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
10394           {
10395             bfd_signed_vma offset;
10396             /* TLS stubs are arm mode.  The original symbol is a
10397                data object, so branch_type is bogus.  */
10398             branch_type = ST_BRANCH_TO_ARM;
10399             enum elf32_arm_stub_type stub_type
10400               = arm_type_of_stub (info, input_section, rel,
10401                                   st_type, &branch_type,
10402                                   (struct elf32_arm_link_hash_entry *)h,
10403                                   globals->tls_trampoline, globals->root.splt,
10404                                   input_bfd, sym_name);
10405
10406             if (stub_type != arm_stub_none)
10407               {
10408                 struct elf32_arm_stub_hash_entry *stub_entry
10409                   = elf32_arm_get_stub_entry
10410                   (input_section, globals->root.splt, 0, rel,
10411                    globals, stub_type);
10412                 offset = (stub_entry->stub_offset
10413                           + stub_entry->stub_sec->output_offset
10414                           + stub_entry->stub_sec->output_section->vma);
10415               }
10416             else
10417               offset = (globals->root.splt->output_section->vma
10418                         + globals->root.splt->output_offset
10419                         + globals->tls_trampoline);
10420
10421             if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
10422               {
10423                 unsigned long inst;
10424
10425                 offset -= (input_section->output_section->vma
10426                            + input_section->output_offset
10427                            + rel->r_offset + 8);
10428
10429                 inst = offset >> 2;
10430                 inst &= 0x00ffffff;
10431                 value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
10432               }
10433             else
10434               {
10435                 /* Thumb blx encodes the offset in a complicated
10436                    fashion.  */
10437                 unsigned upper_insn, lower_insn;
10438                 unsigned neg;
10439
10440                 offset -= (input_section->output_section->vma
10441                            + input_section->output_offset
10442                            + rel->r_offset + 4);
10443
10444                 if (stub_type != arm_stub_none
10445                     && arm_stub_is_thumb (stub_type))
10446                   {
10447                     lower_insn = 0xd000;
10448                   }
10449                 else
10450                   {
10451                     lower_insn = 0xc000;
10452                     /* Round up the offset to a word boundary.  */
10453                     offset = (offset + 2) & ~2;
10454                   }
10455
10456                 neg = offset < 0;
10457                 upper_insn = (0xf000
10458                               | ((offset >> 12) & 0x3ff)
10459                               | (neg << 10));
10460                 lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
10461                               | (((!((offset >> 22) & 1)) ^ neg) << 11)
10462                               | ((offset >> 1) & 0x7ff);
10463                 bfd_put_16 (input_bfd, upper_insn, hit_data);
10464                 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
10465                 return bfd_reloc_ok;
10466               }
10467           }
10468         /* These relocations needs special care, as besides the fact
10469            they point somewhere in .gotplt, the addend must be
10470            adjusted accordingly depending on the type of instruction
10471            we refer to.  */
10472         else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
10473           {
10474             unsigned long data, insn;
10475             unsigned thumb;
10476
10477             data = bfd_get_32 (input_bfd, hit_data);
10478             thumb = data & 1;
10479             data &= ~1u;
10480
10481             if (thumb)
10482               {
10483                 insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
10484                 if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
10485                   insn = (insn << 16)
10486                     | bfd_get_16 (input_bfd,
10487                                   contents + rel->r_offset - data + 2);
10488                 if ((insn & 0xf800c000) == 0xf000c000)
10489                   /* bl/blx */
10490                   value = -6;
10491                 else if ((insn & 0xffffff00) == 0x4400)
10492                   /* add */
10493                   value = -5;
10494                 else
10495                   {
10496                     (*_bfd_error_handler)
10497                       (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' referenced by TLS_GOTDESC"),
10498                        input_bfd, input_section,
10499                        (unsigned long)rel->r_offset, insn);
10500                     return bfd_reloc_notsupported;
10501                   }
10502               }
10503             else
10504               {
10505                 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
10506
10507                 switch (insn >> 24)
10508                   {
10509                   case 0xeb:  /* bl */
10510                   case 0xfa:  /* blx */
10511                     value = -4;
10512                     break;
10513
10514                   case 0xe0:    /* add */
10515                     value = -8;
10516                     break;
10517
10518                   default:
10519                     (*_bfd_error_handler)
10520                       (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' referenced by TLS_GOTDESC"),
10521                        input_bfd, input_section,
10522                        (unsigned long)rel->r_offset, insn);
10523                     return bfd_reloc_notsupported;
10524                   }
10525               }
10526
10527             value += ((globals->root.sgotplt->output_section->vma
10528                        + globals->root.sgotplt->output_offset + off)
10529                       - (input_section->output_section->vma
10530                          + input_section->output_offset
10531                          + rel->r_offset)
10532                       + globals->sgotplt_jump_table_size);
10533           }
10534         else
10535           value = ((globals->root.sgot->output_section->vma
10536                     + globals->root.sgot->output_offset + off)
10537                    - (input_section->output_section->vma
10538                       + input_section->output_offset + rel->r_offset));
10539
10540         return _bfd_final_link_relocate (howto, input_bfd, input_section,
10541                                          contents, rel->r_offset, value,
10542                                          rel->r_addend);
10543       }
10544
10545     case R_ARM_TLS_LE32:
10546       if (bfd_link_dll (info))
10547         {
10548           (*_bfd_error_handler)
10549             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
10550              input_bfd, input_section,
10551              (long) rel->r_offset, howto->name);
10552           return bfd_reloc_notsupported;
10553         }
10554       else
10555         value = tpoff (info, value);
10556
10557       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10558                                        contents, rel->r_offset, value,
10559                                        rel->r_addend);
10560
10561     case R_ARM_V4BX:
10562       if (globals->fix_v4bx)
10563         {
10564           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10565
10566           /* Ensure that we have a BX instruction.  */
10567           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
10568
10569           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
10570             {
10571               /* Branch to veneer.  */
10572               bfd_vma glue_addr;
10573               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
10574               glue_addr -= input_section->output_section->vma
10575                            + input_section->output_offset
10576                            + rel->r_offset + 8;
10577               insn = (insn & 0xf0000000) | 0x0a000000
10578                      | ((glue_addr >> 2) & 0x00ffffff);
10579             }
10580           else
10581             {
10582               /* Preserve Rm (lowest four bits) and the condition code
10583                  (highest four bits). Other bits encode MOV PC,Rm.  */
10584               insn = (insn & 0xf000000f) | 0x01a0f000;
10585             }
10586
10587           bfd_put_32 (input_bfd, insn, hit_data);
10588         }
10589       return bfd_reloc_ok;
10590
10591     case R_ARM_MOVW_ABS_NC:
10592     case R_ARM_MOVT_ABS:
10593     case R_ARM_MOVW_PREL_NC:
10594     case R_ARM_MOVT_PREL:
10595     /* Until we properly support segment-base-relative addressing then
10596        we assume the segment base to be zero, as for the group relocations.
10597        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
10598        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
10599     case R_ARM_MOVW_BREL_NC:
10600     case R_ARM_MOVW_BREL:
10601     case R_ARM_MOVT_BREL:
10602       {
10603         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10604
10605         if (globals->use_rel)
10606           {
10607             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
10608             signed_addend = (addend ^ 0x8000) - 0x8000;
10609           }
10610
10611         value += signed_addend;
10612
10613         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
10614           value -= (input_section->output_section->vma
10615                     + input_section->output_offset + rel->r_offset);
10616
10617         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
10618           return bfd_reloc_overflow;
10619
10620         if (branch_type == ST_BRANCH_TO_THUMB)
10621           value |= 1;
10622
10623         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
10624             || r_type == R_ARM_MOVT_BREL)
10625           value >>= 16;
10626
10627         insn &= 0xfff0f000;
10628         insn |= value & 0xfff;
10629         insn |= (value & 0xf000) << 4;
10630         bfd_put_32 (input_bfd, insn, hit_data);
10631       }
10632       return bfd_reloc_ok;
10633
10634     case R_ARM_THM_MOVW_ABS_NC:
10635     case R_ARM_THM_MOVT_ABS:
10636     case R_ARM_THM_MOVW_PREL_NC:
10637     case R_ARM_THM_MOVT_PREL:
10638     /* Until we properly support segment-base-relative addressing then
10639        we assume the segment base to be zero, as for the above relocations.
10640        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
10641        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
10642        as R_ARM_THM_MOVT_ABS.  */
10643     case R_ARM_THM_MOVW_BREL_NC:
10644     case R_ARM_THM_MOVW_BREL:
10645     case R_ARM_THM_MOVT_BREL:
10646       {
10647         bfd_vma insn;
10648
10649         insn = bfd_get_16 (input_bfd, hit_data) << 16;
10650         insn |= bfd_get_16 (input_bfd, hit_data + 2);
10651
10652         if (globals->use_rel)
10653           {
10654             addend = ((insn >> 4)  & 0xf000)
10655                    | ((insn >> 15) & 0x0800)
10656                    | ((insn >> 4)  & 0x0700)
10657                    | (insn         & 0x00ff);
10658             signed_addend = (addend ^ 0x8000) - 0x8000;
10659           }
10660
10661         value += signed_addend;
10662
10663         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
10664           value -= (input_section->output_section->vma
10665                     + input_section->output_offset + rel->r_offset);
10666
10667         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
10668           return bfd_reloc_overflow;
10669
10670         if (branch_type == ST_BRANCH_TO_THUMB)
10671           value |= 1;
10672
10673         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
10674             || r_type == R_ARM_THM_MOVT_BREL)
10675           value >>= 16;
10676
10677         insn &= 0xfbf08f00;
10678         insn |= (value & 0xf000) << 4;
10679         insn |= (value & 0x0800) << 15;
10680         insn |= (value & 0x0700) << 4;
10681         insn |= (value & 0x00ff);
10682
10683         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10684         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10685       }
10686       return bfd_reloc_ok;
10687
10688     case R_ARM_ALU_PC_G0_NC:
10689     case R_ARM_ALU_PC_G1_NC:
10690     case R_ARM_ALU_PC_G0:
10691     case R_ARM_ALU_PC_G1:
10692     case R_ARM_ALU_PC_G2:
10693     case R_ARM_ALU_SB_G0_NC:
10694     case R_ARM_ALU_SB_G1_NC:
10695     case R_ARM_ALU_SB_G0:
10696     case R_ARM_ALU_SB_G1:
10697     case R_ARM_ALU_SB_G2:
10698       {
10699         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10700         bfd_vma pc = input_section->output_section->vma
10701                      + input_section->output_offset + rel->r_offset;
10702         /* sb is the origin of the *segment* containing the symbol.  */
10703         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10704         bfd_vma residual;
10705         bfd_vma g_n;
10706         bfd_signed_vma signed_value;
10707         int group = 0;
10708
10709         /* Determine which group of bits to select.  */
10710         switch (r_type)
10711           {
10712           case R_ARM_ALU_PC_G0_NC:
10713           case R_ARM_ALU_PC_G0:
10714           case R_ARM_ALU_SB_G0_NC:
10715           case R_ARM_ALU_SB_G0:
10716             group = 0;
10717             break;
10718
10719           case R_ARM_ALU_PC_G1_NC:
10720           case R_ARM_ALU_PC_G1:
10721           case R_ARM_ALU_SB_G1_NC:
10722           case R_ARM_ALU_SB_G1:
10723             group = 1;
10724             break;
10725
10726           case R_ARM_ALU_PC_G2:
10727           case R_ARM_ALU_SB_G2:
10728             group = 2;
10729             break;
10730
10731           default:
10732             abort ();
10733           }
10734
10735         /* If REL, extract the addend from the insn.  If RELA, it will
10736            have already been fetched for us.  */
10737         if (globals->use_rel)
10738           {
10739             int negative;
10740             bfd_vma constant = insn & 0xff;
10741             bfd_vma rotation = (insn & 0xf00) >> 8;
10742
10743             if (rotation == 0)
10744               signed_addend = constant;
10745             else
10746               {
10747                 /* Compensate for the fact that in the instruction, the
10748                    rotation is stored in multiples of 2 bits.  */
10749                 rotation *= 2;
10750
10751                 /* Rotate "constant" right by "rotation" bits.  */
10752                 signed_addend = (constant >> rotation) |
10753                                 (constant << (8 * sizeof (bfd_vma) - rotation));
10754               }
10755
10756             /* Determine if the instruction is an ADD or a SUB.
10757                (For REL, this determines the sign of the addend.)  */
10758             negative = identify_add_or_sub (insn);
10759             if (negative == 0)
10760               {
10761                 (*_bfd_error_handler)
10762                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
10763                   input_bfd, input_section,
10764                   (long) rel->r_offset, howto->name);
10765                 return bfd_reloc_overflow;
10766               }
10767
10768             signed_addend *= negative;
10769           }
10770
10771         /* Compute the value (X) to go in the place.  */
10772         if (r_type == R_ARM_ALU_PC_G0_NC
10773             || r_type == R_ARM_ALU_PC_G1_NC
10774             || r_type == R_ARM_ALU_PC_G0
10775             || r_type == R_ARM_ALU_PC_G1
10776             || r_type == R_ARM_ALU_PC_G2)
10777           /* PC relative.  */
10778           signed_value = value - pc + signed_addend;
10779         else
10780           /* Section base relative.  */
10781           signed_value = value - sb + signed_addend;
10782
10783         /* If the target symbol is a Thumb function, then set the
10784            Thumb bit in the address.  */
10785         if (branch_type == ST_BRANCH_TO_THUMB)
10786           signed_value |= 1;
10787
10788         /* Calculate the value of the relevant G_n, in encoded
10789            constant-with-rotation format.  */
10790         g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10791                                           group, &residual);
10792
10793         /* Check for overflow if required.  */
10794         if ((r_type == R_ARM_ALU_PC_G0
10795              || r_type == R_ARM_ALU_PC_G1
10796              || r_type == R_ARM_ALU_PC_G2
10797              || r_type == R_ARM_ALU_SB_G0
10798              || r_type == R_ARM_ALU_SB_G1
10799              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
10800           {
10801             (*_bfd_error_handler)
10802               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10803               input_bfd, input_section,
10804                (long) rel->r_offset, signed_value < 0 ? - signed_value : signed_value,
10805                howto->name);
10806             return bfd_reloc_overflow;
10807           }
10808
10809         /* Mask out the value and the ADD/SUB part of the opcode; take care
10810            not to destroy the S bit.  */
10811         insn &= 0xff1ff000;
10812
10813         /* Set the opcode according to whether the value to go in the
10814            place is negative.  */
10815         if (signed_value < 0)
10816           insn |= 1 << 22;
10817         else
10818           insn |= 1 << 23;
10819
10820         /* Encode the offset.  */
10821         insn |= g_n;
10822
10823         bfd_put_32 (input_bfd, insn, hit_data);
10824       }
10825       return bfd_reloc_ok;
10826
10827     case R_ARM_LDR_PC_G0:
10828     case R_ARM_LDR_PC_G1:
10829     case R_ARM_LDR_PC_G2:
10830     case R_ARM_LDR_SB_G0:
10831     case R_ARM_LDR_SB_G1:
10832     case R_ARM_LDR_SB_G2:
10833       {
10834         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10835         bfd_vma pc = input_section->output_section->vma
10836                      + input_section->output_offset + rel->r_offset;
10837         /* sb is the origin of the *segment* containing the symbol.  */
10838         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10839         bfd_vma residual;
10840         bfd_signed_vma signed_value;
10841         int group = 0;
10842
10843         /* Determine which groups of bits to calculate.  */
10844         switch (r_type)
10845           {
10846           case R_ARM_LDR_PC_G0:
10847           case R_ARM_LDR_SB_G0:
10848             group = 0;
10849             break;
10850
10851           case R_ARM_LDR_PC_G1:
10852           case R_ARM_LDR_SB_G1:
10853             group = 1;
10854             break;
10855
10856           case R_ARM_LDR_PC_G2:
10857           case R_ARM_LDR_SB_G2:
10858             group = 2;
10859             break;
10860
10861           default:
10862             abort ();
10863           }
10864
10865         /* If REL, extract the addend from the insn.  If RELA, it will
10866            have already been fetched for us.  */
10867         if (globals->use_rel)
10868           {
10869             int negative = (insn & (1 << 23)) ? 1 : -1;
10870             signed_addend = negative * (insn & 0xfff);
10871           }
10872
10873         /* Compute the value (X) to go in the place.  */
10874         if (r_type == R_ARM_LDR_PC_G0
10875             || r_type == R_ARM_LDR_PC_G1
10876             || r_type == R_ARM_LDR_PC_G2)
10877           /* PC relative.  */
10878           signed_value = value - pc + signed_addend;
10879         else
10880           /* Section base relative.  */
10881           signed_value = value - sb + signed_addend;
10882
10883         /* Calculate the value of the relevant G_{n-1} to obtain
10884            the residual at that stage.  */
10885         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10886                                     group - 1, &residual);
10887
10888         /* Check for overflow.  */
10889         if (residual >= 0x1000)
10890           {
10891             (*_bfd_error_handler)
10892               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10893                input_bfd, input_section,
10894                (long) rel->r_offset, labs (signed_value), howto->name);
10895             return bfd_reloc_overflow;
10896           }
10897
10898         /* Mask out the value and U bit.  */
10899         insn &= 0xff7ff000;
10900
10901         /* Set the U bit if the value to go in the place is non-negative.  */
10902         if (signed_value >= 0)
10903           insn |= 1 << 23;
10904
10905         /* Encode the offset.  */
10906         insn |= residual;
10907
10908         bfd_put_32 (input_bfd, insn, hit_data);
10909       }
10910       return bfd_reloc_ok;
10911
10912     case R_ARM_LDRS_PC_G0:
10913     case R_ARM_LDRS_PC_G1:
10914     case R_ARM_LDRS_PC_G2:
10915     case R_ARM_LDRS_SB_G0:
10916     case R_ARM_LDRS_SB_G1:
10917     case R_ARM_LDRS_SB_G2:
10918       {
10919         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10920         bfd_vma pc = input_section->output_section->vma
10921                      + input_section->output_offset + rel->r_offset;
10922         /* sb is the origin of the *segment* containing the symbol.  */
10923         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10924         bfd_vma residual;
10925         bfd_signed_vma signed_value;
10926         int group = 0;
10927
10928         /* Determine which groups of bits to calculate.  */
10929         switch (r_type)
10930           {
10931           case R_ARM_LDRS_PC_G0:
10932           case R_ARM_LDRS_SB_G0:
10933             group = 0;
10934             break;
10935
10936           case R_ARM_LDRS_PC_G1:
10937           case R_ARM_LDRS_SB_G1:
10938             group = 1;
10939             break;
10940
10941           case R_ARM_LDRS_PC_G2:
10942           case R_ARM_LDRS_SB_G2:
10943             group = 2;
10944             break;
10945
10946           default:
10947             abort ();
10948           }
10949
10950         /* If REL, extract the addend from the insn.  If RELA, it will
10951            have already been fetched for us.  */
10952         if (globals->use_rel)
10953           {
10954             int negative = (insn & (1 << 23)) ? 1 : -1;
10955             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
10956           }
10957
10958         /* Compute the value (X) to go in the place.  */
10959         if (r_type == R_ARM_LDRS_PC_G0
10960             || r_type == R_ARM_LDRS_PC_G1
10961             || r_type == R_ARM_LDRS_PC_G2)
10962           /* PC relative.  */
10963           signed_value = value - pc + signed_addend;
10964         else
10965           /* Section base relative.  */
10966           signed_value = value - sb + signed_addend;
10967
10968         /* Calculate the value of the relevant G_{n-1} to obtain
10969            the residual at that stage.  */
10970         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10971                                     group - 1, &residual);
10972
10973         /* Check for overflow.  */
10974         if (residual >= 0x100)
10975           {
10976             (*_bfd_error_handler)
10977               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10978                input_bfd, input_section,
10979                (long) rel->r_offset, labs (signed_value), howto->name);
10980             return bfd_reloc_overflow;
10981           }
10982
10983         /* Mask out the value and U bit.  */
10984         insn &= 0xff7ff0f0;
10985
10986         /* Set the U bit if the value to go in the place is non-negative.  */
10987         if (signed_value >= 0)
10988           insn |= 1 << 23;
10989
10990         /* Encode the offset.  */
10991         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
10992
10993         bfd_put_32 (input_bfd, insn, hit_data);
10994       }
10995       return bfd_reloc_ok;
10996
10997     case R_ARM_LDC_PC_G0:
10998     case R_ARM_LDC_PC_G1:
10999     case R_ARM_LDC_PC_G2:
11000     case R_ARM_LDC_SB_G0:
11001     case R_ARM_LDC_SB_G1:
11002     case R_ARM_LDC_SB_G2:
11003       {
11004         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
11005         bfd_vma pc = input_section->output_section->vma
11006                      + input_section->output_offset + rel->r_offset;
11007         /* sb is the origin of the *segment* containing the symbol.  */
11008         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
11009         bfd_vma residual;
11010         bfd_signed_vma signed_value;
11011         int group = 0;
11012
11013         /* Determine which groups of bits to calculate.  */
11014         switch (r_type)
11015           {
11016           case R_ARM_LDC_PC_G0:
11017           case R_ARM_LDC_SB_G0:
11018             group = 0;
11019             break;
11020
11021           case R_ARM_LDC_PC_G1:
11022           case R_ARM_LDC_SB_G1:
11023             group = 1;
11024             break;
11025
11026           case R_ARM_LDC_PC_G2:
11027           case R_ARM_LDC_SB_G2:
11028             group = 2;
11029             break;
11030
11031           default:
11032             abort ();
11033           }
11034
11035         /* If REL, extract the addend from the insn.  If RELA, it will
11036            have already been fetched for us.  */
11037         if (globals->use_rel)
11038           {
11039             int negative = (insn & (1 << 23)) ? 1 : -1;
11040             signed_addend = negative * ((insn & 0xff) << 2);
11041           }
11042
11043         /* Compute the value (X) to go in the place.  */
11044         if (r_type == R_ARM_LDC_PC_G0
11045             || r_type == R_ARM_LDC_PC_G1
11046             || r_type == R_ARM_LDC_PC_G2)
11047           /* PC relative.  */
11048           signed_value = value - pc + signed_addend;
11049         else
11050           /* Section base relative.  */
11051           signed_value = value - sb + signed_addend;
11052
11053         /* Calculate the value of the relevant G_{n-1} to obtain
11054            the residual at that stage.  */
11055         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
11056                                     group - 1, &residual);
11057
11058         /* Check for overflow.  (The absolute value to go in the place must be
11059            divisible by four and, after having been divided by four, must
11060            fit in eight bits.)  */
11061         if ((residual & 0x3) != 0 || residual >= 0x400)
11062           {
11063             (*_bfd_error_handler)
11064               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
11065               input_bfd, input_section,
11066               (long) rel->r_offset, labs (signed_value), howto->name);
11067             return bfd_reloc_overflow;
11068           }
11069
11070         /* Mask out the value and U bit.  */
11071         insn &= 0xff7fff00;
11072
11073         /* Set the U bit if the value to go in the place is non-negative.  */
11074         if (signed_value >= 0)
11075           insn |= 1 << 23;
11076
11077         /* Encode the offset.  */
11078         insn |= residual >> 2;
11079
11080         bfd_put_32 (input_bfd, insn, hit_data);
11081       }
11082       return bfd_reloc_ok;
11083
11084     case R_ARM_THM_ALU_ABS_G0_NC:
11085     case R_ARM_THM_ALU_ABS_G1_NC:
11086     case R_ARM_THM_ALU_ABS_G2_NC:
11087     case R_ARM_THM_ALU_ABS_G3_NC:
11088         {
11089             const int shift_array[4] = {0, 8, 16, 24};
11090             bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
11091             bfd_vma addr = value;
11092             int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
11093
11094             /* Compute address.  */
11095             if (globals->use_rel)
11096                 signed_addend = insn & 0xff;
11097             addr += signed_addend;
11098             if (branch_type == ST_BRANCH_TO_THUMB)
11099                 addr |= 1;
11100             /* Clean imm8 insn.  */
11101             insn &= 0xff00;
11102             /* And update with correct part of address.  */
11103             insn |= (addr >> shift) & 0xff;
11104             /* Update insn.  */
11105             bfd_put_16 (input_bfd, insn, hit_data);
11106         }
11107
11108         *unresolved_reloc_p = FALSE;
11109         return bfd_reloc_ok;
11110
11111     default:
11112       return bfd_reloc_notsupported;
11113     }
11114 }
11115
11116 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
11117 static void
11118 arm_add_to_rel (bfd *              abfd,
11119                 bfd_byte *         address,
11120                 reloc_howto_type * howto,
11121                 bfd_signed_vma     increment)
11122 {
11123   bfd_signed_vma addend;
11124
11125   if (howto->type == R_ARM_THM_CALL
11126       || howto->type == R_ARM_THM_JUMP24)
11127     {
11128       int upper_insn, lower_insn;
11129       int upper, lower;
11130
11131       upper_insn = bfd_get_16 (abfd, address);
11132       lower_insn = bfd_get_16 (abfd, address + 2);
11133       upper = upper_insn & 0x7ff;
11134       lower = lower_insn & 0x7ff;
11135
11136       addend = (upper << 12) | (lower << 1);
11137       addend += increment;
11138       addend >>= 1;
11139
11140       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
11141       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
11142
11143       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
11144       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
11145     }
11146   else
11147     {
11148       bfd_vma        contents;
11149
11150       contents = bfd_get_32 (abfd, address);
11151
11152       /* Get the (signed) value from the instruction.  */
11153       addend = contents & howto->src_mask;
11154       if (addend & ((howto->src_mask + 1) >> 1))
11155         {
11156           bfd_signed_vma mask;
11157
11158           mask = -1;
11159           mask &= ~ howto->src_mask;
11160           addend |= mask;
11161         }
11162
11163       /* Add in the increment, (which is a byte value).  */
11164       switch (howto->type)
11165         {
11166         default:
11167           addend += increment;
11168           break;
11169
11170         case R_ARM_PC24:
11171         case R_ARM_PLT32:
11172         case R_ARM_CALL:
11173         case R_ARM_JUMP24:
11174           addend <<= howto->size;
11175           addend += increment;
11176
11177           /* Should we check for overflow here ?  */
11178
11179           /* Drop any undesired bits.  */
11180           addend >>= howto->rightshift;
11181           break;
11182         }
11183
11184       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
11185
11186       bfd_put_32 (abfd, contents, address);
11187     }
11188 }
11189
11190 #define IS_ARM_TLS_RELOC(R_TYPE)        \
11191   ((R_TYPE) == R_ARM_TLS_GD32           \
11192    || (R_TYPE) == R_ARM_TLS_LDO32       \
11193    || (R_TYPE) == R_ARM_TLS_LDM32       \
11194    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
11195    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
11196    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
11197    || (R_TYPE) == R_ARM_TLS_LE32        \
11198    || (R_TYPE) == R_ARM_TLS_IE32        \
11199    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
11200
11201 /* Specific set of relocations for the gnu tls dialect.  */
11202 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)    \
11203   ((R_TYPE) == R_ARM_TLS_GOTDESC        \
11204    || (R_TYPE) == R_ARM_TLS_CALL        \
11205    || (R_TYPE) == R_ARM_THM_TLS_CALL    \
11206    || (R_TYPE) == R_ARM_TLS_DESCSEQ     \
11207    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
11208
11209 /* Relocate an ARM ELF section.  */
11210
11211 static bfd_boolean
11212 elf32_arm_relocate_section (bfd *                  output_bfd,
11213                             struct bfd_link_info * info,
11214                             bfd *                  input_bfd,
11215                             asection *             input_section,
11216                             bfd_byte *             contents,
11217                             Elf_Internal_Rela *    relocs,
11218                             Elf_Internal_Sym *     local_syms,
11219                             asection **            local_sections)
11220 {
11221   Elf_Internal_Shdr *symtab_hdr;
11222   struct elf_link_hash_entry **sym_hashes;
11223   Elf_Internal_Rela *rel;
11224   Elf_Internal_Rela *relend;
11225   const char *name;
11226   struct elf32_arm_link_hash_table * globals;
11227
11228   globals = elf32_arm_hash_table (info);
11229   if (globals == NULL)
11230     return FALSE;
11231
11232   symtab_hdr = & elf_symtab_hdr (input_bfd);
11233   sym_hashes = elf_sym_hashes (input_bfd);
11234
11235   rel = relocs;
11236   relend = relocs + input_section->reloc_count;
11237   for (; rel < relend; rel++)
11238     {
11239       int                          r_type;
11240       reloc_howto_type *           howto;
11241       unsigned long                r_symndx;
11242       Elf_Internal_Sym *           sym;
11243       asection *                   sec;
11244       struct elf_link_hash_entry * h;
11245       bfd_vma                      relocation;
11246       bfd_reloc_status_type        r;
11247       arelent                      bfd_reloc;
11248       char                         sym_type;
11249       bfd_boolean                  unresolved_reloc = FALSE;
11250       char *error_message = NULL;
11251
11252       r_symndx = ELF32_R_SYM (rel->r_info);
11253       r_type   = ELF32_R_TYPE (rel->r_info);
11254       r_type   = arm_real_reloc_type (globals, r_type);
11255
11256       if (   r_type == R_ARM_GNU_VTENTRY
11257           || r_type == R_ARM_GNU_VTINHERIT)
11258         continue;
11259
11260       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
11261       howto = bfd_reloc.howto;
11262
11263       h = NULL;
11264       sym = NULL;
11265       sec = NULL;
11266
11267       if (r_symndx < symtab_hdr->sh_info)
11268         {
11269           sym = local_syms + r_symndx;
11270           sym_type = ELF32_ST_TYPE (sym->st_info);
11271           sec = local_sections[r_symndx];
11272
11273           /* An object file might have a reference to a local
11274              undefined symbol.  This is a daft object file, but we
11275              should at least do something about it.  V4BX & NONE
11276              relocations do not use the symbol and are explicitly
11277              allowed to use the undefined symbol, so allow those.
11278              Likewise for relocations against STN_UNDEF.  */
11279           if (r_type != R_ARM_V4BX
11280               && r_type != R_ARM_NONE
11281               && r_symndx != STN_UNDEF
11282               && bfd_is_und_section (sec)
11283               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
11284             {
11285               if (!info->callbacks->undefined_symbol
11286                   (info, bfd_elf_string_from_elf_section
11287                    (input_bfd, symtab_hdr->sh_link, sym->st_name),
11288                    input_bfd, input_section,
11289                    rel->r_offset, TRUE))
11290                 return FALSE;
11291             }
11292
11293           if (globals->use_rel)
11294             {
11295               relocation = (sec->output_section->vma
11296                             + sec->output_offset
11297                             + sym->st_value);
11298               if (!bfd_link_relocatable (info)
11299                   && (sec->flags & SEC_MERGE)
11300                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11301                 {
11302                   asection *msec;
11303                   bfd_vma addend, value;
11304
11305                   switch (r_type)
11306                     {
11307                     case R_ARM_MOVW_ABS_NC:
11308                     case R_ARM_MOVT_ABS:
11309                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
11310                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
11311                       addend = (addend ^ 0x8000) - 0x8000;
11312                       break;
11313
11314                     case R_ARM_THM_MOVW_ABS_NC:
11315                     case R_ARM_THM_MOVT_ABS:
11316                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
11317                               << 16;
11318                       value |= bfd_get_16 (input_bfd,
11319                                            contents + rel->r_offset + 2);
11320                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
11321                                | ((value & 0x04000000) >> 15);
11322                       addend = (addend ^ 0x8000) - 0x8000;
11323                       break;
11324
11325                     default:
11326                       if (howto->rightshift
11327                           || (howto->src_mask & (howto->src_mask + 1)))
11328                         {
11329                           (*_bfd_error_handler)
11330                             (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
11331                              input_bfd, input_section,
11332                              (long) rel->r_offset, howto->name);
11333                           return FALSE;
11334                         }
11335
11336                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
11337
11338                       /* Get the (signed) value from the instruction.  */
11339                       addend = value & howto->src_mask;
11340                       if (addend & ((howto->src_mask + 1) >> 1))
11341                         {
11342                           bfd_signed_vma mask;
11343
11344                           mask = -1;
11345                           mask &= ~ howto->src_mask;
11346                           addend |= mask;
11347                         }
11348                       break;
11349                     }
11350
11351                   msec = sec;
11352                   addend =
11353                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
11354                     - relocation;
11355                   addend += msec->output_section->vma + msec->output_offset;
11356
11357                   /* Cases here must match those in the preceding
11358                      switch statement.  */
11359                   switch (r_type)
11360                     {
11361                     case R_ARM_MOVW_ABS_NC:
11362                     case R_ARM_MOVT_ABS:
11363                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
11364                               | (addend & 0xfff);
11365                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
11366                       break;
11367
11368                     case R_ARM_THM_MOVW_ABS_NC:
11369                     case R_ARM_THM_MOVT_ABS:
11370                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
11371                               | (addend & 0xff) | ((addend & 0x0800) << 15);
11372                       bfd_put_16 (input_bfd, value >> 16,
11373                                   contents + rel->r_offset);
11374                       bfd_put_16 (input_bfd, value,
11375                                   contents + rel->r_offset + 2);
11376                       break;
11377
11378                     default:
11379                       value = (value & ~ howto->dst_mask)
11380                               | (addend & howto->dst_mask);
11381                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
11382                       break;
11383                     }
11384                 }
11385             }
11386           else
11387             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11388         }
11389       else
11390         {
11391           bfd_boolean warned, ignored;
11392
11393           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11394                                    r_symndx, symtab_hdr, sym_hashes,
11395                                    h, sec, relocation,
11396                                    unresolved_reloc, warned, ignored);
11397
11398           sym_type = h->type;
11399         }
11400
11401       if (sec != NULL && discarded_section (sec))
11402         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11403                                          rel, 1, relend, howto, 0, contents);
11404
11405       if (bfd_link_relocatable (info))
11406         {
11407           /* This is a relocatable link.  We don't have to change
11408              anything, unless the reloc is against a section symbol,
11409              in which case we have to adjust according to where the
11410              section symbol winds up in the output section.  */
11411           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11412             {
11413               if (globals->use_rel)
11414                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
11415                                 howto, (bfd_signed_vma) sec->output_offset);
11416               else
11417                 rel->r_addend += sec->output_offset;
11418             }
11419           continue;
11420         }
11421
11422       if (h != NULL)
11423         name = h->root.root.string;
11424       else
11425         {
11426           name = (bfd_elf_string_from_elf_section
11427                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
11428           if (name == NULL || *name == '\0')
11429             name = bfd_section_name (input_bfd, sec);
11430         }
11431
11432       if (r_symndx != STN_UNDEF
11433           && r_type != R_ARM_NONE
11434           && (h == NULL
11435               || h->root.type == bfd_link_hash_defined
11436               || h->root.type == bfd_link_hash_defweak)
11437           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
11438         {
11439           (*_bfd_error_handler)
11440             ((sym_type == STT_TLS
11441               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11442               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
11443              input_bfd,
11444              input_section,
11445              (long) rel->r_offset,
11446              howto->name,
11447              name);
11448         }
11449
11450       /* We call elf32_arm_final_link_relocate unless we're completely
11451          done, i.e., the relaxation produced the final output we want,
11452          and we won't let anybody mess with it. Also, we have to do
11453          addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
11454          both in relaxed and non-relaxed cases.  */
11455      if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
11456          || (IS_ARM_TLS_GNU_RELOC (r_type)
11457              && !((h ? elf32_arm_hash_entry (h)->tls_type :
11458                    elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
11459                   & GOT_TLS_GDESC)))
11460        {
11461          r = elf32_arm_tls_relax (globals, input_bfd, input_section,
11462                                   contents, rel, h == NULL);
11463          /* This may have been marked unresolved because it came from
11464             a shared library.  But we've just dealt with that.  */
11465          unresolved_reloc = 0;
11466        }
11467      else
11468        r = bfd_reloc_continue;
11469
11470      if (r == bfd_reloc_continue)
11471        r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
11472                                           input_section, contents, rel,
11473                                           relocation, info, sec, name, sym_type,
11474                                           (h ? h->target_internal
11475                                            : ARM_SYM_BRANCH_TYPE (sym)), h,
11476                                           &unresolved_reloc, &error_message);
11477
11478       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11479          because such sections are not SEC_ALLOC and thus ld.so will
11480          not process them.  */
11481       if (unresolved_reloc
11482           && !((input_section->flags & SEC_DEBUGGING) != 0
11483                && h->def_dynamic)
11484           && _bfd_elf_section_offset (output_bfd, info, input_section,
11485                                       rel->r_offset) != (bfd_vma) -1)
11486         {
11487           (*_bfd_error_handler)
11488             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11489              input_bfd,
11490              input_section,
11491              (long) rel->r_offset,
11492              howto->name,
11493              h->root.root.string);
11494           return FALSE;
11495         }
11496
11497       if (r != bfd_reloc_ok)
11498         {
11499           switch (r)
11500             {
11501             case bfd_reloc_overflow:
11502               /* If the overflowing reloc was to an undefined symbol,
11503                  we have already printed one error message and there
11504                  is no point complaining again.  */
11505               if ((! h ||
11506                    h->root.type != bfd_link_hash_undefined)
11507                   && (!((*info->callbacks->reloc_overflow)
11508                         (info, (h ? &h->root : NULL), name, howto->name,
11509                          (bfd_vma) 0, input_bfd, input_section,
11510                          rel->r_offset))))
11511                   return FALSE;
11512               break;
11513
11514             case bfd_reloc_undefined:
11515               if (!((*info->callbacks->undefined_symbol)
11516                     (info, name, input_bfd, input_section,
11517                      rel->r_offset, TRUE)))
11518                 return FALSE;
11519               break;
11520
11521             case bfd_reloc_outofrange:
11522               error_message = _("out of range");
11523               goto common_error;
11524
11525             case bfd_reloc_notsupported:
11526               error_message = _("unsupported relocation");
11527               goto common_error;
11528
11529             case bfd_reloc_dangerous:
11530               /* error_message should already be set.  */
11531               goto common_error;
11532
11533             default:
11534               error_message = _("unknown error");
11535               /* Fall through.  */
11536
11537             common_error:
11538               BFD_ASSERT (error_message != NULL);
11539               if (!((*info->callbacks->reloc_dangerous)
11540                     (info, error_message, input_bfd, input_section,
11541                      rel->r_offset)))
11542                 return FALSE;
11543               break;
11544             }
11545         }
11546     }
11547
11548   return TRUE;
11549 }
11550
11551 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
11552    adds the edit to the start of the list.  (The list must be built in order of
11553    ascending TINDEX: the function's callers are primarily responsible for
11554    maintaining that condition).  */
11555
11556 static void
11557 add_unwind_table_edit (arm_unwind_table_edit **head,
11558                        arm_unwind_table_edit **tail,
11559                        arm_unwind_edit_type type,
11560                        asection *linked_section,
11561                        unsigned int tindex)
11562 {
11563   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
11564       xmalloc (sizeof (arm_unwind_table_edit));
11565
11566   new_edit->type = type;
11567   new_edit->linked_section = linked_section;
11568   new_edit->index = tindex;
11569
11570   if (tindex > 0)
11571     {
11572       new_edit->next = NULL;
11573
11574       if (*tail)
11575         (*tail)->next = new_edit;
11576
11577       (*tail) = new_edit;
11578
11579       if (!*head)
11580         (*head) = new_edit;
11581     }
11582   else
11583     {
11584       new_edit->next = *head;
11585
11586       if (!*tail)
11587         *tail = new_edit;
11588
11589       *head = new_edit;
11590     }
11591 }
11592
11593 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
11594
11595 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
11596 static void
11597 adjust_exidx_size(asection *exidx_sec, int adjust)
11598 {
11599   asection *out_sec;
11600
11601   if (!exidx_sec->rawsize)
11602     exidx_sec->rawsize = exidx_sec->size;
11603
11604   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
11605   out_sec = exidx_sec->output_section;
11606   /* Adjust size of output section.  */
11607   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
11608 }
11609
11610 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
11611 static void
11612 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
11613 {
11614   struct _arm_elf_section_data *exidx_arm_data;
11615
11616   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
11617   add_unwind_table_edit (
11618     &exidx_arm_data->u.exidx.unwind_edit_list,
11619     &exidx_arm_data->u.exidx.unwind_edit_tail,
11620     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
11621
11622   adjust_exidx_size(exidx_sec, 8);
11623 }
11624
11625 /* Scan .ARM.exidx tables, and create a list describing edits which should be
11626    made to those tables, such that:
11627
11628      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
11629      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
11630         codes which have been inlined into the index).
11631
11632    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
11633
11634    The edits are applied when the tables are written
11635    (in elf32_arm_write_section).  */
11636
11637 bfd_boolean
11638 elf32_arm_fix_exidx_coverage (asection **text_section_order,
11639                               unsigned int num_text_sections,
11640                               struct bfd_link_info *info,
11641                               bfd_boolean merge_exidx_entries)
11642 {
11643   bfd *inp;
11644   unsigned int last_second_word = 0, i;
11645   asection *last_exidx_sec = NULL;
11646   asection *last_text_sec = NULL;
11647   int last_unwind_type = -1;
11648
11649   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
11650      text sections.  */
11651   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
11652     {
11653       asection *sec;
11654
11655       for (sec = inp->sections; sec != NULL; sec = sec->next)
11656         {
11657           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
11658           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
11659
11660           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
11661             continue;
11662
11663           if (elf_sec->linked_to)
11664             {
11665               Elf_Internal_Shdr *linked_hdr
11666                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
11667               struct _arm_elf_section_data *linked_sec_arm_data
11668                 = get_arm_elf_section_data (linked_hdr->bfd_section);
11669
11670               if (linked_sec_arm_data == NULL)
11671                 continue;
11672
11673               /* Link this .ARM.exidx section back from the text section it
11674                  describes.  */
11675               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
11676             }
11677         }
11678     }
11679
11680   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
11681      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
11682      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
11683
11684   for (i = 0; i < num_text_sections; i++)
11685     {
11686       asection *sec = text_section_order[i];
11687       asection *exidx_sec;
11688       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
11689       struct _arm_elf_section_data *exidx_arm_data;
11690       bfd_byte *contents = NULL;
11691       int deleted_exidx_bytes = 0;
11692       bfd_vma j;
11693       arm_unwind_table_edit *unwind_edit_head = NULL;
11694       arm_unwind_table_edit *unwind_edit_tail = NULL;
11695       Elf_Internal_Shdr *hdr;
11696       bfd *ibfd;
11697
11698       if (arm_data == NULL)
11699         continue;
11700
11701       exidx_sec = arm_data->u.text.arm_exidx_sec;
11702       if (exidx_sec == NULL)
11703         {
11704           /* Section has no unwind data.  */
11705           if (last_unwind_type == 0 || !last_exidx_sec)
11706             continue;
11707
11708           /* Ignore zero sized sections.  */
11709           if (sec->size == 0)
11710             continue;
11711
11712           insert_cantunwind_after(last_text_sec, last_exidx_sec);
11713           last_unwind_type = 0;
11714           continue;
11715         }
11716
11717       /* Skip /DISCARD/ sections.  */
11718       if (bfd_is_abs_section (exidx_sec->output_section))
11719         continue;
11720
11721       hdr = &elf_section_data (exidx_sec)->this_hdr;
11722       if (hdr->sh_type != SHT_ARM_EXIDX)
11723         continue;
11724
11725       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
11726       if (exidx_arm_data == NULL)
11727         continue;
11728
11729       ibfd = exidx_sec->owner;
11730
11731       if (hdr->contents != NULL)
11732         contents = hdr->contents;
11733       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
11734         /* An error?  */
11735         continue;
11736
11737       for (j = 0; j < hdr->sh_size; j += 8)
11738         {
11739           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
11740           int unwind_type;
11741           int elide = 0;
11742
11743           /* An EXIDX_CANTUNWIND entry.  */
11744           if (second_word == 1)
11745             {
11746               if (last_unwind_type == 0)
11747                 elide = 1;
11748               unwind_type = 0;
11749             }
11750           /* Inlined unwinding data.  Merge if equal to previous.  */
11751           else if ((second_word & 0x80000000) != 0)
11752             {
11753               if (merge_exidx_entries
11754                    && last_second_word == second_word && last_unwind_type == 1)
11755                 elide = 1;
11756               unwind_type = 1;
11757               last_second_word = second_word;
11758             }
11759           /* Normal table entry.  In theory we could merge these too,
11760              but duplicate entries are likely to be much less common.  */
11761           else
11762             unwind_type = 2;
11763
11764           if (elide)
11765             {
11766               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
11767                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
11768
11769               deleted_exidx_bytes += 8;
11770             }
11771
11772           last_unwind_type = unwind_type;
11773         }
11774
11775       /* Free contents if we allocated it ourselves.  */
11776       if (contents != hdr->contents)
11777         free (contents);
11778
11779       /* Record edits to be applied later (in elf32_arm_write_section).  */
11780       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
11781       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
11782
11783       if (deleted_exidx_bytes > 0)
11784         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
11785
11786       last_exidx_sec = exidx_sec;
11787       last_text_sec = sec;
11788     }
11789
11790   /* Add terminating CANTUNWIND entry.  */
11791   if (last_exidx_sec && last_unwind_type != 0)
11792     insert_cantunwind_after(last_text_sec, last_exidx_sec);
11793
11794   return TRUE;
11795 }
11796
11797 static bfd_boolean
11798 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
11799                                bfd *ibfd, const char *name)
11800 {
11801   asection *sec, *osec;
11802
11803   sec = bfd_get_linker_section (ibfd, name);
11804   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
11805     return TRUE;
11806
11807   osec = sec->output_section;
11808   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
11809     return TRUE;
11810
11811   if (! bfd_set_section_contents (obfd, osec, sec->contents,
11812                                   sec->output_offset, sec->size))
11813     return FALSE;
11814
11815   return TRUE;
11816 }
11817
11818 static bfd_boolean
11819 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
11820 {
11821   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
11822   asection *sec, *osec;
11823
11824   if (globals == NULL)
11825     return FALSE;
11826
11827   /* Invoke the regular ELF backend linker to do all the work.  */
11828   if (!bfd_elf_final_link (abfd, info))
11829     return FALSE;
11830
11831   /* Process stub sections (eg BE8 encoding, ...).  */
11832   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
11833   unsigned int i;
11834   for (i=0; i<htab->top_id; i++)
11835     {
11836       sec = htab->stub_group[i].stub_sec;
11837       /* Only process it once, in its link_sec slot.  */
11838       if (sec && i == htab->stub_group[i].link_sec->id)
11839         {
11840           osec = sec->output_section;
11841           elf32_arm_write_section (abfd, info, sec, sec->contents);
11842           if (! bfd_set_section_contents (abfd, osec, sec->contents,
11843                                           sec->output_offset, sec->size))
11844             return FALSE;
11845         }
11846     }
11847
11848   /* Write out any glue sections now that we have created all the
11849      stubs.  */
11850   if (globals->bfd_of_glue_owner != NULL)
11851     {
11852       if (! elf32_arm_output_glue_section (info, abfd,
11853                                            globals->bfd_of_glue_owner,
11854                                            ARM2THUMB_GLUE_SECTION_NAME))
11855         return FALSE;
11856
11857       if (! elf32_arm_output_glue_section (info, abfd,
11858                                            globals->bfd_of_glue_owner,
11859                                            THUMB2ARM_GLUE_SECTION_NAME))
11860         return FALSE;
11861
11862       if (! elf32_arm_output_glue_section (info, abfd,
11863                                            globals->bfd_of_glue_owner,
11864                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
11865         return FALSE;
11866
11867       if (! elf32_arm_output_glue_section (info, abfd,
11868                                            globals->bfd_of_glue_owner,
11869                                            STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
11870         return FALSE;
11871
11872       if (! elf32_arm_output_glue_section (info, abfd,
11873                                            globals->bfd_of_glue_owner,
11874                                            ARM_BX_GLUE_SECTION_NAME))
11875         return FALSE;
11876     }
11877
11878   return TRUE;
11879 }
11880
11881 /* Return a best guess for the machine number based on the attributes.  */
11882
11883 static unsigned int
11884 bfd_arm_get_mach_from_attributes (bfd * abfd)
11885 {
11886   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
11887
11888   switch (arch)
11889     {
11890     case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
11891     case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
11892     case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
11893
11894     case TAG_CPU_ARCH_V5TE:
11895       {
11896         char * name;
11897
11898         BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
11899         name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
11900
11901         if (name)
11902           {
11903             if (strcmp (name, "IWMMXT2") == 0)
11904               return bfd_mach_arm_iWMMXt2;
11905
11906             if (strcmp (name, "IWMMXT") == 0)
11907               return bfd_mach_arm_iWMMXt;
11908
11909             if (strcmp (name, "XSCALE") == 0)
11910               {
11911                 int wmmx;
11912
11913                 BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
11914                 wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
11915                 switch (wmmx)
11916                   {
11917                   case 1: return bfd_mach_arm_iWMMXt;
11918                   case 2: return bfd_mach_arm_iWMMXt2;
11919                   default: return bfd_mach_arm_XScale;
11920                   }
11921               }
11922           }
11923
11924         return bfd_mach_arm_5TE;
11925       }
11926
11927     default:
11928       return bfd_mach_arm_unknown;
11929     }
11930 }
11931
11932 /* Set the right machine number.  */
11933
11934 static bfd_boolean
11935 elf32_arm_object_p (bfd *abfd)
11936 {
11937   unsigned int mach;
11938
11939   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
11940
11941   if (mach == bfd_mach_arm_unknown)
11942     {
11943       if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
11944         mach = bfd_mach_arm_ep9312;
11945       else
11946         mach = bfd_arm_get_mach_from_attributes (abfd);
11947     }
11948
11949   bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
11950   return TRUE;
11951 }
11952
11953 /* Function to keep ARM specific flags in the ELF header.  */
11954
11955 static bfd_boolean
11956 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
11957 {
11958   if (elf_flags_init (abfd)
11959       && elf_elfheader (abfd)->e_flags != flags)
11960     {
11961       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
11962         {
11963           if (flags & EF_ARM_INTERWORK)
11964             (*_bfd_error_handler)
11965               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
11966                abfd);
11967           else
11968             _bfd_error_handler
11969               (_("Warning: Clearing the interworking flag of %B due to outside request"),
11970                abfd);
11971         }
11972     }
11973   else
11974     {
11975       elf_elfheader (abfd)->e_flags = flags;
11976       elf_flags_init (abfd) = TRUE;
11977     }
11978
11979   return TRUE;
11980 }
11981
11982 /* Copy backend specific data from one object module to another.  */
11983
11984 static bfd_boolean
11985 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
11986 {
11987   flagword in_flags;
11988   flagword out_flags;
11989
11990   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
11991     return TRUE;
11992
11993   in_flags  = elf_elfheader (ibfd)->e_flags;
11994   out_flags = elf_elfheader (obfd)->e_flags;
11995
11996   if (elf_flags_init (obfd)
11997       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
11998       && in_flags != out_flags)
11999     {
12000       /* Cannot mix APCS26 and APCS32 code.  */
12001       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
12002         return FALSE;
12003
12004       /* Cannot mix float APCS and non-float APCS code.  */
12005       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
12006         return FALSE;
12007
12008       /* If the src and dest have different interworking flags
12009          then turn off the interworking bit.  */
12010       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
12011         {
12012           if (out_flags & EF_ARM_INTERWORK)
12013             _bfd_error_handler
12014               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
12015                obfd, ibfd);
12016
12017           in_flags &= ~EF_ARM_INTERWORK;
12018         }
12019
12020       /* Likewise for PIC, though don't warn for this case.  */
12021       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
12022         in_flags &= ~EF_ARM_PIC;
12023     }
12024
12025   elf_elfheader (obfd)->e_flags = in_flags;
12026   elf_flags_init (obfd) = TRUE;
12027
12028   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
12029 }
12030
12031 /* Values for Tag_ABI_PCS_R9_use.  */
12032 enum
12033 {
12034   AEABI_R9_V6,
12035   AEABI_R9_SB,
12036   AEABI_R9_TLS,
12037   AEABI_R9_unused
12038 };
12039
12040 /* Values for Tag_ABI_PCS_RW_data.  */
12041 enum
12042 {
12043   AEABI_PCS_RW_data_absolute,
12044   AEABI_PCS_RW_data_PCrel,
12045   AEABI_PCS_RW_data_SBrel,
12046   AEABI_PCS_RW_data_unused
12047 };
12048
12049 /* Values for Tag_ABI_enum_size.  */
12050 enum
12051 {
12052   AEABI_enum_unused,
12053   AEABI_enum_short,
12054   AEABI_enum_wide,
12055   AEABI_enum_forced_wide
12056 };
12057
12058 /* Determine whether an object attribute tag takes an integer, a
12059    string or both.  */
12060
12061 static int
12062 elf32_arm_obj_attrs_arg_type (int tag)
12063 {
12064   if (tag == Tag_compatibility)
12065     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
12066   else if (tag == Tag_nodefaults)
12067     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
12068   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
12069     return ATTR_TYPE_FLAG_STR_VAL;
12070   else if (tag < 32)
12071     return ATTR_TYPE_FLAG_INT_VAL;
12072   else
12073     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
12074 }
12075
12076 /* The ABI defines that Tag_conformance should be emitted first, and that
12077    Tag_nodefaults should be second (if either is defined).  This sets those
12078    two positions, and bumps up the position of all the remaining tags to
12079    compensate.  */
12080 static int
12081 elf32_arm_obj_attrs_order (int num)
12082 {
12083   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
12084     return Tag_conformance;
12085   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
12086     return Tag_nodefaults;
12087   if ((num - 2) < Tag_nodefaults)
12088     return num - 2;
12089   if ((num - 1) < Tag_conformance)
12090     return num - 1;
12091   return num;
12092 }
12093
12094 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
12095 static bfd_boolean
12096 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
12097 {
12098   if ((tag & 127) < 64)
12099     {
12100       _bfd_error_handler
12101         (_("%B: Unknown mandatory EABI object attribute %d"),
12102          abfd, tag);
12103       bfd_set_error (bfd_error_bad_value);
12104       return FALSE;
12105     }
12106   else
12107     {
12108       _bfd_error_handler
12109         (_("Warning: %B: Unknown EABI object attribute %d"),
12110          abfd, tag);
12111       return TRUE;
12112     }
12113 }
12114
12115 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
12116    Returns -1 if no architecture could be read.  */
12117
12118 static int
12119 get_secondary_compatible_arch (bfd *abfd)
12120 {
12121   obj_attribute *attr =
12122     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
12123
12124   /* Note: the tag and its argument below are uleb128 values, though
12125      currently-defined values fit in one byte for each.  */
12126   if (attr->s
12127       && attr->s[0] == Tag_CPU_arch
12128       && (attr->s[1] & 128) != 128
12129       && attr->s[2] == 0)
12130    return attr->s[1];
12131
12132   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
12133   return -1;
12134 }
12135
12136 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
12137    The tag is removed if ARCH is -1.  */
12138
12139 static void
12140 set_secondary_compatible_arch (bfd *abfd, int arch)
12141 {
12142   obj_attribute *attr =
12143     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
12144
12145   if (arch == -1)
12146     {
12147       attr->s = NULL;
12148       return;
12149     }
12150
12151   /* Note: the tag and its argument below are uleb128 values, though
12152      currently-defined values fit in one byte for each.  */
12153   if (!attr->s)
12154     attr->s = (char *) bfd_alloc (abfd, 3);
12155   attr->s[0] = Tag_CPU_arch;
12156   attr->s[1] = arch;
12157   attr->s[2] = '\0';
12158 }
12159
12160 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
12161    into account.  */
12162
12163 static int
12164 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
12165                       int newtag, int secondary_compat)
12166 {
12167 #define T(X) TAG_CPU_ARCH_##X
12168   int tagl, tagh, result;
12169   const int v6t2[] =
12170     {
12171       T(V6T2),   /* PRE_V4.  */
12172       T(V6T2),   /* V4.  */
12173       T(V6T2),   /* V4T.  */
12174       T(V6T2),   /* V5T.  */
12175       T(V6T2),   /* V5TE.  */
12176       T(V6T2),   /* V5TEJ.  */
12177       T(V6T2),   /* V6.  */
12178       T(V7),     /* V6KZ.  */
12179       T(V6T2)    /* V6T2.  */
12180     };
12181   const int v6k[] =
12182     {
12183       T(V6K),    /* PRE_V4.  */
12184       T(V6K),    /* V4.  */
12185       T(V6K),    /* V4T.  */
12186       T(V6K),    /* V5T.  */
12187       T(V6K),    /* V5TE.  */
12188       T(V6K),    /* V5TEJ.  */
12189       T(V6K),    /* V6.  */
12190       T(V6KZ),   /* V6KZ.  */
12191       T(V7),     /* V6T2.  */
12192       T(V6K)     /* V6K.  */
12193     };
12194   const int v7[] =
12195     {
12196       T(V7),     /* PRE_V4.  */
12197       T(V7),     /* V4.  */
12198       T(V7),     /* V4T.  */
12199       T(V7),     /* V5T.  */
12200       T(V7),     /* V5TE.  */
12201       T(V7),     /* V5TEJ.  */
12202       T(V7),     /* V6.  */
12203       T(V7),     /* V6KZ.  */
12204       T(V7),     /* V6T2.  */
12205       T(V7),     /* V6K.  */
12206       T(V7)      /* V7.  */
12207     };
12208   const int v6_m[] =
12209     {
12210       -1,        /* PRE_V4.  */
12211       -1,        /* V4.  */
12212       T(V6K),    /* V4T.  */
12213       T(V6K),    /* V5T.  */
12214       T(V6K),    /* V5TE.  */
12215       T(V6K),    /* V5TEJ.  */
12216       T(V6K),    /* V6.  */
12217       T(V6KZ),   /* V6KZ.  */
12218       T(V7),     /* V6T2.  */
12219       T(V6K),    /* V6K.  */
12220       T(V7),     /* V7.  */
12221       T(V6_M)    /* V6_M.  */
12222     };
12223   const int v6s_m[] =
12224     {
12225       -1,        /* PRE_V4.  */
12226       -1,        /* V4.  */
12227       T(V6K),    /* V4T.  */
12228       T(V6K),    /* V5T.  */
12229       T(V6K),    /* V5TE.  */
12230       T(V6K),    /* V5TEJ.  */
12231       T(V6K),    /* V6.  */
12232       T(V6KZ),   /* V6KZ.  */
12233       T(V7),     /* V6T2.  */
12234       T(V6K),    /* V6K.  */
12235       T(V7),     /* V7.  */
12236       T(V6S_M),  /* V6_M.  */
12237       T(V6S_M)   /* V6S_M.  */
12238     };
12239   const int v7e_m[] =
12240     {
12241       -1,        /* PRE_V4.  */
12242       -1,        /* V4.  */
12243       T(V7E_M),  /* V4T.  */
12244       T(V7E_M),  /* V5T.  */
12245       T(V7E_M),  /* V5TE.  */
12246       T(V7E_M),  /* V5TEJ.  */
12247       T(V7E_M),  /* V6.  */
12248       T(V7E_M),  /* V6KZ.  */
12249       T(V7E_M),  /* V6T2.  */
12250       T(V7E_M),  /* V6K.  */
12251       T(V7E_M),  /* V7.  */
12252       T(V7E_M),  /* V6_M.  */
12253       T(V7E_M),  /* V6S_M.  */
12254       T(V7E_M)   /* V7E_M.  */
12255     };
12256   const int v8[] =
12257     {
12258       T(V8),            /* PRE_V4.  */
12259       T(V8),            /* V4.  */
12260       T(V8),            /* V4T.  */
12261       T(V8),            /* V5T.  */
12262       T(V8),            /* V5TE.  */
12263       T(V8),            /* V5TEJ.  */
12264       T(V8),            /* V6.  */
12265       T(V8),            /* V6KZ.  */
12266       T(V8),            /* V6T2.  */
12267       T(V8),            /* V6K.  */
12268       T(V8),            /* V7.  */
12269       T(V8),            /* V6_M.  */
12270       T(V8),            /* V6S_M.  */
12271       T(V8),            /* V7E_M.  */
12272       T(V8)             /* V8.  */
12273     };
12274   const int v4t_plus_v6_m[] =
12275     {
12276       -1,               /* PRE_V4.  */
12277       -1,               /* V4.  */
12278       T(V4T),           /* V4T.  */
12279       T(V5T),           /* V5T.  */
12280       T(V5TE),          /* V5TE.  */
12281       T(V5TEJ),         /* V5TEJ.  */
12282       T(V6),            /* V6.  */
12283       T(V6KZ),          /* V6KZ.  */
12284       T(V6T2),          /* V6T2.  */
12285       T(V6K),           /* V6K.  */
12286       T(V7),            /* V7.  */
12287       T(V6_M),          /* V6_M.  */
12288       T(V6S_M),         /* V6S_M.  */
12289       T(V7E_M),         /* V7E_M.  */
12290       T(V8),            /* V8.  */
12291       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
12292     };
12293   const int *comb[] =
12294     {
12295       v6t2,
12296       v6k,
12297       v7,
12298       v6_m,
12299       v6s_m,
12300       v7e_m,
12301       v8,
12302       /* Pseudo-architecture.  */
12303       v4t_plus_v6_m
12304     };
12305
12306   /* Check we've not got a higher architecture than we know about.  */
12307
12308   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
12309     {
12310       _bfd_error_handler (_("error: %B: Unknown CPU architecture"), ibfd);
12311       return -1;
12312     }
12313
12314   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
12315
12316   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
12317       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
12318     oldtag = T(V4T_PLUS_V6_M);
12319
12320   /* And override the new tag if we have a Tag_also_compatible_with on the
12321      input.  */
12322
12323   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
12324       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
12325     newtag = T(V4T_PLUS_V6_M);
12326
12327   tagl = (oldtag < newtag) ? oldtag : newtag;
12328   result = tagh = (oldtag > newtag) ? oldtag : newtag;
12329
12330   /* Architectures before V6KZ add features monotonically.  */
12331   if (tagh <= TAG_CPU_ARCH_V6KZ)
12332     return result;
12333
12334   result = comb[tagh - T(V6T2)][tagl];
12335
12336   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
12337      as the canonical version.  */
12338   if (result == T(V4T_PLUS_V6_M))
12339     {
12340       result = T(V4T);
12341       *secondary_compat_out = T(V6_M);
12342     }
12343   else
12344     *secondary_compat_out = -1;
12345
12346   if (result == -1)
12347     {
12348       _bfd_error_handler (_("error: %B: Conflicting CPU architectures %d/%d"),
12349                           ibfd, oldtag, newtag);
12350       return -1;
12351     }
12352
12353   return result;
12354 #undef T
12355 }
12356
12357 /* Query attributes object to see if integer divide instructions may be
12358    present in an object.  */
12359 static bfd_boolean
12360 elf32_arm_attributes_accept_div (const obj_attribute *attr)
12361 {
12362   int arch = attr[Tag_CPU_arch].i;
12363   int profile = attr[Tag_CPU_arch_profile].i;
12364
12365   switch (attr[Tag_DIV_use].i)
12366     {
12367     case 0:
12368       /* Integer divide allowed if instruction contained in archetecture.  */
12369       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
12370         return TRUE;
12371       else if (arch >= TAG_CPU_ARCH_V7E_M)
12372         return TRUE;
12373       else
12374         return FALSE;
12375
12376     case 1:
12377       /* Integer divide explicitly prohibited.  */
12378       return FALSE;
12379
12380     default:
12381       /* Unrecognised case - treat as allowing divide everywhere.  */
12382     case 2:
12383       /* Integer divide allowed in ARM state.  */
12384       return TRUE;
12385     }
12386 }
12387
12388 /* Query attributes object to see if integer divide instructions are
12389    forbidden to be in the object.  This is not the inverse of
12390    elf32_arm_attributes_accept_div.  */
12391 static bfd_boolean
12392 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
12393 {
12394   return attr[Tag_DIV_use].i == 1;
12395 }
12396
12397 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
12398    are conflicting attributes.  */
12399
12400 static bfd_boolean
12401 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
12402 {
12403   obj_attribute *in_attr;
12404   obj_attribute *out_attr;
12405   /* Some tags have 0 = don't care, 1 = strong requirement,
12406      2 = weak requirement.  */
12407   static const int order_021[3] = {0, 2, 1};
12408   int i;
12409   bfd_boolean result = TRUE;
12410   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
12411
12412   /* Skip the linker stubs file.  This preserves previous behavior
12413      of accepting unknown attributes in the first input file - but
12414      is that a bug?  */
12415   if (ibfd->flags & BFD_LINKER_CREATED)
12416     return TRUE;
12417
12418   /* Skip any input that hasn't attribute section.
12419      This enables to link object files without attribute section with
12420      any others.  */
12421   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
12422     return TRUE;
12423
12424   if (!elf_known_obj_attributes_proc (obfd)[0].i)
12425     {
12426       /* This is the first object.  Copy the attributes.  */
12427       _bfd_elf_copy_obj_attributes (ibfd, obfd);
12428
12429       out_attr = elf_known_obj_attributes_proc (obfd);
12430
12431       /* Use the Tag_null value to indicate the attributes have been
12432          initialized.  */
12433       out_attr[0].i = 1;
12434
12435       /* We do not output objects with Tag_MPextension_use_legacy - we move
12436          the attribute's value to Tag_MPextension_use.  */
12437       if (out_attr[Tag_MPextension_use_legacy].i != 0)
12438         {
12439           if (out_attr[Tag_MPextension_use].i != 0
12440               && out_attr[Tag_MPextension_use_legacy].i
12441                 != out_attr[Tag_MPextension_use].i)
12442             {
12443               _bfd_error_handler
12444                 (_("Error: %B has both the current and legacy "
12445                    "Tag_MPextension_use attributes"), ibfd);
12446               result = FALSE;
12447             }
12448
12449           out_attr[Tag_MPextension_use] =
12450             out_attr[Tag_MPextension_use_legacy];
12451           out_attr[Tag_MPextension_use_legacy].type = 0;
12452           out_attr[Tag_MPextension_use_legacy].i = 0;
12453         }
12454
12455       return result;
12456     }
12457
12458   in_attr = elf_known_obj_attributes_proc (ibfd);
12459   out_attr = elf_known_obj_attributes_proc (obfd);
12460   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
12461   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
12462     {
12463       /* Ignore mismatches if the object doesn't use floating point or is
12464          floating point ABI independent.  */
12465       if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
12466           || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
12467               && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
12468         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
12469       else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
12470                && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
12471         {
12472           _bfd_error_handler
12473             (_("error: %B uses VFP register arguments, %B does not"),
12474              in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
12475              in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
12476           result = FALSE;
12477         }
12478     }
12479
12480   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
12481     {
12482       /* Merge this attribute with existing attributes.  */
12483       switch (i)
12484         {
12485         case Tag_CPU_raw_name:
12486         case Tag_CPU_name:
12487           /* These are merged after Tag_CPU_arch.  */
12488           break;
12489
12490         case Tag_ABI_optimization_goals:
12491         case Tag_ABI_FP_optimization_goals:
12492           /* Use the first value seen.  */
12493           break;
12494
12495         case Tag_CPU_arch:
12496           {
12497             int secondary_compat = -1, secondary_compat_out = -1;
12498             unsigned int saved_out_attr = out_attr[i].i;
12499             int arch_attr;
12500             static const char *name_table[] =
12501               {
12502                 /* These aren't real CPU names, but we can't guess
12503                    that from the architecture version alone.  */
12504                 "Pre v4",
12505                 "ARM v4",
12506                 "ARM v4T",
12507                 "ARM v5T",
12508                 "ARM v5TE",
12509                 "ARM v5TEJ",
12510                 "ARM v6",
12511                 "ARM v6KZ",
12512                 "ARM v6T2",
12513                 "ARM v6K",
12514                 "ARM v7",
12515                 "ARM v6-M",
12516                 "ARM v6S-M",
12517                 "ARM v8"
12518             };
12519
12520             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
12521             secondary_compat = get_secondary_compatible_arch (ibfd);
12522             secondary_compat_out = get_secondary_compatible_arch (obfd);
12523             arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
12524                                               &secondary_compat_out,
12525                                               in_attr[i].i,
12526                                               secondary_compat);
12527
12528             /* Return with error if failed to merge.  */
12529             if (arch_attr == -1)
12530               return FALSE;
12531
12532             out_attr[i].i = arch_attr;
12533
12534             set_secondary_compatible_arch (obfd, secondary_compat_out);
12535
12536             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
12537             if (out_attr[i].i == saved_out_attr)
12538               ; /* Leave the names alone.  */
12539             else if (out_attr[i].i == in_attr[i].i)
12540               {
12541                 /* The output architecture has been changed to match the
12542                    input architecture.  Use the input names.  */
12543                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
12544                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
12545                   : NULL;
12546                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
12547                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
12548                   : NULL;
12549               }
12550             else
12551               {
12552                 out_attr[Tag_CPU_name].s = NULL;
12553                 out_attr[Tag_CPU_raw_name].s = NULL;
12554               }
12555
12556             /* If we still don't have a value for Tag_CPU_name,
12557                make one up now.  Tag_CPU_raw_name remains blank.  */
12558             if (out_attr[Tag_CPU_name].s == NULL
12559                 && out_attr[i].i < ARRAY_SIZE (name_table))
12560               out_attr[Tag_CPU_name].s =
12561                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
12562           }
12563           break;
12564
12565         case Tag_ARM_ISA_use:
12566         case Tag_THUMB_ISA_use:
12567         case Tag_WMMX_arch:
12568         case Tag_Advanced_SIMD_arch:
12569           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
12570         case Tag_ABI_FP_rounding:
12571         case Tag_ABI_FP_exceptions:
12572         case Tag_ABI_FP_user_exceptions:
12573         case Tag_ABI_FP_number_model:
12574         case Tag_FP_HP_extension:
12575         case Tag_CPU_unaligned_access:
12576         case Tag_T2EE_use:
12577         case Tag_MPextension_use:
12578           /* Use the largest value specified.  */
12579           if (in_attr[i].i > out_attr[i].i)
12580             out_attr[i].i = in_attr[i].i;
12581           break;
12582
12583         case Tag_ABI_align_preserved:
12584         case Tag_ABI_PCS_RO_data:
12585           /* Use the smallest value specified.  */
12586           if (in_attr[i].i < out_attr[i].i)
12587             out_attr[i].i = in_attr[i].i;
12588           break;
12589
12590         case Tag_ABI_align_needed:
12591           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
12592               && (in_attr[Tag_ABI_align_preserved].i == 0
12593                   || out_attr[Tag_ABI_align_preserved].i == 0))
12594             {
12595               /* This error message should be enabled once all non-conformant
12596                  binaries in the toolchain have had the attributes set
12597                  properly.
12598               _bfd_error_handler
12599                 (_("error: %B: 8-byte data alignment conflicts with %B"),
12600                  obfd, ibfd);
12601               result = FALSE; */
12602             }
12603           /* Fall through.  */
12604         case Tag_ABI_FP_denormal:
12605         case Tag_ABI_PCS_GOT_use:
12606           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
12607              value if greater than 2 (for future-proofing).  */
12608           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
12609               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
12610                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
12611             out_attr[i].i = in_attr[i].i;
12612           break;
12613
12614         case Tag_Virtualization_use:
12615           /* The virtualization tag effectively stores two bits of
12616              information: the intended use of TrustZone (in bit 0), and the
12617              intended use of Virtualization (in bit 1).  */
12618           if (out_attr[i].i == 0)
12619             out_attr[i].i = in_attr[i].i;
12620           else if (in_attr[i].i != 0
12621                    && in_attr[i].i != out_attr[i].i)
12622             {
12623               if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
12624                 out_attr[i].i = 3;
12625               else
12626                 {
12627                   _bfd_error_handler
12628                     (_("error: %B: unable to merge virtualization attributes "
12629                        "with %B"),
12630                      obfd, ibfd);
12631                   result = FALSE;
12632                 }
12633             }
12634           break;
12635
12636         case Tag_CPU_arch_profile:
12637           if (out_attr[i].i != in_attr[i].i)
12638             {
12639               /* 0 will merge with anything.
12640                  'A' and 'S' merge to 'A'.
12641                  'R' and 'S' merge to 'R'.
12642                  'M' and 'A|R|S' is an error.  */
12643               if (out_attr[i].i == 0
12644                   || (out_attr[i].i == 'S'
12645                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
12646                 out_attr[i].i = in_attr[i].i;
12647               else if (in_attr[i].i == 0
12648                        || (in_attr[i].i == 'S'
12649                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
12650                 ; /* Do nothing.  */
12651               else
12652                 {
12653                   _bfd_error_handler
12654                     (_("error: %B: Conflicting architecture profiles %c/%c"),
12655                      ibfd,
12656                      in_attr[i].i ? in_attr[i].i : '0',
12657                      out_attr[i].i ? out_attr[i].i : '0');
12658                   result = FALSE;
12659                 }
12660             }
12661           break;
12662         case Tag_FP_arch:
12663             {
12664               /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
12665                  the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
12666                  when it's 0.  It might mean absence of FP hardware if
12667                  Tag_FP_arch is zero.  */
12668
12669 #define VFP_VERSION_COUNT 9
12670               static const struct
12671               {
12672                   int ver;
12673                   int regs;
12674               } vfp_versions[VFP_VERSION_COUNT] =
12675                 {
12676                   {0, 0},
12677                   {1, 16},
12678                   {2, 16},
12679                   {3, 32},
12680                   {3, 16},
12681                   {4, 32},
12682                   {4, 16},
12683                   {8, 32},
12684                   {8, 16}
12685                 };
12686               int ver;
12687               int regs;
12688               int newval;
12689
12690               /* If the output has no requirement about FP hardware,
12691                  follow the requirement of the input.  */
12692               if (out_attr[i].i == 0)
12693                 {
12694                   BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
12695                   out_attr[i].i = in_attr[i].i;
12696                   out_attr[Tag_ABI_HardFP_use].i
12697                     = in_attr[Tag_ABI_HardFP_use].i;
12698                   break;
12699                 }
12700               /* If the input has no requirement about FP hardware, do
12701                  nothing.  */
12702               else if (in_attr[i].i == 0)
12703                 {
12704                   BFD_ASSERT (in_attr[Tag_ABI_HardFP_use].i == 0);
12705                   break;
12706                 }
12707
12708               /* Both the input and the output have nonzero Tag_FP_arch.
12709                  So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
12710
12711               /* If both the input and the output have zero Tag_ABI_HardFP_use,
12712                  do nothing.  */
12713               if (in_attr[Tag_ABI_HardFP_use].i == 0
12714                   && out_attr[Tag_ABI_HardFP_use].i == 0)
12715                 ;
12716               /* If the input and the output have different Tag_ABI_HardFP_use,
12717                  the combination of them is 0 (implied by Tag_FP_arch).  */
12718               else if (in_attr[Tag_ABI_HardFP_use].i
12719                        != out_attr[Tag_ABI_HardFP_use].i)
12720                 out_attr[Tag_ABI_HardFP_use].i = 0;
12721
12722               /* Now we can handle Tag_FP_arch.  */
12723
12724               /* Values of VFP_VERSION_COUNT or more aren't defined, so just
12725                  pick the biggest.  */
12726               if (in_attr[i].i >= VFP_VERSION_COUNT
12727                   && in_attr[i].i > out_attr[i].i)
12728                 {
12729                   out_attr[i] = in_attr[i];
12730                   break;
12731                 }
12732               /* The output uses the superset of input features
12733                  (ISA version) and registers.  */
12734               ver = vfp_versions[in_attr[i].i].ver;
12735               if (ver < vfp_versions[out_attr[i].i].ver)
12736                 ver = vfp_versions[out_attr[i].i].ver;
12737               regs = vfp_versions[in_attr[i].i].regs;
12738               if (regs < vfp_versions[out_attr[i].i].regs)
12739                 regs = vfp_versions[out_attr[i].i].regs;
12740               /* This assumes all possible supersets are also a valid
12741                  options.  */
12742               for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
12743                 {
12744                   if (regs == vfp_versions[newval].regs
12745                       && ver == vfp_versions[newval].ver)
12746                     break;
12747                 }
12748               out_attr[i].i = newval;
12749             }
12750           break;
12751         case Tag_PCS_config:
12752           if (out_attr[i].i == 0)
12753             out_attr[i].i = in_attr[i].i;
12754           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
12755             {
12756               /* It's sometimes ok to mix different configs, so this is only
12757                  a warning.  */
12758               _bfd_error_handler
12759                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
12760             }
12761           break;
12762         case Tag_ABI_PCS_R9_use:
12763           if (in_attr[i].i != out_attr[i].i
12764               && out_attr[i].i != AEABI_R9_unused
12765               && in_attr[i].i != AEABI_R9_unused)
12766             {
12767               _bfd_error_handler
12768                 (_("error: %B: Conflicting use of R9"), ibfd);
12769               result = FALSE;
12770             }
12771           if (out_attr[i].i == AEABI_R9_unused)
12772             out_attr[i].i = in_attr[i].i;
12773           break;
12774         case Tag_ABI_PCS_RW_data:
12775           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
12776               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
12777               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
12778             {
12779               _bfd_error_handler
12780                 (_("error: %B: SB relative addressing conflicts with use of R9"),
12781                  ibfd);
12782               result = FALSE;
12783             }
12784           /* Use the smallest value specified.  */
12785           if (in_attr[i].i < out_attr[i].i)
12786             out_attr[i].i = in_attr[i].i;
12787           break;
12788         case Tag_ABI_PCS_wchar_t:
12789           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
12790               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
12791             {
12792               _bfd_error_handler
12793                 (_("warning: %B uses %u-byte wchar_t yet the output is to use %u-byte wchar_t; use of wchar_t values across objects may fail"),
12794                  ibfd, in_attr[i].i, out_attr[i].i);
12795             }
12796           else if (in_attr[i].i && !out_attr[i].i)
12797             out_attr[i].i = in_attr[i].i;
12798           break;
12799         case Tag_ABI_enum_size:
12800           if (in_attr[i].i != AEABI_enum_unused)
12801             {
12802               if (out_attr[i].i == AEABI_enum_unused
12803                   || out_attr[i].i == AEABI_enum_forced_wide)
12804                 {
12805                   /* The existing object is compatible with anything.
12806                      Use whatever requirements the new object has.  */
12807                   out_attr[i].i = in_attr[i].i;
12808                 }
12809               else if (in_attr[i].i != AEABI_enum_forced_wide
12810                        && out_attr[i].i != in_attr[i].i
12811                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
12812                 {
12813                   static const char *aeabi_enum_names[] =
12814                     { "", "variable-size", "32-bit", "" };
12815                   const char *in_name =
12816                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
12817                     ? aeabi_enum_names[in_attr[i].i]
12818                     : "<unknown>";
12819                   const char *out_name =
12820                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
12821                     ? aeabi_enum_names[out_attr[i].i]
12822                     : "<unknown>";
12823                   _bfd_error_handler
12824                     (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
12825                      ibfd, in_name, out_name);
12826                 }
12827             }
12828           break;
12829         case Tag_ABI_VFP_args:
12830           /* Aready done.  */
12831           break;
12832         case Tag_ABI_WMMX_args:
12833           if (in_attr[i].i != out_attr[i].i)
12834             {
12835               _bfd_error_handler
12836                 (_("error: %B uses iWMMXt register arguments, %B does not"),
12837                  ibfd, obfd);
12838               result = FALSE;
12839             }
12840           break;
12841         case Tag_compatibility:
12842           /* Merged in target-independent code.  */
12843           break;
12844         case Tag_ABI_HardFP_use:
12845           /* This is handled along with Tag_FP_arch.  */
12846           break;
12847         case Tag_ABI_FP_16bit_format:
12848           if (in_attr[i].i != 0 && out_attr[i].i != 0)
12849             {
12850               if (in_attr[i].i != out_attr[i].i)
12851                 {
12852                   _bfd_error_handler
12853                     (_("error: fp16 format mismatch between %B and %B"),
12854                      ibfd, obfd);
12855                   result = FALSE;
12856                 }
12857             }
12858           if (in_attr[i].i != 0)
12859             out_attr[i].i = in_attr[i].i;
12860           break;
12861
12862         case Tag_DIV_use:
12863           /* A value of zero on input means that the divide instruction may
12864              be used if available in the base architecture as specified via
12865              Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
12866              the user did not want divide instructions.  A value of 2
12867              explicitly means that divide instructions were allowed in ARM
12868              and Thumb state.  */
12869           if (in_attr[i].i == out_attr[i].i)
12870             /* Do nothing.  */ ;
12871           else if (elf32_arm_attributes_forbid_div (in_attr)
12872                    && !elf32_arm_attributes_accept_div (out_attr))
12873             out_attr[i].i = 1;
12874           else if (elf32_arm_attributes_forbid_div (out_attr)
12875                    && elf32_arm_attributes_accept_div (in_attr))
12876             out_attr[i].i = in_attr[i].i;
12877           else if (in_attr[i].i == 2)
12878             out_attr[i].i = in_attr[i].i;
12879           break;
12880
12881         case Tag_MPextension_use_legacy:
12882           /* We don't output objects with Tag_MPextension_use_legacy - we
12883              move the value to Tag_MPextension_use.  */
12884           if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
12885             {
12886               if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
12887                 {
12888                   _bfd_error_handler
12889                     (_("%B has has both the current and legacy "
12890                        "Tag_MPextension_use attributes"),
12891                      ibfd);
12892                   result = FALSE;
12893                 }
12894             }
12895
12896           if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
12897             out_attr[Tag_MPextension_use] = in_attr[i];
12898
12899           break;
12900
12901         case Tag_nodefaults:
12902           /* This tag is set if it exists, but the value is unused (and is
12903              typically zero).  We don't actually need to do anything here -
12904              the merge happens automatically when the type flags are merged
12905              below.  */
12906           break;
12907         case Tag_also_compatible_with:
12908           /* Already done in Tag_CPU_arch.  */
12909           break;
12910         case Tag_conformance:
12911           /* Keep the attribute if it matches.  Throw it away otherwise.
12912              No attribute means no claim to conform.  */
12913           if (!in_attr[i].s || !out_attr[i].s
12914               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
12915             out_attr[i].s = NULL;
12916           break;
12917
12918         default:
12919           result
12920             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
12921         }
12922
12923       /* If out_attr was copied from in_attr then it won't have a type yet.  */
12924       if (in_attr[i].type && !out_attr[i].type)
12925         out_attr[i].type = in_attr[i].type;
12926     }
12927
12928   /* Merge Tag_compatibility attributes and any common GNU ones.  */
12929   if (!_bfd_elf_merge_object_attributes (ibfd, obfd))
12930     return FALSE;
12931
12932   /* Check for any attributes not known on ARM.  */
12933   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
12934
12935   return result;
12936 }
12937
12938
12939 /* Return TRUE if the two EABI versions are incompatible.  */
12940
12941 static bfd_boolean
12942 elf32_arm_versions_compatible (unsigned iver, unsigned over)
12943 {
12944   /* v4 and v5 are the same spec before and after it was released,
12945      so allow mixing them.  */
12946   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
12947       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
12948     return TRUE;
12949
12950   return (iver == over);
12951 }
12952
12953 /* Merge backend specific data from an object file to the output
12954    object file when linking.  */
12955
12956 static bfd_boolean
12957 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd);
12958
12959 /* Display the flags field.  */
12960
12961 static bfd_boolean
12962 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
12963 {
12964   FILE * file = (FILE *) ptr;
12965   unsigned long flags;
12966
12967   BFD_ASSERT (abfd != NULL && ptr != NULL);
12968
12969   /* Print normal ELF private data.  */
12970   _bfd_elf_print_private_bfd_data (abfd, ptr);
12971
12972   flags = elf_elfheader (abfd)->e_flags;
12973   /* Ignore init flag - it may not be set, despite the flags field
12974      containing valid data.  */
12975
12976   /* xgettext:c-format */
12977   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12978
12979   switch (EF_ARM_EABI_VERSION (flags))
12980     {
12981     case EF_ARM_EABI_UNKNOWN:
12982       /* The following flag bits are GNU extensions and not part of the
12983          official ARM ELF extended ABI.  Hence they are only decoded if
12984          the EABI version is not set.  */
12985       if (flags & EF_ARM_INTERWORK)
12986         fprintf (file, _(" [interworking enabled]"));
12987
12988       if (flags & EF_ARM_APCS_26)
12989         fprintf (file, " [APCS-26]");
12990       else
12991         fprintf (file, " [APCS-32]");
12992
12993       if (flags & EF_ARM_VFP_FLOAT)
12994         fprintf (file, _(" [VFP float format]"));
12995       else if (flags & EF_ARM_MAVERICK_FLOAT)
12996         fprintf (file, _(" [Maverick float format]"));
12997       else
12998         fprintf (file, _(" [FPA float format]"));
12999
13000       if (flags & EF_ARM_APCS_FLOAT)
13001         fprintf (file, _(" [floats passed in float registers]"));
13002
13003       if (flags & EF_ARM_PIC)
13004         fprintf (file, _(" [position independent]"));
13005
13006       if (flags & EF_ARM_NEW_ABI)
13007         fprintf (file, _(" [new ABI]"));
13008
13009       if (flags & EF_ARM_OLD_ABI)
13010         fprintf (file, _(" [old ABI]"));
13011
13012       if (flags & EF_ARM_SOFT_FLOAT)
13013         fprintf (file, _(" [software FP]"));
13014
13015       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
13016                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
13017                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
13018                  | EF_ARM_MAVERICK_FLOAT);
13019       break;
13020
13021     case EF_ARM_EABI_VER1:
13022       fprintf (file, _(" [Version1 EABI]"));
13023
13024       if (flags & EF_ARM_SYMSARESORTED)
13025         fprintf (file, _(" [sorted symbol table]"));
13026       else
13027         fprintf (file, _(" [unsorted symbol table]"));
13028
13029       flags &= ~ EF_ARM_SYMSARESORTED;
13030       break;
13031
13032     case EF_ARM_EABI_VER2:
13033       fprintf (file, _(" [Version2 EABI]"));
13034
13035       if (flags & EF_ARM_SYMSARESORTED)
13036         fprintf (file, _(" [sorted symbol table]"));
13037       else
13038         fprintf (file, _(" [unsorted symbol table]"));
13039
13040       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
13041         fprintf (file, _(" [dynamic symbols use segment index]"));
13042
13043       if (flags & EF_ARM_MAPSYMSFIRST)
13044         fprintf (file, _(" [mapping symbols precede others]"));
13045
13046       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
13047                  | EF_ARM_MAPSYMSFIRST);
13048       break;
13049
13050     case EF_ARM_EABI_VER3:
13051       fprintf (file, _(" [Version3 EABI]"));
13052       break;
13053
13054     case EF_ARM_EABI_VER4:
13055       fprintf (file, _(" [Version4 EABI]"));
13056       goto eabi;
13057
13058     case EF_ARM_EABI_VER5:
13059       fprintf (file, _(" [Version5 EABI]"));
13060
13061       if (flags & EF_ARM_ABI_FLOAT_SOFT)
13062         fprintf (file, _(" [soft-float ABI]"));
13063
13064       if (flags & EF_ARM_ABI_FLOAT_HARD)
13065         fprintf (file, _(" [hard-float ABI]"));
13066
13067       flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
13068
13069     eabi:
13070       if (flags & EF_ARM_BE8)
13071         fprintf (file, _(" [BE8]"));
13072
13073       if (flags & EF_ARM_LE8)
13074         fprintf (file, _(" [LE8]"));
13075
13076       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
13077       break;
13078
13079     default:
13080       fprintf (file, _(" <EABI version unrecognised>"));
13081       break;
13082     }
13083
13084   flags &= ~ EF_ARM_EABIMASK;
13085
13086   if (flags & EF_ARM_RELEXEC)
13087     fprintf (file, _(" [relocatable executable]"));
13088
13089   flags &= ~EF_ARM_RELEXEC;
13090
13091   if (flags)
13092     fprintf (file, _("<Unrecognised flag bits set>"));
13093
13094   fputc ('\n', file);
13095
13096   return TRUE;
13097 }
13098
13099 static int
13100 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
13101 {
13102   switch (ELF_ST_TYPE (elf_sym->st_info))
13103     {
13104     case STT_ARM_TFUNC:
13105       return ELF_ST_TYPE (elf_sym->st_info);
13106
13107     case STT_ARM_16BIT:
13108       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
13109          This allows us to distinguish between data used by Thumb instructions
13110          and non-data (which is probably code) inside Thumb regions of an
13111          executable.  */
13112       if (type != STT_OBJECT && type != STT_TLS)
13113         return ELF_ST_TYPE (elf_sym->st_info);
13114       break;
13115
13116     default:
13117       break;
13118     }
13119
13120   return type;
13121 }
13122
13123 static asection *
13124 elf32_arm_gc_mark_hook (asection *sec,
13125                         struct bfd_link_info *info,
13126                         Elf_Internal_Rela *rel,
13127                         struct elf_link_hash_entry *h,
13128                         Elf_Internal_Sym *sym)
13129 {
13130   if (h != NULL)
13131     switch (ELF32_R_TYPE (rel->r_info))
13132       {
13133       case R_ARM_GNU_VTINHERIT:
13134       case R_ARM_GNU_VTENTRY:
13135         return NULL;
13136       }
13137
13138   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
13139 }
13140
13141 /* Update the got entry reference counts for the section being removed.  */
13142
13143 static bfd_boolean
13144 elf32_arm_gc_sweep_hook (bfd *                     abfd,
13145                          struct bfd_link_info *    info,
13146                          asection *                sec,
13147                          const Elf_Internal_Rela * relocs)
13148 {
13149   Elf_Internal_Shdr *symtab_hdr;
13150   struct elf_link_hash_entry **sym_hashes;
13151   bfd_signed_vma *local_got_refcounts;
13152   const Elf_Internal_Rela *rel, *relend;
13153   struct elf32_arm_link_hash_table * globals;
13154
13155   if (bfd_link_relocatable (info))
13156     return TRUE;
13157
13158   globals = elf32_arm_hash_table (info);
13159   if (globals == NULL)
13160     return FALSE;
13161
13162   elf_section_data (sec)->local_dynrel = NULL;
13163
13164   symtab_hdr = & elf_symtab_hdr (abfd);
13165   sym_hashes = elf_sym_hashes (abfd);
13166   local_got_refcounts = elf_local_got_refcounts (abfd);
13167
13168   check_use_blx (globals);
13169
13170   relend = relocs + sec->reloc_count;
13171   for (rel = relocs; rel < relend; rel++)
13172     {
13173       unsigned long r_symndx;
13174       struct elf_link_hash_entry *h = NULL;
13175       struct elf32_arm_link_hash_entry *eh;
13176       int r_type;
13177       bfd_boolean call_reloc_p;
13178       bfd_boolean may_become_dynamic_p;
13179       bfd_boolean may_need_local_target_p;
13180       union gotplt_union *root_plt;
13181       struct arm_plt_info *arm_plt;
13182
13183       r_symndx = ELF32_R_SYM (rel->r_info);
13184       if (r_symndx >= symtab_hdr->sh_info)
13185         {
13186           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13187           while (h->root.type == bfd_link_hash_indirect
13188                  || h->root.type == bfd_link_hash_warning)
13189             h = (struct elf_link_hash_entry *) h->root.u.i.link;
13190         }
13191       eh = (struct elf32_arm_link_hash_entry *) h;
13192
13193       call_reloc_p = FALSE;
13194       may_become_dynamic_p = FALSE;
13195       may_need_local_target_p = FALSE;
13196
13197       r_type = ELF32_R_TYPE (rel->r_info);
13198       r_type = arm_real_reloc_type (globals, r_type);
13199       switch (r_type)
13200         {
13201         case R_ARM_GOT32:
13202         case R_ARM_GOT_PREL:
13203         case R_ARM_TLS_GD32:
13204         case R_ARM_TLS_IE32:
13205           if (h != NULL)
13206             {
13207               if (h->got.refcount > 0)
13208                 h->got.refcount -= 1;
13209             }
13210           else if (local_got_refcounts != NULL)
13211             {
13212               if (local_got_refcounts[r_symndx] > 0)
13213                 local_got_refcounts[r_symndx] -= 1;
13214             }
13215           break;
13216
13217         case R_ARM_TLS_LDM32:
13218           globals->tls_ldm_got.refcount -= 1;
13219           break;
13220
13221         case R_ARM_PC24:
13222         case R_ARM_PLT32:
13223         case R_ARM_CALL:
13224         case R_ARM_JUMP24:
13225         case R_ARM_PREL31:
13226         case R_ARM_THM_CALL:
13227         case R_ARM_THM_JUMP24:
13228         case R_ARM_THM_JUMP19:
13229           call_reloc_p = TRUE;
13230           may_need_local_target_p = TRUE;
13231           break;
13232
13233         case R_ARM_ABS12:
13234           if (!globals->vxworks_p)
13235             {
13236               may_need_local_target_p = TRUE;
13237               break;
13238             }
13239           /* Fall through.  */
13240         case R_ARM_ABS32:
13241         case R_ARM_ABS32_NOI:
13242         case R_ARM_REL32:
13243         case R_ARM_REL32_NOI:
13244         case R_ARM_MOVW_ABS_NC:
13245         case R_ARM_MOVT_ABS:
13246         case R_ARM_MOVW_PREL_NC:
13247         case R_ARM_MOVT_PREL:
13248         case R_ARM_THM_MOVW_ABS_NC:
13249         case R_ARM_THM_MOVT_ABS:
13250         case R_ARM_THM_MOVW_PREL_NC:
13251         case R_ARM_THM_MOVT_PREL:
13252           /* Should the interworking branches be here also?  */
13253           if ((bfd_link_pic (info) || globals->root.is_relocatable_executable)
13254               && (sec->flags & SEC_ALLOC) != 0)
13255             {
13256               if (h == NULL
13257                   && elf32_arm_howto_from_type (r_type)->pc_relative)
13258                 {
13259                   call_reloc_p = TRUE;
13260                   may_need_local_target_p = TRUE;
13261                 }
13262               else
13263                 may_become_dynamic_p = TRUE;
13264             }
13265           else
13266             may_need_local_target_p = TRUE;
13267           break;
13268
13269         default:
13270           break;
13271         }
13272
13273       if (may_need_local_target_p
13274           && elf32_arm_get_plt_info (abfd, eh, r_symndx, &root_plt, &arm_plt))
13275         {
13276           /* If PLT refcount book-keeping is wrong and too low, we'll
13277              see a zero value (going to -1) for the root PLT reference
13278              count.  */
13279           if (root_plt->refcount >= 0)
13280             {
13281               BFD_ASSERT (root_plt->refcount != 0);
13282               root_plt->refcount -= 1;
13283             }
13284           else
13285             /* A value of -1 means the symbol has become local, forced
13286                or seeing a hidden definition.  Any other negative value
13287                is an error.  */
13288             BFD_ASSERT (root_plt->refcount == -1);
13289
13290           if (!call_reloc_p)
13291             arm_plt->noncall_refcount--;
13292
13293           if (r_type == R_ARM_THM_CALL)
13294             arm_plt->maybe_thumb_refcount--;
13295
13296           if (r_type == R_ARM_THM_JUMP24
13297               || r_type == R_ARM_THM_JUMP19)
13298             arm_plt->thumb_refcount--;
13299         }
13300
13301       if (may_become_dynamic_p)
13302         {
13303           struct elf_dyn_relocs **pp;
13304           struct elf_dyn_relocs *p;
13305
13306           if (h != NULL)
13307             pp = &(eh->dyn_relocs);
13308           else
13309             {
13310               Elf_Internal_Sym *isym;
13311
13312               isym = bfd_sym_from_r_symndx (&globals->sym_cache,
13313                                             abfd, r_symndx);
13314               if (isym == NULL)
13315                 return FALSE;
13316               pp = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
13317               if (pp == NULL)
13318                 return FALSE;
13319             }
13320           for (; (p = *pp) != NULL; pp = &p->next)
13321             if (p->sec == sec)
13322               {
13323                 /* Everything must go for SEC.  */
13324                 *pp = p->next;
13325                 break;
13326               }
13327         }
13328     }
13329
13330   return TRUE;
13331 }
13332
13333 /* Look through the relocs for a section during the first phase.  */
13334
13335 static bfd_boolean
13336 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
13337                         asection *sec, const Elf_Internal_Rela *relocs)
13338 {
13339   Elf_Internal_Shdr *symtab_hdr;
13340   struct elf_link_hash_entry **sym_hashes;
13341   const Elf_Internal_Rela *rel;
13342   const Elf_Internal_Rela *rel_end;
13343   bfd *dynobj;
13344   asection *sreloc;
13345   struct elf32_arm_link_hash_table *htab;
13346   bfd_boolean call_reloc_p;
13347   bfd_boolean may_become_dynamic_p;
13348   bfd_boolean may_need_local_target_p;
13349   unsigned long nsyms;
13350
13351   if (bfd_link_relocatable (info))
13352     return TRUE;
13353
13354   BFD_ASSERT (is_arm_elf (abfd));
13355
13356   htab = elf32_arm_hash_table (info);
13357   if (htab == NULL)
13358     return FALSE;
13359
13360   sreloc = NULL;
13361
13362   /* Create dynamic sections for relocatable executables so that we can
13363      copy relocations.  */
13364   if (htab->root.is_relocatable_executable
13365       && ! htab->root.dynamic_sections_created)
13366     {
13367       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
13368         return FALSE;
13369     }
13370
13371   if (htab->root.dynobj == NULL)
13372     htab->root.dynobj = abfd;
13373   if (!create_ifunc_sections (info))
13374     return FALSE;
13375
13376   dynobj = htab->root.dynobj;
13377
13378   symtab_hdr = & elf_symtab_hdr (abfd);
13379   sym_hashes = elf_sym_hashes (abfd);
13380   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
13381
13382   rel_end = relocs + sec->reloc_count;
13383   for (rel = relocs; rel < rel_end; rel++)
13384     {
13385       Elf_Internal_Sym *isym;
13386       struct elf_link_hash_entry *h;
13387       struct elf32_arm_link_hash_entry *eh;
13388       unsigned long r_symndx;
13389       int r_type;
13390
13391       r_symndx = ELF32_R_SYM (rel->r_info);
13392       r_type = ELF32_R_TYPE (rel->r_info);
13393       r_type = arm_real_reloc_type (htab, r_type);
13394
13395       if (r_symndx >= nsyms
13396           /* PR 9934: It is possible to have relocations that do not
13397              refer to symbols, thus it is also possible to have an
13398              object file containing relocations but no symbol table.  */
13399           && (r_symndx > STN_UNDEF || nsyms > 0))
13400         {
13401           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
13402                                    r_symndx);
13403           return FALSE;
13404         }
13405
13406       h = NULL;
13407       isym = NULL;
13408       if (nsyms > 0)
13409         {
13410           if (r_symndx < symtab_hdr->sh_info)
13411             {
13412               /* A local symbol.  */
13413               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
13414                                             abfd, r_symndx);
13415               if (isym == NULL)
13416                 return FALSE;
13417             }
13418           else
13419             {
13420               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13421               while (h->root.type == bfd_link_hash_indirect
13422                      || h->root.type == bfd_link_hash_warning)
13423                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13424
13425               /* PR15323, ref flags aren't set for references in the
13426                  same object.  */
13427               h->root.non_ir_ref = 1;
13428             }
13429         }
13430
13431       eh = (struct elf32_arm_link_hash_entry *) h;
13432
13433       call_reloc_p = FALSE;
13434       may_become_dynamic_p = FALSE;
13435       may_need_local_target_p = FALSE;
13436
13437       /* Could be done earlier, if h were already available.  */
13438       r_type = elf32_arm_tls_transition (info, r_type, h);
13439       switch (r_type)
13440         {
13441           case R_ARM_GOT32:
13442           case R_ARM_GOT_PREL:
13443           case R_ARM_TLS_GD32:
13444           case R_ARM_TLS_IE32:
13445           case R_ARM_TLS_GOTDESC:
13446           case R_ARM_TLS_DESCSEQ:
13447           case R_ARM_THM_TLS_DESCSEQ:
13448           case R_ARM_TLS_CALL:
13449           case R_ARM_THM_TLS_CALL:
13450             /* This symbol requires a global offset table entry.  */
13451             {
13452               int tls_type, old_tls_type;
13453
13454               switch (r_type)
13455                 {
13456                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
13457
13458                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
13459
13460                 case R_ARM_TLS_GOTDESC:
13461                 case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
13462                 case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
13463                   tls_type = GOT_TLS_GDESC; break;
13464
13465                 default: tls_type = GOT_NORMAL; break;
13466                 }
13467
13468               if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
13469                 info->flags |= DF_STATIC_TLS;
13470
13471               if (h != NULL)
13472                 {
13473                   h->got.refcount++;
13474                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
13475                 }
13476               else
13477                 {
13478                   /* This is a global offset table entry for a local symbol.  */
13479                   if (!elf32_arm_allocate_local_sym_info (abfd))
13480                     return FALSE;
13481                   elf_local_got_refcounts (abfd)[r_symndx] += 1;
13482                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
13483                 }
13484
13485               /* If a variable is accessed with both tls methods, two
13486                  slots may be created.  */
13487               if (GOT_TLS_GD_ANY_P (old_tls_type)
13488                   && GOT_TLS_GD_ANY_P (tls_type))
13489                 tls_type |= old_tls_type;
13490
13491               /* We will already have issued an error message if there
13492                  is a TLS/non-TLS mismatch, based on the symbol
13493                  type.  So just combine any TLS types needed.  */
13494               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
13495                   && tls_type != GOT_NORMAL)
13496                 tls_type |= old_tls_type;
13497
13498               /* If the symbol is accessed in both IE and GDESC
13499                  method, we're able to relax. Turn off the GDESC flag,
13500                  without messing up with any other kind of tls types
13501                  that may be involved.  */
13502               if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
13503                 tls_type &= ~GOT_TLS_GDESC;
13504
13505               if (old_tls_type != tls_type)
13506                 {
13507                   if (h != NULL)
13508                     elf32_arm_hash_entry (h)->tls_type = tls_type;
13509                   else
13510                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
13511                 }
13512             }
13513             /* Fall through.  */
13514
13515           case R_ARM_TLS_LDM32:
13516             if (r_type == R_ARM_TLS_LDM32)
13517                 htab->tls_ldm_got.refcount++;
13518             /* Fall through.  */
13519
13520           case R_ARM_GOTOFF32:
13521           case R_ARM_GOTPC:
13522             if (htab->root.sgot == NULL
13523                 && !create_got_section (htab->root.dynobj, info))
13524               return FALSE;
13525             break;
13526
13527           case R_ARM_PC24:
13528           case R_ARM_PLT32:
13529           case R_ARM_CALL:
13530           case R_ARM_JUMP24:
13531           case R_ARM_PREL31:
13532           case R_ARM_THM_CALL:
13533           case R_ARM_THM_JUMP24:
13534           case R_ARM_THM_JUMP19:
13535             call_reloc_p = TRUE;
13536             may_need_local_target_p = TRUE;
13537             break;
13538
13539           case R_ARM_ABS12:
13540             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
13541                ldr __GOTT_INDEX__ offsets.  */
13542             if (!htab->vxworks_p)
13543               {
13544                 may_need_local_target_p = TRUE;
13545                 break;
13546               }
13547             /* Fall through.  */
13548
13549           case R_ARM_MOVW_ABS_NC:
13550           case R_ARM_MOVT_ABS:
13551           case R_ARM_THM_MOVW_ABS_NC:
13552           case R_ARM_THM_MOVT_ABS:
13553             if (bfd_link_pic (info))
13554               {
13555                 (*_bfd_error_handler)
13556                   (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
13557                    abfd, elf32_arm_howto_table_1[r_type].name,
13558                    (h) ? h->root.root.string : "a local symbol");
13559                 bfd_set_error (bfd_error_bad_value);
13560                 return FALSE;
13561               }
13562
13563             /* Fall through.  */
13564           case R_ARM_ABS32:
13565           case R_ARM_ABS32_NOI:
13566             if (h != NULL && bfd_link_executable (info))
13567               {
13568                 h->pointer_equality_needed = 1;
13569               }
13570             /* Fall through.  */
13571           case R_ARM_REL32:
13572           case R_ARM_REL32_NOI:
13573           case R_ARM_MOVW_PREL_NC:
13574           case R_ARM_MOVT_PREL:
13575           case R_ARM_THM_MOVW_PREL_NC:
13576           case R_ARM_THM_MOVT_PREL:
13577
13578             /* Should the interworking branches be listed here?  */
13579             if ((bfd_link_pic (info) || htab->root.is_relocatable_executable)
13580                 && (sec->flags & SEC_ALLOC) != 0)
13581               {
13582                 if (h == NULL
13583                     && elf32_arm_howto_from_type (r_type)->pc_relative)
13584                   {
13585                     /* In shared libraries and relocatable executables,
13586                        we treat local relative references as calls;
13587                        see the related SYMBOL_CALLS_LOCAL code in
13588                        allocate_dynrelocs.  */
13589                     call_reloc_p = TRUE;
13590                     may_need_local_target_p = TRUE;
13591                   }
13592                 else
13593                   /* We are creating a shared library or relocatable
13594                      executable, and this is a reloc against a global symbol,
13595                      or a non-PC-relative reloc against a local symbol.
13596                      We may need to copy the reloc into the output.  */
13597                   may_become_dynamic_p = TRUE;
13598               }
13599             else
13600               may_need_local_target_p = TRUE;
13601             break;
13602
13603         /* This relocation describes the C++ object vtable hierarchy.
13604            Reconstruct it for later use during GC.  */
13605         case R_ARM_GNU_VTINHERIT:
13606           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
13607             return FALSE;
13608           break;
13609
13610         /* This relocation describes which C++ vtable entries are actually
13611            used.  Record for later use during GC.  */
13612         case R_ARM_GNU_VTENTRY:
13613           BFD_ASSERT (h != NULL);
13614           if (h != NULL
13615               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
13616             return FALSE;
13617           break;
13618         }
13619
13620       if (h != NULL)
13621         {
13622           if (call_reloc_p)
13623             /* We may need a .plt entry if the function this reloc
13624                refers to is in a different object, regardless of the
13625                symbol's type.  We can't tell for sure yet, because
13626                something later might force the symbol local.  */
13627             h->needs_plt = 1;
13628           else if (may_need_local_target_p)
13629             /* If this reloc is in a read-only section, we might
13630                need a copy reloc.  We can't check reliably at this
13631                stage whether the section is read-only, as input
13632                sections have not yet been mapped to output sections.
13633                Tentatively set the flag for now, and correct in
13634                adjust_dynamic_symbol.  */
13635             h->non_got_ref = 1;
13636         }
13637
13638       if (may_need_local_target_p
13639           && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
13640         {
13641           union gotplt_union *root_plt;
13642           struct arm_plt_info *arm_plt;
13643           struct arm_local_iplt_info *local_iplt;
13644
13645           if (h != NULL)
13646             {
13647               root_plt = &h->plt;
13648               arm_plt = &eh->plt;
13649             }
13650           else
13651             {
13652               local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
13653               if (local_iplt == NULL)
13654                 return FALSE;
13655               root_plt = &local_iplt->root;
13656               arm_plt = &local_iplt->arm;
13657             }
13658
13659           /* If the symbol is a function that doesn't bind locally,
13660              this relocation will need a PLT entry.  */
13661           if (root_plt->refcount != -1)
13662             root_plt->refcount += 1;
13663
13664           if (!call_reloc_p)
13665             arm_plt->noncall_refcount++;
13666
13667           /* It's too early to use htab->use_blx here, so we have to
13668              record possible blx references separately from
13669              relocs that definitely need a thumb stub.  */
13670
13671           if (r_type == R_ARM_THM_CALL)
13672             arm_plt->maybe_thumb_refcount += 1;
13673
13674           if (r_type == R_ARM_THM_JUMP24
13675               || r_type == R_ARM_THM_JUMP19)
13676             arm_plt->thumb_refcount += 1;
13677         }
13678
13679       if (may_become_dynamic_p)
13680         {
13681           struct elf_dyn_relocs *p, **head;
13682
13683           /* Create a reloc section in dynobj.  */
13684           if (sreloc == NULL)
13685             {
13686               sreloc = _bfd_elf_make_dynamic_reloc_section
13687                 (sec, dynobj, 2, abfd, ! htab->use_rel);
13688
13689               if (sreloc == NULL)
13690                 return FALSE;
13691
13692               /* BPABI objects never have dynamic relocations mapped.  */
13693               if (htab->symbian_p)
13694                 {
13695                   flagword flags;
13696
13697                   flags = bfd_get_section_flags (dynobj, sreloc);
13698                   flags &= ~(SEC_LOAD | SEC_ALLOC);
13699                   bfd_set_section_flags (dynobj, sreloc, flags);
13700                 }
13701             }
13702
13703           /* If this is a global symbol, count the number of
13704              relocations we need for this symbol.  */
13705           if (h != NULL)
13706             head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
13707           else
13708             {
13709               head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
13710               if (head == NULL)
13711                 return FALSE;
13712             }
13713
13714           p = *head;
13715           if (p == NULL || p->sec != sec)
13716             {
13717               bfd_size_type amt = sizeof *p;
13718
13719               p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
13720               if (p == NULL)
13721                 return FALSE;
13722               p->next = *head;
13723               *head = p;
13724               p->sec = sec;
13725               p->count = 0;
13726               p->pc_count = 0;
13727             }
13728
13729           if (elf32_arm_howto_from_type (r_type)->pc_relative)
13730             p->pc_count += 1;
13731           p->count += 1;
13732         }
13733     }
13734
13735   return TRUE;
13736 }
13737
13738 /* Unwinding tables are not referenced directly.  This pass marks them as
13739    required if the corresponding code section is marked.  */
13740
13741 static bfd_boolean
13742 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
13743                                   elf_gc_mark_hook_fn gc_mark_hook)
13744 {
13745   bfd *sub;
13746   Elf_Internal_Shdr **elf_shdrp;
13747   bfd_boolean again;
13748
13749   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
13750
13751   /* Marking EH data may cause additional code sections to be marked,
13752      requiring multiple passes.  */
13753   again = TRUE;
13754   while (again)
13755     {
13756       again = FALSE;
13757       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13758         {
13759           asection *o;
13760
13761           if (! is_arm_elf (sub))
13762             continue;
13763
13764           elf_shdrp = elf_elfsections (sub);
13765           for (o = sub->sections; o != NULL; o = o->next)
13766             {
13767               Elf_Internal_Shdr *hdr;
13768
13769               hdr = &elf_section_data (o)->this_hdr;
13770               if (hdr->sh_type == SHT_ARM_EXIDX
13771                   && hdr->sh_link
13772                   && hdr->sh_link < elf_numsections (sub)
13773                   && !o->gc_mark
13774                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
13775                 {
13776                   again = TRUE;
13777                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13778                     return FALSE;
13779                 }
13780             }
13781         }
13782     }
13783
13784   return TRUE;
13785 }
13786
13787 /* Treat mapping symbols as special target symbols.  */
13788
13789 static bfd_boolean
13790 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
13791 {
13792   return bfd_is_arm_special_symbol_name (sym->name,
13793                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
13794 }
13795
13796 /* This is a copy of elf_find_function() from elf.c except that
13797    ARM mapping symbols are ignored when looking for function names
13798    and STT_ARM_TFUNC is considered to a function type.  */
13799
13800 static bfd_boolean
13801 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
13802                        asymbol **    symbols,
13803                        asection *    section,
13804                        bfd_vma       offset,
13805                        const char ** filename_ptr,
13806                        const char ** functionname_ptr)
13807 {
13808   const char * filename = NULL;
13809   asymbol * func = NULL;
13810   bfd_vma low_func = 0;
13811   asymbol ** p;
13812
13813   for (p = symbols; *p != NULL; p++)
13814     {
13815       elf_symbol_type *q;
13816
13817       q = (elf_symbol_type *) *p;
13818
13819       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
13820         {
13821         default:
13822           break;
13823         case STT_FILE:
13824           filename = bfd_asymbol_name (&q->symbol);
13825           break;
13826         case STT_FUNC:
13827         case STT_ARM_TFUNC:
13828         case STT_NOTYPE:
13829           /* Skip mapping symbols.  */
13830           if ((q->symbol.flags & BSF_LOCAL)
13831               && bfd_is_arm_special_symbol_name (q->symbol.name,
13832                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
13833             continue;
13834           /* Fall through.  */
13835           if (bfd_get_section (&q->symbol) == section
13836               && q->symbol.value >= low_func
13837               && q->symbol.value <= offset)
13838             {
13839               func = (asymbol *) q;
13840               low_func = q->symbol.value;
13841             }
13842           break;
13843         }
13844     }
13845
13846   if (func == NULL)
13847     return FALSE;
13848
13849   if (filename_ptr)
13850     *filename_ptr = filename;
13851   if (functionname_ptr)
13852     *functionname_ptr = bfd_asymbol_name (func);
13853
13854   return TRUE;
13855 }
13856
13857
13858 /* Find the nearest line to a particular section and offset, for error
13859    reporting.   This code is a duplicate of the code in elf.c, except
13860    that it uses arm_elf_find_function.  */
13861
13862 static bfd_boolean
13863 elf32_arm_find_nearest_line (bfd *          abfd,
13864                              asymbol **     symbols,
13865                              asection *     section,
13866                              bfd_vma        offset,
13867                              const char **  filename_ptr,
13868                              const char **  functionname_ptr,
13869                              unsigned int * line_ptr,
13870                              unsigned int * discriminator_ptr)
13871 {
13872   bfd_boolean found = FALSE;
13873
13874   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13875                                      filename_ptr, functionname_ptr,
13876                                      line_ptr, discriminator_ptr,
13877                                      dwarf_debug_sections, 0,
13878                                      & elf_tdata (abfd)->dwarf2_find_line_info))
13879     {
13880       if (!*functionname_ptr)
13881         arm_elf_find_function (abfd, symbols, section, offset,
13882                                *filename_ptr ? NULL : filename_ptr,
13883                                functionname_ptr);
13884
13885       return TRUE;
13886     }
13887
13888   /* Skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain
13889      uses DWARF1.  */
13890
13891   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
13892                                              & found, filename_ptr,
13893                                              functionname_ptr, line_ptr,
13894                                              & elf_tdata (abfd)->line_info))
13895     return FALSE;
13896
13897   if (found && (*functionname_ptr || *line_ptr))
13898     return TRUE;
13899
13900   if (symbols == NULL)
13901     return FALSE;
13902
13903   if (! arm_elf_find_function (abfd, symbols, section, offset,
13904                                filename_ptr, functionname_ptr))
13905     return FALSE;
13906
13907   *line_ptr = 0;
13908   return TRUE;
13909 }
13910
13911 static bfd_boolean
13912 elf32_arm_find_inliner_info (bfd *          abfd,
13913                              const char **  filename_ptr,
13914                              const char **  functionname_ptr,
13915                              unsigned int * line_ptr)
13916 {
13917   bfd_boolean found;
13918   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13919                                          functionname_ptr, line_ptr,
13920                                          & elf_tdata (abfd)->dwarf2_find_line_info);
13921   return found;
13922 }
13923
13924 /* Adjust a symbol defined by a dynamic object and referenced by a
13925    regular object.  The current definition is in some section of the
13926    dynamic object, but we're not including those sections.  We have to
13927    change the definition to something the rest of the link can
13928    understand.  */
13929
13930 static bfd_boolean
13931 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
13932                                  struct elf_link_hash_entry * h)
13933 {
13934   bfd * dynobj;
13935   asection * s;
13936   struct elf32_arm_link_hash_entry * eh;
13937   struct elf32_arm_link_hash_table *globals;
13938
13939   globals = elf32_arm_hash_table (info);
13940   if (globals == NULL)
13941     return FALSE;
13942
13943   dynobj = elf_hash_table (info)->dynobj;
13944
13945   /* Make sure we know what is going on here.  */
13946   BFD_ASSERT (dynobj != NULL
13947               && (h->needs_plt
13948                   || h->type == STT_GNU_IFUNC
13949                   || h->u.weakdef != NULL
13950                   || (h->def_dynamic
13951                       && h->ref_regular
13952                       && !h->def_regular)));
13953
13954   eh = (struct elf32_arm_link_hash_entry *) h;
13955
13956   /* If this is a function, put it in the procedure linkage table.  We
13957      will fill in the contents of the procedure linkage table later,
13958      when we know the address of the .got section.  */
13959   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
13960     {
13961       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
13962          symbol binds locally.  */
13963       if (h->plt.refcount <= 0
13964           || (h->type != STT_GNU_IFUNC
13965               && (SYMBOL_CALLS_LOCAL (info, h)
13966                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
13967                       && h->root.type == bfd_link_hash_undefweak))))
13968         {
13969           /* This case can occur if we saw a PLT32 reloc in an input
13970              file, but the symbol was never referred to by a dynamic
13971              object, or if all references were garbage collected.  In
13972              such a case, we don't actually need to build a procedure
13973              linkage table, and we can just do a PC24 reloc instead.  */
13974           h->plt.offset = (bfd_vma) -1;
13975           eh->plt.thumb_refcount = 0;
13976           eh->plt.maybe_thumb_refcount = 0;
13977           eh->plt.noncall_refcount = 0;
13978           h->needs_plt = 0;
13979         }
13980
13981       return TRUE;
13982     }
13983   else
13984     {
13985       /* It's possible that we incorrectly decided a .plt reloc was
13986          needed for an R_ARM_PC24 or similar reloc to a non-function sym
13987          in check_relocs.  We can't decide accurately between function
13988          and non-function syms in check-relocs; Objects loaded later in
13989          the link may change h->type.  So fix it now.  */
13990       h->plt.offset = (bfd_vma) -1;
13991       eh->plt.thumb_refcount = 0;
13992       eh->plt.maybe_thumb_refcount = 0;
13993       eh->plt.noncall_refcount = 0;
13994     }
13995
13996   /* If this is a weak symbol, and there is a real definition, the
13997      processor independent code will have arranged for us to see the
13998      real definition first, and we can just use the same value.  */
13999   if (h->u.weakdef != NULL)
14000     {
14001       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
14002                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
14003       h->root.u.def.section = h->u.weakdef->root.u.def.section;
14004       h->root.u.def.value = h->u.weakdef->root.u.def.value;
14005       return TRUE;
14006     }
14007
14008   /* If there are no non-GOT references, we do not need a copy
14009      relocation.  */
14010   if (!h->non_got_ref)
14011     return TRUE;
14012
14013   /* This is a reference to a symbol defined by a dynamic object which
14014      is not a function.  */
14015
14016   /* If we are creating a shared library, we must presume that the
14017      only references to the symbol are via the global offset table.
14018      For such cases we need not do anything here; the relocations will
14019      be handled correctly by relocate_section.  Relocatable executables
14020      can reference data in shared objects directly, so we don't need to
14021      do anything here.  */
14022   if (bfd_link_pic (info) || globals->root.is_relocatable_executable)
14023     return TRUE;
14024
14025   /* We must allocate the symbol in our .dynbss section, which will
14026      become part of the .bss section of the executable.  There will be
14027      an entry for this symbol in the .dynsym section.  The dynamic
14028      object will contain position independent code, so all references
14029      from the dynamic object to this symbol will go through the global
14030      offset table.  The dynamic linker will use the .dynsym entry to
14031      determine the address it must put in the global offset table, so
14032      both the dynamic object and the regular object will refer to the
14033      same memory location for the variable.  */
14034   s = bfd_get_linker_section (dynobj, ".dynbss");
14035   BFD_ASSERT (s != NULL);
14036
14037   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
14038      copy the initial value out of the dynamic object and into the
14039      runtime process image.  We need to remember the offset into the
14040      .rel(a).bss section we are going to use.  */
14041   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
14042     {
14043       asection *srel;
14044
14045       srel = bfd_get_linker_section (dynobj, RELOC_SECTION (globals, ".bss"));
14046       elf32_arm_allocate_dynrelocs (info, srel, 1);
14047       h->needs_copy = 1;
14048     }
14049
14050   return _bfd_elf_adjust_dynamic_copy (info, h, s);
14051 }
14052
14053 /* Allocate space in .plt, .got and associated reloc sections for
14054    dynamic relocs.  */
14055
14056 static bfd_boolean
14057 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
14058 {
14059   struct bfd_link_info *info;
14060   struct elf32_arm_link_hash_table *htab;
14061   struct elf32_arm_link_hash_entry *eh;
14062   struct elf_dyn_relocs *p;
14063
14064   if (h->root.type == bfd_link_hash_indirect)
14065     return TRUE;
14066
14067   eh = (struct elf32_arm_link_hash_entry *) h;
14068
14069   info = (struct bfd_link_info *) inf;
14070   htab = elf32_arm_hash_table (info);
14071   if (htab == NULL)
14072     return FALSE;
14073
14074   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
14075       && h->plt.refcount > 0)
14076     {
14077       /* Make sure this symbol is output as a dynamic symbol.
14078          Undefined weak syms won't yet be marked as dynamic.  */
14079       if (h->dynindx == -1
14080           && !h->forced_local)
14081         {
14082           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14083             return FALSE;
14084         }
14085
14086       /* If the call in the PLT entry binds locally, the associated
14087          GOT entry should use an R_ARM_IRELATIVE relocation instead of
14088          the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
14089          than the .plt section.  */
14090       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
14091         {
14092           eh->is_iplt = 1;
14093           if (eh->plt.noncall_refcount == 0
14094               && SYMBOL_REFERENCES_LOCAL (info, h))
14095             /* All non-call references can be resolved directly.
14096                This means that they can (and in some cases, must)
14097                resolve directly to the run-time target, rather than
14098                to the PLT.  That in turns means that any .got entry
14099                would be equal to the .igot.plt entry, so there's
14100                no point having both.  */
14101             h->got.refcount = 0;
14102         }
14103
14104       if (bfd_link_pic (info)
14105           || eh->is_iplt
14106           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
14107         {
14108           elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
14109
14110           /* If this symbol is not defined in a regular file, and we are
14111              not generating a shared library, then set the symbol to this
14112              location in the .plt.  This is required to make function
14113              pointers compare as equal between the normal executable and
14114              the shared library.  */
14115           if (! bfd_link_pic (info)
14116               && !h->def_regular)
14117             {
14118               h->root.u.def.section = htab->root.splt;
14119               h->root.u.def.value = h->plt.offset;
14120
14121               /* Make sure the function is not marked as Thumb, in case
14122                  it is the target of an ABS32 relocation, which will
14123                  point to the PLT entry.  */
14124               h->target_internal = ST_BRANCH_TO_ARM;
14125             }
14126
14127           /* VxWorks executables have a second set of relocations for
14128              each PLT entry.  They go in a separate relocation section,
14129              which is processed by the kernel loader.  */
14130           if (htab->vxworks_p && !bfd_link_pic (info))
14131             {
14132               /* There is a relocation for the initial PLT entry:
14133                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
14134               if (h->plt.offset == htab->plt_header_size)
14135                 elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
14136
14137               /* There are two extra relocations for each subsequent
14138                  PLT entry: an R_ARM_32 relocation for the GOT entry,
14139                  and an R_ARM_32 relocation for the PLT entry.  */
14140               elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
14141             }
14142         }
14143       else
14144         {
14145           h->plt.offset = (bfd_vma) -1;
14146           h->needs_plt = 0;
14147         }
14148     }
14149   else
14150     {
14151       h->plt.offset = (bfd_vma) -1;
14152       h->needs_plt = 0;
14153     }
14154
14155   eh = (struct elf32_arm_link_hash_entry *) h;
14156   eh->tlsdesc_got = (bfd_vma) -1;
14157
14158   if (h->got.refcount > 0)
14159     {
14160       asection *s;
14161       bfd_boolean dyn;
14162       int tls_type = elf32_arm_hash_entry (h)->tls_type;
14163       int indx;
14164
14165       /* Make sure this symbol is output as a dynamic symbol.
14166          Undefined weak syms won't yet be marked as dynamic.  */
14167       if (h->dynindx == -1
14168           && !h->forced_local)
14169         {
14170           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14171             return FALSE;
14172         }
14173
14174       if (!htab->symbian_p)
14175         {
14176           s = htab->root.sgot;
14177           h->got.offset = s->size;
14178
14179           if (tls_type == GOT_UNKNOWN)
14180             abort ();
14181
14182           if (tls_type == GOT_NORMAL)
14183             /* Non-TLS symbols need one GOT slot.  */
14184             s->size += 4;
14185           else
14186             {
14187               if (tls_type & GOT_TLS_GDESC)
14188                 {
14189                   /* R_ARM_TLS_DESC needs 2 GOT slots.  */
14190                   eh->tlsdesc_got
14191                     = (htab->root.sgotplt->size
14192                        - elf32_arm_compute_jump_table_size (htab));
14193                   htab->root.sgotplt->size += 8;
14194                   h->got.offset = (bfd_vma) -2;
14195                   /* plt.got_offset needs to know there's a TLS_DESC
14196                      reloc in the middle of .got.plt.  */
14197                   htab->num_tls_desc++;
14198                 }
14199
14200               if (tls_type & GOT_TLS_GD)
14201                 {
14202                   /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  If
14203                      the symbol is both GD and GDESC, got.offset may
14204                      have been overwritten.  */
14205                   h->got.offset = s->size;
14206                   s->size += 8;
14207                 }
14208
14209               if (tls_type & GOT_TLS_IE)
14210                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
14211                 s->size += 4;
14212             }
14213
14214           dyn = htab->root.dynamic_sections_created;
14215
14216           indx = 0;
14217           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
14218                                                bfd_link_pic (info),
14219                                                h)
14220               && (!bfd_link_pic (info)
14221                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
14222             indx = h->dynindx;
14223
14224           if (tls_type != GOT_NORMAL
14225               && (bfd_link_pic (info) || indx != 0)
14226               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
14227                   || h->root.type != bfd_link_hash_undefweak))
14228             {
14229               if (tls_type & GOT_TLS_IE)
14230                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14231
14232               if (tls_type & GOT_TLS_GD)
14233                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14234
14235               if (tls_type & GOT_TLS_GDESC)
14236                 {
14237                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
14238                   /* GDESC needs a trampoline to jump to.  */
14239                   htab->tls_trampoline = -1;
14240                 }
14241
14242               /* Only GD needs it.  GDESC just emits one relocation per
14243                  2 entries.  */
14244               if ((tls_type & GOT_TLS_GD) && indx != 0)
14245                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14246             }
14247           else if (indx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
14248             {
14249               if (htab->root.dynamic_sections_created)
14250                 /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
14251                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14252             }
14253           else if (h->type == STT_GNU_IFUNC
14254                    && eh->plt.noncall_refcount == 0)
14255             /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
14256                they all resolve dynamically instead.  Reserve room for the
14257                GOT entry's R_ARM_IRELATIVE relocation.  */
14258             elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
14259           else if (bfd_link_pic (info)
14260                    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
14261                        || h->root.type != bfd_link_hash_undefweak))
14262             /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
14263             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14264         }
14265     }
14266   else
14267     h->got.offset = (bfd_vma) -1;
14268
14269   /* Allocate stubs for exported Thumb functions on v4t.  */
14270   if (!htab->use_blx && h->dynindx != -1
14271       && h->def_regular
14272       && h->target_internal == ST_BRANCH_TO_THUMB
14273       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14274     {
14275       struct elf_link_hash_entry * th;
14276       struct bfd_link_hash_entry * bh;
14277       struct elf_link_hash_entry * myh;
14278       char name[1024];
14279       asection *s;
14280       bh = NULL;
14281       /* Create a new symbol to regist the real location of the function.  */
14282       s = h->root.u.def.section;
14283       sprintf (name, "__real_%s", h->root.root.string);
14284       _bfd_generic_link_add_one_symbol (info, s->owner,
14285                                         name, BSF_GLOBAL, s,
14286                                         h->root.u.def.value,
14287                                         NULL, TRUE, FALSE, &bh);
14288
14289       myh = (struct elf_link_hash_entry *) bh;
14290       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
14291       myh->forced_local = 1;
14292       myh->target_internal = ST_BRANCH_TO_THUMB;
14293       eh->export_glue = myh;
14294       th = record_arm_to_thumb_glue (info, h);
14295       /* Point the symbol at the stub.  */
14296       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
14297       h->target_internal = ST_BRANCH_TO_ARM;
14298       h->root.u.def.section = th->root.u.def.section;
14299       h->root.u.def.value = th->root.u.def.value & ~1;
14300     }
14301
14302   if (eh->dyn_relocs == NULL)
14303     return TRUE;
14304
14305   /* In the shared -Bsymbolic case, discard space allocated for
14306      dynamic pc-relative relocs against symbols which turn out to be
14307      defined in regular objects.  For the normal shared case, discard
14308      space for pc-relative relocs that have become local due to symbol
14309      visibility changes.  */
14310
14311   if (bfd_link_pic (info) || htab->root.is_relocatable_executable)
14312     {
14313       /* Relocs that use pc_count are PC-relative forms, which will appear
14314          on something like ".long foo - ." or "movw REG, foo - .".  We want
14315          calls to protected symbols to resolve directly to the function
14316          rather than going via the plt.  If people want function pointer
14317          comparisons to work as expected then they should avoid writing
14318          assembly like ".long foo - .".  */
14319       if (SYMBOL_CALLS_LOCAL (info, h))
14320         {
14321           struct elf_dyn_relocs **pp;
14322
14323           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
14324             {
14325               p->count -= p->pc_count;
14326               p->pc_count = 0;
14327               if (p->count == 0)
14328                 *pp = p->next;
14329               else
14330                 pp = &p->next;
14331             }
14332         }
14333
14334       if (htab->vxworks_p)
14335         {
14336           struct elf_dyn_relocs **pp;
14337
14338           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
14339             {
14340               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
14341                 *pp = p->next;
14342               else
14343                 pp = &p->next;
14344             }
14345         }
14346
14347       /* Also discard relocs on undefined weak syms with non-default
14348          visibility.  */
14349       if (eh->dyn_relocs != NULL
14350           && h->root.type == bfd_link_hash_undefweak)
14351         {
14352           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
14353             eh->dyn_relocs = NULL;
14354
14355           /* Make sure undefined weak symbols are output as a dynamic
14356              symbol in PIEs.  */
14357           else if (h->dynindx == -1
14358                    && !h->forced_local)
14359             {
14360               if (! bfd_elf_link_record_dynamic_symbol (info, h))
14361                 return FALSE;
14362             }
14363         }
14364
14365       else if (htab->root.is_relocatable_executable && h->dynindx == -1
14366                && h->root.type == bfd_link_hash_new)
14367         {
14368           /* Output absolute symbols so that we can create relocations
14369              against them.  For normal symbols we output a relocation
14370              against the section that contains them.  */
14371           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14372             return FALSE;
14373         }
14374
14375     }
14376   else
14377     {
14378       /* For the non-shared case, discard space for relocs against
14379          symbols which turn out to need copy relocs or are not
14380          dynamic.  */
14381
14382       if (!h->non_got_ref
14383           && ((h->def_dynamic
14384                && !h->def_regular)
14385               || (htab->root.dynamic_sections_created
14386                   && (h->root.type == bfd_link_hash_undefweak
14387                       || h->root.type == bfd_link_hash_undefined))))
14388         {
14389           /* Make sure this symbol is output as a dynamic symbol.
14390              Undefined weak syms won't yet be marked as dynamic.  */
14391           if (h->dynindx == -1
14392               && !h->forced_local)
14393             {
14394               if (! bfd_elf_link_record_dynamic_symbol (info, h))
14395                 return FALSE;
14396             }
14397
14398           /* If that succeeded, we know we'll be keeping all the
14399              relocs.  */
14400           if (h->dynindx != -1)
14401             goto keep;
14402         }
14403
14404       eh->dyn_relocs = NULL;
14405
14406     keep: ;
14407     }
14408
14409   /* Finally, allocate space.  */
14410   for (p = eh->dyn_relocs; p != NULL; p = p->next)
14411     {
14412       asection *sreloc = elf_section_data (p->sec)->sreloc;
14413       if (h->type == STT_GNU_IFUNC
14414           && eh->plt.noncall_refcount == 0
14415           && SYMBOL_REFERENCES_LOCAL (info, h))
14416         elf32_arm_allocate_irelocs (info, sreloc, p->count);
14417       else
14418         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
14419     }
14420
14421   return TRUE;
14422 }
14423
14424 /* Find any dynamic relocs that apply to read-only sections.  */
14425
14426 static bfd_boolean
14427 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
14428 {
14429   struct elf32_arm_link_hash_entry * eh;
14430   struct elf_dyn_relocs * p;
14431
14432   eh = (struct elf32_arm_link_hash_entry *) h;
14433   for (p = eh->dyn_relocs; p != NULL; p = p->next)
14434     {
14435       asection *s = p->sec;
14436
14437       if (s != NULL && (s->flags & SEC_READONLY) != 0)
14438         {
14439           struct bfd_link_info *info = (struct bfd_link_info *) inf;
14440
14441           info->flags |= DF_TEXTREL;
14442
14443           /* Not an error, just cut short the traversal.  */
14444           return FALSE;
14445         }
14446     }
14447   return TRUE;
14448 }
14449
14450 void
14451 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
14452                                  int byteswap_code)
14453 {
14454   struct elf32_arm_link_hash_table *globals;
14455
14456   globals = elf32_arm_hash_table (info);
14457   if (globals == NULL)
14458     return;
14459
14460   globals->byteswap_code = byteswap_code;
14461 }
14462
14463 /* Set the sizes of the dynamic sections.  */
14464
14465 static bfd_boolean
14466 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
14467                                  struct bfd_link_info * info)
14468 {
14469   bfd * dynobj;
14470   asection * s;
14471   bfd_boolean plt;
14472   bfd_boolean relocs;
14473   bfd *ibfd;
14474   struct elf32_arm_link_hash_table *htab;
14475
14476   htab = elf32_arm_hash_table (info);
14477   if (htab == NULL)
14478     return FALSE;
14479
14480   dynobj = elf_hash_table (info)->dynobj;
14481   BFD_ASSERT (dynobj != NULL);
14482   check_use_blx (htab);
14483
14484   if (elf_hash_table (info)->dynamic_sections_created)
14485     {
14486       /* Set the contents of the .interp section to the interpreter.  */
14487       if (bfd_link_executable (info) && !info->nointerp)
14488         {
14489           s = bfd_get_linker_section (dynobj, ".interp");
14490           BFD_ASSERT (s != NULL);
14491           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
14492           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
14493         }
14494     }
14495
14496   /* Set up .got offsets for local syms, and space for local dynamic
14497      relocs.  */
14498   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14499     {
14500       bfd_signed_vma *local_got;
14501       bfd_signed_vma *end_local_got;
14502       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
14503       char *local_tls_type;
14504       bfd_vma *local_tlsdesc_gotent;
14505       bfd_size_type locsymcount;
14506       Elf_Internal_Shdr *symtab_hdr;
14507       asection *srel;
14508       bfd_boolean is_vxworks = htab->vxworks_p;
14509       unsigned int symndx;
14510
14511       if (! is_arm_elf (ibfd))
14512         continue;
14513
14514       for (s = ibfd->sections; s != NULL; s = s->next)
14515         {
14516           struct elf_dyn_relocs *p;
14517
14518           for (p = (struct elf_dyn_relocs *)
14519                    elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
14520             {
14521               if (!bfd_is_abs_section (p->sec)
14522                   && bfd_is_abs_section (p->sec->output_section))
14523                 {
14524                   /* Input section has been discarded, either because
14525                      it is a copy of a linkonce section or due to
14526                      linker script /DISCARD/, so we'll be discarding
14527                      the relocs too.  */
14528                 }
14529               else if (is_vxworks
14530                        && strcmp (p->sec->output_section->name,
14531                                   ".tls_vars") == 0)
14532                 {
14533                   /* Relocations in vxworks .tls_vars sections are
14534                      handled specially by the loader.  */
14535                 }
14536               else if (p->count != 0)
14537                 {
14538                   srel = elf_section_data (p->sec)->sreloc;
14539                   elf32_arm_allocate_dynrelocs (info, srel, p->count);
14540                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
14541                     info->flags |= DF_TEXTREL;
14542                 }
14543             }
14544         }
14545
14546       local_got = elf_local_got_refcounts (ibfd);
14547       if (!local_got)
14548         continue;
14549
14550       symtab_hdr = & elf_symtab_hdr (ibfd);
14551       locsymcount = symtab_hdr->sh_info;
14552       end_local_got = local_got + locsymcount;
14553       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
14554       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
14555       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
14556       symndx = 0;
14557       s = htab->root.sgot;
14558       srel = htab->root.srelgot;
14559       for (; local_got < end_local_got;
14560            ++local_got, ++local_iplt_ptr, ++local_tls_type,
14561            ++local_tlsdesc_gotent, ++symndx)
14562         {
14563           *local_tlsdesc_gotent = (bfd_vma) -1;
14564           local_iplt = *local_iplt_ptr;
14565           if (local_iplt != NULL)
14566             {
14567               struct elf_dyn_relocs *p;
14568
14569               if (local_iplt->root.refcount > 0)
14570                 {
14571                   elf32_arm_allocate_plt_entry (info, TRUE,
14572                                                 &local_iplt->root,
14573                                                 &local_iplt->arm);
14574                   if (local_iplt->arm.noncall_refcount == 0)
14575                     /* All references to the PLT are calls, so all
14576                        non-call references can resolve directly to the
14577                        run-time target.  This means that the .got entry
14578                        would be the same as the .igot.plt entry, so there's
14579                        no point creating both.  */
14580                     *local_got = 0;
14581                 }
14582               else
14583                 {
14584                   BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
14585                   local_iplt->root.offset = (bfd_vma) -1;
14586                 }
14587
14588               for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
14589                 {
14590                   asection *psrel;
14591
14592                   psrel = elf_section_data (p->sec)->sreloc;
14593                   if (local_iplt->arm.noncall_refcount == 0)
14594                     elf32_arm_allocate_irelocs (info, psrel, p->count);
14595                   else
14596                     elf32_arm_allocate_dynrelocs (info, psrel, p->count);
14597                 }
14598             }
14599           if (*local_got > 0)
14600             {
14601               Elf_Internal_Sym *isym;
14602
14603               *local_got = s->size;
14604               if (*local_tls_type & GOT_TLS_GD)
14605                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
14606                 s->size += 8;
14607               if (*local_tls_type & GOT_TLS_GDESC)
14608                 {
14609                   *local_tlsdesc_gotent = htab->root.sgotplt->size
14610                     - elf32_arm_compute_jump_table_size (htab);
14611                   htab->root.sgotplt->size += 8;
14612                   *local_got = (bfd_vma) -2;
14613                   /* plt.got_offset needs to know there's a TLS_DESC
14614                      reloc in the middle of .got.plt.  */
14615                   htab->num_tls_desc++;
14616                 }
14617               if (*local_tls_type & GOT_TLS_IE)
14618                 s->size += 4;
14619
14620               if (*local_tls_type & GOT_NORMAL)
14621                 {
14622                   /* If the symbol is both GD and GDESC, *local_got
14623                      may have been overwritten.  */
14624                   *local_got = s->size;
14625                   s->size += 4;
14626                 }
14627
14628               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
14629               if (isym == NULL)
14630                 return FALSE;
14631
14632               /* If all references to an STT_GNU_IFUNC PLT are calls,
14633                  then all non-call references, including this GOT entry,
14634                  resolve directly to the run-time target.  */
14635               if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
14636                   && (local_iplt == NULL
14637                       || local_iplt->arm.noncall_refcount == 0))
14638                 elf32_arm_allocate_irelocs (info, srel, 1);
14639               else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC)
14640                 {
14641                   if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC))
14642                       || *local_tls_type & GOT_TLS_GD)
14643                     elf32_arm_allocate_dynrelocs (info, srel, 1);
14644
14645                   if (bfd_link_pic (info) && *local_tls_type & GOT_TLS_GDESC)
14646                     {
14647                       elf32_arm_allocate_dynrelocs (info,
14648                                                     htab->root.srelplt, 1);
14649                       htab->tls_trampoline = -1;
14650                     }
14651                 }
14652             }
14653           else
14654             *local_got = (bfd_vma) -1;
14655         }
14656     }
14657
14658   if (htab->tls_ldm_got.refcount > 0)
14659     {
14660       /* Allocate two GOT entries and one dynamic relocation (if necessary)
14661          for R_ARM_TLS_LDM32 relocations.  */
14662       htab->tls_ldm_got.offset = htab->root.sgot->size;
14663       htab->root.sgot->size += 8;
14664       if (bfd_link_pic (info))
14665         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14666     }
14667   else
14668     htab->tls_ldm_got.offset = -1;
14669
14670   /* Allocate global sym .plt and .got entries, and space for global
14671      sym dynamic relocs.  */
14672   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
14673
14674   /* Here we rummage through the found bfds to collect glue information.  */
14675   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14676     {
14677       if (! is_arm_elf (ibfd))
14678         continue;
14679
14680       /* Initialise mapping tables for code/data.  */
14681       bfd_elf32_arm_init_maps (ibfd);
14682
14683       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
14684           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
14685           || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
14686         /* xgettext:c-format */
14687         _bfd_error_handler (_("Errors encountered processing file %s"),
14688                             ibfd->filename);
14689     }
14690
14691   /* Allocate space for the glue sections now that we've sized them.  */
14692   bfd_elf32_arm_allocate_interworking_sections (info);
14693
14694   /* For every jump slot reserved in the sgotplt, reloc_count is
14695      incremented.  However, when we reserve space for TLS descriptors,
14696      it's not incremented, so in order to compute the space reserved
14697      for them, it suffices to multiply the reloc count by the jump
14698      slot size.  */
14699   if (htab->root.srelplt)
14700     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
14701
14702   if (htab->tls_trampoline)
14703     {
14704       if (htab->root.splt->size == 0)
14705         htab->root.splt->size += htab->plt_header_size;
14706
14707       htab->tls_trampoline = htab->root.splt->size;
14708       htab->root.splt->size += htab->plt_entry_size;
14709
14710       /* If we're not using lazy TLS relocations, don't generate the
14711          PLT and GOT entries they require.  */
14712       if (!(info->flags & DF_BIND_NOW))
14713         {
14714           htab->dt_tlsdesc_got = htab->root.sgot->size;
14715           htab->root.sgot->size += 4;
14716
14717           htab->dt_tlsdesc_plt = htab->root.splt->size;
14718           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
14719         }
14720     }
14721
14722   /* The check_relocs and adjust_dynamic_symbol entry points have
14723      determined the sizes of the various dynamic sections.  Allocate
14724      memory for them.  */
14725   plt = FALSE;
14726   relocs = FALSE;
14727   for (s = dynobj->sections; s != NULL; s = s->next)
14728     {
14729       const char * name;
14730
14731       if ((s->flags & SEC_LINKER_CREATED) == 0)
14732         continue;
14733
14734       /* It's OK to base decisions on the section name, because none
14735          of the dynobj section names depend upon the input files.  */
14736       name = bfd_get_section_name (dynobj, s);
14737
14738       if (s == htab->root.splt)
14739         {
14740           /* Remember whether there is a PLT.  */
14741           plt = s->size != 0;
14742         }
14743       else if (CONST_STRNEQ (name, ".rel"))
14744         {
14745           if (s->size != 0)
14746             {
14747               /* Remember whether there are any reloc sections other
14748                  than .rel(a).plt and .rela.plt.unloaded.  */
14749               if (s != htab->root.srelplt && s != htab->srelplt2)
14750                 relocs = TRUE;
14751
14752               /* We use the reloc_count field as a counter if we need
14753                  to copy relocs into the output file.  */
14754               s->reloc_count = 0;
14755             }
14756         }
14757       else if (s != htab->root.sgot
14758                && s != htab->root.sgotplt
14759                && s != htab->root.iplt
14760                && s != htab->root.igotplt
14761                && s != htab->sdynbss)
14762         {
14763           /* It's not one of our sections, so don't allocate space.  */
14764           continue;
14765         }
14766
14767       if (s->size == 0)
14768         {
14769           /* If we don't need this section, strip it from the
14770              output file.  This is mostly to handle .rel(a).bss and
14771              .rel(a).plt.  We must create both sections in
14772              create_dynamic_sections, because they must be created
14773              before the linker maps input sections to output
14774              sections.  The linker does that before
14775              adjust_dynamic_symbol is called, and it is that
14776              function which decides whether anything needs to go
14777              into these sections.  */
14778           s->flags |= SEC_EXCLUDE;
14779           continue;
14780         }
14781
14782       if ((s->flags & SEC_HAS_CONTENTS) == 0)
14783         continue;
14784
14785       /* Allocate memory for the section contents.  */
14786       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
14787       if (s->contents == NULL)
14788         return FALSE;
14789     }
14790
14791   if (elf_hash_table (info)->dynamic_sections_created)
14792     {
14793       /* Add some entries to the .dynamic section.  We fill in the
14794          values later, in elf32_arm_finish_dynamic_sections, but we
14795          must add the entries now so that we get the correct size for
14796          the .dynamic section.  The DT_DEBUG entry is filled in by the
14797          dynamic linker and used by the debugger.  */
14798 #define add_dynamic_entry(TAG, VAL) \
14799   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14800
14801      if (bfd_link_executable (info))
14802         {
14803           if (!add_dynamic_entry (DT_DEBUG, 0))
14804             return FALSE;
14805         }
14806
14807       if (plt)
14808         {
14809           if (   !add_dynamic_entry (DT_PLTGOT, 0)
14810               || !add_dynamic_entry (DT_PLTRELSZ, 0)
14811               || !add_dynamic_entry (DT_PLTREL,
14812                                      htab->use_rel ? DT_REL : DT_RELA)
14813               || !add_dynamic_entry (DT_JMPREL, 0))
14814             return FALSE;
14815
14816           if (htab->dt_tlsdesc_plt &&
14817                 (!add_dynamic_entry (DT_TLSDESC_PLT,0)
14818                  || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
14819             return FALSE;
14820         }
14821
14822       if (relocs)
14823         {
14824           if (htab->use_rel)
14825             {
14826               if (!add_dynamic_entry (DT_REL, 0)
14827                   || !add_dynamic_entry (DT_RELSZ, 0)
14828                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
14829                 return FALSE;
14830             }
14831           else
14832             {
14833               if (!add_dynamic_entry (DT_RELA, 0)
14834                   || !add_dynamic_entry (DT_RELASZ, 0)
14835                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
14836                 return FALSE;
14837             }
14838         }
14839
14840       /* If any dynamic relocs apply to a read-only section,
14841          then we need a DT_TEXTREL entry.  */
14842       if ((info->flags & DF_TEXTREL) == 0)
14843         elf_link_hash_traverse (& htab->root, elf32_arm_readonly_dynrelocs,
14844                                 info);
14845
14846       if ((info->flags & DF_TEXTREL) != 0)
14847         {
14848           if (!add_dynamic_entry (DT_TEXTREL, 0))
14849             return FALSE;
14850         }
14851       if (htab->vxworks_p
14852           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
14853         return FALSE;
14854     }
14855 #undef add_dynamic_entry
14856
14857   return TRUE;
14858 }
14859
14860 /* Size sections even though they're not dynamic.  We use it to setup
14861    _TLS_MODULE_BASE_, if needed.  */
14862
14863 static bfd_boolean
14864 elf32_arm_always_size_sections (bfd *output_bfd,
14865                                 struct bfd_link_info *info)
14866 {
14867   asection *tls_sec;
14868
14869   if (bfd_link_relocatable (info))
14870     return TRUE;
14871
14872   tls_sec = elf_hash_table (info)->tls_sec;
14873
14874   if (tls_sec)
14875     {
14876       struct elf_link_hash_entry *tlsbase;
14877
14878       tlsbase = elf_link_hash_lookup
14879         (elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
14880
14881       if (tlsbase)
14882         {
14883           struct bfd_link_hash_entry *bh = NULL;
14884           const struct elf_backend_data *bed
14885             = get_elf_backend_data (output_bfd);
14886
14887           if (!(_bfd_generic_link_add_one_symbol
14888                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
14889                  tls_sec, 0, NULL, FALSE,
14890                  bed->collect, &bh)))
14891             return FALSE;
14892
14893           tlsbase->type = STT_TLS;
14894           tlsbase = (struct elf_link_hash_entry *)bh;
14895           tlsbase->def_regular = 1;
14896           tlsbase->other = STV_HIDDEN;
14897           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
14898         }
14899     }
14900   return TRUE;
14901 }
14902
14903 /* Finish up dynamic symbol handling.  We set the contents of various
14904    dynamic sections here.  */
14905
14906 static bfd_boolean
14907 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
14908                                  struct bfd_link_info * info,
14909                                  struct elf_link_hash_entry * h,
14910                                  Elf_Internal_Sym * sym)
14911 {
14912   struct elf32_arm_link_hash_table *htab;
14913   struct elf32_arm_link_hash_entry *eh;
14914
14915   htab = elf32_arm_hash_table (info);
14916   if (htab == NULL)
14917     return FALSE;
14918
14919   eh = (struct elf32_arm_link_hash_entry *) h;
14920
14921   if (h->plt.offset != (bfd_vma) -1)
14922     {
14923       if (!eh->is_iplt)
14924         {
14925           BFD_ASSERT (h->dynindx != -1);
14926           if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
14927                                               h->dynindx, 0))
14928             return FALSE;
14929         }
14930
14931       if (!h->def_regular)
14932         {
14933           /* Mark the symbol as undefined, rather than as defined in
14934              the .plt section.  */
14935           sym->st_shndx = SHN_UNDEF;
14936           /* If the symbol is weak we need to clear the value.
14937              Otherwise, the PLT entry would provide a definition for
14938              the symbol even if the symbol wasn't defined anywhere,
14939              and so the symbol would never be NULL.  Leave the value if
14940              there were any relocations where pointer equality matters
14941              (this is a clue for the dynamic linker, to make function
14942              pointer comparisons work between an application and shared
14943              library).  */
14944           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
14945             sym->st_value = 0;
14946         }
14947       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
14948         {
14949           /* At least one non-call relocation references this .iplt entry,
14950              so the .iplt entry is the function's canonical address.  */
14951           sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
14952           sym->st_target_internal = ST_BRANCH_TO_ARM;
14953           sym->st_shndx = (_bfd_elf_section_from_bfd_section
14954                            (output_bfd, htab->root.iplt->output_section));
14955           sym->st_value = (h->plt.offset
14956                            + htab->root.iplt->output_section->vma
14957                            + htab->root.iplt->output_offset);
14958         }
14959     }
14960
14961   if (h->needs_copy)
14962     {
14963       asection * s;
14964       Elf_Internal_Rela rel;
14965
14966       /* This symbol needs a copy reloc.  Set it up.  */
14967       BFD_ASSERT (h->dynindx != -1
14968                   && (h->root.type == bfd_link_hash_defined
14969                       || h->root.type == bfd_link_hash_defweak));
14970
14971       s = htab->srelbss;
14972       BFD_ASSERT (s != NULL);
14973
14974       rel.r_addend = 0;
14975       rel.r_offset = (h->root.u.def.value
14976                       + h->root.u.def.section->output_section->vma
14977                       + h->root.u.def.section->output_offset);
14978       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
14979       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
14980     }
14981
14982   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
14983      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
14984      to the ".got" section.  */
14985   if (h == htab->root.hdynamic
14986       || (!htab->vxworks_p && h == htab->root.hgot))
14987     sym->st_shndx = SHN_ABS;
14988
14989   return TRUE;
14990 }
14991
14992 static void
14993 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
14994                     void *contents,
14995                     const unsigned long *template, unsigned count)
14996 {
14997   unsigned ix;
14998
14999   for (ix = 0; ix != count; ix++)
15000     {
15001       unsigned long insn = template[ix];
15002
15003       /* Emit mov pc,rx if bx is not permitted.  */
15004       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
15005         insn = (insn & 0xf000000f) | 0x01a0f000;
15006       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
15007     }
15008 }
15009
15010 /* Install the special first PLT entry for elf32-arm-nacl.  Unlike
15011    other variants, NaCl needs this entry in a static executable's
15012    .iplt too.  When we're handling that case, GOT_DISPLACEMENT is
15013    zero.  For .iplt really only the last bundle is useful, and .iplt
15014    could have a shorter first entry, with each individual PLT entry's
15015    relative branch calculated differently so it targets the last
15016    bundle instead of the instruction before it (labelled .Lplt_tail
15017    above).  But it's simpler to keep the size and layout of PLT0
15018    consistent with the dynamic case, at the cost of some dead code at
15019    the start of .iplt and the one dead store to the stack at the start
15020    of .Lplt_tail.  */
15021 static void
15022 arm_nacl_put_plt0 (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
15023                    asection *plt, bfd_vma got_displacement)
15024 {
15025   unsigned int i;
15026
15027   put_arm_insn (htab, output_bfd,
15028                 elf32_arm_nacl_plt0_entry[0]
15029                 | arm_movw_immediate (got_displacement),
15030                 plt->contents + 0);
15031   put_arm_insn (htab, output_bfd,
15032                 elf32_arm_nacl_plt0_entry[1]
15033                 | arm_movt_immediate (got_displacement),
15034                 plt->contents + 4);
15035
15036   for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
15037     put_arm_insn (htab, output_bfd,
15038                   elf32_arm_nacl_plt0_entry[i],
15039                   plt->contents + (i * 4));
15040 }
15041
15042 /* Finish up the dynamic sections.  */
15043
15044 static bfd_boolean
15045 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
15046 {
15047   bfd * dynobj;
15048   asection * sgot;
15049   asection * sdyn;
15050   struct elf32_arm_link_hash_table *htab;
15051
15052   htab = elf32_arm_hash_table (info);
15053   if (htab == NULL)
15054     return FALSE;
15055
15056   dynobj = elf_hash_table (info)->dynobj;
15057
15058   sgot = htab->root.sgotplt;
15059   /* A broken linker script might have discarded the dynamic sections.
15060      Catch this here so that we do not seg-fault later on.  */
15061   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
15062     return FALSE;
15063   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15064
15065   if (elf_hash_table (info)->dynamic_sections_created)
15066     {
15067       asection *splt;
15068       Elf32_External_Dyn *dyncon, *dynconend;
15069
15070       splt = htab->root.splt;
15071       BFD_ASSERT (splt != NULL && sdyn != NULL);
15072       BFD_ASSERT (htab->symbian_p || sgot != NULL);
15073
15074       dyncon = (Elf32_External_Dyn *) sdyn->contents;
15075       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
15076
15077       for (; dyncon < dynconend; dyncon++)
15078         {
15079           Elf_Internal_Dyn dyn;
15080           const char * name;
15081           asection * s;
15082
15083           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
15084
15085           switch (dyn.d_tag)
15086             {
15087               unsigned int type;
15088
15089             default:
15090               if (htab->vxworks_p
15091                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
15092                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15093               break;
15094
15095             case DT_HASH:
15096               name = ".hash";
15097               goto get_vma_if_bpabi;
15098             case DT_STRTAB:
15099               name = ".dynstr";
15100               goto get_vma_if_bpabi;
15101             case DT_SYMTAB:
15102               name = ".dynsym";
15103               goto get_vma_if_bpabi;
15104             case DT_VERSYM:
15105               name = ".gnu.version";
15106               goto get_vma_if_bpabi;
15107             case DT_VERDEF:
15108               name = ".gnu.version_d";
15109               goto get_vma_if_bpabi;
15110             case DT_VERNEED:
15111               name = ".gnu.version_r";
15112               goto get_vma_if_bpabi;
15113
15114             case DT_PLTGOT:
15115               name = ".got";
15116               goto get_vma;
15117             case DT_JMPREL:
15118               name = RELOC_SECTION (htab, ".plt");
15119             get_vma:
15120               s = bfd_get_section_by_name (output_bfd, name);
15121               if (s == NULL)
15122                 {
15123                   /* PR ld/14397: Issue an error message if a required section is missing.  */
15124                   (*_bfd_error_handler)
15125                     (_("error: required section '%s' not found in the linker script"), name);
15126                   bfd_set_error (bfd_error_invalid_operation);
15127                   return FALSE;
15128                 }
15129               if (!htab->symbian_p)
15130                 dyn.d_un.d_ptr = s->vma;
15131               else
15132                 /* In the BPABI, tags in the PT_DYNAMIC section point
15133                    at the file offset, not the memory address, for the
15134                    convenience of the post linker.  */
15135                 dyn.d_un.d_ptr = s->filepos;
15136               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15137               break;
15138
15139             get_vma_if_bpabi:
15140               if (htab->symbian_p)
15141                 goto get_vma;
15142               break;
15143
15144             case DT_PLTRELSZ:
15145               s = htab->root.srelplt;
15146               BFD_ASSERT (s != NULL);
15147               dyn.d_un.d_val = s->size;
15148               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15149               break;
15150
15151             case DT_RELSZ:
15152             case DT_RELASZ:
15153               if (!htab->symbian_p)
15154                 {
15155                   /* My reading of the SVR4 ABI indicates that the
15156                      procedure linkage table relocs (DT_JMPREL) should be
15157                      included in the overall relocs (DT_REL).  This is
15158                      what Solaris does.  However, UnixWare can not handle
15159                      that case.  Therefore, we override the DT_RELSZ entry
15160                      here to make it not include the JMPREL relocs.  Since
15161                      the linker script arranges for .rel(a).plt to follow all
15162                      other relocation sections, we don't have to worry
15163                      about changing the DT_REL entry.  */
15164                   s = htab->root.srelplt;
15165                   if (s != NULL)
15166                     dyn.d_un.d_val -= s->size;
15167                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15168                   break;
15169                 }
15170               /* Fall through.  */
15171
15172             case DT_REL:
15173             case DT_RELA:
15174               /* In the BPABI, the DT_REL tag must point at the file
15175                  offset, not the VMA, of the first relocation
15176                  section.  So, we use code similar to that in
15177                  elflink.c, but do not check for SHF_ALLOC on the
15178                  relcoation section, since relocations sections are
15179                  never allocated under the BPABI.  The comments above
15180                  about Unixware notwithstanding, we include all of the
15181                  relocations here.  */
15182               if (htab->symbian_p)
15183                 {
15184                   unsigned int i;
15185                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
15186                           ? SHT_REL : SHT_RELA);
15187                   dyn.d_un.d_val = 0;
15188                   for (i = 1; i < elf_numsections (output_bfd); i++)
15189                     {
15190                       Elf_Internal_Shdr *hdr
15191                         = elf_elfsections (output_bfd)[i];
15192                       if (hdr->sh_type == type)
15193                         {
15194                           if (dyn.d_tag == DT_RELSZ
15195                               || dyn.d_tag == DT_RELASZ)
15196                             dyn.d_un.d_val += hdr->sh_size;
15197                           else if ((ufile_ptr) hdr->sh_offset
15198                                    <= dyn.d_un.d_val - 1)
15199                             dyn.d_un.d_val = hdr->sh_offset;
15200                         }
15201                     }
15202                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15203                 }
15204               break;
15205
15206             case DT_TLSDESC_PLT:
15207               s = htab->root.splt;
15208               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
15209                                 + htab->dt_tlsdesc_plt);
15210               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15211               break;
15212
15213             case DT_TLSDESC_GOT:
15214               s = htab->root.sgot;
15215               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
15216                                 + htab->dt_tlsdesc_got);
15217               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15218               break;
15219
15220               /* Set the bottom bit of DT_INIT/FINI if the
15221                  corresponding function is Thumb.  */
15222             case DT_INIT:
15223               name = info->init_function;
15224               goto get_sym;
15225             case DT_FINI:
15226               name = info->fini_function;
15227             get_sym:
15228               /* If it wasn't set by elf_bfd_final_link
15229                  then there is nothing to adjust.  */
15230               if (dyn.d_un.d_val != 0)
15231                 {
15232                   struct elf_link_hash_entry * eh;
15233
15234                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
15235                                              FALSE, FALSE, TRUE);
15236                   if (eh != NULL && eh->target_internal == ST_BRANCH_TO_THUMB)
15237                     {
15238                       dyn.d_un.d_val |= 1;
15239                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15240                     }
15241                 }
15242               break;
15243             }
15244         }
15245
15246       /* Fill in the first entry in the procedure linkage table.  */
15247       if (splt->size > 0 && htab->plt_header_size)
15248         {
15249           const bfd_vma *plt0_entry;
15250           bfd_vma got_address, plt_address, got_displacement;
15251
15252           /* Calculate the addresses of the GOT and PLT.  */
15253           got_address = sgot->output_section->vma + sgot->output_offset;
15254           plt_address = splt->output_section->vma + splt->output_offset;
15255
15256           if (htab->vxworks_p)
15257             {
15258               /* The VxWorks GOT is relocated by the dynamic linker.
15259                  Therefore, we must emit relocations rather than simply
15260                  computing the values now.  */
15261               Elf_Internal_Rela rel;
15262
15263               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
15264               put_arm_insn (htab, output_bfd, plt0_entry[0],
15265                             splt->contents + 0);
15266               put_arm_insn (htab, output_bfd, plt0_entry[1],
15267                             splt->contents + 4);
15268               put_arm_insn (htab, output_bfd, plt0_entry[2],
15269                             splt->contents + 8);
15270               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
15271
15272               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
15273               rel.r_offset = plt_address + 12;
15274               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
15275               rel.r_addend = 0;
15276               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
15277                                      htab->srelplt2->contents);
15278             }
15279           else if (htab->nacl_p)
15280             arm_nacl_put_plt0 (htab, output_bfd, splt,
15281                                got_address + 8 - (plt_address + 16));
15282           else if (using_thumb_only (htab))
15283             {
15284               got_displacement = got_address - (plt_address + 12);
15285
15286               plt0_entry = elf32_thumb2_plt0_entry;
15287               put_arm_insn (htab, output_bfd, plt0_entry[0],
15288                             splt->contents + 0);
15289               put_arm_insn (htab, output_bfd, plt0_entry[1],
15290                             splt->contents + 4);
15291               put_arm_insn (htab, output_bfd, plt0_entry[2],
15292                             splt->contents + 8);
15293
15294               bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
15295             }
15296           else
15297             {
15298               got_displacement = got_address - (plt_address + 16);
15299
15300               plt0_entry = elf32_arm_plt0_entry;
15301               put_arm_insn (htab, output_bfd, plt0_entry[0],
15302                             splt->contents + 0);
15303               put_arm_insn (htab, output_bfd, plt0_entry[1],
15304                             splt->contents + 4);
15305               put_arm_insn (htab, output_bfd, plt0_entry[2],
15306                             splt->contents + 8);
15307               put_arm_insn (htab, output_bfd, plt0_entry[3],
15308                             splt->contents + 12);
15309
15310 #ifdef FOUR_WORD_PLT
15311               /* The displacement value goes in the otherwise-unused
15312                  last word of the second entry.  */
15313               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
15314 #else
15315               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
15316 #endif
15317             }
15318         }
15319
15320       /* UnixWare sets the entsize of .plt to 4, although that doesn't
15321          really seem like the right value.  */
15322       if (splt->output_section->owner == output_bfd)
15323         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
15324
15325       if (htab->dt_tlsdesc_plt)
15326         {
15327           bfd_vma got_address
15328             = sgot->output_section->vma + sgot->output_offset;
15329           bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
15330                                     + htab->root.sgot->output_offset);
15331           bfd_vma plt_address
15332             = splt->output_section->vma + splt->output_offset;
15333
15334           arm_put_trampoline (htab, output_bfd,
15335                               splt->contents + htab->dt_tlsdesc_plt,
15336                               dl_tlsdesc_lazy_trampoline, 6);
15337
15338           bfd_put_32 (output_bfd,
15339                       gotplt_address + htab->dt_tlsdesc_got
15340                       - (plt_address + htab->dt_tlsdesc_plt)
15341                       - dl_tlsdesc_lazy_trampoline[6],
15342                       splt->contents + htab->dt_tlsdesc_plt + 24);
15343           bfd_put_32 (output_bfd,
15344                       got_address - (plt_address + htab->dt_tlsdesc_plt)
15345                       - dl_tlsdesc_lazy_trampoline[7],
15346                       splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
15347         }
15348
15349       if (htab->tls_trampoline)
15350         {
15351           arm_put_trampoline (htab, output_bfd,
15352                               splt->contents + htab->tls_trampoline,
15353                               tls_trampoline, 3);
15354 #ifdef FOUR_WORD_PLT
15355           bfd_put_32 (output_bfd, 0x00000000,
15356                       splt->contents + htab->tls_trampoline + 12);
15357 #endif
15358         }
15359
15360       if (htab->vxworks_p
15361           && !bfd_link_pic (info)
15362           && htab->root.splt->size > 0)
15363         {
15364           /* Correct the .rel(a).plt.unloaded relocations.  They will have
15365              incorrect symbol indexes.  */
15366           int num_plts;
15367           unsigned char *p;
15368
15369           num_plts = ((htab->root.splt->size - htab->plt_header_size)
15370                       / htab->plt_entry_size);
15371           p = htab->srelplt2->contents + RELOC_SIZE (htab);
15372
15373           for (; num_plts; num_plts--)
15374             {
15375               Elf_Internal_Rela rel;
15376
15377               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
15378               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
15379               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
15380               p += RELOC_SIZE (htab);
15381
15382               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
15383               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
15384               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
15385               p += RELOC_SIZE (htab);
15386             }
15387         }
15388     }
15389
15390   if (htab->nacl_p && htab->root.iplt != NULL && htab->root.iplt->size > 0)
15391     /* NaCl uses a special first entry in .iplt too.  */
15392     arm_nacl_put_plt0 (htab, output_bfd, htab->root.iplt, 0);
15393
15394   /* Fill in the first three entries in the global offset table.  */
15395   if (sgot)
15396     {
15397       if (sgot->size > 0)
15398         {
15399           if (sdyn == NULL)
15400             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
15401           else
15402             bfd_put_32 (output_bfd,
15403                         sdyn->output_section->vma + sdyn->output_offset,
15404                         sgot->contents);
15405           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
15406           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
15407         }
15408
15409       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
15410     }
15411
15412   return TRUE;
15413 }
15414
15415 static void
15416 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
15417 {
15418   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
15419   struct elf32_arm_link_hash_table *globals;
15420   struct elf_segment_map *m;
15421
15422   i_ehdrp = elf_elfheader (abfd);
15423
15424   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
15425     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
15426   else
15427     _bfd_elf_post_process_headers (abfd, link_info);
15428   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
15429
15430   if (link_info)
15431     {
15432       globals = elf32_arm_hash_table (link_info);
15433       if (globals != NULL && globals->byteswap_code)
15434         i_ehdrp->e_flags |= EF_ARM_BE8;
15435     }
15436
15437   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
15438       && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
15439     {
15440       int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
15441       if (abi == AEABI_VFP_args_vfp)
15442         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
15443       else
15444         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
15445     }
15446
15447   /* Scan segment to set p_flags attribute if it contains only sections with
15448      SHF_ARM_NOREAD flag.  */
15449   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
15450     {
15451       unsigned int j;
15452
15453       if (m->count == 0)
15454         continue;
15455       for (j = 0; j < m->count; j++)
15456         {
15457           if (!(elf_section_flags (m->sections[j]) & SHF_ARM_NOREAD))
15458             break;
15459         }
15460       if (j == m->count)
15461         {
15462           m->p_flags = PF_X;
15463           m->p_flags_valid = 1;
15464         }
15465     }
15466 }
15467
15468 static enum elf_reloc_type_class
15469 elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
15470                             const asection *rel_sec ATTRIBUTE_UNUSED,
15471                             const Elf_Internal_Rela *rela)
15472 {
15473   switch ((int) ELF32_R_TYPE (rela->r_info))
15474     {
15475     case R_ARM_RELATIVE:
15476       return reloc_class_relative;
15477     case R_ARM_JUMP_SLOT:
15478       return reloc_class_plt;
15479     case R_ARM_COPY:
15480       return reloc_class_copy;
15481     default:
15482       return reloc_class_normal;
15483     }
15484 }
15485
15486 static void
15487 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
15488 {
15489   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
15490 }
15491
15492 /* Return TRUE if this is an unwinding table entry.  */
15493
15494 static bfd_boolean
15495 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
15496 {
15497   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
15498           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
15499 }
15500
15501
15502 /* Set the type and flags for an ARM section.  We do this by
15503    the section name, which is a hack, but ought to work.  */
15504
15505 static bfd_boolean
15506 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
15507 {
15508   const char * name;
15509
15510   name = bfd_get_section_name (abfd, sec);
15511
15512   if (is_arm_elf_unwind_section_name (abfd, name))
15513     {
15514       hdr->sh_type = SHT_ARM_EXIDX;
15515       hdr->sh_flags |= SHF_LINK_ORDER;
15516     }
15517
15518   if (sec->flags & SEC_ELF_NOREAD)
15519     hdr->sh_flags |= SHF_ARM_NOREAD;
15520
15521   return TRUE;
15522 }
15523
15524 /* Handle an ARM specific section when reading an object file.  This is
15525    called when bfd_section_from_shdr finds a section with an unknown
15526    type.  */
15527
15528 static bfd_boolean
15529 elf32_arm_section_from_shdr (bfd *abfd,
15530                              Elf_Internal_Shdr * hdr,
15531                              const char *name,
15532                              int shindex)
15533 {
15534   /* There ought to be a place to keep ELF backend specific flags, but
15535      at the moment there isn't one.  We just keep track of the
15536      sections by their name, instead.  Fortunately, the ABI gives
15537      names for all the ARM specific sections, so we will probably get
15538      away with this.  */
15539   switch (hdr->sh_type)
15540     {
15541     case SHT_ARM_EXIDX:
15542     case SHT_ARM_PREEMPTMAP:
15543     case SHT_ARM_ATTRIBUTES:
15544       break;
15545
15546     default:
15547       return FALSE;
15548     }
15549
15550   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
15551     return FALSE;
15552
15553   return TRUE;
15554 }
15555
15556 static _arm_elf_section_data *
15557 get_arm_elf_section_data (asection * sec)
15558 {
15559   if (sec && sec->owner && is_arm_elf (sec->owner))
15560     return elf32_arm_section_data (sec);
15561   else
15562     return NULL;
15563 }
15564
15565 typedef struct
15566 {
15567   void *flaginfo;
15568   struct bfd_link_info *info;
15569   asection *sec;
15570   int sec_shndx;
15571   int (*func) (void *, const char *, Elf_Internal_Sym *,
15572                asection *, struct elf_link_hash_entry *);
15573 } output_arch_syminfo;
15574
15575 enum map_symbol_type
15576 {
15577   ARM_MAP_ARM,
15578   ARM_MAP_THUMB,
15579   ARM_MAP_DATA
15580 };
15581
15582
15583 /* Output a single mapping symbol.  */
15584
15585 static bfd_boolean
15586 elf32_arm_output_map_sym (output_arch_syminfo *osi,
15587                           enum map_symbol_type type,
15588                           bfd_vma offset)
15589 {
15590   static const char *names[3] = {"$a", "$t", "$d"};
15591   Elf_Internal_Sym sym;
15592
15593   sym.st_value = osi->sec->output_section->vma
15594                  + osi->sec->output_offset
15595                  + offset;
15596   sym.st_size = 0;
15597   sym.st_other = 0;
15598   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
15599   sym.st_shndx = osi->sec_shndx;
15600   sym.st_target_internal = 0;
15601   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
15602   return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
15603 }
15604
15605 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
15606    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
15607
15608 static bfd_boolean
15609 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
15610                             bfd_boolean is_iplt_entry_p,
15611                             union gotplt_union *root_plt,
15612                             struct arm_plt_info *arm_plt)
15613 {
15614   struct elf32_arm_link_hash_table *htab;
15615   bfd_vma addr, plt_header_size;
15616
15617   if (root_plt->offset == (bfd_vma) -1)
15618     return TRUE;
15619
15620   htab = elf32_arm_hash_table (osi->info);
15621   if (htab == NULL)
15622     return FALSE;
15623
15624   if (is_iplt_entry_p)
15625     {
15626       osi->sec = htab->root.iplt;
15627       plt_header_size = 0;
15628     }
15629   else
15630     {
15631       osi->sec = htab->root.splt;
15632       plt_header_size = htab->plt_header_size;
15633     }
15634   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
15635                     (osi->info->output_bfd, osi->sec->output_section));
15636
15637   addr = root_plt->offset & -2;
15638   if (htab->symbian_p)
15639     {
15640       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15641         return FALSE;
15642       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
15643         return FALSE;
15644     }
15645   else if (htab->vxworks_p)
15646     {
15647       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15648         return FALSE;
15649       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
15650         return FALSE;
15651       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
15652         return FALSE;
15653       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
15654         return FALSE;
15655     }
15656   else if (htab->nacl_p)
15657     {
15658       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15659         return FALSE;
15660     }
15661   else if (using_thumb_only (htab))
15662     {
15663       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
15664         return FALSE;
15665     }
15666   else
15667     {
15668       bfd_boolean thumb_stub_p;
15669
15670       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
15671       if (thumb_stub_p)
15672         {
15673           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
15674             return FALSE;
15675         }
15676 #ifdef FOUR_WORD_PLT
15677       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15678         return FALSE;
15679       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
15680         return FALSE;
15681 #else
15682       /* A three-word PLT with no Thumb thunk contains only Arm code,
15683          so only need to output a mapping symbol for the first PLT entry and
15684          entries with thumb thunks.  */
15685       if (thumb_stub_p || addr == plt_header_size)
15686         {
15687           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15688             return FALSE;
15689         }
15690 #endif
15691     }
15692
15693   return TRUE;
15694 }
15695
15696 /* Output mapping symbols for PLT entries associated with H.  */
15697
15698 static bfd_boolean
15699 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
15700 {
15701   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
15702   struct elf32_arm_link_hash_entry *eh;
15703
15704   if (h->root.type == bfd_link_hash_indirect)
15705     return TRUE;
15706
15707   if (h->root.type == bfd_link_hash_warning)
15708     /* When warning symbols are created, they **replace** the "real"
15709        entry in the hash table, thus we never get to see the real
15710        symbol in a hash traversal.  So look at it now.  */
15711     h = (struct elf_link_hash_entry *) h->root.u.i.link;
15712
15713   eh = (struct elf32_arm_link_hash_entry *) h;
15714   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
15715                                      &h->plt, &eh->plt);
15716 }
15717
15718 /* Output a single local symbol for a generated stub.  */
15719
15720 static bfd_boolean
15721 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
15722                            bfd_vma offset, bfd_vma size)
15723 {
15724   Elf_Internal_Sym sym;
15725
15726   sym.st_value = osi->sec->output_section->vma
15727                  + osi->sec->output_offset
15728                  + offset;
15729   sym.st_size = size;
15730   sym.st_other = 0;
15731   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
15732   sym.st_shndx = osi->sec_shndx;
15733   sym.st_target_internal = 0;
15734   return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
15735 }
15736
15737 static bfd_boolean
15738 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
15739                   void * in_arg)
15740 {
15741   struct elf32_arm_stub_hash_entry *stub_entry;
15742   asection *stub_sec;
15743   bfd_vma addr;
15744   char *stub_name;
15745   output_arch_syminfo *osi;
15746   const insn_sequence *template_sequence;
15747   enum stub_insn_type prev_type;
15748   int size;
15749   int i;
15750   enum map_symbol_type sym_type;
15751
15752   /* Massage our args to the form they really have.  */
15753   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
15754   osi = (output_arch_syminfo *) in_arg;
15755
15756   stub_sec = stub_entry->stub_sec;
15757
15758   /* Ensure this stub is attached to the current section being
15759      processed.  */
15760   if (stub_sec != osi->sec)
15761     return TRUE;
15762
15763   addr = (bfd_vma) stub_entry->stub_offset;
15764   stub_name = stub_entry->output_name;
15765
15766   template_sequence = stub_entry->stub_template;
15767   switch (template_sequence[0].type)
15768     {
15769     case ARM_TYPE:
15770       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, stub_entry->stub_size))
15771         return FALSE;
15772       break;
15773     case THUMB16_TYPE:
15774     case THUMB32_TYPE:
15775       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
15776                                       stub_entry->stub_size))
15777         return FALSE;
15778       break;
15779     default:
15780       BFD_FAIL ();
15781       return 0;
15782     }
15783
15784   prev_type = DATA_TYPE;
15785   size = 0;
15786   for (i = 0; i < stub_entry->stub_template_size; i++)
15787     {
15788       switch (template_sequence[i].type)
15789         {
15790         case ARM_TYPE:
15791           sym_type = ARM_MAP_ARM;
15792           break;
15793
15794         case THUMB16_TYPE:
15795         case THUMB32_TYPE:
15796           sym_type = ARM_MAP_THUMB;
15797           break;
15798
15799         case DATA_TYPE:
15800           sym_type = ARM_MAP_DATA;
15801           break;
15802
15803         default:
15804           BFD_FAIL ();
15805           return FALSE;
15806         }
15807
15808       if (template_sequence[i].type != prev_type)
15809         {
15810           prev_type = template_sequence[i].type;
15811           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
15812             return FALSE;
15813         }
15814
15815       switch (template_sequence[i].type)
15816         {
15817         case ARM_TYPE:
15818         case THUMB32_TYPE:
15819           size += 4;
15820           break;
15821
15822         case THUMB16_TYPE:
15823           size += 2;
15824           break;
15825
15826         case DATA_TYPE:
15827           size += 4;
15828           break;
15829
15830         default:
15831           BFD_FAIL ();
15832           return FALSE;
15833         }
15834     }
15835
15836   return TRUE;
15837 }
15838
15839 /* Output mapping symbols for linker generated sections,
15840    and for those data-only sections that do not have a
15841    $d.  */
15842
15843 static bfd_boolean
15844 elf32_arm_output_arch_local_syms (bfd *output_bfd,
15845                                   struct bfd_link_info *info,
15846                                   void *flaginfo,
15847                                   int (*func) (void *, const char *,
15848                                                Elf_Internal_Sym *,
15849                                                asection *,
15850                                                struct elf_link_hash_entry *))
15851 {
15852   output_arch_syminfo osi;
15853   struct elf32_arm_link_hash_table *htab;
15854   bfd_vma offset;
15855   bfd_size_type size;
15856   bfd *input_bfd;
15857
15858   htab = elf32_arm_hash_table (info);
15859   if (htab == NULL)
15860     return FALSE;
15861
15862   check_use_blx (htab);
15863
15864   osi.flaginfo = flaginfo;
15865   osi.info = info;
15866   osi.func = func;
15867
15868   /* Add a $d mapping symbol to data-only sections that
15869      don't have any mapping symbol.  This may result in (harmless) redundant
15870      mapping symbols.  */
15871   for (input_bfd = info->input_bfds;
15872        input_bfd != NULL;
15873        input_bfd = input_bfd->link.next)
15874     {
15875       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
15876         for (osi.sec = input_bfd->sections;
15877              osi.sec != NULL;
15878              osi.sec = osi.sec->next)
15879           {
15880             if (osi.sec->output_section != NULL
15881                 && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
15882                     != 0)
15883                 && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
15884                    == SEC_HAS_CONTENTS
15885                 && get_arm_elf_section_data (osi.sec) != NULL
15886                 && get_arm_elf_section_data (osi.sec)->mapcount == 0
15887                 && osi.sec->size > 0
15888                 && (osi.sec->flags & SEC_EXCLUDE) == 0)
15889               {
15890                 osi.sec_shndx = _bfd_elf_section_from_bfd_section
15891                   (output_bfd, osi.sec->output_section);
15892                 if (osi.sec_shndx != (int)SHN_BAD)
15893                   elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
15894               }
15895           }
15896     }
15897
15898   /* ARM->Thumb glue.  */
15899   if (htab->arm_glue_size > 0)
15900     {
15901       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15902                                         ARM2THUMB_GLUE_SECTION_NAME);
15903
15904       osi.sec_shndx = _bfd_elf_section_from_bfd_section
15905           (output_bfd, osi.sec->output_section);
15906       if (bfd_link_pic (info) || htab->root.is_relocatable_executable
15907           || htab->pic_veneer)
15908         size = ARM2THUMB_PIC_GLUE_SIZE;
15909       else if (htab->use_blx)
15910         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
15911       else
15912         size = ARM2THUMB_STATIC_GLUE_SIZE;
15913
15914       for (offset = 0; offset < htab->arm_glue_size; offset += size)
15915         {
15916           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
15917           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
15918         }
15919     }
15920
15921   /* Thumb->ARM glue.  */
15922   if (htab->thumb_glue_size > 0)
15923     {
15924       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15925                                         THUMB2ARM_GLUE_SECTION_NAME);
15926
15927       osi.sec_shndx = _bfd_elf_section_from_bfd_section
15928           (output_bfd, osi.sec->output_section);
15929       size = THUMB2ARM_GLUE_SIZE;
15930
15931       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
15932         {
15933           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
15934           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
15935         }
15936     }
15937
15938   /* ARMv4 BX veneers.  */
15939   if (htab->bx_glue_size > 0)
15940     {
15941       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15942                                         ARM_BX_GLUE_SECTION_NAME);
15943
15944       osi.sec_shndx = _bfd_elf_section_from_bfd_section
15945           (output_bfd, osi.sec->output_section);
15946
15947       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
15948     }
15949
15950   /* Long calls stubs.  */
15951   if (htab->stub_bfd && htab->stub_bfd->sections)
15952     {
15953       asection* stub_sec;
15954
15955       for (stub_sec = htab->stub_bfd->sections;
15956            stub_sec != NULL;
15957            stub_sec = stub_sec->next)
15958         {
15959           /* Ignore non-stub sections.  */
15960           if (!strstr (stub_sec->name, STUB_SUFFIX))
15961             continue;
15962
15963           osi.sec = stub_sec;
15964
15965           osi.sec_shndx = _bfd_elf_section_from_bfd_section
15966             (output_bfd, osi.sec->output_section);
15967
15968           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
15969         }
15970     }
15971
15972   /* Finally, output mapping symbols for the PLT.  */
15973   if (htab->root.splt && htab->root.splt->size > 0)
15974     {
15975       osi.sec = htab->root.splt;
15976       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
15977                        (output_bfd, osi.sec->output_section));
15978
15979       /* Output mapping symbols for the plt header.  SymbianOS does not have a
15980          plt header.  */
15981       if (htab->vxworks_p)
15982         {
15983           /* VxWorks shared libraries have no PLT header.  */
15984           if (!bfd_link_pic (info))
15985             {
15986               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
15987                 return FALSE;
15988               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
15989                 return FALSE;
15990             }
15991         }
15992       else if (htab->nacl_p)
15993         {
15994           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
15995             return FALSE;
15996         }
15997       else if (using_thumb_only (htab))
15998         {
15999           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
16000             return FALSE;
16001           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
16002             return FALSE;
16003           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
16004             return FALSE;
16005         }
16006       else if (!htab->symbian_p)
16007         {
16008           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16009             return FALSE;
16010 #ifndef FOUR_WORD_PLT
16011           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
16012             return FALSE;
16013 #endif
16014         }
16015     }
16016   if (htab->nacl_p && htab->root.iplt && htab->root.iplt->size > 0)
16017     {
16018       /* NaCl uses a special first entry in .iplt too.  */
16019       osi.sec = htab->root.iplt;
16020       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
16021                        (output_bfd, osi.sec->output_section));
16022       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16023         return FALSE;
16024     }
16025   if ((htab->root.splt && htab->root.splt->size > 0)
16026       || (htab->root.iplt && htab->root.iplt->size > 0))
16027     {
16028       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
16029       for (input_bfd = info->input_bfds;
16030            input_bfd != NULL;
16031            input_bfd = input_bfd->link.next)
16032         {
16033           struct arm_local_iplt_info **local_iplt;
16034           unsigned int i, num_syms;
16035
16036           local_iplt = elf32_arm_local_iplt (input_bfd);
16037           if (local_iplt != NULL)
16038             {
16039               num_syms = elf_symtab_hdr (input_bfd).sh_info;
16040               for (i = 0; i < num_syms; i++)
16041                 if (local_iplt[i] != NULL
16042                     && !elf32_arm_output_plt_map_1 (&osi, TRUE,
16043                                                     &local_iplt[i]->root,
16044                                                     &local_iplt[i]->arm))
16045                   return FALSE;
16046             }
16047         }
16048     }
16049   if (htab->dt_tlsdesc_plt != 0)
16050     {
16051       /* Mapping symbols for the lazy tls trampoline.  */
16052       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
16053         return FALSE;
16054
16055       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
16056                                      htab->dt_tlsdesc_plt + 24))
16057         return FALSE;
16058     }
16059   if (htab->tls_trampoline != 0)
16060     {
16061       /* Mapping symbols for the tls trampoline.  */
16062       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
16063         return FALSE;
16064 #ifdef FOUR_WORD_PLT
16065       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
16066                                      htab->tls_trampoline + 12))
16067         return FALSE;
16068 #endif
16069     }
16070
16071   return TRUE;
16072 }
16073
16074 /* Allocate target specific section data.  */
16075
16076 static bfd_boolean
16077 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
16078 {
16079   if (!sec->used_by_bfd)
16080     {
16081       _arm_elf_section_data *sdata;
16082       bfd_size_type amt = sizeof (*sdata);
16083
16084       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
16085       if (sdata == NULL)
16086         return FALSE;
16087       sec->used_by_bfd = sdata;
16088     }
16089
16090   return _bfd_elf_new_section_hook (abfd, sec);
16091 }
16092
16093
16094 /* Used to order a list of mapping symbols by address.  */
16095
16096 static int
16097 elf32_arm_compare_mapping (const void * a, const void * b)
16098 {
16099   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
16100   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
16101
16102   if (amap->vma > bmap->vma)
16103     return 1;
16104   else if (amap->vma < bmap->vma)
16105     return -1;
16106   else if (amap->type > bmap->type)
16107     /* Ensure results do not depend on the host qsort for objects with
16108        multiple mapping symbols at the same address by sorting on type
16109        after vma.  */
16110     return 1;
16111   else if (amap->type < bmap->type)
16112     return -1;
16113   else
16114     return 0;
16115 }
16116
16117 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
16118
16119 static unsigned long
16120 offset_prel31 (unsigned long addr, bfd_vma offset)
16121 {
16122   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
16123 }
16124
16125 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
16126    relocations.  */
16127
16128 static void
16129 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
16130 {
16131   unsigned long first_word = bfd_get_32 (output_bfd, from);
16132   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
16133
16134   /* High bit of first word is supposed to be zero.  */
16135   if ((first_word & 0x80000000ul) == 0)
16136     first_word = offset_prel31 (first_word, offset);
16137
16138   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
16139      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
16140   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
16141     second_word = offset_prel31 (second_word, offset);
16142
16143   bfd_put_32 (output_bfd, first_word, to);
16144   bfd_put_32 (output_bfd, second_word, to + 4);
16145 }
16146
16147 /* Data for make_branch_to_a8_stub().  */
16148
16149 struct a8_branch_to_stub_data
16150 {
16151   asection *writing_section;
16152   bfd_byte *contents;
16153 };
16154
16155
16156 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
16157    places for a particular section.  */
16158
16159 static bfd_boolean
16160 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
16161                        void *in_arg)
16162 {
16163   struct elf32_arm_stub_hash_entry *stub_entry;
16164   struct a8_branch_to_stub_data *data;
16165   bfd_byte *contents;
16166   unsigned long branch_insn;
16167   bfd_vma veneered_insn_loc, veneer_entry_loc;
16168   bfd_signed_vma branch_offset;
16169   bfd *abfd;
16170   unsigned int target;
16171
16172   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
16173   data = (struct a8_branch_to_stub_data *) in_arg;
16174
16175   if (stub_entry->target_section != data->writing_section
16176       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
16177     return TRUE;
16178
16179   contents = data->contents;
16180
16181   veneered_insn_loc = stub_entry->target_section->output_section->vma
16182                       + stub_entry->target_section->output_offset
16183                       + stub_entry->target_value;
16184
16185   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
16186                      + stub_entry->stub_sec->output_offset
16187                      + stub_entry->stub_offset;
16188
16189   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
16190     veneered_insn_loc &= ~3u;
16191
16192   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
16193
16194   abfd = stub_entry->target_section->owner;
16195   target = stub_entry->target_value;
16196
16197   /* We attempt to avoid this condition by setting stubs_always_after_branch
16198      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
16199      This check is just to be on the safe side...  */
16200   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
16201     {
16202       (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub is "
16203                                "allocated in unsafe location"), abfd);
16204       return FALSE;
16205     }
16206
16207   switch (stub_entry->stub_type)
16208     {
16209     case arm_stub_a8_veneer_b:
16210     case arm_stub_a8_veneer_b_cond:
16211       branch_insn = 0xf0009000;
16212       goto jump24;
16213
16214     case arm_stub_a8_veneer_blx:
16215       branch_insn = 0xf000e800;
16216       goto jump24;
16217
16218     case arm_stub_a8_veneer_bl:
16219       {
16220         unsigned int i1, j1, i2, j2, s;
16221
16222         branch_insn = 0xf000d000;
16223
16224       jump24:
16225         if (branch_offset < -16777216 || branch_offset > 16777214)
16226           {
16227             /* There's not much we can do apart from complain if this
16228                happens.  */
16229             (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub out "
16230                                      "of range (input file too large)"), abfd);
16231             return FALSE;
16232           }
16233
16234         /* i1 = not(j1 eor s), so:
16235            not i1 = j1 eor s
16236            j1 = (not i1) eor s.  */
16237
16238         branch_insn |= (branch_offset >> 1) & 0x7ff;
16239         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
16240         i2 = (branch_offset >> 22) & 1;
16241         i1 = (branch_offset >> 23) & 1;
16242         s = (branch_offset >> 24) & 1;
16243         j1 = (!i1) ^ s;
16244         j2 = (!i2) ^ s;
16245         branch_insn |= j2 << 11;
16246         branch_insn |= j1 << 13;
16247         branch_insn |= s << 26;
16248       }
16249       break;
16250
16251     default:
16252       BFD_FAIL ();
16253       return FALSE;
16254     }
16255
16256   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[target]);
16257   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[target + 2]);
16258
16259   return TRUE;
16260 }
16261
16262 /* Beginning of stm32l4xx work-around.  */
16263
16264 /* Functions encoding instructions necessary for the emission of the
16265    fix-stm32l4xx-629360.
16266    Encoding is extracted from the
16267    ARM (C) Architecture Reference Manual
16268    ARMv7-A and ARMv7-R edition
16269    ARM DDI 0406C.b (ID072512).  */
16270
16271 static inline bfd_vma
16272 create_instruction_branch_absolute (int branch_offset)
16273 {
16274   /* A8.8.18 B (A8-334)
16275      B target_address (Encoding T4).  */
16276   /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
16277   /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
16278   /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
16279
16280   int s = ((branch_offset & 0x1000000) >> 24);
16281   int j1 = s ^ !((branch_offset & 0x800000) >> 23);
16282   int j2 = s ^ !((branch_offset & 0x400000) >> 22);
16283
16284   if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
16285     BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
16286
16287   bfd_vma patched_inst = 0xf0009000
16288     | s << 26 /* S.  */
16289     | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
16290     | j1 << 13 /* J1.  */
16291     | j2 << 11 /* J2.  */
16292     | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
16293
16294   return patched_inst;
16295 }
16296
16297 static inline bfd_vma
16298 create_instruction_ldmia (int base_reg, int wback, int reg_mask)
16299 {
16300   /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
16301      LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
16302   bfd_vma patched_inst = 0xe8900000
16303     | (/*W=*/wback << 21)
16304     | (base_reg << 16)
16305     | (reg_mask & 0x0000ffff);
16306
16307   return patched_inst;
16308 }
16309
16310 static inline bfd_vma
16311 create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
16312 {
16313   /* A8.8.60 LDMDB/LDMEA (A8-402)
16314      LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
16315   bfd_vma patched_inst = 0xe9100000
16316     | (/*W=*/wback << 21)
16317     | (base_reg << 16)
16318     | (reg_mask & 0x0000ffff);
16319
16320   return patched_inst;
16321 }
16322
16323 static inline bfd_vma
16324 create_instruction_mov (int target_reg, int source_reg)
16325 {
16326   /* A8.8.103 MOV (register) (A8-486)
16327      MOV Rd, Rm (Encoding T1).  */
16328   bfd_vma patched_inst = 0x4600
16329     | (target_reg & 0x7)
16330     | ((target_reg & 0x8) >> 3) << 7
16331     | (source_reg << 3);
16332
16333   return patched_inst;
16334 }
16335
16336 static inline bfd_vma
16337 create_instruction_sub (int target_reg, int source_reg, int value)
16338 {
16339   /* A8.8.221 SUB (immediate) (A8-708)
16340      SUB Rd, Rn, #value (Encoding T3).  */
16341   bfd_vma patched_inst = 0xf1a00000
16342     | (target_reg << 8)
16343     | (source_reg << 16)
16344     | (/*S=*/0 << 20)
16345     | ((value & 0x800) >> 11) << 26
16346     | ((value & 0x700) >>  8) << 12
16347     | (value & 0x0ff);
16348
16349   return patched_inst;
16350 }
16351
16352 static inline bfd_vma
16353 create_instruction_vldmia (int base_reg, int wback, int num_regs,
16354                            int first_reg)
16355 {
16356   /* A8.8.332 VLDM (A8-922)
16357      VLMD{MODE} Rn{!}, {list} (Encoding T2).  */
16358   bfd_vma patched_inst = 0xec900a00
16359     | (/*W=*/wback << 21)
16360     | (base_reg << 16)
16361     | (num_regs & 0x000000ff)
16362     | (((unsigned)first_reg>>1) & 0x0000000f) << 12
16363     | (first_reg & 0x00000001) << 22;
16364
16365   return patched_inst;
16366 }
16367
16368 static inline bfd_vma
16369 create_instruction_vldmdb (int base_reg, int num_regs, int first_reg)
16370 {
16371   /* A8.8.332 VLDM (A8-922)
16372      VLMD{MODE} Rn!, {} (Encoding T2).  */
16373   bfd_vma patched_inst = 0xed300a00
16374     | (base_reg << 16)
16375     | (num_regs & 0x000000ff)
16376     | (((unsigned)first_reg>>1) & 0x0000000f) << 12
16377     | (first_reg & 0x00000001) << 22;
16378
16379   return patched_inst;
16380 }
16381
16382 static inline bfd_vma
16383 create_instruction_udf_w (int value)
16384 {
16385   /* A8.8.247 UDF (A8-758)
16386      Undefined (Encoding T2).  */
16387   bfd_vma patched_inst = 0xf7f0a000
16388     | (value & 0x00000fff)
16389     | (value & 0x000f0000) << 16;
16390
16391   return patched_inst;
16392 }
16393
16394 static inline bfd_vma
16395 create_instruction_udf (int value)
16396 {
16397   /* A8.8.247 UDF (A8-758)
16398      Undefined (Encoding T1).  */
16399   bfd_vma patched_inst = 0xde00
16400     | (value & 0xff);
16401
16402   return patched_inst;
16403 }
16404
16405 /* Functions writing an instruction in memory, returning the next
16406    memory position to write to.  */
16407
16408 static inline bfd_byte *
16409 push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
16410                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
16411 {
16412   put_thumb2_insn (htab, output_bfd, insn, pt);
16413   return pt + 4;
16414 }
16415
16416 static inline bfd_byte *
16417 push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
16418                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
16419 {
16420   put_thumb_insn (htab, output_bfd, insn, pt);
16421   return pt + 2;
16422 }
16423
16424 /* Function filling up a region in memory with T1 and T2 UDFs taking
16425    care of alignment.  */
16426
16427 static bfd_byte *
16428 stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
16429                          bfd *                   output_bfd,
16430                          const bfd_byte * const  base_stub_contents,
16431                          bfd_byte * const        from_stub_contents,
16432                          const bfd_byte * const  end_stub_contents)
16433 {
16434   bfd_byte *current_stub_contents = from_stub_contents;
16435
16436   /* Fill the remaining of the stub with deterministic contents : UDF
16437      instructions.
16438      Check if realignment is needed on modulo 4 frontier using T1, to
16439      further use T2.  */
16440   if ((current_stub_contents < end_stub_contents)
16441       && !((current_stub_contents - base_stub_contents) % 2)
16442       && ((current_stub_contents - base_stub_contents) % 4))
16443     current_stub_contents =
16444       push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16445                           create_instruction_udf (0));
16446
16447   for (; current_stub_contents < end_stub_contents;)
16448     current_stub_contents =
16449       push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16450                           create_instruction_udf_w (0));
16451
16452   return current_stub_contents;
16453 }
16454
16455 /* Functions writing the stream of instructions equivalent to the
16456    derived sequence for ldmia, ldmdb, vldm respectively.  */
16457
16458 static void
16459 stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
16460                                        bfd * output_bfd,
16461                                        const insn32 initial_insn,
16462                                        const bfd_byte *const initial_insn_addr,
16463                                        bfd_byte *const base_stub_contents)
16464 {
16465   int wback = (initial_insn & 0x00200000) >> 21;
16466   int ri, rn = (initial_insn & 0x000F0000) >> 16;
16467   int insn_all_registers = initial_insn & 0x0000ffff;
16468   int insn_low_registers, insn_high_registers;
16469   int usable_register_mask;
16470   int nb_registers = popcount (insn_all_registers);
16471   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
16472   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
16473   bfd_byte *current_stub_contents = base_stub_contents;
16474
16475   BFD_ASSERT (is_thumb2_ldmia (initial_insn));
16476
16477   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16478      smaller than 8 registers load sequences that do not cause the
16479      hardware issue.  */
16480   if (nb_registers <= 8)
16481     {
16482       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
16483       current_stub_contents =
16484         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16485                             initial_insn);
16486
16487       /* B initial_insn_addr+4.  */
16488       if (!restore_pc)
16489         current_stub_contents =
16490           push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16491                               create_instruction_branch_absolute
16492                               (initial_insn_addr - current_stub_contents));
16493                                
16494
16495       /* Fill the remaining of the stub with deterministic contents.  */
16496       current_stub_contents =
16497         stm32l4xx_fill_stub_udf (htab, output_bfd,
16498                                  base_stub_contents, current_stub_contents,
16499                                  base_stub_contents +
16500                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16501
16502       return;
16503     }
16504
16505   /* - reg_list[13] == 0.  */
16506   BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
16507
16508   /* - reg_list[14] & reg_list[15] != 1.  */
16509   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
16510
16511   /* - if (wback==1) reg_list[rn] == 0.  */
16512   BFD_ASSERT (!wback || !restore_rn);
16513
16514   /* - nb_registers > 8.  */
16515   BFD_ASSERT (popcount (insn_all_registers) > 8);
16516
16517   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
16518
16519   /* In the following algorithm, we split this wide LDM using 2 LDM insns:
16520     - One with the 7 lowest registers (register mask 0x007F)
16521       This LDM will finally contain between 2 and 7 registers
16522     - One with the 7 highest registers (register mask 0xDF80)
16523       This ldm will finally contain between 2 and 7 registers.  */
16524   insn_low_registers = insn_all_registers & 0x007F;
16525   insn_high_registers = insn_all_registers & 0xDF80;
16526
16527   /* A spare register may be needed during this veneer to temporarily
16528      handle the base register.  This register will be restored with the
16529      last LDM operation.
16530      The usable register may be any general purpose register (that
16531      excludes PC, SP, LR : register mask is 0x1FFF).  */
16532   usable_register_mask = 0x1FFF;
16533
16534   /* Generate the stub function.  */
16535   if (wback)
16536     {
16537       /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
16538       current_stub_contents =
16539         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16540                             create_instruction_ldmia
16541                             (rn, /*wback=*/1, insn_low_registers));
16542
16543       /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
16544       current_stub_contents =
16545         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16546                             create_instruction_ldmia
16547                             (rn, /*wback=*/1, insn_high_registers));
16548       if (!restore_pc)
16549         {
16550           /* B initial_insn_addr+4.  */
16551           current_stub_contents =
16552             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16553                                 create_instruction_branch_absolute
16554                                 (initial_insn_addr - current_stub_contents));
16555        }
16556     }
16557   else /* if (!wback).  */
16558     {
16559       ri = rn;
16560
16561       /* If Rn is not part of the high-register-list, move it there.  */
16562       if (!(insn_high_registers & (1 << rn)))
16563         {
16564           /* Choose a Ri in the high-register-list that will be restored.  */
16565           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16566
16567           /* MOV Ri, Rn.  */
16568           current_stub_contents =
16569             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16570                                 create_instruction_mov (ri, rn));
16571         }
16572
16573       /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
16574       current_stub_contents =
16575         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16576                             create_instruction_ldmia
16577                             (ri, /*wback=*/1, insn_low_registers));
16578
16579       /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
16580       current_stub_contents =
16581         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16582                             create_instruction_ldmia
16583                             (ri, /*wback=*/0, insn_high_registers));
16584
16585       if (!restore_pc)
16586         {
16587           /* B initial_insn_addr+4.  */
16588           current_stub_contents =
16589             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16590                                 create_instruction_branch_absolute
16591                                 (initial_insn_addr - current_stub_contents));
16592         }
16593     }
16594
16595   /* Fill the remaining of the stub with deterministic contents.  */
16596   current_stub_contents =
16597     stm32l4xx_fill_stub_udf (htab, output_bfd,
16598                              base_stub_contents, current_stub_contents,
16599                              base_stub_contents +
16600                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16601 }
16602
16603 static void
16604 stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
16605                                        bfd * output_bfd,
16606                                        const insn32 initial_insn,
16607                                        const bfd_byte *const initial_insn_addr,
16608                                        bfd_byte *const base_stub_contents)
16609 {
16610   int wback = (initial_insn & 0x00200000) >> 21;
16611   int ri, rn = (initial_insn & 0x000f0000) >> 16;
16612   int insn_all_registers = initial_insn & 0x0000ffff;
16613   int insn_low_registers, insn_high_registers;
16614   int usable_register_mask;
16615   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
16616   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
16617   int nb_registers = popcount (insn_all_registers);
16618   bfd_byte *current_stub_contents = base_stub_contents;
16619
16620   BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
16621
16622   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16623      smaller than 8 registers load sequences that do not cause the
16624      hardware issue.  */
16625   if (nb_registers <= 8)
16626     {
16627       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
16628       current_stub_contents =
16629         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16630                             initial_insn);
16631
16632       /* B initial_insn_addr+4.  */
16633       current_stub_contents =
16634         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16635                             create_instruction_branch_absolute
16636                             (initial_insn_addr - current_stub_contents));
16637
16638       /* Fill the remaining of the stub with deterministic contents.  */
16639       current_stub_contents =
16640         stm32l4xx_fill_stub_udf (htab, output_bfd,
16641                                  base_stub_contents, current_stub_contents,
16642                                  base_stub_contents +
16643                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16644
16645       return;
16646     }
16647
16648   /* - reg_list[13] == 0.  */
16649   BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
16650
16651   /* - reg_list[14] & reg_list[15] != 1.  */
16652   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
16653
16654   /* - if (wback==1) reg_list[rn] == 0.  */
16655   BFD_ASSERT (!wback || !restore_rn);
16656
16657   /* - nb_registers > 8.  */
16658   BFD_ASSERT (popcount (insn_all_registers) > 8);
16659
16660   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
16661
16662   /* In the following algorithm, we split this wide LDM using 2 LDM insn:
16663     - One with the 7 lowest registers (register mask 0x007F)
16664       This LDM will finally contain between 2 and 7 registers
16665     - One with the 7 highest registers (register mask 0xDF80)
16666       This ldm will finally contain between 2 and 7 registers.  */
16667   insn_low_registers = insn_all_registers & 0x007F;
16668   insn_high_registers = insn_all_registers & 0xDF80;
16669
16670   /* A spare register may be needed during this veneer to temporarily
16671      handle the base register.  This register will be restored with
16672      the last LDM operation.
16673      The usable register may be any general purpose register (that excludes
16674      PC, SP, LR : register mask is 0x1FFF).  */
16675   usable_register_mask = 0x1FFF;
16676
16677   /* Generate the stub function.  */
16678   if (!wback && !restore_pc && !restore_rn)
16679     {
16680       /* Choose a Ri in the low-register-list that will be restored.  */
16681       ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
16682
16683       /* MOV Ri, Rn.  */
16684       current_stub_contents =
16685         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16686                             create_instruction_mov (ri, rn));
16687
16688       /* LDMDB Ri!, {R-high-register-list}.  */
16689       current_stub_contents =
16690         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16691                             create_instruction_ldmdb
16692                             (ri, /*wback=*/1, insn_high_registers));
16693
16694       /* LDMDB Ri, {R-low-register-list}.  */
16695       current_stub_contents =
16696         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16697                             create_instruction_ldmdb
16698                             (ri, /*wback=*/0, insn_low_registers));
16699
16700       /* B initial_insn_addr+4.  */
16701       current_stub_contents =
16702         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16703                             create_instruction_branch_absolute
16704                             (initial_insn_addr - current_stub_contents));
16705     }
16706   else if (wback && !restore_pc && !restore_rn)
16707     {
16708       /* LDMDB Rn!, {R-high-register-list}.  */
16709       current_stub_contents =
16710         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16711                             create_instruction_ldmdb
16712                             (rn, /*wback=*/1, insn_high_registers));
16713
16714       /* LDMDB Rn!, {R-low-register-list}.  */
16715       current_stub_contents =
16716         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16717                             create_instruction_ldmdb
16718                             (rn, /*wback=*/1, insn_low_registers));
16719
16720       /* B initial_insn_addr+4.  */
16721       current_stub_contents =
16722         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16723                             create_instruction_branch_absolute
16724                             (initial_insn_addr - current_stub_contents));
16725     }
16726   else if (!wback && restore_pc && !restore_rn)
16727     {
16728       /* Choose a Ri in the high-register-list that will be restored.  */
16729       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16730
16731       /* SUB Ri, Rn, #(4*nb_registers).  */
16732       current_stub_contents =
16733         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16734                             create_instruction_sub (ri, rn, (4 * nb_registers)));
16735
16736       /* LDMIA Ri!, {R-low-register-list}.  */
16737       current_stub_contents =
16738         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16739                             create_instruction_ldmia
16740                             (ri, /*wback=*/1, insn_low_registers));
16741
16742       /* LDMIA Ri, {R-high-register-list}.  */
16743       current_stub_contents =
16744         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16745                             create_instruction_ldmia
16746                             (ri, /*wback=*/0, insn_high_registers));
16747     }
16748   else if (wback && restore_pc && !restore_rn)
16749     {
16750       /* Choose a Ri in the high-register-list that will be restored.  */
16751       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16752
16753       /* SUB Rn, Rn, #(4*nb_registers)  */
16754       current_stub_contents =
16755         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16756                             create_instruction_sub (rn, rn, (4 * nb_registers)));
16757
16758       /* MOV Ri, Rn.  */
16759       current_stub_contents =
16760         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16761                             create_instruction_mov (ri, rn));
16762
16763       /* LDMIA Ri!, {R-low-register-list}.  */
16764       current_stub_contents =
16765         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16766                             create_instruction_ldmia
16767                             (ri, /*wback=*/1, insn_low_registers));
16768
16769       /* LDMIA Ri, {R-high-register-list}.  */
16770       current_stub_contents =
16771         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16772                             create_instruction_ldmia
16773                             (ri, /*wback=*/0, insn_high_registers));
16774     }
16775   else if (!wback && !restore_pc && restore_rn)
16776     {
16777       ri = rn;
16778       if (!(insn_low_registers & (1 << rn)))
16779         {
16780           /* Choose a Ri in the low-register-list that will be restored.  */
16781           ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
16782
16783           /* MOV Ri, Rn.  */
16784           current_stub_contents =
16785             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16786                                 create_instruction_mov (ri, rn));
16787         }
16788
16789       /* LDMDB Ri!, {R-high-register-list}.  */
16790       current_stub_contents =
16791         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16792                             create_instruction_ldmdb
16793                             (ri, /*wback=*/1, insn_high_registers));
16794
16795       /* LDMDB Ri, {R-low-register-list}.  */
16796       current_stub_contents =
16797         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16798                             create_instruction_ldmdb
16799                             (ri, /*wback=*/0, insn_low_registers));
16800
16801       /* B initial_insn_addr+4.  */
16802       current_stub_contents =
16803         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16804                             create_instruction_branch_absolute
16805                             (initial_insn_addr - current_stub_contents));
16806     }
16807   else if (!wback && restore_pc && restore_rn)
16808     {
16809       ri = rn;
16810       if (!(insn_high_registers & (1 << rn)))
16811         {
16812           /* Choose a Ri in the high-register-list that will be restored.  */
16813           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16814         }
16815
16816       /* SUB Ri, Rn, #(4*nb_registers).  */
16817       current_stub_contents =
16818         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16819                             create_instruction_sub (ri, rn, (4 * nb_registers)));
16820
16821       /* LDMIA Ri!, {R-low-register-list}.  */
16822       current_stub_contents =
16823         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16824                             create_instruction_ldmia
16825                             (ri, /*wback=*/1, insn_low_registers));
16826
16827       /* LDMIA Ri, {R-high-register-list}.  */
16828       current_stub_contents =
16829         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16830                             create_instruction_ldmia
16831                             (ri, /*wback=*/0, insn_high_registers));
16832     }
16833   else if (wback && restore_rn)
16834     {
16835       /* The assembler should not have accepted to encode this.  */
16836       BFD_ASSERT (0 && "Cannot patch an instruction that has an "
16837         "undefined behavior.\n");
16838     }
16839
16840   /* Fill the remaining of the stub with deterministic contents.  */
16841   current_stub_contents =
16842     stm32l4xx_fill_stub_udf (htab, output_bfd,
16843                              base_stub_contents, current_stub_contents,
16844                              base_stub_contents +
16845                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16846
16847 }
16848
16849 static void
16850 stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
16851                                       bfd * output_bfd,
16852                                       const insn32 initial_insn,
16853                                       const bfd_byte *const initial_insn_addr,
16854                                       bfd_byte *const base_stub_contents)
16855 {
16856   int num_regs = ((unsigned int)initial_insn << 24) >> 24;
16857   bfd_byte *current_stub_contents = base_stub_contents;
16858
16859   BFD_ASSERT (is_thumb2_vldm (initial_insn));
16860
16861   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16862      smaller than 8 registers load sequences that do not cause the
16863      hardware issue.  */
16864   if (num_regs <= 8)
16865     {
16866       /* Untouched instruction.  */
16867       current_stub_contents =
16868         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16869                             initial_insn);
16870
16871       /* B initial_insn_addr+4.  */
16872       current_stub_contents =
16873         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16874                             create_instruction_branch_absolute
16875                             (initial_insn_addr - current_stub_contents));
16876     }
16877   else
16878     {
16879       bfd_boolean is_ia_nobang = /* (IA without !).  */
16880         (((initial_insn << 7) >> 28) & 0xd) == 0x4;
16881       bfd_boolean is_ia_bang = /* (IA with !) - includes VPOP.  */
16882         (((initial_insn << 7) >> 28) & 0xd) == 0x5;
16883       bfd_boolean is_db_bang = /* (DB with !).  */
16884         (((initial_insn << 7) >> 28) & 0xd) == 0x9;
16885       int base_reg = ((unsigned int)initial_insn << 12) >> 28;
16886       /* d = UInt (Vd:D);.  */
16887       int first_reg = ((((unsigned int)initial_insn << 16) >> 28) << 1)
16888         | (((unsigned int)initial_insn << 9) >> 31);
16889
16890       /* Compute the number of 8-register chunks needed to split.  */
16891       int chunks = (num_regs%8) ? (num_regs/8 + 1) : (num_regs/8);
16892       int chunk;
16893
16894       /* The test coverage has been done assuming the following
16895          hypothesis that exactly one of the previous is_ predicates is
16896          true.  */
16897       BFD_ASSERT ((is_ia_nobang ^ is_ia_bang ^ is_db_bang) &&
16898                   !(is_ia_nobang & is_ia_bang & is_db_bang));
16899
16900       /* We treat the cutting of the register in one pass for all
16901          cases, then we emit the adjustments:
16902
16903          vldm rx, {...}
16904          -> vldm rx!, {8_words_or_less} for each needed 8_word
16905          -> sub rx, rx, #size (list)
16906
16907          vldm rx!, {...}
16908          -> vldm rx!, {8_words_or_less} for each needed 8_word
16909          This also handles vpop instruction (when rx is sp)
16910
16911          vldmd rx!, {...}
16912          -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
16913       for (chunk = 0; chunk<chunks; ++chunk)
16914         {
16915           if (is_ia_nobang || is_ia_bang)
16916             {
16917               current_stub_contents =
16918                 push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16919                                     create_instruction_vldmia
16920                                     (base_reg,
16921                                      /*wback= .  */1,
16922                                      chunks - (chunk + 1) ?
16923                                      8 : num_regs - chunk * 8,
16924                                      first_reg + chunk * 8));
16925             }
16926           else if (is_db_bang)
16927             {
16928               current_stub_contents =
16929                 push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16930                                     create_instruction_vldmdb
16931                                     (base_reg,
16932                                      chunks - (chunk + 1) ?
16933                                      8 : num_regs - chunk * 8,
16934                                      first_reg + chunk * 8));
16935             }
16936         }
16937
16938       /* Only this case requires the base register compensation
16939          subtract.  */
16940       if (is_ia_nobang)
16941         {
16942           current_stub_contents =
16943             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16944                                 create_instruction_sub
16945                                 (base_reg, base_reg, 4*num_regs));
16946         }
16947
16948       /* B initial_insn_addr+4.  */
16949       current_stub_contents =
16950         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16951                             create_instruction_branch_absolute
16952                             (initial_insn_addr - current_stub_contents));
16953     }
16954
16955   /* Fill the remaining of the stub with deterministic contents.  */
16956   current_stub_contents =
16957     stm32l4xx_fill_stub_udf (htab, output_bfd,
16958                              base_stub_contents, current_stub_contents,
16959                              base_stub_contents +
16960                              STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
16961 }
16962
16963 static void
16964 stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
16965                                  bfd * output_bfd,
16966                                  const insn32 wrong_insn,
16967                                  const bfd_byte *const wrong_insn_addr,
16968                                  bfd_byte *const stub_contents)
16969 {
16970   if (is_thumb2_ldmia (wrong_insn))
16971     stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
16972                                            wrong_insn, wrong_insn_addr,
16973                                            stub_contents);
16974   else if (is_thumb2_ldmdb (wrong_insn))
16975     stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
16976                                            wrong_insn, wrong_insn_addr,
16977                                            stub_contents);
16978   else if (is_thumb2_vldm (wrong_insn))
16979     stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
16980                                           wrong_insn, wrong_insn_addr,
16981                                           stub_contents);
16982 }
16983
16984 /* End of stm32l4xx work-around.  */
16985
16986
16987 /* Do code byteswapping.  Return FALSE afterwards so that the section is
16988    written out as normal.  */
16989
16990 static bfd_boolean
16991 elf32_arm_write_section (bfd *output_bfd,
16992                          struct bfd_link_info *link_info,
16993                          asection *sec,
16994                          bfd_byte *contents)
16995 {
16996   unsigned int mapcount, errcount;
16997   _arm_elf_section_data *arm_data;
16998   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
16999   elf32_arm_section_map *map;
17000   elf32_vfp11_erratum_list *errnode;
17001   elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
17002   bfd_vma ptr;
17003   bfd_vma end;
17004   bfd_vma offset = sec->output_section->vma + sec->output_offset;
17005   bfd_byte tmp;
17006   unsigned int i;
17007
17008   if (globals == NULL)
17009     return FALSE;
17010
17011   /* If this section has not been allocated an _arm_elf_section_data
17012      structure then we cannot record anything.  */
17013   arm_data = get_arm_elf_section_data (sec);
17014   if (arm_data == NULL)
17015     return FALSE;
17016
17017   mapcount = arm_data->mapcount;
17018   map = arm_data->map;
17019   errcount = arm_data->erratumcount;
17020
17021   if (errcount != 0)
17022     {
17023       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
17024
17025       for (errnode = arm_data->erratumlist; errnode != 0;
17026            errnode = errnode->next)
17027         {
17028           bfd_vma target = errnode->vma - offset;
17029
17030           switch (errnode->type)
17031             {
17032             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
17033               {
17034                 bfd_vma branch_to_veneer;
17035                 /* Original condition code of instruction, plus bit mask for
17036                    ARM B instruction.  */
17037                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
17038                                   | 0x0a000000;
17039
17040                 /* The instruction is before the label.  */
17041                 target -= 4;
17042
17043                 /* Above offset included in -4 below.  */
17044                 branch_to_veneer = errnode->u.b.veneer->vma
17045                                    - errnode->vma - 4;
17046
17047                 if ((signed) branch_to_veneer < -(1 << 25)
17048                     || (signed) branch_to_veneer >= (1 << 25))
17049                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
17050                                            "range"), output_bfd);
17051
17052                 insn |= (branch_to_veneer >> 2) & 0xffffff;
17053                 contents[endianflip ^ target] = insn & 0xff;
17054                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
17055                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
17056                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
17057               }
17058               break;
17059
17060             case VFP11_ERRATUM_ARM_VENEER:
17061               {
17062                 bfd_vma branch_from_veneer;
17063                 unsigned int insn;
17064
17065                 /* Take size of veneer into account.  */
17066                 branch_from_veneer = errnode->u.v.branch->vma
17067                                      - errnode->vma - 12;
17068
17069                 if ((signed) branch_from_veneer < -(1 << 25)
17070                     || (signed) branch_from_veneer >= (1 << 25))
17071                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
17072                                            "range"), output_bfd);
17073
17074                 /* Original instruction.  */
17075                 insn = errnode->u.v.branch->u.b.vfp_insn;
17076                 contents[endianflip ^ target] = insn & 0xff;
17077                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
17078                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
17079                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
17080
17081                 /* Branch back to insn after original insn.  */
17082                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
17083                 contents[endianflip ^ (target + 4)] = insn & 0xff;
17084                 contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
17085                 contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
17086                 contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
17087               }
17088               break;
17089
17090             default:
17091               abort ();
17092             }
17093         }
17094     }
17095
17096   if (arm_data->stm32l4xx_erratumcount != 0)
17097     {
17098       for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
17099            stm32l4xx_errnode != 0;
17100            stm32l4xx_errnode = stm32l4xx_errnode->next)
17101         {
17102           bfd_vma target = stm32l4xx_errnode->vma - offset;
17103
17104           switch (stm32l4xx_errnode->type)
17105             {
17106             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
17107               {
17108                 unsigned int insn;
17109                 bfd_vma branch_to_veneer =
17110                   stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
17111
17112                 if ((signed) branch_to_veneer < -(1 << 24)
17113                     || (signed) branch_to_veneer >= (1 << 24))
17114                   {
17115                     bfd_vma out_of_range =
17116                       ((signed) branch_to_veneer < -(1 << 24)) ?
17117                       - branch_to_veneer - (1 << 24) :
17118                       ((signed) branch_to_veneer >= (1 << 24)) ?
17119                       branch_to_veneer - (1 << 24) : 0;
17120
17121                     (*_bfd_error_handler)
17122                       (_("%B(%#x): error: Cannot create STM32L4XX veneer. "
17123                          "Jump out of range by %ld bytes. "
17124                          "Cannot encode branch instruction. "),
17125                        output_bfd,
17126                        (long) (stm32l4xx_errnode->vma - 4),
17127                        out_of_range);
17128                     continue;
17129                   }
17130
17131                 insn = create_instruction_branch_absolute
17132                   (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
17133
17134                 /* The instruction is before the label.  */
17135                 target -= 4;
17136
17137                 put_thumb2_insn (globals, output_bfd,
17138                                  (bfd_vma) insn, contents + target);
17139               }
17140               break;
17141
17142             case STM32L4XX_ERRATUM_VENEER:
17143               {
17144                 bfd_byte * veneer;
17145                 bfd_byte * veneer_r;
17146                 unsigned int insn;
17147
17148                 veneer = contents + target;
17149                 veneer_r = veneer
17150                   + stm32l4xx_errnode->u.b.veneer->vma
17151                   - stm32l4xx_errnode->vma - 4;
17152
17153                 if ((signed) (veneer_r - veneer -
17154                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
17155                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
17156                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
17157                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
17158                     || (signed) (veneer_r - veneer) >= (1 << 24))
17159                   {
17160                     (*_bfd_error_handler) (_("%B: error: Cannot create STM32L4XX "
17161                                              "veneer."), output_bfd);
17162                      continue;
17163                   }
17164
17165                 /* Original instruction.  */
17166                 insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
17167
17168                 stm32l4xx_create_replacing_stub
17169                   (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
17170               }
17171               break;
17172
17173             default:
17174               abort ();
17175             }
17176         }
17177     }
17178
17179   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
17180     {
17181       arm_unwind_table_edit *edit_node
17182         = arm_data->u.exidx.unwind_edit_list;
17183       /* Now, sec->size is the size of the section we will write.  The original
17184          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
17185          markers) was sec->rawsize.  (This isn't the case if we perform no
17186          edits, then rawsize will be zero and we should use size).  */
17187       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
17188       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
17189       unsigned int in_index, out_index;
17190       bfd_vma add_to_offsets = 0;
17191
17192       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
17193         {
17194           if (edit_node)
17195             {
17196               unsigned int edit_index = edit_node->index;
17197
17198               if (in_index < edit_index && in_index * 8 < input_size)
17199                 {
17200                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
17201                                     contents + in_index * 8, add_to_offsets);
17202                   out_index++;
17203                   in_index++;
17204                 }
17205               else if (in_index == edit_index
17206                        || (in_index * 8 >= input_size
17207                            && edit_index == UINT_MAX))
17208                 {
17209                   switch (edit_node->type)
17210                     {
17211                     case DELETE_EXIDX_ENTRY:
17212                       in_index++;
17213                       add_to_offsets += 8;
17214                       break;
17215
17216                     case INSERT_EXIDX_CANTUNWIND_AT_END:
17217                       {
17218                         asection *text_sec = edit_node->linked_section;
17219                         bfd_vma text_offset = text_sec->output_section->vma
17220                                               + text_sec->output_offset
17221                                               + text_sec->size;
17222                         bfd_vma exidx_offset = offset + out_index * 8;
17223                         unsigned long prel31_offset;
17224
17225                         /* Note: this is meant to be equivalent to an
17226                            R_ARM_PREL31 relocation.  These synthetic
17227                            EXIDX_CANTUNWIND markers are not relocated by the
17228                            usual BFD method.  */
17229                         prel31_offset = (text_offset - exidx_offset)
17230                                         & 0x7ffffffful;
17231
17232                         /* First address we can't unwind.  */
17233                         bfd_put_32 (output_bfd, prel31_offset,
17234                                     &edited_contents[out_index * 8]);
17235
17236                         /* Code for EXIDX_CANTUNWIND.  */
17237                         bfd_put_32 (output_bfd, 0x1,
17238                                     &edited_contents[out_index * 8 + 4]);
17239
17240                         out_index++;
17241                         add_to_offsets -= 8;
17242                       }
17243                       break;
17244                     }
17245
17246                   edit_node = edit_node->next;
17247                 }
17248             }
17249           else
17250             {
17251               /* No more edits, copy remaining entries verbatim.  */
17252               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
17253                                 contents + in_index * 8, add_to_offsets);
17254               out_index++;
17255               in_index++;
17256             }
17257         }
17258
17259       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
17260         bfd_set_section_contents (output_bfd, sec->output_section,
17261                                   edited_contents,
17262                                   (file_ptr) sec->output_offset, sec->size);
17263
17264       return TRUE;
17265     }
17266
17267   /* Fix code to point to Cortex-A8 erratum stubs.  */
17268   if (globals->fix_cortex_a8)
17269     {
17270       struct a8_branch_to_stub_data data;
17271
17272       data.writing_section = sec;
17273       data.contents = contents;
17274
17275       bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
17276                          & data);
17277     }
17278
17279   if (mapcount == 0)
17280     return FALSE;
17281
17282   if (globals->byteswap_code)
17283     {
17284       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
17285
17286       ptr = map[0].vma;
17287       for (i = 0; i < mapcount; i++)
17288         {
17289           if (i == mapcount - 1)
17290             end = sec->size;
17291           else
17292             end = map[i + 1].vma;
17293
17294           switch (map[i].type)
17295             {
17296             case 'a':
17297               /* Byte swap code words.  */
17298               while (ptr + 3 < end)
17299                 {
17300                   tmp = contents[ptr];
17301                   contents[ptr] = contents[ptr + 3];
17302                   contents[ptr + 3] = tmp;
17303                   tmp = contents[ptr + 1];
17304                   contents[ptr + 1] = contents[ptr + 2];
17305                   contents[ptr + 2] = tmp;
17306                   ptr += 4;
17307                 }
17308               break;
17309
17310             case 't':
17311               /* Byte swap code halfwords.  */
17312               while (ptr + 1 < end)
17313                 {
17314                   tmp = contents[ptr];
17315                   contents[ptr] = contents[ptr + 1];
17316                   contents[ptr + 1] = tmp;
17317                   ptr += 2;
17318                 }
17319               break;
17320
17321             case 'd':
17322               /* Leave data alone.  */
17323               break;
17324             }
17325           ptr = end;
17326         }
17327     }
17328
17329   free (map);
17330   arm_data->mapcount = -1;
17331   arm_data->mapsize = 0;
17332   arm_data->map = NULL;
17333
17334   return FALSE;
17335 }
17336
17337 /* Mangle thumb function symbols as we read them in.  */
17338
17339 static bfd_boolean
17340 elf32_arm_swap_symbol_in (bfd * abfd,
17341                           const void *psrc,
17342                           const void *pshn,
17343                           Elf_Internal_Sym *dst)
17344 {
17345   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
17346     return FALSE;
17347
17348   /* New EABI objects mark thumb function symbols by setting the low bit of
17349      the address.  */
17350   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
17351       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
17352     {
17353       if (dst->st_value & 1)
17354         {
17355           dst->st_value &= ~(bfd_vma) 1;
17356           dst->st_target_internal = ST_BRANCH_TO_THUMB;
17357         }
17358       else
17359         dst->st_target_internal = ST_BRANCH_TO_ARM;
17360     }
17361   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
17362     {
17363       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
17364       dst->st_target_internal = ST_BRANCH_TO_THUMB;
17365     }
17366   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
17367     dst->st_target_internal = ST_BRANCH_LONG;
17368   else
17369     dst->st_target_internal = ST_BRANCH_UNKNOWN;
17370
17371   return TRUE;
17372 }
17373
17374
17375 /* Mangle thumb function symbols as we write them out.  */
17376
17377 static void
17378 elf32_arm_swap_symbol_out (bfd *abfd,
17379                            const Elf_Internal_Sym *src,
17380                            void *cdst,
17381                            void *shndx)
17382 {
17383   Elf_Internal_Sym newsym;
17384
17385   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
17386      of the address set, as per the new EABI.  We do this unconditionally
17387      because objcopy does not set the elf header flags until after
17388      it writes out the symbol table.  */
17389   if (src->st_target_internal == ST_BRANCH_TO_THUMB)
17390     {
17391       newsym = *src;
17392       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
17393         newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
17394       if (newsym.st_shndx != SHN_UNDEF)
17395         {
17396           /* Do this only for defined symbols. At link type, the static
17397              linker will simulate the work of dynamic linker of resolving
17398              symbols and will carry over the thumbness of found symbols to
17399              the output symbol table. It's not clear how it happens, but
17400              the thumbness of undefined symbols can well be different at
17401              runtime, and writing '1' for them will be confusing for users
17402              and possibly for dynamic linker itself.
17403           */
17404           newsym.st_value |= 1;
17405         }
17406
17407       src = &newsym;
17408     }
17409   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
17410 }
17411
17412 /* Add the PT_ARM_EXIDX program header.  */
17413
17414 static bfd_boolean
17415 elf32_arm_modify_segment_map (bfd *abfd,
17416                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
17417 {
17418   struct elf_segment_map *m;
17419   asection *sec;
17420
17421   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
17422   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
17423     {
17424       /* If there is already a PT_ARM_EXIDX header, then we do not
17425          want to add another one.  This situation arises when running
17426          "strip"; the input binary already has the header.  */
17427       m = elf_seg_map (abfd);
17428       while (m && m->p_type != PT_ARM_EXIDX)
17429         m = m->next;
17430       if (!m)
17431         {
17432           m = (struct elf_segment_map *)
17433               bfd_zalloc (abfd, sizeof (struct elf_segment_map));
17434           if (m == NULL)
17435             return FALSE;
17436           m->p_type = PT_ARM_EXIDX;
17437           m->count = 1;
17438           m->sections[0] = sec;
17439
17440           m->next = elf_seg_map (abfd);
17441           elf_seg_map (abfd) = m;
17442         }
17443     }
17444
17445   return TRUE;
17446 }
17447
17448 /* We may add a PT_ARM_EXIDX program header.  */
17449
17450 static int
17451 elf32_arm_additional_program_headers (bfd *abfd,
17452                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
17453 {
17454   asection *sec;
17455
17456   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
17457   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
17458     return 1;
17459   else
17460     return 0;
17461 }
17462
17463 /* Hook called by the linker routine which adds symbols from an object
17464    file.  */
17465
17466 static bfd_boolean
17467 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
17468                            Elf_Internal_Sym *sym, const char **namep,
17469                            flagword *flagsp, asection **secp, bfd_vma *valp)
17470 {
17471   if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
17472        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
17473       && (abfd->flags & DYNAMIC) == 0
17474       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
17475     elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
17476
17477   if (elf32_arm_hash_table (info) == NULL)
17478     return FALSE;
17479
17480   if (elf32_arm_hash_table (info)->vxworks_p
17481       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
17482                                        flagsp, secp, valp))
17483     return FALSE;
17484
17485   return TRUE;
17486 }
17487
17488 /* We use this to override swap_symbol_in and swap_symbol_out.  */
17489 const struct elf_size_info elf32_arm_size_info =
17490 {
17491   sizeof (Elf32_External_Ehdr),
17492   sizeof (Elf32_External_Phdr),
17493   sizeof (Elf32_External_Shdr),
17494   sizeof (Elf32_External_Rel),
17495   sizeof (Elf32_External_Rela),
17496   sizeof (Elf32_External_Sym),
17497   sizeof (Elf32_External_Dyn),
17498   sizeof (Elf_External_Note),
17499   4,
17500   1,
17501   32, 2,
17502   ELFCLASS32, EV_CURRENT,
17503   bfd_elf32_write_out_phdrs,
17504   bfd_elf32_write_shdrs_and_ehdr,
17505   bfd_elf32_checksum_contents,
17506   bfd_elf32_write_relocs,
17507   elf32_arm_swap_symbol_in,
17508   elf32_arm_swap_symbol_out,
17509   bfd_elf32_slurp_reloc_table,
17510   bfd_elf32_slurp_symbol_table,
17511   bfd_elf32_swap_dyn_in,
17512   bfd_elf32_swap_dyn_out,
17513   bfd_elf32_swap_reloc_in,
17514   bfd_elf32_swap_reloc_out,
17515   bfd_elf32_swap_reloca_in,
17516   bfd_elf32_swap_reloca_out
17517 };
17518
17519 static bfd_vma
17520 read_code32 (const bfd *abfd, const bfd_byte *addr)
17521 {
17522   /* V7 BE8 code is always little endian.  */
17523   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
17524     return bfd_getl32 (addr);
17525
17526   return bfd_get_32 (abfd, addr);
17527 }
17528
17529 static bfd_vma
17530 read_code16 (const bfd *abfd, const bfd_byte *addr)
17531 {
17532   /* V7 BE8 code is always little endian.  */
17533   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
17534     return bfd_getl16 (addr);
17535
17536   return bfd_get_16 (abfd, addr);
17537 }
17538
17539 /* Return size of plt0 entry starting at ADDR
17540    or (bfd_vma) -1 if size can not be determined.  */
17541
17542 static bfd_vma
17543 elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr)
17544 {
17545   bfd_vma first_word;
17546   bfd_vma plt0_size;
17547
17548   first_word = read_code32 (abfd, addr);
17549
17550   if (first_word == elf32_arm_plt0_entry[0])
17551     plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
17552   else if (first_word == elf32_thumb2_plt0_entry[0])
17553     plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
17554   else
17555     /* We don't yet handle this PLT format.  */
17556     return (bfd_vma) -1;
17557
17558   return plt0_size;
17559 }
17560
17561 /* Return size of plt entry starting at offset OFFSET
17562    of plt section located at address START
17563    or (bfd_vma) -1 if size can not be determined.  */
17564
17565 static bfd_vma
17566 elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset)
17567 {
17568   bfd_vma first_insn;
17569   bfd_vma plt_size = 0;
17570   const bfd_byte *addr = start + offset;
17571
17572   /* PLT entry size if fixed on Thumb-only platforms.  */
17573   if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
17574       return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
17575
17576   /* Respect Thumb stub if necessary.  */
17577   if (read_code16 (abfd, addr) == elf32_arm_plt_thumb_stub[0])
17578     {
17579       plt_size += 2 * ARRAY_SIZE(elf32_arm_plt_thumb_stub);
17580     }
17581
17582   /* Strip immediate from first add.  */
17583   first_insn = read_code32 (abfd, addr + plt_size) & 0xffffff00;
17584
17585 #ifdef FOUR_WORD_PLT
17586   if (first_insn == elf32_arm_plt_entry[0])
17587     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
17588 #else
17589   if (first_insn == elf32_arm_plt_entry_long[0])
17590     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
17591   else if (first_insn == elf32_arm_plt_entry_short[0])
17592     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
17593 #endif
17594   else
17595     /* We don't yet handle this PLT format.  */
17596     return (bfd_vma) -1;
17597
17598   return plt_size;
17599 }
17600
17601 /* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
17602
17603 static long
17604 elf32_arm_get_synthetic_symtab (bfd *abfd,
17605                                long symcount ATTRIBUTE_UNUSED,
17606                                asymbol **syms ATTRIBUTE_UNUSED,
17607                                long dynsymcount,
17608                                asymbol **dynsyms,
17609                                asymbol **ret)
17610 {
17611   asection *relplt;
17612   asymbol *s;
17613   arelent *p;
17614   long count, i, n;
17615   size_t size;
17616   Elf_Internal_Shdr *hdr;
17617   char *names;
17618   asection *plt;
17619   bfd_vma offset;
17620   bfd_byte *data;
17621
17622   *ret = NULL;
17623
17624   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
17625     return 0;
17626
17627   if (dynsymcount <= 0)
17628     return 0;
17629
17630   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
17631   if (relplt == NULL)
17632     return 0;
17633
17634   hdr = &elf_section_data (relplt)->this_hdr;
17635   if (hdr->sh_link != elf_dynsymtab (abfd)
17636       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
17637     return 0;
17638
17639   plt = bfd_get_section_by_name (abfd, ".plt");
17640   if (plt == NULL)
17641     return 0;
17642
17643   if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, TRUE))
17644     return -1;
17645
17646   data = plt->contents;
17647   if (data == NULL)
17648     {
17649       if (!bfd_get_full_section_contents(abfd, (asection *) plt, &data) || data == NULL)
17650         return -1;
17651       bfd_cache_section_contents((asection *) plt, data);
17652     }
17653
17654   count = relplt->size / hdr->sh_entsize;
17655   size = count * sizeof (asymbol);
17656   p = relplt->relocation;
17657   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
17658     {
17659       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
17660       if (p->addend != 0)
17661         size += sizeof ("+0x") - 1 + 8;
17662     }
17663
17664   s = *ret = (asymbol *) bfd_malloc (size);
17665   if (s == NULL)
17666     return -1;
17667
17668   offset = elf32_arm_plt0_size (abfd, data);
17669   if (offset == (bfd_vma) -1)
17670     return -1;
17671
17672   names = (char *) (s + count);
17673   p = relplt->relocation;
17674   n = 0;
17675   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
17676     {
17677       size_t len;
17678
17679       bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset);
17680       if (plt_size == (bfd_vma) -1)
17681         break;
17682
17683       *s = **p->sym_ptr_ptr;
17684       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
17685          we are defining a symbol, ensure one of them is set.  */
17686       if ((s->flags & BSF_LOCAL) == 0)
17687         s->flags |= BSF_GLOBAL;
17688       s->flags |= BSF_SYNTHETIC;
17689       s->section = plt;
17690       s->value = offset;
17691       s->name = names;
17692       s->udata.p = NULL;
17693       len = strlen ((*p->sym_ptr_ptr)->name);
17694       memcpy (names, (*p->sym_ptr_ptr)->name, len);
17695       names += len;
17696       if (p->addend != 0)
17697         {
17698           char buf[30], *a;
17699
17700           memcpy (names, "+0x", sizeof ("+0x") - 1);
17701           names += sizeof ("+0x") - 1;
17702           bfd_sprintf_vma (abfd, buf, p->addend);
17703           for (a = buf; *a == '0'; ++a)
17704             ;
17705           len = strlen (a);
17706           memcpy (names, a, len);
17707           names += len;
17708         }
17709       memcpy (names, "@plt", sizeof ("@plt"));
17710       names += sizeof ("@plt");
17711       ++s, ++n;
17712       offset += plt_size;
17713     }
17714
17715   return n;
17716 }
17717
17718 static const struct bfd_elf_special_section
17719 elf32_arm_special_sections[] =
17720 {
17721 /* Catch sections with .text.noread prefix and apply allocate, execute and
17722    noread section attributes.  */
17723   { STRING_COMMA_LEN (".text.noread"),  -2, SHT_PROGBITS,
17724     SHF_ALLOC + SHF_EXECINSTR + SHF_ARM_NOREAD },
17725   { NULL,                             0, 0, 0,                  0 }
17726 };
17727
17728 static bfd_boolean
17729 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr * hdr)
17730 {
17731   if (hdr->sh_flags & SHF_ARM_NOREAD)
17732     *flags |= SEC_ELF_NOREAD;
17733   return TRUE;
17734 }
17735
17736 static flagword
17737 elf32_arm_lookup_section_flags (char *flag_name)
17738 {
17739   if (!strcmp (flag_name, "SHF_ARM_NOREAD"))
17740     return SHF_ARM_NOREAD;
17741
17742   return SEC_NO_FLAGS;
17743 }
17744
17745 #define ELF_ARCH                        bfd_arch_arm
17746 #define ELF_TARGET_ID                   ARM_ELF_DATA
17747 #define ELF_MACHINE_CODE                EM_ARM
17748 #ifdef __QNXTARGET__
17749 #define ELF_MAXPAGESIZE                 0x1000
17750 #else
17751 #define ELF_MAXPAGESIZE                 0x10000
17752 #endif
17753 #define ELF_MINPAGESIZE                 0x1000
17754 #define ELF_COMMONPAGESIZE              0x1000
17755
17756 #define bfd_elf32_mkobject                      elf32_arm_mkobject
17757
17758 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
17759 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
17760 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
17761 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
17762 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
17763 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
17764 #define bfd_elf32_bfd_reloc_name_lookup         elf32_arm_reloc_name_lookup
17765 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
17766 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
17767 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
17768 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
17769 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
17770 #define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
17771
17772 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
17773 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
17774 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
17775 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
17776 #define elf_backend_check_relocs                elf32_arm_check_relocs
17777 #define elf_backend_relocate_section            elf32_arm_relocate_section
17778 #define elf_backend_write_section               elf32_arm_write_section
17779 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
17780 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
17781 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
17782 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
17783 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
17784 #define elf_backend_always_size_sections        elf32_arm_always_size_sections
17785 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
17786 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
17787 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
17788 #define elf_backend_object_p                    elf32_arm_object_p
17789 #define elf_backend_fake_sections               elf32_arm_fake_sections
17790 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
17791 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
17792 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
17793 #define elf_backend_size_info                   elf32_arm_size_info
17794 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
17795 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
17796 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
17797 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
17798 #define elf_backend_add_symbol_hook             elf32_arm_add_symbol_hook
17799
17800 #define elf_backend_can_refcount       1
17801 #define elf_backend_can_gc_sections    1
17802 #define elf_backend_plt_readonly       1
17803 #define elf_backend_want_got_plt       1
17804 #define elf_backend_want_plt_sym       0
17805 #define elf_backend_may_use_rel_p      1
17806 #define elf_backend_may_use_rela_p     0
17807 #define elf_backend_default_use_rela_p 0
17808
17809 #define elf_backend_got_header_size     12
17810 #define elf_backend_extern_protected_data 1
17811
17812 #undef  elf_backend_obj_attrs_vendor
17813 #define elf_backend_obj_attrs_vendor            "aeabi"
17814 #undef  elf_backend_obj_attrs_section
17815 #define elf_backend_obj_attrs_section           ".ARM.attributes"
17816 #undef  elf_backend_obj_attrs_arg_type
17817 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
17818 #undef  elf_backend_obj_attrs_section_type
17819 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
17820 #define elf_backend_obj_attrs_order             elf32_arm_obj_attrs_order
17821 #define elf_backend_obj_attrs_handle_unknown    elf32_arm_obj_attrs_handle_unknown
17822
17823 #undef  elf_backend_special_sections
17824 #define elf_backend_special_sections            elf32_arm_special_sections
17825 #undef elf_backend_section_flags
17826 #define elf_backend_section_flags               elf32_arm_section_flags
17827 #undef elf_backend_lookup_section_flags_hook
17828 #define elf_backend_lookup_section_flags_hook   elf32_arm_lookup_section_flags
17829
17830 #include "elf32-target.h"
17831
17832 /* Native Client targets.  */
17833
17834 #undef  TARGET_LITTLE_SYM
17835 #define TARGET_LITTLE_SYM               arm_elf32_nacl_le_vec
17836 #undef  TARGET_LITTLE_NAME
17837 #define TARGET_LITTLE_NAME              "elf32-littlearm-nacl"
17838 #undef  TARGET_BIG_SYM
17839 #define TARGET_BIG_SYM                  arm_elf32_nacl_be_vec
17840 #undef  TARGET_BIG_NAME
17841 #define TARGET_BIG_NAME                 "elf32-bigarm-nacl"
17842
17843 /* Like elf32_arm_link_hash_table_create -- but overrides
17844    appropriately for NaCl.  */
17845
17846 static struct bfd_link_hash_table *
17847 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
17848 {
17849   struct bfd_link_hash_table *ret;
17850
17851   ret = elf32_arm_link_hash_table_create (abfd);
17852   if (ret)
17853     {
17854       struct elf32_arm_link_hash_table *htab
17855         = (struct elf32_arm_link_hash_table *) ret;
17856
17857       htab->nacl_p = 1;
17858
17859       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
17860       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
17861     }
17862   return ret;
17863 }
17864
17865 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
17866    really need to use elf32_arm_modify_segment_map.  But we do it
17867    anyway just to reduce gratuitous differences with the stock ARM backend.  */
17868
17869 static bfd_boolean
17870 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
17871 {
17872   return (elf32_arm_modify_segment_map (abfd, info)
17873           && nacl_modify_segment_map (abfd, info));
17874 }
17875
17876 static void
17877 elf32_arm_nacl_final_write_processing (bfd *abfd, bfd_boolean linker)
17878 {
17879   elf32_arm_final_write_processing (abfd, linker);
17880   nacl_final_write_processing (abfd, linker);
17881 }
17882
17883 static bfd_vma
17884 elf32_arm_nacl_plt_sym_val (bfd_vma i, const asection *plt,
17885                             const arelent *rel ATTRIBUTE_UNUSED)
17886 {
17887   return plt->vma
17888     + 4 * (ARRAY_SIZE (elf32_arm_nacl_plt0_entry) +
17889            i * ARRAY_SIZE (elf32_arm_nacl_plt_entry));
17890 }
17891
17892 #undef  elf32_bed
17893 #define elf32_bed                               elf32_arm_nacl_bed
17894 #undef  bfd_elf32_bfd_link_hash_table_create
17895 #define bfd_elf32_bfd_link_hash_table_create    \
17896   elf32_arm_nacl_link_hash_table_create
17897 #undef  elf_backend_plt_alignment
17898 #define elf_backend_plt_alignment               4
17899 #undef  elf_backend_modify_segment_map
17900 #define elf_backend_modify_segment_map          elf32_arm_nacl_modify_segment_map
17901 #undef  elf_backend_modify_program_headers
17902 #define elf_backend_modify_program_headers      nacl_modify_program_headers
17903 #undef  elf_backend_final_write_processing
17904 #define elf_backend_final_write_processing      elf32_arm_nacl_final_write_processing
17905 #undef bfd_elf32_get_synthetic_symtab
17906 #undef  elf_backend_plt_sym_val
17907 #define elf_backend_plt_sym_val                 elf32_arm_nacl_plt_sym_val
17908
17909 #undef  ELF_MINPAGESIZE
17910 #undef  ELF_COMMONPAGESIZE
17911
17912
17913 #include "elf32-target.h"
17914
17915 /* Reset to defaults.  */
17916 #undef  elf_backend_plt_alignment
17917 #undef  elf_backend_modify_segment_map
17918 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
17919 #undef  elf_backend_modify_program_headers
17920 #undef  elf_backend_final_write_processing
17921 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
17922 #undef  ELF_MINPAGESIZE
17923 #define ELF_MINPAGESIZE                 0x1000
17924 #undef  ELF_COMMONPAGESIZE
17925 #define ELF_COMMONPAGESIZE              0x1000
17926
17927
17928 /* VxWorks Targets.  */
17929
17930 #undef  TARGET_LITTLE_SYM
17931 #define TARGET_LITTLE_SYM               arm_elf32_vxworks_le_vec
17932 #undef  TARGET_LITTLE_NAME
17933 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
17934 #undef  TARGET_BIG_SYM
17935 #define TARGET_BIG_SYM                  arm_elf32_vxworks_be_vec
17936 #undef  TARGET_BIG_NAME
17937 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
17938
17939 /* Like elf32_arm_link_hash_table_create -- but overrides
17940    appropriately for VxWorks.  */
17941
17942 static struct bfd_link_hash_table *
17943 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
17944 {
17945   struct bfd_link_hash_table *ret;
17946
17947   ret = elf32_arm_link_hash_table_create (abfd);
17948   if (ret)
17949     {
17950       struct elf32_arm_link_hash_table *htab
17951         = (struct elf32_arm_link_hash_table *) ret;
17952       htab->use_rel = 0;
17953       htab->vxworks_p = 1;
17954     }
17955   return ret;
17956 }
17957
17958 static void
17959 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
17960 {
17961   elf32_arm_final_write_processing (abfd, linker);
17962   elf_vxworks_final_write_processing (abfd, linker);
17963 }
17964
17965 #undef  elf32_bed
17966 #define elf32_bed elf32_arm_vxworks_bed
17967
17968 #undef  bfd_elf32_bfd_link_hash_table_create
17969 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
17970 #undef  elf_backend_final_write_processing
17971 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
17972 #undef  elf_backend_emit_relocs
17973 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
17974
17975 #undef  elf_backend_may_use_rel_p
17976 #define elf_backend_may_use_rel_p       0
17977 #undef  elf_backend_may_use_rela_p
17978 #define elf_backend_may_use_rela_p      1
17979 #undef  elf_backend_default_use_rela_p
17980 #define elf_backend_default_use_rela_p  1
17981 #undef  elf_backend_want_plt_sym
17982 #define elf_backend_want_plt_sym        1
17983 #undef  ELF_MAXPAGESIZE
17984 #define ELF_MAXPAGESIZE                 0x1000
17985
17986 #include "elf32-target.h"
17987
17988
17989 /* Merge backend specific data from an object file to the output
17990    object file when linking.  */
17991
17992 static bfd_boolean
17993 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
17994 {
17995   flagword out_flags;
17996   flagword in_flags;
17997   bfd_boolean flags_compatible = TRUE;
17998   asection *sec;
17999
18000   /* Check if we have the same endianness.  */
18001   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
18002     return FALSE;
18003
18004   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
18005     return TRUE;
18006
18007   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
18008     return FALSE;
18009
18010   /* The input BFD must have had its flags initialised.  */
18011   /* The following seems bogus to me -- The flags are initialized in
18012      the assembler but I don't think an elf_flags_init field is
18013      written into the object.  */
18014   /* BFD_ASSERT (elf_flags_init (ibfd)); */
18015
18016   in_flags  = elf_elfheader (ibfd)->e_flags;
18017   out_flags = elf_elfheader (obfd)->e_flags;
18018
18019   /* In theory there is no reason why we couldn't handle this.  However
18020      in practice it isn't even close to working and there is no real
18021      reason to want it.  */
18022   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
18023       && !(ibfd->flags & DYNAMIC)
18024       && (in_flags & EF_ARM_BE8))
18025     {
18026       _bfd_error_handler (_("error: %B is already in final BE8 format"),
18027                           ibfd);
18028       return FALSE;
18029     }
18030
18031   if (!elf_flags_init (obfd))
18032     {
18033       /* If the input is the default architecture and had the default
18034          flags then do not bother setting the flags for the output
18035          architecture, instead allow future merges to do this.  If no
18036          future merges ever set these flags then they will retain their
18037          uninitialised values, which surprise surprise, correspond
18038          to the default values.  */
18039       if (bfd_get_arch_info (ibfd)->the_default
18040           && elf_elfheader (ibfd)->e_flags == 0)
18041         return TRUE;
18042
18043       elf_flags_init (obfd) = TRUE;
18044       elf_elfheader (obfd)->e_flags = in_flags;
18045
18046       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
18047           && bfd_get_arch_info (obfd)->the_default)
18048         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
18049
18050       return TRUE;
18051     }
18052
18053   /* Determine what should happen if the input ARM architecture
18054      does not match the output ARM architecture.  */
18055   if (! bfd_arm_merge_machines (ibfd, obfd))
18056     return FALSE;
18057
18058   /* Identical flags must be compatible.  */
18059   if (in_flags == out_flags)
18060     return TRUE;
18061
18062   /* Check to see if the input BFD actually contains any sections.  If
18063      not, its flags may not have been initialised either, but it
18064      cannot actually cause any incompatiblity.  Do not short-circuit
18065      dynamic objects; their section list may be emptied by
18066     elf_link_add_object_symbols.
18067
18068     Also check to see if there are no code sections in the input.
18069     In this case there is no need to check for code specific flags.
18070     XXX - do we need to worry about floating-point format compatability
18071     in data sections ?  */
18072   if (!(ibfd->flags & DYNAMIC))
18073     {
18074       bfd_boolean null_input_bfd = TRUE;
18075       bfd_boolean only_data_sections = TRUE;
18076
18077       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
18078         {
18079           /* Ignore synthetic glue sections.  */
18080           if (strcmp (sec->name, ".glue_7")
18081               && strcmp (sec->name, ".glue_7t"))
18082             {
18083               if ((bfd_get_section_flags (ibfd, sec)
18084                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
18085                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
18086                 only_data_sections = FALSE;
18087
18088               null_input_bfd = FALSE;
18089               break;
18090             }
18091         }
18092
18093       if (null_input_bfd || only_data_sections)
18094         return TRUE;
18095     }
18096
18097   /* Complain about various flag mismatches.  */
18098   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
18099                                       EF_ARM_EABI_VERSION (out_flags)))
18100     {
18101       _bfd_error_handler
18102         (_("error: Source object %B has EABI version %d, but target %B has EABI version %d"),
18103          ibfd, obfd,
18104          (in_flags & EF_ARM_EABIMASK) >> 24,
18105          (out_flags & EF_ARM_EABIMASK) >> 24);
18106       return FALSE;
18107     }
18108
18109   /* Not sure what needs to be checked for EABI versions >= 1.  */
18110   /* VxWorks libraries do not use these flags.  */
18111   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
18112       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
18113       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
18114     {
18115       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
18116         {
18117           _bfd_error_handler
18118             (_("error: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
18119              ibfd, obfd,
18120              in_flags & EF_ARM_APCS_26 ? 26 : 32,
18121              out_flags & EF_ARM_APCS_26 ? 26 : 32);
18122           flags_compatible = FALSE;
18123         }
18124
18125       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
18126         {
18127           if (in_flags & EF_ARM_APCS_FLOAT)
18128             _bfd_error_handler
18129               (_("error: %B passes floats in float registers, whereas %B passes them in integer registers"),
18130                ibfd, obfd);
18131           else
18132             _bfd_error_handler
18133               (_("error: %B passes floats in integer registers, whereas %B passes them in float registers"),
18134                ibfd, obfd);
18135
18136           flags_compatible = FALSE;
18137         }
18138
18139       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
18140         {
18141           if (in_flags & EF_ARM_VFP_FLOAT)
18142             _bfd_error_handler
18143               (_("error: %B uses VFP instructions, whereas %B does not"),
18144                ibfd, obfd);
18145           else
18146             _bfd_error_handler
18147               (_("error: %B uses FPA instructions, whereas %B does not"),
18148                ibfd, obfd);
18149
18150           flags_compatible = FALSE;
18151         }
18152
18153       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
18154         {
18155           if (in_flags & EF_ARM_MAVERICK_FLOAT)
18156             _bfd_error_handler
18157               (_("error: %B uses Maverick instructions, whereas %B does not"),
18158                ibfd, obfd);
18159           else
18160             _bfd_error_handler
18161               (_("error: %B does not use Maverick instructions, whereas %B does"),
18162                ibfd, obfd);
18163
18164           flags_compatible = FALSE;
18165         }
18166
18167 #ifdef EF_ARM_SOFT_FLOAT
18168       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
18169         {
18170           /* We can allow interworking between code that is VFP format
18171              layout, and uses either soft float or integer regs for
18172              passing floating point arguments and results.  We already
18173              know that the APCS_FLOAT flags match; similarly for VFP
18174              flags.  */
18175           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
18176               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
18177             {
18178               if (in_flags & EF_ARM_SOFT_FLOAT)
18179                 _bfd_error_handler
18180                   (_("error: %B uses software FP, whereas %B uses hardware FP"),
18181                    ibfd, obfd);
18182               else
18183                 _bfd_error_handler
18184                   (_("error: %B uses hardware FP, whereas %B uses software FP"),
18185                    ibfd, obfd);
18186
18187               flags_compatible = FALSE;
18188             }
18189         }
18190 #endif
18191
18192       /* Interworking mismatch is only a warning.  */
18193       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
18194         {
18195           if (in_flags & EF_ARM_INTERWORK)
18196             {
18197               _bfd_error_handler
18198                 (_("Warning: %B supports interworking, whereas %B does not"),
18199                  ibfd, obfd);
18200             }
18201           else
18202             {
18203               _bfd_error_handler
18204                 (_("Warning: %B does not support interworking, whereas %B does"),
18205                  ibfd, obfd);
18206             }
18207         }
18208     }
18209
18210   return flags_compatible;
18211 }
18212
18213
18214 /* Symbian OS Targets.  */
18215
18216 #undef  TARGET_LITTLE_SYM
18217 #define TARGET_LITTLE_SYM               arm_elf32_symbian_le_vec
18218 #undef  TARGET_LITTLE_NAME
18219 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
18220 #undef  TARGET_BIG_SYM
18221 #define TARGET_BIG_SYM                  arm_elf32_symbian_be_vec
18222 #undef  TARGET_BIG_NAME
18223 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
18224
18225 /* Like elf32_arm_link_hash_table_create -- but overrides
18226    appropriately for Symbian OS.  */
18227
18228 static struct bfd_link_hash_table *
18229 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
18230 {
18231   struct bfd_link_hash_table *ret;
18232
18233   ret = elf32_arm_link_hash_table_create (abfd);
18234   if (ret)
18235     {
18236       struct elf32_arm_link_hash_table *htab
18237         = (struct elf32_arm_link_hash_table *)ret;
18238       /* There is no PLT header for Symbian OS.  */
18239       htab->plt_header_size = 0;
18240       /* The PLT entries are each one instruction and one word.  */
18241       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
18242       htab->symbian_p = 1;
18243       /* Symbian uses armv5t or above, so use_blx is always true.  */
18244       htab->use_blx = 1;
18245       htab->root.is_relocatable_executable = 1;
18246     }
18247   return ret;
18248 }
18249
18250 static const struct bfd_elf_special_section
18251 elf32_arm_symbian_special_sections[] =
18252 {
18253   /* In a BPABI executable, the dynamic linking sections do not go in
18254      the loadable read-only segment.  The post-linker may wish to
18255      refer to these sections, but they are not part of the final
18256      program image.  */
18257   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
18258   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
18259   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
18260   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
18261   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
18262   /* These sections do not need to be writable as the SymbianOS
18263      postlinker will arrange things so that no dynamic relocation is
18264      required.  */
18265   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
18266   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
18267   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
18268   { NULL,                             0, 0, 0,                 0 }
18269 };
18270
18271 static void
18272 elf32_arm_symbian_begin_write_processing (bfd *abfd,
18273                                           struct bfd_link_info *link_info)
18274 {
18275   /* BPABI objects are never loaded directly by an OS kernel; they are
18276      processed by a postlinker first, into an OS-specific format.  If
18277      the D_PAGED bit is set on the file, BFD will align segments on
18278      page boundaries, so that an OS can directly map the file.  With
18279      BPABI objects, that just results in wasted space.  In addition,
18280      because we clear the D_PAGED bit, map_sections_to_segments will
18281      recognize that the program headers should not be mapped into any
18282      loadable segment.  */
18283   abfd->flags &= ~D_PAGED;
18284   elf32_arm_begin_write_processing (abfd, link_info);
18285 }
18286
18287 static bfd_boolean
18288 elf32_arm_symbian_modify_segment_map (bfd *abfd,
18289                                       struct bfd_link_info *info)
18290 {
18291   struct elf_segment_map *m;
18292   asection *dynsec;
18293
18294   /* BPABI shared libraries and executables should have a PT_DYNAMIC
18295      segment.  However, because the .dynamic section is not marked
18296      with SEC_LOAD, the generic ELF code will not create such a
18297      segment.  */
18298   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
18299   if (dynsec)
18300     {
18301       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
18302         if (m->p_type == PT_DYNAMIC)
18303           break;
18304
18305       if (m == NULL)
18306         {
18307           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
18308           m->next = elf_seg_map (abfd);
18309           elf_seg_map (abfd) = m;
18310         }
18311     }
18312
18313   /* Also call the generic arm routine.  */
18314   return elf32_arm_modify_segment_map (abfd, info);
18315 }
18316
18317 /* Return address for Ith PLT stub in section PLT, for relocation REL
18318    or (bfd_vma) -1 if it should not be included.  */
18319
18320 static bfd_vma
18321 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
18322                                const arelent *rel ATTRIBUTE_UNUSED)
18323 {
18324   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
18325 }
18326
18327
18328 #undef  elf32_bed
18329 #define elf32_bed elf32_arm_symbian_bed
18330
18331 /* The dynamic sections are not allocated on SymbianOS; the postlinker
18332    will process them and then discard them.  */
18333 #undef  ELF_DYNAMIC_SEC_FLAGS
18334 #define ELF_DYNAMIC_SEC_FLAGS \
18335   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
18336
18337 #undef elf_backend_emit_relocs
18338
18339 #undef  bfd_elf32_bfd_link_hash_table_create
18340 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
18341 #undef  elf_backend_special_sections
18342 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
18343 #undef  elf_backend_begin_write_processing
18344 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
18345 #undef  elf_backend_final_write_processing
18346 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
18347
18348 #undef  elf_backend_modify_segment_map
18349 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
18350
18351 /* There is no .got section for BPABI objects, and hence no header.  */
18352 #undef  elf_backend_got_header_size
18353 #define elf_backend_got_header_size 0
18354
18355 /* Similarly, there is no .got.plt section.  */
18356 #undef  elf_backend_want_got_plt
18357 #define elf_backend_want_got_plt 0
18358
18359 #undef  elf_backend_plt_sym_val
18360 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
18361
18362 #undef  elf_backend_may_use_rel_p
18363 #define elf_backend_may_use_rel_p       1
18364 #undef  elf_backend_may_use_rela_p
18365 #define elf_backend_may_use_rela_p      0
18366 #undef  elf_backend_default_use_rela_p
18367 #define elf_backend_default_use_rela_p  0
18368 #undef  elf_backend_want_plt_sym
18369 #define elf_backend_want_plt_sym        0
18370 #undef  ELF_MAXPAGESIZE
18371 #define ELF_MAXPAGESIZE                 0x8000
18372
18373 #include "elf32-target.h"