Add support for linking ARMv8-M object files
[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   unsigned int additional_reloc_count;
2806   /* Information about unwind tables.  */
2807   union
2808   {
2809     /* Unwind info attached to a text section.  */
2810     struct
2811     {
2812       asection *arm_exidx_sec;
2813     } text;
2814
2815     /* Unwind info attached to an .ARM.exidx section.  */
2816     struct
2817     {
2818       arm_unwind_table_edit *unwind_edit_list;
2819       arm_unwind_table_edit *unwind_edit_tail;
2820     } exidx;
2821   } u;
2822 }
2823 _arm_elf_section_data;
2824
2825 #define elf32_arm_section_data(sec) \
2826   ((_arm_elf_section_data *) elf_section_data (sec))
2827
2828 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
2829    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
2830    so may be created multiple times: we use an array of these entries whilst
2831    relaxing which we can refresh easily, then create stubs for each potentially
2832    erratum-triggering instruction once we've settled on a solution.  */
2833
2834 struct a8_erratum_fix
2835 {
2836   bfd *input_bfd;
2837   asection *section;
2838   bfd_vma offset;
2839   bfd_vma addend;
2840   unsigned long orig_insn;
2841   char *stub_name;
2842   enum elf32_arm_stub_type stub_type;
2843   enum arm_st_branch_type branch_type;
2844 };
2845
2846 /* A table of relocs applied to branches which might trigger Cortex-A8
2847    erratum.  */
2848
2849 struct a8_erratum_reloc
2850 {
2851   bfd_vma from;
2852   bfd_vma destination;
2853   struct elf32_arm_link_hash_entry *hash;
2854   const char *sym_name;
2855   unsigned int r_type;
2856   enum arm_st_branch_type branch_type;
2857   bfd_boolean non_a8_stub;
2858 };
2859
2860 /* The size of the thread control block.  */
2861 #define TCB_SIZE        8
2862
2863 /* ARM-specific information about a PLT entry, over and above the usual
2864    gotplt_union.  */
2865 struct arm_plt_info
2866 {
2867   /* We reference count Thumb references to a PLT entry separately,
2868      so that we can emit the Thumb trampoline only if needed.  */
2869   bfd_signed_vma thumb_refcount;
2870
2871   /* Some references from Thumb code may be eliminated by BL->BLX
2872      conversion, so record them separately.  */
2873   bfd_signed_vma maybe_thumb_refcount;
2874
2875   /* How many of the recorded PLT accesses were from non-call relocations.
2876      This information is useful when deciding whether anything takes the
2877      address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
2878      non-call references to the function should resolve directly to the
2879      real runtime target.  */
2880   unsigned int noncall_refcount;
2881
2882   /* Since PLT entries have variable size if the Thumb prologue is
2883      used, we need to record the index into .got.plt instead of
2884      recomputing it from the PLT offset.  */
2885   bfd_signed_vma got_offset;
2886 };
2887
2888 /* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
2889 struct arm_local_iplt_info
2890 {
2891   /* The information that is usually found in the generic ELF part of
2892      the hash table entry.  */
2893   union gotplt_union root;
2894
2895   /* The information that is usually found in the ARM-specific part of
2896      the hash table entry.  */
2897   struct arm_plt_info arm;
2898
2899   /* A list of all potential dynamic relocations against this symbol.  */
2900   struct elf_dyn_relocs *dyn_relocs;
2901 };
2902
2903 struct elf_arm_obj_tdata
2904 {
2905   struct elf_obj_tdata root;
2906
2907   /* tls_type for each local got entry.  */
2908   char *local_got_tls_type;
2909
2910   /* GOTPLT entries for TLS descriptors.  */
2911   bfd_vma *local_tlsdesc_gotent;
2912
2913   /* Information for local symbols that need entries in .iplt.  */
2914   struct arm_local_iplt_info **local_iplt;
2915
2916   /* Zero to warn when linking objects with incompatible enum sizes.  */
2917   int no_enum_size_warning;
2918
2919   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
2920   int no_wchar_size_warning;
2921 };
2922
2923 #define elf_arm_tdata(bfd) \
2924   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
2925
2926 #define elf32_arm_local_got_tls_type(bfd) \
2927   (elf_arm_tdata (bfd)->local_got_tls_type)
2928
2929 #define elf32_arm_local_tlsdesc_gotent(bfd) \
2930   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
2931
2932 #define elf32_arm_local_iplt(bfd) \
2933   (elf_arm_tdata (bfd)->local_iplt)
2934
2935 #define is_arm_elf(bfd) \
2936   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2937    && elf_tdata (bfd) != NULL \
2938    && elf_object_id (bfd) == ARM_ELF_DATA)
2939
2940 static bfd_boolean
2941 elf32_arm_mkobject (bfd *abfd)
2942 {
2943   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
2944                                   ARM_ELF_DATA);
2945 }
2946
2947 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2948
2949 /* Arm ELF linker hash entry.  */
2950 struct elf32_arm_link_hash_entry
2951 {
2952   struct elf_link_hash_entry root;
2953
2954   /* Track dynamic relocs copied for this symbol.  */
2955   struct elf_dyn_relocs *dyn_relocs;
2956
2957   /* ARM-specific PLT information.  */
2958   struct arm_plt_info plt;
2959
2960 #define GOT_UNKNOWN     0
2961 #define GOT_NORMAL      1
2962 #define GOT_TLS_GD      2
2963 #define GOT_TLS_IE      4
2964 #define GOT_TLS_GDESC   8
2965 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
2966   unsigned int tls_type : 8;
2967
2968   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
2969   unsigned int is_iplt : 1;
2970
2971   unsigned int unused : 23;
2972
2973   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
2974      starting at the end of the jump table.  */
2975   bfd_vma tlsdesc_got;
2976
2977   /* The symbol marking the real symbol location for exported thumb
2978      symbols with Arm stubs.  */
2979   struct elf_link_hash_entry *export_glue;
2980
2981   /* A pointer to the most recently used stub hash entry against this
2982      symbol.  */
2983   struct elf32_arm_stub_hash_entry *stub_cache;
2984 };
2985
2986 /* Traverse an arm ELF linker hash table.  */
2987 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2988   (elf_link_hash_traverse                                               \
2989    (&(table)->root,                                                     \
2990     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2991     (info)))
2992
2993 /* Get the ARM elf linker hash table from a link_info structure.  */
2994 #define elf32_arm_hash_table(info) \
2995   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
2996   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
2997
2998 #define arm_stub_hash_lookup(table, string, create, copy) \
2999   ((struct elf32_arm_stub_hash_entry *) \
3000    bfd_hash_lookup ((table), (string), (create), (copy)))
3001
3002 /* Array to keep track of which stub sections have been created, and
3003    information on stub grouping.  */
3004 struct map_stub
3005 {
3006   /* This is the section to which stubs in the group will be
3007      attached.  */
3008   asection *link_sec;
3009   /* The stub section.  */
3010   asection *stub_sec;
3011 };
3012
3013 #define elf32_arm_compute_jump_table_size(htab) \
3014   ((htab)->next_tls_desc_index * 4)
3015
3016 /* ARM ELF linker hash table.  */
3017 struct elf32_arm_link_hash_table
3018 {
3019   /* The main hash table.  */
3020   struct elf_link_hash_table root;
3021
3022   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
3023   bfd_size_type thumb_glue_size;
3024
3025   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
3026   bfd_size_type arm_glue_size;
3027
3028   /* The size in bytes of section containing the ARMv4 BX veneers.  */
3029   bfd_size_type bx_glue_size;
3030
3031   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
3032      veneer has been populated.  */
3033   bfd_vma bx_glue_offset[15];
3034
3035   /* The size in bytes of the section containing glue for VFP11 erratum
3036      veneers.  */
3037   bfd_size_type vfp11_erratum_glue_size;
3038
3039  /* The size in bytes of the section containing glue for STM32L4XX erratum
3040      veneers.  */
3041   bfd_size_type stm32l4xx_erratum_glue_size;
3042
3043   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
3044      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
3045      elf32_arm_write_section().  */
3046   struct a8_erratum_fix *a8_erratum_fixes;
3047   unsigned int num_a8_erratum_fixes;
3048
3049   /* An arbitrary input BFD chosen to hold the glue sections.  */
3050   bfd * bfd_of_glue_owner;
3051
3052   /* Nonzero to output a BE8 image.  */
3053   int byteswap_code;
3054
3055   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
3056      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
3057   int target1_is_rel;
3058
3059   /* The relocation to use for R_ARM_TARGET2 relocations.  */
3060   int target2_reloc;
3061
3062   /* 0 = Ignore R_ARM_V4BX.
3063      1 = Convert BX to MOV PC.
3064      2 = Generate v4 interworing stubs.  */
3065   int fix_v4bx;
3066
3067   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
3068   int fix_cortex_a8;
3069
3070   /* Whether we should fix the ARM1176 BLX immediate issue.  */
3071   int fix_arm1176;
3072
3073   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
3074   int use_blx;
3075
3076   /* What sort of code sequences we should look for which may trigger the
3077      VFP11 denorm erratum.  */
3078   bfd_arm_vfp11_fix vfp11_fix;
3079
3080   /* Global counter for the number of fixes we have emitted.  */
3081   int num_vfp11_fixes;
3082
3083   /* What sort of code sequences we should look for which may trigger the
3084      STM32L4XX erratum.  */
3085   bfd_arm_stm32l4xx_fix stm32l4xx_fix;
3086
3087   /* Global counter for the number of fixes we have emitted.  */
3088   int num_stm32l4xx_fixes;
3089
3090   /* Nonzero to force PIC branch veneers.  */
3091   int pic_veneer;
3092
3093   /* The number of bytes in the initial entry in the PLT.  */
3094   bfd_size_type plt_header_size;
3095
3096   /* The number of bytes in the subsequent PLT etries.  */
3097   bfd_size_type plt_entry_size;
3098
3099   /* True if the target system is VxWorks.  */
3100   int vxworks_p;
3101
3102   /* True if the target system is Symbian OS.  */
3103   int symbian_p;
3104
3105   /* True if the target system is Native Client.  */
3106   int nacl_p;
3107
3108   /* True if the target uses REL relocations.  */
3109   int use_rel;
3110
3111   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
3112   bfd_vma next_tls_desc_index;
3113
3114   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
3115   bfd_vma num_tls_desc;
3116
3117   /* Short-cuts to get to dynamic linker sections.  */
3118   asection *sdynbss;
3119   asection *srelbss;
3120
3121   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
3122   asection *srelplt2;
3123
3124   /* The offset into splt of the PLT entry for the TLS descriptor
3125      resolver.  Special values are 0, if not necessary (or not found
3126      to be necessary yet), and -1 if needed but not determined
3127      yet.  */
3128   bfd_vma dt_tlsdesc_plt;
3129
3130   /* The offset into sgot of the GOT entry used by the PLT entry
3131      above.  */
3132   bfd_vma dt_tlsdesc_got;
3133
3134   /* Offset in .plt section of tls_arm_trampoline.  */
3135   bfd_vma tls_trampoline;
3136
3137   /* Data for R_ARM_TLS_LDM32 relocations.  */
3138   union
3139   {
3140     bfd_signed_vma refcount;
3141     bfd_vma offset;
3142   } tls_ldm_got;
3143
3144   /* Small local sym cache.  */
3145   struct sym_cache sym_cache;
3146
3147   /* For convenience in allocate_dynrelocs.  */
3148   bfd * obfd;
3149
3150   /* The amount of space used by the reserved portion of the sgotplt
3151      section, plus whatever space is used by the jump slots.  */
3152   bfd_vma sgotplt_jump_table_size;
3153
3154   /* The stub hash table.  */
3155   struct bfd_hash_table stub_hash_table;
3156
3157   /* Linker stub bfd.  */
3158   bfd *stub_bfd;
3159
3160   /* Linker call-backs.  */
3161   asection * (*add_stub_section) (const char *, asection *, unsigned int);
3162   void (*layout_sections_again) (void);
3163
3164   /* Array to keep track of which stub sections have been created, and
3165      information on stub grouping.  */
3166   struct map_stub *stub_group;
3167
3168   /* Number of elements in stub_group.  */
3169   unsigned int top_id;
3170
3171   /* Assorted information used by elf32_arm_size_stubs.  */
3172   unsigned int bfd_count;
3173   unsigned int top_index;
3174   asection **input_list;
3175 };
3176
3177 static inline int
3178 ctz (unsigned int mask)
3179 {
3180 #if GCC_VERSION >= 3004
3181   return __builtin_ctz (mask);
3182 #else
3183   unsigned int i;
3184
3185   for (i = 0; i < 8 * sizeof (mask); i++)
3186     {
3187       if (mask & 0x1)
3188         break;
3189       mask = (mask >> 1);
3190     }
3191   return i;
3192 #endif
3193 }
3194
3195 static inline int
3196 popcount (unsigned int mask)
3197 {
3198 #if GCC_VERSION >= 3004
3199   return __builtin_popcount (mask);
3200 #else
3201   unsigned int i, sum = 0;
3202
3203   for (i = 0; i < 8 * sizeof (mask); i++)
3204     {
3205       if (mask & 0x1)
3206         sum++;
3207       mask = (mask >> 1);
3208     }
3209   return sum;
3210 #endif
3211 }
3212
3213 /* Create an entry in an ARM ELF linker hash table.  */
3214
3215 static struct bfd_hash_entry *
3216 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3217                              struct bfd_hash_table * table,
3218                              const char * string)
3219 {
3220   struct elf32_arm_link_hash_entry * ret =
3221     (struct elf32_arm_link_hash_entry *) entry;
3222
3223   /* Allocate the structure if it has not already been allocated by a
3224      subclass.  */
3225   if (ret == NULL)
3226     ret = (struct elf32_arm_link_hash_entry *)
3227         bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3228   if (ret == NULL)
3229     return (struct bfd_hash_entry *) ret;
3230
3231   /* Call the allocation method of the superclass.  */
3232   ret = ((struct elf32_arm_link_hash_entry *)
3233          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3234                                      table, string));
3235   if (ret != NULL)
3236     {
3237       ret->dyn_relocs = NULL;
3238       ret->tls_type = GOT_UNKNOWN;
3239       ret->tlsdesc_got = (bfd_vma) -1;
3240       ret->plt.thumb_refcount = 0;
3241       ret->plt.maybe_thumb_refcount = 0;
3242       ret->plt.noncall_refcount = 0;
3243       ret->plt.got_offset = -1;
3244       ret->is_iplt = FALSE;
3245       ret->export_glue = NULL;
3246
3247       ret->stub_cache = NULL;
3248     }
3249
3250   return (struct bfd_hash_entry *) ret;
3251 }
3252
3253 /* Ensure that we have allocated bookkeeping structures for ABFD's local
3254    symbols.  */
3255
3256 static bfd_boolean
3257 elf32_arm_allocate_local_sym_info (bfd *abfd)
3258 {
3259   if (elf_local_got_refcounts (abfd) == NULL)
3260     {
3261       bfd_size_type num_syms;
3262       bfd_size_type size;
3263       char *data;
3264
3265       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3266       size = num_syms * (sizeof (bfd_signed_vma)
3267                          + sizeof (struct arm_local_iplt_info *)
3268                          + sizeof (bfd_vma)
3269                          + sizeof (char));
3270       data = bfd_zalloc (abfd, size);
3271       if (data == NULL)
3272         return FALSE;
3273
3274       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
3275       data += num_syms * sizeof (bfd_signed_vma);
3276
3277       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
3278       data += num_syms * sizeof (struct arm_local_iplt_info *);
3279
3280       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
3281       data += num_syms * sizeof (bfd_vma);
3282
3283       elf32_arm_local_got_tls_type (abfd) = data;
3284     }
3285   return TRUE;
3286 }
3287
3288 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
3289    to input bfd ABFD.  Create the information if it doesn't already exist.
3290    Return null if an allocation fails.  */
3291
3292 static struct arm_local_iplt_info *
3293 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3294 {
3295   struct arm_local_iplt_info **ptr;
3296
3297   if (!elf32_arm_allocate_local_sym_info (abfd))
3298     return NULL;
3299
3300   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3301   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3302   if (*ptr == NULL)
3303     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3304   return *ptr;
3305 }
3306
3307 /* Try to obtain PLT information for the symbol with index R_SYMNDX
3308    in ABFD's symbol table.  If the symbol is global, H points to its
3309    hash table entry, otherwise H is null.
3310
3311    Return true if the symbol does have PLT information.  When returning
3312    true, point *ROOT_PLT at the target-independent reference count/offset
3313    union and *ARM_PLT at the ARM-specific information.  */
3314
3315 static bfd_boolean
3316 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_entry *h,
3317                         unsigned long r_symndx, union gotplt_union **root_plt,
3318                         struct arm_plt_info **arm_plt)
3319 {
3320   struct arm_local_iplt_info *local_iplt;
3321
3322   if (h != NULL)
3323     {
3324       *root_plt = &h->root.plt;
3325       *arm_plt = &h->plt;
3326       return TRUE;
3327     }
3328
3329   if (elf32_arm_local_iplt (abfd) == NULL)
3330     return FALSE;
3331
3332   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3333   if (local_iplt == NULL)
3334     return FALSE;
3335
3336   *root_plt = &local_iplt->root;
3337   *arm_plt = &local_iplt->arm;
3338   return TRUE;
3339 }
3340
3341 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
3342    before it.  */
3343
3344 static bfd_boolean
3345 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3346                                   struct arm_plt_info *arm_plt)
3347 {
3348   struct elf32_arm_link_hash_table *htab;
3349
3350   htab = elf32_arm_hash_table (info);
3351   return (arm_plt->thumb_refcount != 0
3352           || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0));
3353 }
3354
3355 /* Return a pointer to the head of the dynamic reloc list that should
3356    be used for local symbol ISYM, which is symbol number R_SYMNDX in
3357    ABFD's symbol table.  Return null if an error occurs.  */
3358
3359 static struct elf_dyn_relocs **
3360 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3361                                    Elf_Internal_Sym *isym)
3362 {
3363   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3364     {
3365       struct arm_local_iplt_info *local_iplt;
3366
3367       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3368       if (local_iplt == NULL)
3369         return NULL;
3370       return &local_iplt->dyn_relocs;
3371     }
3372   else
3373     {
3374       /* Track dynamic relocs needed for local syms too.
3375          We really need local syms available to do this
3376          easily.  Oh well.  */
3377       asection *s;
3378       void *vpp;
3379
3380       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3381       if (s == NULL)
3382         abort ();
3383
3384       vpp = &elf_section_data (s)->local_dynrel;
3385       return (struct elf_dyn_relocs **) vpp;
3386     }
3387 }
3388
3389 /* Initialize an entry in the stub hash table.  */
3390
3391 static struct bfd_hash_entry *
3392 stub_hash_newfunc (struct bfd_hash_entry *entry,
3393                    struct bfd_hash_table *table,
3394                    const char *string)
3395 {
3396   /* Allocate the structure if it has not already been allocated by a
3397      subclass.  */
3398   if (entry == NULL)
3399     {
3400       entry = (struct bfd_hash_entry *)
3401           bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3402       if (entry == NULL)
3403         return entry;
3404     }
3405
3406   /* Call the allocation method of the superclass.  */
3407   entry = bfd_hash_newfunc (entry, table, string);
3408   if (entry != NULL)
3409     {
3410       struct elf32_arm_stub_hash_entry *eh;
3411
3412       /* Initialize the local fields.  */
3413       eh = (struct elf32_arm_stub_hash_entry *) entry;
3414       eh->stub_sec = NULL;
3415       eh->stub_offset = 0;
3416       eh->target_value = 0;
3417       eh->target_section = NULL;
3418       eh->target_addend = 0;
3419       eh->orig_insn = 0;
3420       eh->stub_type = arm_stub_none;
3421       eh->stub_size = 0;
3422       eh->stub_template = NULL;
3423       eh->stub_template_size = 0;
3424       eh->h = NULL;
3425       eh->id_sec = NULL;
3426       eh->output_name = NULL;
3427     }
3428
3429   return entry;
3430 }
3431
3432 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3433    shortcuts to them in our hash table.  */
3434
3435 static bfd_boolean
3436 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3437 {
3438   struct elf32_arm_link_hash_table *htab;
3439
3440   htab = elf32_arm_hash_table (info);
3441   if (htab == NULL)
3442     return FALSE;
3443
3444   /* BPABI objects never have a GOT, or associated sections.  */
3445   if (htab->symbian_p)
3446     return TRUE;
3447
3448   if (! _bfd_elf_create_got_section (dynobj, info))
3449     return FALSE;
3450
3451   return TRUE;
3452 }
3453
3454 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3455
3456 static bfd_boolean
3457 create_ifunc_sections (struct bfd_link_info *info)
3458 {
3459   struct elf32_arm_link_hash_table *htab;
3460   const struct elf_backend_data *bed;
3461   bfd *dynobj;
3462   asection *s;
3463   flagword flags;
3464
3465   htab = elf32_arm_hash_table (info);
3466   dynobj = htab->root.dynobj;
3467   bed = get_elf_backend_data (dynobj);
3468   flags = bed->dynamic_sec_flags;
3469
3470   if (htab->root.iplt == NULL)
3471     {
3472       s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3473                                               flags | SEC_READONLY | SEC_CODE);
3474       if (s == NULL
3475           || !bfd_set_section_alignment (dynobj, s, bed->plt_alignment))
3476         return FALSE;
3477       htab->root.iplt = s;
3478     }
3479
3480   if (htab->root.irelplt == NULL)
3481     {
3482       s = bfd_make_section_anyway_with_flags (dynobj,
3483                                               RELOC_SECTION (htab, ".iplt"),
3484                                               flags | SEC_READONLY);
3485       if (s == NULL
3486           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3487         return FALSE;
3488       htab->root.irelplt = s;
3489     }
3490
3491   if (htab->root.igotplt == NULL)
3492     {
3493       s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3494       if (s == NULL
3495           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3496         return FALSE;
3497       htab->root.igotplt = s;
3498     }
3499   return TRUE;
3500 }
3501
3502 /* Determine if we're dealing with a Thumb only architecture.  */
3503
3504 static bfd_boolean
3505 using_thumb_only (struct elf32_arm_link_hash_table *globals)
3506 {
3507   int arch;
3508   int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3509                                           Tag_CPU_arch_profile);
3510
3511   if (profile)
3512     return profile == 'M';
3513
3514   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3515
3516   if (arch == TAG_CPU_ARCH_V6_M
3517       || arch == TAG_CPU_ARCH_V6S_M
3518       || arch == TAG_CPU_ARCH_V7E_M
3519       || arch == TAG_CPU_ARCH_V8M_BASE
3520       || arch == TAG_CPU_ARCH_V8M_MAIN)
3521     return TRUE;
3522
3523   return FALSE;
3524 }
3525
3526 /* Determine if we're dealing with a Thumb-2 object.  */
3527
3528 static bfd_boolean
3529 using_thumb2 (struct elf32_arm_link_hash_table *globals)
3530 {
3531   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3532                                        Tag_CPU_arch);
3533   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
3534 }
3535
3536 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3537    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3538    hash table.  */
3539
3540 static bfd_boolean
3541 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3542 {
3543   struct elf32_arm_link_hash_table *htab;
3544
3545   htab = elf32_arm_hash_table (info);
3546   if (htab == NULL)
3547     return FALSE;
3548
3549   if (!htab->root.sgot && !create_got_section (dynobj, info))
3550     return FALSE;
3551
3552   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3553     return FALSE;
3554
3555   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
3556   if (!bfd_link_pic (info))
3557     htab->srelbss = bfd_get_linker_section (dynobj,
3558                                             RELOC_SECTION (htab, ".bss"));
3559
3560   if (htab->vxworks_p)
3561     {
3562       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3563         return FALSE;
3564
3565       if (bfd_link_pic (info))
3566         {
3567           htab->plt_header_size = 0;
3568           htab->plt_entry_size
3569             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3570         }
3571       else
3572         {
3573           htab->plt_header_size
3574             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3575           htab->plt_entry_size
3576             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3577         }
3578     }
3579   else
3580     {
3581       /* PR ld/16017
3582          Test for thumb only architectures.  Note - we cannot just call
3583          using_thumb_only() as the attributes in the output bfd have not been
3584          initialised at this point, so instead we use the input bfd.  */
3585       bfd * saved_obfd = htab->obfd;
3586
3587       htab->obfd = dynobj;
3588       if (using_thumb_only (htab))
3589         {
3590           htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
3591           htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
3592         }
3593       htab->obfd = saved_obfd;
3594     }
3595
3596   if (!htab->root.splt
3597       || !htab->root.srelplt
3598       || !htab->sdynbss
3599       || (!bfd_link_pic (info) && !htab->srelbss))
3600     abort ();
3601
3602   return TRUE;
3603 }
3604
3605 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3606
3607 static void
3608 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3609                                 struct elf_link_hash_entry *dir,
3610                                 struct elf_link_hash_entry *ind)
3611 {
3612   struct elf32_arm_link_hash_entry *edir, *eind;
3613
3614   edir = (struct elf32_arm_link_hash_entry *) dir;
3615   eind = (struct elf32_arm_link_hash_entry *) ind;
3616
3617   if (eind->dyn_relocs != NULL)
3618     {
3619       if (edir->dyn_relocs != NULL)
3620         {
3621           struct elf_dyn_relocs **pp;
3622           struct elf_dyn_relocs *p;
3623
3624           /* Add reloc counts against the indirect sym to the direct sym
3625              list.  Merge any entries against the same section.  */
3626           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3627             {
3628               struct elf_dyn_relocs *q;
3629
3630               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3631                 if (q->sec == p->sec)
3632                   {
3633                     q->pc_count += p->pc_count;
3634                     q->count += p->count;
3635                     *pp = p->next;
3636                     break;
3637                   }
3638               if (q == NULL)
3639                 pp = &p->next;
3640             }
3641           *pp = edir->dyn_relocs;
3642         }
3643
3644       edir->dyn_relocs = eind->dyn_relocs;
3645       eind->dyn_relocs = NULL;
3646     }
3647
3648   if (ind->root.type == bfd_link_hash_indirect)
3649     {
3650       /* Copy over PLT info.  */
3651       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3652       eind->plt.thumb_refcount = 0;
3653       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3654       eind->plt.maybe_thumb_refcount = 0;
3655       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3656       eind->plt.noncall_refcount = 0;
3657
3658       /* We should only allocate a function to .iplt once the final
3659          symbol information is known.  */
3660       BFD_ASSERT (!eind->is_iplt);
3661
3662       if (dir->got.refcount <= 0)
3663         {
3664           edir->tls_type = eind->tls_type;
3665           eind->tls_type = GOT_UNKNOWN;
3666         }
3667     }
3668
3669   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3670 }
3671
3672 /* Destroy an ARM elf linker hash table.  */
3673
3674 static void
3675 elf32_arm_link_hash_table_free (bfd *obfd)
3676 {
3677   struct elf32_arm_link_hash_table *ret
3678     = (struct elf32_arm_link_hash_table *) obfd->link.hash;
3679
3680   bfd_hash_table_free (&ret->stub_hash_table);
3681   _bfd_elf_link_hash_table_free (obfd);
3682 }
3683
3684 /* Create an ARM elf linker hash table.  */
3685
3686 static struct bfd_link_hash_table *
3687 elf32_arm_link_hash_table_create (bfd *abfd)
3688 {
3689   struct elf32_arm_link_hash_table *ret;
3690   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
3691
3692   ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
3693   if (ret == NULL)
3694     return NULL;
3695
3696   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
3697                                       elf32_arm_link_hash_newfunc,
3698                                       sizeof (struct elf32_arm_link_hash_entry),
3699                                       ARM_ELF_DATA))
3700     {
3701       free (ret);
3702       return NULL;
3703     }
3704
3705   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
3706   ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
3707 #ifdef FOUR_WORD_PLT
3708   ret->plt_header_size = 16;
3709   ret->plt_entry_size = 16;
3710 #else
3711   ret->plt_header_size = 20;
3712   ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
3713 #endif
3714   ret->use_rel = 1;
3715   ret->obfd = abfd;
3716
3717   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
3718                             sizeof (struct elf32_arm_stub_hash_entry)))
3719     {
3720       _bfd_elf_link_hash_table_free (abfd);
3721       return NULL;
3722     }
3723   ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
3724
3725   return &ret->root.root;
3726 }
3727
3728 /* Determine what kind of NOPs are available.  */
3729
3730 static bfd_boolean
3731 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
3732 {
3733   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3734                                              Tag_CPU_arch);
3735   return arch == TAG_CPU_ARCH_V6T2
3736          || arch == TAG_CPU_ARCH_V6K
3737          || arch == TAG_CPU_ARCH_V7
3738          || arch == TAG_CPU_ARCH_V7E_M;
3739 }
3740
3741 static bfd_boolean
3742 arch_has_thumb2_nop (struct elf32_arm_link_hash_table *globals)
3743 {
3744   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3745                                              Tag_CPU_arch);
3746   return (arch == TAG_CPU_ARCH_V6T2 || arch == TAG_CPU_ARCH_V7
3747           || arch == TAG_CPU_ARCH_V7E_M);
3748 }
3749
3750 static bfd_boolean
3751 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
3752 {
3753   switch (stub_type)
3754     {
3755     case arm_stub_long_branch_thumb_only:
3756     case arm_stub_long_branch_v4t_thumb_arm:
3757     case arm_stub_short_branch_v4t_thumb_arm:
3758     case arm_stub_long_branch_v4t_thumb_arm_pic:
3759     case arm_stub_long_branch_v4t_thumb_tls_pic:
3760     case arm_stub_long_branch_thumb_only_pic:
3761       return TRUE;
3762     case arm_stub_none:
3763       BFD_FAIL ();
3764       return FALSE;
3765       break;
3766     default:
3767       return FALSE;
3768     }
3769 }
3770
3771 /* Determine the type of stub needed, if any, for a call.  */
3772
3773 static enum elf32_arm_stub_type
3774 arm_type_of_stub (struct bfd_link_info *info,
3775                   asection *input_sec,
3776                   const Elf_Internal_Rela *rel,
3777                   unsigned char st_type,
3778                   enum arm_st_branch_type *actual_branch_type,
3779                   struct elf32_arm_link_hash_entry *hash,
3780                   bfd_vma destination,
3781                   asection *sym_sec,
3782                   bfd *input_bfd,
3783                   const char *name)
3784 {
3785   bfd_vma location;
3786   bfd_signed_vma branch_offset;
3787   unsigned int r_type;
3788   struct elf32_arm_link_hash_table * globals;
3789   int thumb2;
3790   int thumb_only;
3791   enum elf32_arm_stub_type stub_type = arm_stub_none;
3792   int use_plt = 0;
3793   enum arm_st_branch_type branch_type = *actual_branch_type;
3794   union gotplt_union *root_plt;
3795   struct arm_plt_info *arm_plt;
3796
3797   if (branch_type == ST_BRANCH_LONG)
3798     return stub_type;
3799
3800   globals = elf32_arm_hash_table (info);
3801   if (globals == NULL)
3802     return stub_type;
3803
3804   thumb_only = using_thumb_only (globals);
3805
3806   thumb2 = using_thumb2 (globals);
3807
3808   /* Determine where the call point is.  */
3809   location = (input_sec->output_offset
3810               + input_sec->output_section->vma
3811               + rel->r_offset);
3812
3813   r_type = ELF32_R_TYPE (rel->r_info);
3814
3815   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
3816      are considering a function call relocation.  */
3817   if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
3818                      || r_type == R_ARM_THM_JUMP19)
3819       && branch_type == ST_BRANCH_TO_ARM)
3820     branch_type = ST_BRANCH_TO_THUMB;
3821
3822   /* For TLS call relocs, it is the caller's responsibility to provide
3823      the address of the appropriate trampoline.  */
3824   if (r_type != R_ARM_TLS_CALL
3825       && r_type != R_ARM_THM_TLS_CALL
3826       && elf32_arm_get_plt_info (input_bfd, hash, ELF32_R_SYM (rel->r_info),
3827                                  &root_plt, &arm_plt)
3828       && root_plt->offset != (bfd_vma) -1)
3829     {
3830       asection *splt;
3831
3832       if (hash == NULL || hash->is_iplt)
3833         splt = globals->root.iplt;
3834       else
3835         splt = globals->root.splt;
3836       if (splt != NULL)
3837         {
3838           use_plt = 1;
3839
3840           /* Note when dealing with PLT entries: the main PLT stub is in
3841              ARM mode, so if the branch is in Thumb mode, another
3842              Thumb->ARM stub will be inserted later just before the ARM
3843              PLT stub. We don't take this extra distance into account
3844              here, because if a long branch stub is needed, we'll add a
3845              Thumb->Arm one and branch directly to the ARM PLT entry
3846              because it avoids spreading offset corrections in several
3847              places.  */
3848
3849           destination = (splt->output_section->vma
3850                          + splt->output_offset
3851                          + root_plt->offset);
3852           st_type = STT_FUNC;
3853           branch_type = ST_BRANCH_TO_ARM;
3854         }
3855     }
3856   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
3857   BFD_ASSERT (st_type != STT_GNU_IFUNC);
3858
3859   branch_offset = (bfd_signed_vma)(destination - location);
3860
3861   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
3862       || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
3863     {
3864       /* Handle cases where:
3865          - this call goes too far (different Thumb/Thumb2 max
3866            distance)
3867          - it's a Thumb->Arm call and blx is not available, or it's a
3868            Thumb->Arm branch (not bl). A stub is needed in this case,
3869            but only if this call is not through a PLT entry. Indeed,
3870            PLT stubs handle mode switching already.
3871       */
3872       if ((!thumb2
3873             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3874                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3875           || (thumb2
3876               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3877                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3878           || (thumb2
3879               && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
3880                   || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
3881               && (r_type == R_ARM_THM_JUMP19))
3882           || (branch_type == ST_BRANCH_TO_ARM
3883               && (((r_type == R_ARM_THM_CALL
3884                     || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
3885                   || (r_type == R_ARM_THM_JUMP24)
3886                   || (r_type == R_ARM_THM_JUMP19))
3887               && !use_plt))
3888         {
3889           if (branch_type == ST_BRANCH_TO_THUMB)
3890             {
3891               /* Thumb to thumb.  */
3892               if (!thumb_only)
3893                 {
3894                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3895                     /* PIC stubs.  */
3896                     ? ((globals->use_blx
3897                         && (r_type == R_ARM_THM_CALL))
3898                        /* V5T and above. Stub starts with ARM code, so
3899                           we must be able to switch mode before
3900                           reaching it, which is only possible for 'bl'
3901                           (ie R_ARM_THM_CALL relocation).  */
3902                        ? arm_stub_long_branch_any_thumb_pic
3903                        /* On V4T, use Thumb code only.  */
3904                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
3905
3906                     /* non-PIC stubs.  */
3907                     : ((globals->use_blx
3908                         && (r_type == R_ARM_THM_CALL))
3909                        /* V5T and above.  */
3910                        ? arm_stub_long_branch_any_any
3911                        /* V4T.  */
3912                        : arm_stub_long_branch_v4t_thumb_thumb);
3913                 }
3914               else
3915                 {
3916                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3917                     /* PIC stub.  */
3918                     ? arm_stub_long_branch_thumb_only_pic
3919                     /* non-PIC stub.  */
3920                     : arm_stub_long_branch_thumb_only;
3921                 }
3922             }
3923           else
3924             {
3925               /* Thumb to arm.  */
3926               if (sym_sec != NULL
3927                   && sym_sec->owner != NULL
3928                   && !INTERWORK_FLAG (sym_sec->owner))
3929                 {
3930                   (*_bfd_error_handler)
3931                     (_("%B(%s): warning: interworking not enabled.\n"
3932                        "  first occurrence: %B: Thumb call to ARM"),
3933                      sym_sec->owner, input_bfd, name);
3934                 }
3935
3936               stub_type =
3937                 (bfd_link_pic (info) | globals->pic_veneer)
3938                 /* PIC stubs.  */
3939                 ? (r_type == R_ARM_THM_TLS_CALL
3940                    /* TLS PIC stubs.  */
3941                    ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
3942                       : arm_stub_long_branch_v4t_thumb_tls_pic)
3943                    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3944                       /* V5T PIC and above.  */
3945                       ? arm_stub_long_branch_any_arm_pic
3946                       /* V4T PIC stub.  */
3947                       : arm_stub_long_branch_v4t_thumb_arm_pic))
3948
3949                 /* non-PIC stubs.  */
3950                 : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3951                    /* V5T and above.  */
3952                    ? arm_stub_long_branch_any_any
3953                    /* V4T.  */
3954                    : arm_stub_long_branch_v4t_thumb_arm);
3955
3956               /* Handle v4t short branches.  */
3957               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3958                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3959                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3960                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3961             }
3962         }
3963     }
3964   else if (r_type == R_ARM_CALL
3965            || r_type == R_ARM_JUMP24
3966            || r_type == R_ARM_PLT32
3967            || r_type == R_ARM_TLS_CALL)
3968     {
3969       if (branch_type == ST_BRANCH_TO_THUMB)
3970         {
3971           /* Arm to thumb.  */
3972
3973           if (sym_sec != NULL
3974               && sym_sec->owner != NULL
3975               && !INTERWORK_FLAG (sym_sec->owner))
3976             {
3977               (*_bfd_error_handler)
3978                 (_("%B(%s): warning: interworking not enabled.\n"
3979                    "  first occurrence: %B: ARM call to Thumb"),
3980                  sym_sec->owner, input_bfd, name);
3981             }
3982
3983           /* We have an extra 2-bytes reach because of
3984              the mode change (bit 24 (H) of BLX encoding).  */
3985           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3986               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3987               || (r_type == R_ARM_CALL && !globals->use_blx)
3988               || (r_type == R_ARM_JUMP24)
3989               || (r_type == R_ARM_PLT32))
3990             {
3991               stub_type = (bfd_link_pic (info) | globals->pic_veneer)
3992                 /* PIC stubs.  */
3993                 ? ((globals->use_blx)
3994                    /* V5T and above.  */
3995                    ? arm_stub_long_branch_any_thumb_pic
3996                    /* V4T stub.  */
3997                    : arm_stub_long_branch_v4t_arm_thumb_pic)
3998
3999                 /* non-PIC stubs.  */
4000                 : ((globals->use_blx)
4001                    /* V5T and above.  */
4002                    ? arm_stub_long_branch_any_any
4003                    /* V4T.  */
4004                    : arm_stub_long_branch_v4t_arm_thumb);
4005             }
4006         }
4007       else
4008         {
4009           /* Arm to arm.  */
4010           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4011               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4012             {
4013               stub_type =
4014                 (bfd_link_pic (info) | globals->pic_veneer)
4015                 /* PIC stubs.  */
4016                 ? (r_type == R_ARM_TLS_CALL
4017                    /* TLS PIC Stub.  */
4018                    ? arm_stub_long_branch_any_tls_pic
4019                    : (globals->nacl_p
4020                       ? arm_stub_long_branch_arm_nacl_pic
4021                       : arm_stub_long_branch_any_arm_pic))
4022                 /* non-PIC stubs.  */
4023                 : (globals->nacl_p
4024                    ? arm_stub_long_branch_arm_nacl
4025                    : arm_stub_long_branch_any_any);
4026             }
4027         }
4028     }
4029
4030   /* If a stub is needed, record the actual destination type.  */
4031   if (stub_type != arm_stub_none)
4032     *actual_branch_type = branch_type;
4033
4034   return stub_type;
4035 }
4036
4037 /* Build a name for an entry in the stub hash table.  */
4038
4039 static char *
4040 elf32_arm_stub_name (const asection *input_section,
4041                      const asection *sym_sec,
4042                      const struct elf32_arm_link_hash_entry *hash,
4043                      const Elf_Internal_Rela *rel,
4044                      enum elf32_arm_stub_type stub_type)
4045 {
4046   char *stub_name;
4047   bfd_size_type len;
4048
4049   if (hash)
4050     {
4051       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4052       stub_name = (char *) bfd_malloc (len);
4053       if (stub_name != NULL)
4054         sprintf (stub_name, "%08x_%s+%x_%d",
4055                  input_section->id & 0xffffffff,
4056                  hash->root.root.root.string,
4057                  (int) rel->r_addend & 0xffffffff,
4058                  (int) stub_type);
4059     }
4060   else
4061     {
4062       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4063       stub_name = (char *) bfd_malloc (len);
4064       if (stub_name != NULL)
4065         sprintf (stub_name, "%08x_%x:%x+%x_%d",
4066                  input_section->id & 0xffffffff,
4067                  sym_sec->id & 0xffffffff,
4068                  ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4069                  || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4070                  ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4071                  (int) rel->r_addend & 0xffffffff,
4072                  (int) stub_type);
4073     }
4074
4075   return stub_name;
4076 }
4077
4078 /* Look up an entry in the stub hash.  Stub entries are cached because
4079    creating the stub name takes a bit of time.  */
4080
4081 static struct elf32_arm_stub_hash_entry *
4082 elf32_arm_get_stub_entry (const asection *input_section,
4083                           const asection *sym_sec,
4084                           struct elf_link_hash_entry *hash,
4085                           const Elf_Internal_Rela *rel,
4086                           struct elf32_arm_link_hash_table *htab,
4087                           enum elf32_arm_stub_type stub_type)
4088 {
4089   struct elf32_arm_stub_hash_entry *stub_entry;
4090   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4091   const asection *id_sec;
4092
4093   if ((input_section->flags & SEC_CODE) == 0)
4094     return NULL;
4095
4096   /* If this input section is part of a group of sections sharing one
4097      stub section, then use the id of the first section in the group.
4098      Stub names need to include a section id, as there may well be
4099      more than one stub used to reach say, printf, and we need to
4100      distinguish between them.  */
4101   id_sec = htab->stub_group[input_section->id].link_sec;
4102
4103   if (h != NULL && h->stub_cache != NULL
4104       && h->stub_cache->h == h
4105       && h->stub_cache->id_sec == id_sec
4106       && h->stub_cache->stub_type == stub_type)
4107     {
4108       stub_entry = h->stub_cache;
4109     }
4110   else
4111     {
4112       char *stub_name;
4113
4114       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4115       if (stub_name == NULL)
4116         return NULL;
4117
4118       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4119                                         stub_name, FALSE, FALSE);
4120       if (h != NULL)
4121         h->stub_cache = stub_entry;
4122
4123       free (stub_name);
4124     }
4125
4126   return stub_entry;
4127 }
4128
4129 /* Find or create a stub section.  Returns a pointer to the stub section, and
4130    the section to which the stub section will be attached (in *LINK_SEC_P).
4131    LINK_SEC_P may be NULL.  */
4132
4133 static asection *
4134 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4135                                    struct elf32_arm_link_hash_table *htab)
4136 {
4137   asection *link_sec;
4138   asection *stub_sec;
4139
4140   link_sec = htab->stub_group[section->id].link_sec;
4141   BFD_ASSERT (link_sec != NULL);
4142   stub_sec = htab->stub_group[section->id].stub_sec;
4143
4144   if (stub_sec == NULL)
4145     {
4146       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4147       if (stub_sec == NULL)
4148         {
4149           size_t namelen;
4150           bfd_size_type len;
4151           char *s_name;
4152
4153           namelen = strlen (link_sec->name);
4154           len = namelen + sizeof (STUB_SUFFIX);
4155           s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4156           if (s_name == NULL)
4157             return NULL;
4158
4159           memcpy (s_name, link_sec->name, namelen);
4160           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4161           stub_sec = (*htab->add_stub_section) (s_name, link_sec,
4162                                                 htab->nacl_p ? 4 : 3);
4163           if (stub_sec == NULL)
4164             return NULL;
4165           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4166         }
4167       htab->stub_group[section->id].stub_sec = stub_sec;
4168     }
4169
4170   if (link_sec_p)
4171     *link_sec_p = link_sec;
4172
4173   return stub_sec;
4174 }
4175
4176 /* Add a new stub entry to the stub hash.  Not all fields of the new
4177    stub entry are initialised.  */
4178
4179 static struct elf32_arm_stub_hash_entry *
4180 elf32_arm_add_stub (const char *stub_name,
4181                     asection *section,
4182                     struct elf32_arm_link_hash_table *htab)
4183 {
4184   asection *link_sec;
4185   asection *stub_sec;
4186   struct elf32_arm_stub_hash_entry *stub_entry;
4187
4188   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab);
4189   if (stub_sec == NULL)
4190     return NULL;
4191
4192   /* Enter this entry into the linker stub hash table.  */
4193   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4194                                      TRUE, FALSE);
4195   if (stub_entry == NULL)
4196     {
4197       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
4198                              section->owner,
4199                              stub_name);
4200       return NULL;
4201     }
4202
4203   stub_entry->stub_sec = stub_sec;
4204   stub_entry->stub_offset = 0;
4205   stub_entry->id_sec = link_sec;
4206
4207   return stub_entry;
4208 }
4209
4210 /* Store an Arm insn into an output section not processed by
4211    elf32_arm_write_section.  */
4212
4213 static void
4214 put_arm_insn (struct elf32_arm_link_hash_table * htab,
4215               bfd * output_bfd, bfd_vma val, void * ptr)
4216 {
4217   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4218     bfd_putl32 (val, ptr);
4219   else
4220     bfd_putb32 (val, ptr);
4221 }
4222
4223 /* Store a 16-bit Thumb insn into an output section not processed by
4224    elf32_arm_write_section.  */
4225
4226 static void
4227 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4228                 bfd * output_bfd, bfd_vma val, void * ptr)
4229 {
4230   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4231     bfd_putl16 (val, ptr);
4232   else
4233     bfd_putb16 (val, ptr);
4234 }
4235
4236 /* Store a Thumb2 insn into an output section not processed by
4237    elf32_arm_write_section.  */
4238
4239 static void
4240 put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4241                  bfd * output_bfd, bfd_vma val, void * ptr)
4242 {
4243   /* T2 instructions are 16-bit streamed.  */
4244   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4245     {
4246       bfd_putl16 ((val >> 16) & 0xffff, ptr);
4247       bfd_putl16 ((val & 0xffff), ptr + 2);
4248     }
4249   else
4250     {
4251       bfd_putb16 ((val >> 16) & 0xffff, ptr);
4252       bfd_putb16 ((val & 0xffff), ptr + 2);
4253     }
4254 }
4255
4256 /* If it's possible to change R_TYPE to a more efficient access
4257    model, return the new reloc type.  */
4258
4259 static unsigned
4260 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4261                           struct elf_link_hash_entry *h)
4262 {
4263   int is_local = (h == NULL);
4264
4265   if (bfd_link_pic (info)
4266       || (h && h->root.type == bfd_link_hash_undefweak))
4267     return r_type;
4268
4269   /* We do not support relaxations for Old TLS models.  */
4270   switch (r_type)
4271     {
4272     case R_ARM_TLS_GOTDESC:
4273     case R_ARM_TLS_CALL:
4274     case R_ARM_THM_TLS_CALL:
4275     case R_ARM_TLS_DESCSEQ:
4276     case R_ARM_THM_TLS_DESCSEQ:
4277       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4278     }
4279
4280   return r_type;
4281 }
4282
4283 static bfd_reloc_status_type elf32_arm_final_link_relocate
4284   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4285    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4286    const char *, unsigned char, enum arm_st_branch_type,
4287    struct elf_link_hash_entry *, bfd_boolean *, char **);
4288
4289 static unsigned int
4290 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4291 {
4292   switch (stub_type)
4293     {
4294     case arm_stub_a8_veneer_b_cond:
4295     case arm_stub_a8_veneer_b:
4296     case arm_stub_a8_veneer_bl:
4297       return 2;
4298
4299     case arm_stub_long_branch_any_any:
4300     case arm_stub_long_branch_v4t_arm_thumb:
4301     case arm_stub_long_branch_thumb_only:
4302     case arm_stub_long_branch_v4t_thumb_thumb:
4303     case arm_stub_long_branch_v4t_thumb_arm:
4304     case arm_stub_short_branch_v4t_thumb_arm:
4305     case arm_stub_long_branch_any_arm_pic:
4306     case arm_stub_long_branch_any_thumb_pic:
4307     case arm_stub_long_branch_v4t_thumb_thumb_pic:
4308     case arm_stub_long_branch_v4t_arm_thumb_pic:
4309     case arm_stub_long_branch_v4t_thumb_arm_pic:
4310     case arm_stub_long_branch_thumb_only_pic:
4311     case arm_stub_long_branch_any_tls_pic:
4312     case arm_stub_long_branch_v4t_thumb_tls_pic:
4313     case arm_stub_a8_veneer_blx:
4314       return 4;
4315
4316     case arm_stub_long_branch_arm_nacl:
4317     case arm_stub_long_branch_arm_nacl_pic:
4318       return 16;
4319
4320     default:
4321       abort ();  /* Should be unreachable.  */
4322     }
4323 }
4324
4325 static bfd_boolean
4326 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4327                     void * in_arg)
4328 {
4329 #define MAXRELOCS 3
4330   struct elf32_arm_stub_hash_entry *stub_entry;
4331   struct elf32_arm_link_hash_table *globals;
4332   struct bfd_link_info *info;
4333   asection *stub_sec;
4334   bfd *stub_bfd;
4335   bfd_byte *loc;
4336   bfd_vma sym_value;
4337   int template_size;
4338   int size;
4339   const insn_sequence *template_sequence;
4340   int i;
4341   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4342   int stub_reloc_offset[MAXRELOCS] = {0, 0};
4343   int nrelocs = 0;
4344
4345   /* Massage our args to the form they really have.  */
4346   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4347   info = (struct bfd_link_info *) in_arg;
4348
4349   globals = elf32_arm_hash_table (info);
4350   if (globals == NULL)
4351     return FALSE;
4352
4353   stub_sec = stub_entry->stub_sec;
4354
4355   if ((globals->fix_cortex_a8 < 0)
4356       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4357     /* We have to do less-strictly-aligned fixes last.  */
4358     return TRUE;
4359
4360   /* Make a note of the offset within the stubs for this entry.  */
4361   stub_entry->stub_offset = stub_sec->size;
4362   loc = stub_sec->contents + stub_entry->stub_offset;
4363
4364   stub_bfd = stub_sec->owner;
4365
4366   /* This is the address of the stub destination.  */
4367   sym_value = (stub_entry->target_value
4368                + stub_entry->target_section->output_offset
4369                + stub_entry->target_section->output_section->vma);
4370
4371   template_sequence = stub_entry->stub_template;
4372   template_size = stub_entry->stub_template_size;
4373
4374   size = 0;
4375   for (i = 0; i < template_size; i++)
4376     {
4377       switch (template_sequence[i].type)
4378         {
4379         case THUMB16_TYPE:
4380           {
4381             bfd_vma data = (bfd_vma) template_sequence[i].data;
4382             if (template_sequence[i].reloc_addend != 0)
4383               {
4384                 /* We've borrowed the reloc_addend field to mean we should
4385                    insert a condition code into this (Thumb-1 branch)
4386                    instruction.  See THUMB16_BCOND_INSN.  */
4387                 BFD_ASSERT ((data & 0xff00) == 0xd000);
4388                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
4389               }
4390             bfd_put_16 (stub_bfd, data, loc + size);
4391             size += 2;
4392           }
4393           break;
4394
4395         case THUMB32_TYPE:
4396           bfd_put_16 (stub_bfd,
4397                       (template_sequence[i].data >> 16) & 0xffff,
4398                       loc + size);
4399           bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
4400                       loc + size + 2);
4401           if (template_sequence[i].r_type != R_ARM_NONE)
4402             {
4403               stub_reloc_idx[nrelocs] = i;
4404               stub_reloc_offset[nrelocs++] = size;
4405             }
4406           size += 4;
4407           break;
4408
4409         case ARM_TYPE:
4410           bfd_put_32 (stub_bfd, template_sequence[i].data,
4411                       loc + size);
4412           /* Handle cases where the target is encoded within the
4413              instruction.  */
4414           if (template_sequence[i].r_type == R_ARM_JUMP24)
4415             {
4416               stub_reloc_idx[nrelocs] = i;
4417               stub_reloc_offset[nrelocs++] = size;
4418             }
4419           size += 4;
4420           break;
4421
4422         case DATA_TYPE:
4423           bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
4424           stub_reloc_idx[nrelocs] = i;
4425           stub_reloc_offset[nrelocs++] = size;
4426           size += 4;
4427           break;
4428
4429         default:
4430           BFD_FAIL ();
4431           return FALSE;
4432         }
4433     }
4434
4435   stub_sec->size += size;
4436
4437   /* Stub size has already been computed in arm_size_one_stub. Check
4438      consistency.  */
4439   BFD_ASSERT (size == stub_entry->stub_size);
4440
4441   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
4442   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
4443     sym_value |= 1;
4444
4445   /* Assume there is at least one and at most MAXRELOCS entries to relocate
4446      in each stub.  */
4447   BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
4448
4449   for (i = 0; i < nrelocs; i++)
4450     if (template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP24
4451         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP19
4452         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_CALL
4453         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_XPC22)
4454       {
4455         Elf_Internal_Rela rel;
4456         bfd_boolean unresolved_reloc;
4457         char *error_message;
4458         enum arm_st_branch_type branch_type
4459           = (template_sequence[stub_reloc_idx[i]].r_type != R_ARM_THM_XPC22
4460              ? ST_BRANCH_TO_THUMB : ST_BRANCH_TO_ARM);
4461         bfd_vma points_to = sym_value + stub_entry->target_addend;
4462
4463         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4464         rel.r_info = ELF32_R_INFO (0,
4465                                    template_sequence[stub_reloc_idx[i]].r_type);
4466         rel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
4467
4468         if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
4469           /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
4470              template should refer back to the instruction after the original
4471              branch.  */
4472           points_to = sym_value;
4473
4474         /* There may be unintended consequences if this is not true.  */
4475         BFD_ASSERT (stub_entry->h == NULL);
4476
4477         /* Note: _bfd_final_link_relocate doesn't handle these relocations
4478            properly.  We should probably use this function unconditionally,
4479            rather than only for certain relocations listed in the enclosing
4480            conditional, for the sake of consistency.  */
4481         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4482             (template_sequence[stub_reloc_idx[i]].r_type),
4483           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4484           points_to, info, stub_entry->target_section, "", STT_FUNC,
4485           branch_type, (struct elf_link_hash_entry *) stub_entry->h,
4486           &unresolved_reloc, &error_message);
4487       }
4488     else
4489       {
4490         Elf_Internal_Rela rel;
4491         bfd_boolean unresolved_reloc;
4492         char *error_message;
4493         bfd_vma points_to = sym_value + stub_entry->target_addend
4494           + template_sequence[stub_reloc_idx[i]].reloc_addend;
4495
4496         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4497         rel.r_info = ELF32_R_INFO (0,
4498                                    template_sequence[stub_reloc_idx[i]].r_type);
4499         rel.r_addend = 0;
4500
4501         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4502             (template_sequence[stub_reloc_idx[i]].r_type),
4503           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4504           points_to, info, stub_entry->target_section, "", STT_FUNC,
4505           stub_entry->branch_type,
4506           (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
4507           &error_message);
4508       }
4509
4510   return TRUE;
4511 #undef MAXRELOCS
4512 }
4513
4514 /* Calculate the template, template size and instruction size for a stub.
4515    Return value is the instruction size.  */
4516
4517 static unsigned int
4518 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
4519                              const insn_sequence **stub_template,
4520                              int *stub_template_size)
4521 {
4522   const insn_sequence *template_sequence = NULL;
4523   int template_size = 0, i;
4524   unsigned int size;
4525
4526   template_sequence = stub_definitions[stub_type].template_sequence;
4527   if (stub_template)
4528     *stub_template = template_sequence;
4529
4530   template_size = stub_definitions[stub_type].template_size;
4531   if (stub_template_size)
4532     *stub_template_size = template_size;
4533
4534   size = 0;
4535   for (i = 0; i < template_size; i++)
4536     {
4537       switch (template_sequence[i].type)
4538         {
4539         case THUMB16_TYPE:
4540           size += 2;
4541           break;
4542
4543         case ARM_TYPE:
4544         case THUMB32_TYPE:
4545         case DATA_TYPE:
4546           size += 4;
4547           break;
4548
4549         default:
4550           BFD_FAIL ();
4551           return 0;
4552         }
4553     }
4554
4555   return size;
4556 }
4557
4558 /* As above, but don't actually build the stub.  Just bump offset so
4559    we know stub section sizes.  */
4560
4561 static bfd_boolean
4562 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
4563                    void *in_arg ATTRIBUTE_UNUSED)
4564 {
4565   struct elf32_arm_stub_hash_entry *stub_entry;
4566   const insn_sequence *template_sequence;
4567   int template_size, size;
4568
4569   /* Massage our args to the form they really have.  */
4570   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4571
4572   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
4573              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
4574
4575   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
4576                                       &template_size);
4577
4578   stub_entry->stub_size = size;
4579   stub_entry->stub_template = template_sequence;
4580   stub_entry->stub_template_size = template_size;
4581
4582   size = (size + 7) & ~7;
4583   stub_entry->stub_sec->size += size;
4584
4585   return TRUE;
4586 }
4587
4588 /* External entry points for sizing and building linker stubs.  */
4589
4590 /* Set up various things so that we can make a list of input sections
4591    for each output section included in the link.  Returns -1 on error,
4592    0 when no stubs will be needed, and 1 on success.  */
4593
4594 int
4595 elf32_arm_setup_section_lists (bfd *output_bfd,
4596                                struct bfd_link_info *info)
4597 {
4598   bfd *input_bfd;
4599   unsigned int bfd_count;
4600   unsigned int top_id, top_index;
4601   asection *section;
4602   asection **input_list, **list;
4603   bfd_size_type amt;
4604   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4605
4606   if (htab == NULL)
4607     return 0;
4608   if (! is_elf_hash_table (htab))
4609     return 0;
4610
4611   /* Count the number of input BFDs and find the top input section id.  */
4612   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
4613        input_bfd != NULL;
4614        input_bfd = input_bfd->link.next)
4615     {
4616       bfd_count += 1;
4617       for (section = input_bfd->sections;
4618            section != NULL;
4619            section = section->next)
4620         {
4621           if (top_id < section->id)
4622             top_id = section->id;
4623         }
4624     }
4625   htab->bfd_count = bfd_count;
4626
4627   amt = sizeof (struct map_stub) * (top_id + 1);
4628   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4629   if (htab->stub_group == NULL)
4630     return -1;
4631   htab->top_id = top_id;
4632
4633   /* We can't use output_bfd->section_count here to find the top output
4634      section index as some sections may have been removed, and
4635      _bfd_strip_section_from_output doesn't renumber the indices.  */
4636   for (section = output_bfd->sections, top_index = 0;
4637        section != NULL;
4638        section = section->next)
4639     {
4640       if (top_index < section->index)
4641         top_index = section->index;
4642     }
4643
4644   htab->top_index = top_index;
4645   amt = sizeof (asection *) * (top_index + 1);
4646   input_list = (asection **) bfd_malloc (amt);
4647   htab->input_list = input_list;
4648   if (input_list == NULL)
4649     return -1;
4650
4651   /* For sections we aren't interested in, mark their entries with a
4652      value we can check later.  */
4653   list = input_list + top_index;
4654   do
4655     *list = bfd_abs_section_ptr;
4656   while (list-- != input_list);
4657
4658   for (section = output_bfd->sections;
4659        section != NULL;
4660        section = section->next)
4661     {
4662       if ((section->flags & SEC_CODE) != 0)
4663         input_list[section->index] = NULL;
4664     }
4665
4666   return 1;
4667 }
4668
4669 /* The linker repeatedly calls this function for each input section,
4670    in the order that input sections are linked into output sections.
4671    Build lists of input sections to determine groupings between which
4672    we may insert linker stubs.  */
4673
4674 void
4675 elf32_arm_next_input_section (struct bfd_link_info *info,
4676                               asection *isec)
4677 {
4678   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4679
4680   if (htab == NULL)
4681     return;
4682
4683   if (isec->output_section->index <= htab->top_index)
4684     {
4685       asection **list = htab->input_list + isec->output_section->index;
4686
4687       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
4688         {
4689           /* Steal the link_sec pointer for our list.  */
4690 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4691           /* This happens to make the list in reverse order,
4692              which we reverse later.  */
4693           PREV_SEC (isec) = *list;
4694           *list = isec;
4695         }
4696     }
4697 }
4698
4699 /* See whether we can group stub sections together.  Grouping stub
4700    sections may result in fewer stubs.  More importantly, we need to
4701    put all .init* and .fini* stubs at the end of the .init or
4702    .fini output sections respectively, because glibc splits the
4703    _init and _fini functions into multiple parts.  Putting a stub in
4704    the middle of a function is not a good idea.  */
4705
4706 static void
4707 group_sections (struct elf32_arm_link_hash_table *htab,
4708                 bfd_size_type stub_group_size,
4709                 bfd_boolean stubs_always_after_branch)
4710 {
4711   asection **list = htab->input_list;
4712
4713   do
4714     {
4715       asection *tail = *list;
4716       asection *head;
4717
4718       if (tail == bfd_abs_section_ptr)
4719         continue;
4720
4721       /* Reverse the list: we must avoid placing stubs at the
4722          beginning of the section because the beginning of the text
4723          section may be required for an interrupt vector in bare metal
4724          code.  */
4725 #define NEXT_SEC PREV_SEC
4726       head = NULL;
4727       while (tail != NULL)
4728         {
4729           /* Pop from tail.  */
4730           asection *item = tail;
4731           tail = PREV_SEC (item);
4732
4733           /* Push on head.  */
4734           NEXT_SEC (item) = head;
4735           head = item;
4736         }
4737
4738       while (head != NULL)
4739         {
4740           asection *curr;
4741           asection *next;
4742           bfd_vma stub_group_start = head->output_offset;
4743           bfd_vma end_of_next;
4744
4745           curr = head;
4746           while (NEXT_SEC (curr) != NULL)
4747             {
4748               next = NEXT_SEC (curr);
4749               end_of_next = next->output_offset + next->size;
4750               if (end_of_next - stub_group_start >= stub_group_size)
4751                 /* End of NEXT is too far from start, so stop.  */
4752                 break;
4753               /* Add NEXT to the group.  */
4754               curr = next;
4755             }
4756
4757           /* OK, the size from the start to the start of CURR is less
4758              than stub_group_size and thus can be handled by one stub
4759              section.  (Or the head section is itself larger than
4760              stub_group_size, in which case we may be toast.)
4761              We should really be keeping track of the total size of
4762              stubs added here, as stubs contribute to the final output
4763              section size.  */
4764           do
4765             {
4766               next = NEXT_SEC (head);
4767               /* Set up this stub group.  */
4768               htab->stub_group[head->id].link_sec = curr;
4769             }
4770           while (head != curr && (head = next) != NULL);
4771
4772           /* But wait, there's more!  Input sections up to stub_group_size
4773              bytes after the stub section can be handled by it too.  */
4774           if (!stubs_always_after_branch)
4775             {
4776               stub_group_start = curr->output_offset + curr->size;
4777
4778               while (next != NULL)
4779                 {
4780                   end_of_next = next->output_offset + next->size;
4781                   if (end_of_next - stub_group_start >= stub_group_size)
4782                     /* End of NEXT is too far from stubs, so stop.  */
4783                     break;
4784                   /* Add NEXT to the stub group.  */
4785                   head = next;
4786                   next = NEXT_SEC (head);
4787                   htab->stub_group[head->id].link_sec = curr;
4788                 }
4789             }
4790           head = next;
4791         }
4792     }
4793   while (list++ != htab->input_list + htab->top_index);
4794
4795   free (htab->input_list);
4796 #undef PREV_SEC
4797 #undef NEXT_SEC
4798 }
4799
4800 /* Comparison function for sorting/searching relocations relating to Cortex-A8
4801    erratum fix.  */
4802
4803 static int
4804 a8_reloc_compare (const void *a, const void *b)
4805 {
4806   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
4807   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
4808
4809   if (ra->from < rb->from)
4810     return -1;
4811   else if (ra->from > rb->from)
4812     return 1;
4813   else
4814     return 0;
4815 }
4816
4817 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
4818                                                     const char *, char **);
4819
4820 /* Helper function to scan code for sequences which might trigger the Cortex-A8
4821    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
4822    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
4823    otherwise.  */
4824
4825 static bfd_boolean
4826 cortex_a8_erratum_scan (bfd *input_bfd,
4827                         struct bfd_link_info *info,
4828                         struct a8_erratum_fix **a8_fixes_p,
4829                         unsigned int *num_a8_fixes_p,
4830                         unsigned int *a8_fix_table_size_p,
4831                         struct a8_erratum_reloc *a8_relocs,
4832                         unsigned int num_a8_relocs,
4833                         unsigned prev_num_a8_fixes,
4834                         bfd_boolean *stub_changed_p)
4835 {
4836   asection *section;
4837   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4838   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
4839   unsigned int num_a8_fixes = *num_a8_fixes_p;
4840   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
4841
4842   if (htab == NULL)
4843     return FALSE;
4844
4845   for (section = input_bfd->sections;
4846        section != NULL;
4847        section = section->next)
4848     {
4849       bfd_byte *contents = NULL;
4850       struct _arm_elf_section_data *sec_data;
4851       unsigned int span;
4852       bfd_vma base_vma;
4853
4854       if (elf_section_type (section) != SHT_PROGBITS
4855           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4856           || (section->flags & SEC_EXCLUDE) != 0
4857           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4858           || (section->output_section == bfd_abs_section_ptr))
4859         continue;
4860
4861       base_vma = section->output_section->vma + section->output_offset;
4862
4863       if (elf_section_data (section)->this_hdr.contents != NULL)
4864         contents = elf_section_data (section)->this_hdr.contents;
4865       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4866         return TRUE;
4867
4868       sec_data = elf32_arm_section_data (section);
4869
4870       for (span = 0; span < sec_data->mapcount; span++)
4871         {
4872           unsigned int span_start = sec_data->map[span].vma;
4873           unsigned int span_end = (span == sec_data->mapcount - 1)
4874             ? section->size : sec_data->map[span + 1].vma;
4875           unsigned int i;
4876           char span_type = sec_data->map[span].type;
4877           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
4878
4879           if (span_type != 't')
4880             continue;
4881
4882           /* Span is entirely within a single 4KB region: skip scanning.  */
4883           if (((base_vma + span_start) & ~0xfff)
4884               == ((base_vma + span_end) & ~0xfff))
4885             continue;
4886
4887           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
4888
4889                * The opcode is BLX.W, BL.W, B.W, Bcc.W
4890                * The branch target is in the same 4KB region as the
4891                  first half of the branch.
4892                * The instruction before the branch is a 32-bit
4893                  length non-branch instruction.  */
4894           for (i = span_start; i < span_end;)
4895             {
4896               unsigned int insn = bfd_getl16 (&contents[i]);
4897               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
4898               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
4899
4900               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
4901                 insn_32bit = TRUE;
4902
4903               if (insn_32bit)
4904                 {
4905                   /* Load the rest of the insn (in manual-friendly order).  */
4906                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
4907
4908                   /* Encoding T4: B<c>.W.  */
4909                   is_b = (insn & 0xf800d000) == 0xf0009000;
4910                   /* Encoding T1: BL<c>.W.  */
4911                   is_bl = (insn & 0xf800d000) == 0xf000d000;
4912                   /* Encoding T2: BLX<c>.W.  */
4913                   is_blx = (insn & 0xf800d000) == 0xf000c000;
4914                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
4915                   is_bcc = (insn & 0xf800d000) == 0xf0008000
4916                            && (insn & 0x07f00000) != 0x03800000;
4917                 }
4918
4919               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
4920
4921               if (((base_vma + i) & 0xfff) == 0xffe
4922                   && insn_32bit
4923                   && is_32bit_branch
4924                   && last_was_32bit
4925                   && ! last_was_branch)
4926                 {
4927                   bfd_signed_vma offset = 0;
4928                   bfd_boolean force_target_arm = FALSE;
4929                   bfd_boolean force_target_thumb = FALSE;
4930                   bfd_vma target;
4931                   enum elf32_arm_stub_type stub_type = arm_stub_none;
4932                   struct a8_erratum_reloc key, *found;
4933                   bfd_boolean use_plt = FALSE;
4934
4935                   key.from = base_vma + i;
4936                   found = (struct a8_erratum_reloc *)
4937                       bsearch (&key, a8_relocs, num_a8_relocs,
4938                                sizeof (struct a8_erratum_reloc),
4939                                &a8_reloc_compare);
4940
4941                   if (found)
4942                     {
4943                       char *error_message = NULL;
4944                       struct elf_link_hash_entry *entry;
4945
4946                       /* We don't care about the error returned from this
4947                          function, only if there is glue or not.  */
4948                       entry = find_thumb_glue (info, found->sym_name,
4949                                                &error_message);
4950
4951                       if (entry)
4952                         found->non_a8_stub = TRUE;
4953
4954                       /* Keep a simpler condition, for the sake of clarity.  */
4955                       if (htab->root.splt != NULL && found->hash != NULL
4956                           && found->hash->root.plt.offset != (bfd_vma) -1)
4957                         use_plt = TRUE;
4958
4959                       if (found->r_type == R_ARM_THM_CALL)
4960                         {
4961                           if (found->branch_type == ST_BRANCH_TO_ARM
4962                               || use_plt)
4963                             force_target_arm = TRUE;
4964                           else
4965                             force_target_thumb = TRUE;
4966                         }
4967                     }
4968
4969                   /* Check if we have an offending branch instruction.  */
4970
4971                   if (found && found->non_a8_stub)
4972                     /* We've already made a stub for this instruction, e.g.
4973                        it's a long branch or a Thumb->ARM stub.  Assume that
4974                        stub will suffice to work around the A8 erratum (see
4975                        setting of always_after_branch above).  */
4976                     ;
4977                   else if (is_bcc)
4978                     {
4979                       offset = (insn & 0x7ff) << 1;
4980                       offset |= (insn & 0x3f0000) >> 4;
4981                       offset |= (insn & 0x2000) ? 0x40000 : 0;
4982                       offset |= (insn & 0x800) ? 0x80000 : 0;
4983                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
4984                       if (offset & 0x100000)
4985                         offset |= ~ ((bfd_signed_vma) 0xfffff);
4986                       stub_type = arm_stub_a8_veneer_b_cond;
4987                     }
4988                   else if (is_b || is_bl || is_blx)
4989                     {
4990                       int s = (insn & 0x4000000) != 0;
4991                       int j1 = (insn & 0x2000) != 0;
4992                       int j2 = (insn & 0x800) != 0;
4993                       int i1 = !(j1 ^ s);
4994                       int i2 = !(j2 ^ s);
4995
4996                       offset = (insn & 0x7ff) << 1;
4997                       offset |= (insn & 0x3ff0000) >> 4;
4998                       offset |= i2 << 22;
4999                       offset |= i1 << 23;
5000                       offset |= s << 24;
5001                       if (offset & 0x1000000)
5002                         offset |= ~ ((bfd_signed_vma) 0xffffff);
5003
5004                       if (is_blx)
5005                         offset &= ~ ((bfd_signed_vma) 3);
5006
5007                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
5008                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5009                     }
5010
5011                   if (stub_type != arm_stub_none)
5012                     {
5013                       bfd_vma pc_for_insn = base_vma + i + 4;
5014
5015                       /* The original instruction is a BL, but the target is
5016                          an ARM instruction.  If we were not making a stub,
5017                          the BL would have been converted to a BLX.  Use the
5018                          BLX stub instead in that case.  */
5019                       if (htab->use_blx && force_target_arm
5020                           && stub_type == arm_stub_a8_veneer_bl)
5021                         {
5022                           stub_type = arm_stub_a8_veneer_blx;
5023                           is_blx = TRUE;
5024                           is_bl = FALSE;
5025                         }
5026                       /* Conversely, if the original instruction was
5027                          BLX but the target is Thumb mode, use the BL
5028                          stub.  */
5029                       else if (force_target_thumb
5030                                && stub_type == arm_stub_a8_veneer_blx)
5031                         {
5032                           stub_type = arm_stub_a8_veneer_bl;
5033                           is_blx = FALSE;
5034                           is_bl = TRUE;
5035                         }
5036
5037                       if (is_blx)
5038                         pc_for_insn &= ~ ((bfd_vma) 3);
5039
5040                       /* If we found a relocation, use the proper destination,
5041                          not the offset in the (unrelocated) instruction.
5042                          Note this is always done if we switched the stub type
5043                          above.  */
5044                       if (found)
5045                         offset =
5046                           (bfd_signed_vma) (found->destination - pc_for_insn);
5047
5048                       /* If the stub will use a Thumb-mode branch to a
5049                          PLT target, redirect it to the preceding Thumb
5050                          entry point.  */
5051                       if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5052                         offset -= PLT_THUMB_STUB_SIZE;
5053
5054                       target = pc_for_insn + offset;
5055
5056                       /* The BLX stub is ARM-mode code.  Adjust the offset to
5057                          take the different PC value (+8 instead of +4) into
5058                          account.  */
5059                       if (stub_type == arm_stub_a8_veneer_blx)
5060                         offset += 4;
5061
5062                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5063                         {
5064                           char *stub_name = NULL;
5065
5066                           if (num_a8_fixes == a8_fix_table_size)
5067                             {
5068                               a8_fix_table_size *= 2;
5069                               a8_fixes = (struct a8_erratum_fix *)
5070                                   bfd_realloc (a8_fixes,
5071                                                sizeof (struct a8_erratum_fix)
5072                                                * a8_fix_table_size);
5073                             }
5074
5075                           if (num_a8_fixes < prev_num_a8_fixes)
5076                             {
5077                               /* If we're doing a subsequent scan,
5078                                  check if we've found the same fix as
5079                                  before, and try and reuse the stub
5080                                  name.  */
5081                               stub_name = a8_fixes[num_a8_fixes].stub_name;
5082                               if ((a8_fixes[num_a8_fixes].section != section)
5083                                   || (a8_fixes[num_a8_fixes].offset != i))
5084                                 {
5085                                   free (stub_name);
5086                                   stub_name = NULL;
5087                                   *stub_changed_p = TRUE;
5088                                 }
5089                             }
5090
5091                           if (!stub_name)
5092                             {
5093                               stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5094                               if (stub_name != NULL)
5095                                 sprintf (stub_name, "%x:%x", section->id, i);
5096                             }
5097
5098                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5099                           a8_fixes[num_a8_fixes].section = section;
5100                           a8_fixes[num_a8_fixes].offset = i;
5101                           a8_fixes[num_a8_fixes].addend = offset;
5102                           a8_fixes[num_a8_fixes].orig_insn = insn;
5103                           a8_fixes[num_a8_fixes].stub_name = stub_name;
5104                           a8_fixes[num_a8_fixes].stub_type = stub_type;
5105                           a8_fixes[num_a8_fixes].branch_type =
5106                             is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5107
5108                           num_a8_fixes++;
5109                         }
5110                     }
5111                 }
5112
5113               i += insn_32bit ? 4 : 2;
5114               last_was_32bit = insn_32bit;
5115               last_was_branch = is_32bit_branch;
5116             }
5117         }
5118
5119       if (elf_section_data (section)->this_hdr.contents == NULL)
5120         free (contents);
5121     }
5122
5123   *a8_fixes_p = a8_fixes;
5124   *num_a8_fixes_p = num_a8_fixes;
5125   *a8_fix_table_size_p = a8_fix_table_size;
5126
5127   return FALSE;
5128 }
5129
5130 /* Determine and set the size of the stub section for a final link.
5131
5132    The basic idea here is to examine all the relocations looking for
5133    PC-relative calls to a target that is unreachable with a "bl"
5134    instruction.  */
5135
5136 bfd_boolean
5137 elf32_arm_size_stubs (bfd *output_bfd,
5138                       bfd *stub_bfd,
5139                       struct bfd_link_info *info,
5140                       bfd_signed_vma group_size,
5141                       asection * (*add_stub_section) (const char *, asection *,
5142                                                       unsigned int),
5143                       void (*layout_sections_again) (void))
5144 {
5145   bfd_size_type stub_group_size;
5146   bfd_boolean stubs_always_after_branch;
5147   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5148   struct a8_erratum_fix *a8_fixes = NULL;
5149   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
5150   struct a8_erratum_reloc *a8_relocs = NULL;
5151   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
5152
5153   if (htab == NULL)
5154     return FALSE;
5155
5156   if (htab->fix_cortex_a8)
5157     {
5158       a8_fixes = (struct a8_erratum_fix *)
5159           bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
5160       a8_relocs = (struct a8_erratum_reloc *)
5161           bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
5162     }
5163
5164   /* Propagate mach to stub bfd, because it may not have been
5165      finalized when we created stub_bfd.  */
5166   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
5167                      bfd_get_mach (output_bfd));
5168
5169   /* Stash our params away.  */
5170   htab->stub_bfd = stub_bfd;
5171   htab->add_stub_section = add_stub_section;
5172   htab->layout_sections_again = layout_sections_again;
5173   stubs_always_after_branch = group_size < 0;
5174
5175   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
5176      as the first half of a 32-bit branch straddling two 4K pages.  This is a
5177      crude way of enforcing that.  */
5178   if (htab->fix_cortex_a8)
5179     stubs_always_after_branch = 1;
5180
5181   if (group_size < 0)
5182     stub_group_size = -group_size;
5183   else
5184     stub_group_size = group_size;
5185
5186   if (stub_group_size == 1)
5187     {
5188       /* Default values.  */
5189       /* Thumb branch range is +-4MB has to be used as the default
5190          maximum size (a given section can contain both ARM and Thumb
5191          code, so the worst case has to be taken into account).
5192
5193          This value is 24K less than that, which allows for 2025
5194          12-byte stubs.  If we exceed that, then we will fail to link.
5195          The user will have to relink with an explicit group size
5196          option.  */
5197       stub_group_size = 4170000;
5198     }
5199
5200   group_sections (htab, stub_group_size, stubs_always_after_branch);
5201
5202   /* If we're applying the cortex A8 fix, we need to determine the
5203      program header size now, because we cannot change it later --
5204      that could alter section placements.  Notice the A8 erratum fix
5205      ends up requiring the section addresses to remain unchanged
5206      modulo the page size.  That's something we cannot represent
5207      inside BFD, and we don't want to force the section alignment to
5208      be the page size.  */
5209   if (htab->fix_cortex_a8)
5210     (*htab->layout_sections_again) ();
5211
5212   while (1)
5213     {
5214       bfd *input_bfd;
5215       unsigned int bfd_indx;
5216       asection *stub_sec;
5217       bfd_boolean stub_changed = FALSE;
5218       unsigned prev_num_a8_fixes = num_a8_fixes;
5219
5220       num_a8_fixes = 0;
5221       for (input_bfd = info->input_bfds, bfd_indx = 0;
5222            input_bfd != NULL;
5223            input_bfd = input_bfd->link.next, bfd_indx++)
5224         {
5225           Elf_Internal_Shdr *symtab_hdr;
5226           asection *section;
5227           Elf_Internal_Sym *local_syms = NULL;
5228
5229           if (!is_arm_elf (input_bfd))
5230             continue;
5231
5232           num_a8_relocs = 0;
5233
5234           /* We'll need the symbol table in a second.  */
5235           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5236           if (symtab_hdr->sh_info == 0)
5237             continue;
5238
5239           /* Walk over each section attached to the input bfd.  */
5240           for (section = input_bfd->sections;
5241                section != NULL;
5242                section = section->next)
5243             {
5244               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5245
5246               /* If there aren't any relocs, then there's nothing more
5247                  to do.  */
5248               if ((section->flags & SEC_RELOC) == 0
5249                   || section->reloc_count == 0
5250                   || (section->flags & SEC_CODE) == 0)
5251                 continue;
5252
5253               /* If this section is a link-once section that will be
5254                  discarded, then don't create any stubs.  */
5255               if (section->output_section == NULL
5256                   || section->output_section->owner != output_bfd)
5257                 continue;
5258
5259               /* Get the relocs.  */
5260               internal_relocs
5261                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
5262                                              NULL, info->keep_memory);
5263               if (internal_relocs == NULL)
5264                 goto error_ret_free_local;
5265
5266               /* Now examine each relocation.  */
5267               irela = internal_relocs;
5268               irelaend = irela + section->reloc_count;
5269               for (; irela < irelaend; irela++)
5270                 {
5271                   unsigned int r_type, r_indx;
5272                   enum elf32_arm_stub_type stub_type;
5273                   struct elf32_arm_stub_hash_entry *stub_entry;
5274                   asection *sym_sec;
5275                   bfd_vma sym_value;
5276                   bfd_vma destination;
5277                   struct elf32_arm_link_hash_entry *hash;
5278                   const char *sym_name;
5279                   char *stub_name;
5280                   const asection *id_sec;
5281                   unsigned char st_type;
5282                   enum arm_st_branch_type branch_type;
5283                   bfd_boolean created_stub = FALSE;
5284
5285                   r_type = ELF32_R_TYPE (irela->r_info);
5286                   r_indx = ELF32_R_SYM (irela->r_info);
5287
5288                   if (r_type >= (unsigned int) R_ARM_max)
5289                     {
5290                       bfd_set_error (bfd_error_bad_value);
5291                     error_ret_free_internal:
5292                       if (elf_section_data (section)->relocs == NULL)
5293                         free (internal_relocs);
5294                       goto error_ret_free_local;
5295                     }
5296
5297                   hash = NULL;
5298                   if (r_indx >= symtab_hdr->sh_info)
5299                     hash = elf32_arm_hash_entry
5300                       (elf_sym_hashes (input_bfd)
5301                        [r_indx - symtab_hdr->sh_info]);
5302
5303                   /* Only look for stubs on branch instructions, or
5304                      non-relaxed TLSCALL  */
5305                   if ((r_type != (unsigned int) R_ARM_CALL)
5306                       && (r_type != (unsigned int) R_ARM_THM_CALL)
5307                       && (r_type != (unsigned int) R_ARM_JUMP24)
5308                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
5309                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
5310                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
5311                       && (r_type != (unsigned int) R_ARM_PLT32)
5312                       && !((r_type == (unsigned int) R_ARM_TLS_CALL
5313                             || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5314                            && r_type == elf32_arm_tls_transition
5315                                (info, r_type, &hash->root)
5316                            && ((hash ? hash->tls_type
5317                                 : (elf32_arm_local_got_tls_type
5318                                    (input_bfd)[r_indx]))
5319                                & GOT_TLS_GDESC) != 0))
5320                     continue;
5321
5322                   /* Now determine the call target, its name, value,
5323                      section.  */
5324                   sym_sec = NULL;
5325                   sym_value = 0;
5326                   destination = 0;
5327                   sym_name = NULL;
5328
5329                   if (r_type == (unsigned int) R_ARM_TLS_CALL
5330                       || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5331                     {
5332                       /* A non-relaxed TLS call.  The target is the
5333                          plt-resident trampoline and nothing to do
5334                          with the symbol.  */
5335                       BFD_ASSERT (htab->tls_trampoline > 0);
5336                       sym_sec = htab->root.splt;
5337                       sym_value = htab->tls_trampoline;
5338                       hash = 0;
5339                       st_type = STT_FUNC;
5340                       branch_type = ST_BRANCH_TO_ARM;
5341                     }
5342                   else if (!hash)
5343                     {
5344                       /* It's a local symbol.  */
5345                       Elf_Internal_Sym *sym;
5346
5347                       if (local_syms == NULL)
5348                         {
5349                           local_syms
5350                             = (Elf_Internal_Sym *) symtab_hdr->contents;
5351                           if (local_syms == NULL)
5352                             local_syms
5353                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5354                                                       symtab_hdr->sh_info, 0,
5355                                                       NULL, NULL, NULL);
5356                           if (local_syms == NULL)
5357                             goto error_ret_free_internal;
5358                         }
5359
5360                       sym = local_syms + r_indx;
5361                       if (sym->st_shndx == SHN_UNDEF)
5362                         sym_sec = bfd_und_section_ptr;
5363                       else if (sym->st_shndx == SHN_ABS)
5364                         sym_sec = bfd_abs_section_ptr;
5365                       else if (sym->st_shndx == SHN_COMMON)
5366                         sym_sec = bfd_com_section_ptr;
5367                       else
5368                         sym_sec =
5369                           bfd_section_from_elf_index (input_bfd, sym->st_shndx);
5370
5371                       if (!sym_sec)
5372                         /* This is an undefined symbol.  It can never
5373                            be resolved.  */
5374                         continue;
5375
5376                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5377                         sym_value = sym->st_value;
5378                       destination = (sym_value + irela->r_addend
5379                                      + sym_sec->output_offset
5380                                      + sym_sec->output_section->vma);
5381                       st_type = ELF_ST_TYPE (sym->st_info);
5382                       branch_type = ARM_SYM_BRANCH_TYPE (sym);
5383                       sym_name
5384                         = bfd_elf_string_from_elf_section (input_bfd,
5385                                                            symtab_hdr->sh_link,
5386                                                            sym->st_name);
5387                     }
5388                   else
5389                     {
5390                       /* It's an external symbol.  */
5391                       while (hash->root.root.type == bfd_link_hash_indirect
5392                              || hash->root.root.type == bfd_link_hash_warning)
5393                         hash = ((struct elf32_arm_link_hash_entry *)
5394                                 hash->root.root.u.i.link);
5395
5396                       if (hash->root.root.type == bfd_link_hash_defined
5397                           || hash->root.root.type == bfd_link_hash_defweak)
5398                         {
5399                           sym_sec = hash->root.root.u.def.section;
5400                           sym_value = hash->root.root.u.def.value;
5401
5402                           struct elf32_arm_link_hash_table *globals =
5403                                                   elf32_arm_hash_table (info);
5404
5405                           /* For a destination in a shared library,
5406                              use the PLT stub as target address to
5407                              decide whether a branch stub is
5408                              needed.  */
5409                           if (globals != NULL
5410                               && globals->root.splt != NULL
5411                               && hash != NULL
5412                               && hash->root.plt.offset != (bfd_vma) -1)
5413                             {
5414                               sym_sec = globals->root.splt;
5415                               sym_value = hash->root.plt.offset;
5416                               if (sym_sec->output_section != NULL)
5417                                 destination = (sym_value
5418                                                + sym_sec->output_offset
5419                                                + sym_sec->output_section->vma);
5420                             }
5421                           else if (sym_sec->output_section != NULL)
5422                             destination = (sym_value + irela->r_addend
5423                                            + sym_sec->output_offset
5424                                            + sym_sec->output_section->vma);
5425                         }
5426                       else if ((hash->root.root.type == bfd_link_hash_undefined)
5427                                || (hash->root.root.type == bfd_link_hash_undefweak))
5428                         {
5429                           /* For a shared library, use the PLT stub as
5430                              target address to decide whether a long
5431                              branch stub is needed.
5432                              For absolute code, they cannot be handled.  */
5433                           struct elf32_arm_link_hash_table *globals =
5434                             elf32_arm_hash_table (info);
5435
5436                           if (globals != NULL
5437                               && globals->root.splt != NULL
5438                               && hash != NULL
5439                               && hash->root.plt.offset != (bfd_vma) -1)
5440                             {
5441                               sym_sec = globals->root.splt;
5442                               sym_value = hash->root.plt.offset;
5443                               if (sym_sec->output_section != NULL)
5444                                 destination = (sym_value
5445                                                + sym_sec->output_offset
5446                                                + sym_sec->output_section->vma);
5447                             }
5448                           else
5449                             continue;
5450                         }
5451                       else
5452                         {
5453                           bfd_set_error (bfd_error_bad_value);
5454                           goto error_ret_free_internal;
5455                         }
5456                       st_type = hash->root.type;
5457                       branch_type = hash->root.target_internal;
5458                       sym_name = hash->root.root.root.string;
5459                     }
5460
5461                   do
5462                     {
5463                       /* Determine what (if any) linker stub is needed.  */
5464                       stub_type = arm_type_of_stub (info, section, irela,
5465                                                     st_type, &branch_type,
5466                                                     hash, destination, sym_sec,
5467                                                     input_bfd, sym_name);
5468                       if (stub_type == arm_stub_none)
5469                         break;
5470
5471                       /* Support for grouping stub sections.  */
5472                       id_sec = htab->stub_group[section->id].link_sec;
5473
5474                       /* Get the name of this stub.  */
5475                       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash,
5476                                                        irela, stub_type);
5477                       if (!stub_name)
5478                         goto error_ret_free_internal;
5479
5480                       /* We've either created a stub for this reloc already,
5481                          or we are about to.  */
5482                       created_stub = TRUE;
5483
5484                       stub_entry = arm_stub_hash_lookup
5485                                      (&htab->stub_hash_table, stub_name,
5486                                       FALSE, FALSE);
5487                       if (stub_entry != NULL)
5488                         {
5489                           /* The proper stub has already been created.  */
5490                           free (stub_name);
5491                           stub_entry->target_value = sym_value;
5492                           break;
5493                         }
5494
5495                       stub_entry = elf32_arm_add_stub (stub_name, section,
5496                                                        htab);
5497                       if (stub_entry == NULL)
5498                         {
5499                           free (stub_name);
5500                           goto error_ret_free_internal;
5501                         }
5502
5503                       stub_entry->target_value = sym_value;
5504                       stub_entry->target_section = sym_sec;
5505                       stub_entry->stub_type = stub_type;
5506                       stub_entry->h = hash;
5507                       stub_entry->branch_type = branch_type;
5508
5509                       if (sym_name == NULL)
5510                         sym_name = "unnamed";
5511                       stub_entry->output_name = (char *)
5512                           bfd_alloc (htab->stub_bfd,
5513                                      sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5514                                      + strlen (sym_name));
5515                       if (stub_entry->output_name == NULL)
5516                         {
5517                           free (stub_name);
5518                           goto error_ret_free_internal;
5519                         }
5520
5521                       /* For historical reasons, use the existing names for
5522                          ARM-to-Thumb and Thumb-to-ARM stubs.  */
5523                       if ((r_type == (unsigned int) R_ARM_THM_CALL
5524                            || r_type == (unsigned int) R_ARM_THM_JUMP24
5525                            || r_type == (unsigned int) R_ARM_THM_JUMP19)
5526                           && branch_type == ST_BRANCH_TO_ARM)
5527                         sprintf (stub_entry->output_name,
5528                                  THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5529                       else if ((r_type == (unsigned int) R_ARM_CALL
5530                                || r_type == (unsigned int) R_ARM_JUMP24)
5531                                && branch_type == ST_BRANCH_TO_THUMB)
5532                         sprintf (stub_entry->output_name,
5533                                  ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5534                       else
5535                         sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
5536                                  sym_name);
5537
5538                       stub_changed = TRUE;
5539                     }
5540                   while (0);
5541
5542                   /* Look for relocations which might trigger Cortex-A8
5543                      erratum.  */
5544                   if (htab->fix_cortex_a8
5545                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
5546                           || r_type == (unsigned int) R_ARM_THM_JUMP19
5547                           || r_type == (unsigned int) R_ARM_THM_CALL
5548                           || r_type == (unsigned int) R_ARM_THM_XPC22))
5549                     {
5550                       bfd_vma from = section->output_section->vma
5551                                      + section->output_offset
5552                                      + irela->r_offset;
5553
5554                       if ((from & 0xfff) == 0xffe)
5555                         {
5556                           /* Found a candidate.  Note we haven't checked the
5557                              destination is within 4K here: if we do so (and
5558                              don't create an entry in a8_relocs) we can't tell
5559                              that a branch should have been relocated when
5560                              scanning later.  */
5561                           if (num_a8_relocs == a8_reloc_table_size)
5562                             {
5563                               a8_reloc_table_size *= 2;
5564                               a8_relocs = (struct a8_erratum_reloc *)
5565                                   bfd_realloc (a8_relocs,
5566                                                sizeof (struct a8_erratum_reloc)
5567                                                * a8_reloc_table_size);
5568                             }
5569
5570                           a8_relocs[num_a8_relocs].from = from;
5571                           a8_relocs[num_a8_relocs].destination = destination;
5572                           a8_relocs[num_a8_relocs].r_type = r_type;
5573                           a8_relocs[num_a8_relocs].branch_type = branch_type;
5574                           a8_relocs[num_a8_relocs].sym_name = sym_name;
5575                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
5576                           a8_relocs[num_a8_relocs].hash = hash;
5577
5578                           num_a8_relocs++;
5579                         }
5580                     }
5581                 }
5582
5583               /* We're done with the internal relocs, free them.  */
5584               if (elf_section_data (section)->relocs == NULL)
5585                 free (internal_relocs);
5586             }
5587
5588           if (htab->fix_cortex_a8)
5589             {
5590               /* Sort relocs which might apply to Cortex-A8 erratum.  */
5591               qsort (a8_relocs, num_a8_relocs,
5592                      sizeof (struct a8_erratum_reloc),
5593                      &a8_reloc_compare);
5594
5595               /* Scan for branches which might trigger Cortex-A8 erratum.  */
5596               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
5597                                           &num_a8_fixes, &a8_fix_table_size,
5598                                           a8_relocs, num_a8_relocs,
5599                                           prev_num_a8_fixes, &stub_changed)
5600                   != 0)
5601                 goto error_ret_free_local;
5602             }
5603         }
5604
5605       if (prev_num_a8_fixes != num_a8_fixes)
5606         stub_changed = TRUE;
5607
5608       if (!stub_changed)
5609         break;
5610
5611       /* OK, we've added some stubs.  Find out the new size of the
5612          stub sections.  */
5613       for (stub_sec = htab->stub_bfd->sections;
5614            stub_sec != NULL;
5615            stub_sec = stub_sec->next)
5616         {
5617           /* Ignore non-stub sections.  */
5618           if (!strstr (stub_sec->name, STUB_SUFFIX))
5619             continue;
5620
5621           stub_sec->size = 0;
5622         }
5623
5624       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
5625
5626       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
5627       if (htab->fix_cortex_a8)
5628         for (i = 0; i < num_a8_fixes; i++)
5629           {
5630             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
5631                          a8_fixes[i].section, htab);
5632
5633             if (stub_sec == NULL)
5634               goto error_ret_free_local;
5635
5636             stub_sec->size
5637               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
5638                                               NULL);
5639           }
5640
5641
5642       /* Ask the linker to do its stuff.  */
5643       (*htab->layout_sections_again) ();
5644     }
5645
5646   /* Add stubs for Cortex-A8 erratum fixes now.  */
5647   if (htab->fix_cortex_a8)
5648     {
5649       for (i = 0; i < num_a8_fixes; i++)
5650         {
5651           struct elf32_arm_stub_hash_entry *stub_entry;
5652           char *stub_name = a8_fixes[i].stub_name;
5653           asection *section = a8_fixes[i].section;
5654           unsigned int section_id = a8_fixes[i].section->id;
5655           asection *link_sec = htab->stub_group[section_id].link_sec;
5656           asection *stub_sec = htab->stub_group[section_id].stub_sec;
5657           const insn_sequence *template_sequence;
5658           int template_size, size = 0;
5659
5660           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
5661                                              TRUE, FALSE);
5662           if (stub_entry == NULL)
5663             {
5664               (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
5665                                      section->owner,
5666                                      stub_name);
5667               return FALSE;
5668             }
5669
5670           stub_entry->stub_sec = stub_sec;
5671           stub_entry->stub_offset = 0;
5672           stub_entry->id_sec = link_sec;
5673           stub_entry->stub_type = a8_fixes[i].stub_type;
5674           stub_entry->target_section = a8_fixes[i].section;
5675           stub_entry->target_value = a8_fixes[i].offset;
5676           stub_entry->target_addend = a8_fixes[i].addend;
5677           stub_entry->orig_insn = a8_fixes[i].orig_insn;
5678           stub_entry->branch_type = a8_fixes[i].branch_type;
5679
5680           size = find_stub_size_and_template (a8_fixes[i].stub_type,
5681                                               &template_sequence,
5682                                               &template_size);
5683
5684           stub_entry->stub_size = size;
5685           stub_entry->stub_template = template_sequence;
5686           stub_entry->stub_template_size = template_size;
5687         }
5688
5689       /* Stash the Cortex-A8 erratum fix array for use later in
5690          elf32_arm_write_section().  */
5691       htab->a8_erratum_fixes = a8_fixes;
5692       htab->num_a8_erratum_fixes = num_a8_fixes;
5693     }
5694   else
5695     {
5696       htab->a8_erratum_fixes = NULL;
5697       htab->num_a8_erratum_fixes = 0;
5698     }
5699   return TRUE;
5700
5701  error_ret_free_local:
5702   return FALSE;
5703 }
5704
5705 /* Build all the stubs associated with the current output file.  The
5706    stubs are kept in a hash table attached to the main linker hash
5707    table.  We also set up the .plt entries for statically linked PIC
5708    functions here.  This function is called via arm_elf_finish in the
5709    linker.  */
5710
5711 bfd_boolean
5712 elf32_arm_build_stubs (struct bfd_link_info *info)
5713 {
5714   asection *stub_sec;
5715   struct bfd_hash_table *table;
5716   struct elf32_arm_link_hash_table *htab;
5717
5718   htab = elf32_arm_hash_table (info);
5719   if (htab == NULL)
5720     return FALSE;
5721
5722   for (stub_sec = htab->stub_bfd->sections;
5723        stub_sec != NULL;
5724        stub_sec = stub_sec->next)
5725     {
5726       bfd_size_type size;
5727
5728       /* Ignore non-stub sections.  */
5729       if (!strstr (stub_sec->name, STUB_SUFFIX))
5730         continue;
5731
5732       /* Allocate memory to hold the linker stubs.  */
5733       size = stub_sec->size;
5734       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
5735       if (stub_sec->contents == NULL && size != 0)
5736         return FALSE;
5737       stub_sec->size = 0;
5738     }
5739
5740   /* Build the stubs as directed by the stub hash table.  */
5741   table = &htab->stub_hash_table;
5742   bfd_hash_traverse (table, arm_build_one_stub, info);
5743   if (htab->fix_cortex_a8)
5744     {
5745       /* Place the cortex a8 stubs last.  */
5746       htab->fix_cortex_a8 = -1;
5747       bfd_hash_traverse (table, arm_build_one_stub, info);
5748     }
5749
5750   return TRUE;
5751 }
5752
5753 /* Locate the Thumb encoded calling stub for NAME.  */
5754
5755 static struct elf_link_hash_entry *
5756 find_thumb_glue (struct bfd_link_info *link_info,
5757                  const char *name,
5758                  char **error_message)
5759 {
5760   char *tmp_name;
5761   struct elf_link_hash_entry *hash;
5762   struct elf32_arm_link_hash_table *hash_table;
5763
5764   /* We need a pointer to the armelf specific hash table.  */
5765   hash_table = elf32_arm_hash_table (link_info);
5766   if (hash_table == NULL)
5767     return NULL;
5768
5769   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5770                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
5771
5772   BFD_ASSERT (tmp_name);
5773
5774   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
5775
5776   hash = elf_link_hash_lookup
5777     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5778
5779   if (hash == NULL
5780       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
5781                    tmp_name, name) == -1)
5782     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5783
5784   free (tmp_name);
5785
5786   return hash;
5787 }
5788
5789 /* Locate the ARM encoded calling stub for NAME.  */
5790
5791 static struct elf_link_hash_entry *
5792 find_arm_glue (struct bfd_link_info *link_info,
5793                const char *name,
5794                char **error_message)
5795 {
5796   char *tmp_name;
5797   struct elf_link_hash_entry *myh;
5798   struct elf32_arm_link_hash_table *hash_table;
5799
5800   /* We need a pointer to the elfarm specific hash table.  */
5801   hash_table = elf32_arm_hash_table (link_info);
5802   if (hash_table == NULL)
5803     return NULL;
5804
5805   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5806                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5807
5808   BFD_ASSERT (tmp_name);
5809
5810   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5811
5812   myh = elf_link_hash_lookup
5813     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5814
5815   if (myh == NULL
5816       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
5817                    tmp_name, name) == -1)
5818     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5819
5820   free (tmp_name);
5821
5822   return myh;
5823 }
5824
5825 /* ARM->Thumb glue (static images):
5826
5827    .arm
5828    __func_from_arm:
5829    ldr r12, __func_addr
5830    bx  r12
5831    __func_addr:
5832    .word func    @ behave as if you saw a ARM_32 reloc.
5833
5834    (v5t static images)
5835    .arm
5836    __func_from_arm:
5837    ldr pc, __func_addr
5838    __func_addr:
5839    .word func    @ behave as if you saw a ARM_32 reloc.
5840
5841    (relocatable images)
5842    .arm
5843    __func_from_arm:
5844    ldr r12, __func_offset
5845    add r12, r12, pc
5846    bx  r12
5847    __func_offset:
5848    .word func - .   */
5849
5850 #define ARM2THUMB_STATIC_GLUE_SIZE 12
5851 static const insn32 a2t1_ldr_insn = 0xe59fc000;
5852 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
5853 static const insn32 a2t3_func_addr_insn = 0x00000001;
5854
5855 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
5856 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
5857 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
5858
5859 #define ARM2THUMB_PIC_GLUE_SIZE 16
5860 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
5861 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
5862 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
5863
5864 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
5865
5866      .thumb                             .thumb
5867      .align 2                           .align 2
5868  __func_from_thumb:                 __func_from_thumb:
5869      bx pc                              push {r6, lr}
5870      nop                                ldr  r6, __func_addr
5871      .arm                               mov  lr, pc
5872      b func                             bx   r6
5873                                         .arm
5874                                     ;; back_to_thumb
5875                                         ldmia r13! {r6, lr}
5876                                         bx    lr
5877                                     __func_addr:
5878                                         .word        func  */
5879
5880 #define THUMB2ARM_GLUE_SIZE 8
5881 static const insn16 t2a1_bx_pc_insn = 0x4778;
5882 static const insn16 t2a2_noop_insn = 0x46c0;
5883 static const insn32 t2a3_b_insn = 0xea000000;
5884
5885 #define VFP11_ERRATUM_VENEER_SIZE 8
5886 #define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
5887 #define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
5888
5889 #define ARM_BX_VENEER_SIZE 12
5890 static const insn32 armbx1_tst_insn = 0xe3100001;
5891 static const insn32 armbx2_moveq_insn = 0x01a0f000;
5892 static const insn32 armbx3_bx_insn = 0xe12fff10;
5893
5894 #ifndef ELFARM_NABI_C_INCLUDED
5895 static void
5896 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
5897 {
5898   asection * s;
5899   bfd_byte * contents;
5900
5901   if (size == 0)
5902     {
5903       /* Do not include empty glue sections in the output.  */
5904       if (abfd != NULL)
5905         {
5906           s = bfd_get_linker_section (abfd, name);
5907           if (s != NULL)
5908             s->flags |= SEC_EXCLUDE;
5909         }
5910       return;
5911     }
5912
5913   BFD_ASSERT (abfd != NULL);
5914
5915   s = bfd_get_linker_section (abfd, name);
5916   BFD_ASSERT (s != NULL);
5917
5918   contents = (bfd_byte *) bfd_alloc (abfd, size);
5919
5920   BFD_ASSERT (s->size == size);
5921   s->contents = contents;
5922 }
5923
5924 bfd_boolean
5925 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
5926 {
5927   struct elf32_arm_link_hash_table * globals;
5928
5929   globals = elf32_arm_hash_table (info);
5930   BFD_ASSERT (globals != NULL);
5931
5932   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5933                                    globals->arm_glue_size,
5934                                    ARM2THUMB_GLUE_SECTION_NAME);
5935
5936   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5937                                    globals->thumb_glue_size,
5938                                    THUMB2ARM_GLUE_SECTION_NAME);
5939
5940   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5941                                    globals->vfp11_erratum_glue_size,
5942                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
5943
5944   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5945                                    globals->stm32l4xx_erratum_glue_size,
5946                                    STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
5947
5948   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5949                                    globals->bx_glue_size,
5950                                    ARM_BX_GLUE_SECTION_NAME);
5951
5952   return TRUE;
5953 }
5954
5955 /* Allocate space and symbols for calling a Thumb function from Arm mode.
5956    returns the symbol identifying the stub.  */
5957
5958 static struct elf_link_hash_entry *
5959 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
5960                           struct elf_link_hash_entry * h)
5961 {
5962   const char * name = h->root.root.string;
5963   asection * s;
5964   char * tmp_name;
5965   struct elf_link_hash_entry * myh;
5966   struct bfd_link_hash_entry * bh;
5967   struct elf32_arm_link_hash_table * globals;
5968   bfd_vma val;
5969   bfd_size_type size;
5970
5971   globals = elf32_arm_hash_table (link_info);
5972   BFD_ASSERT (globals != NULL);
5973   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5974
5975   s = bfd_get_linker_section
5976     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
5977
5978   BFD_ASSERT (s != NULL);
5979
5980   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5981                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5982
5983   BFD_ASSERT (tmp_name);
5984
5985   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5986
5987   myh = elf_link_hash_lookup
5988     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
5989
5990   if (myh != NULL)
5991     {
5992       /* We've already seen this guy.  */
5993       free (tmp_name);
5994       return myh;
5995     }
5996
5997   /* The only trick here is using hash_table->arm_glue_size as the value.
5998      Even though the section isn't allocated yet, this is where we will be
5999      putting it.  The +1 on the value marks that the stub has not been
6000      output yet - not that it is a Thumb function.  */
6001   bh = NULL;
6002   val = globals->arm_glue_size + 1;
6003   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
6004                                     tmp_name, BSF_GLOBAL, s, val,
6005                                     NULL, TRUE, FALSE, &bh);
6006
6007   myh = (struct elf_link_hash_entry *) bh;
6008   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6009   myh->forced_local = 1;
6010
6011   free (tmp_name);
6012
6013   if (bfd_link_pic (link_info)
6014       || globals->root.is_relocatable_executable
6015       || globals->pic_veneer)
6016     size = ARM2THUMB_PIC_GLUE_SIZE;
6017   else if (globals->use_blx)
6018     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
6019   else
6020     size = ARM2THUMB_STATIC_GLUE_SIZE;
6021
6022   s->size += size;
6023   globals->arm_glue_size += size;
6024
6025   return myh;
6026 }
6027
6028 /* Allocate space for ARMv4 BX veneers.  */
6029
6030 static void
6031 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
6032 {
6033   asection * s;
6034   struct elf32_arm_link_hash_table *globals;
6035   char *tmp_name;
6036   struct elf_link_hash_entry *myh;
6037   struct bfd_link_hash_entry *bh;
6038   bfd_vma val;
6039
6040   /* BX PC does not need a veneer.  */
6041   if (reg == 15)
6042     return;
6043
6044   globals = elf32_arm_hash_table (link_info);
6045   BFD_ASSERT (globals != NULL);
6046   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6047
6048   /* Check if this veneer has already been allocated.  */
6049   if (globals->bx_glue_offset[reg])
6050     return;
6051
6052   s = bfd_get_linker_section
6053     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
6054
6055   BFD_ASSERT (s != NULL);
6056
6057   /* Add symbol for veneer.  */
6058   tmp_name = (char *)
6059       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
6060
6061   BFD_ASSERT (tmp_name);
6062
6063   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
6064
6065   myh = elf_link_hash_lookup
6066     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
6067
6068   BFD_ASSERT (myh == NULL);
6069
6070   bh = NULL;
6071   val = globals->bx_glue_size;
6072   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
6073                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6074                                     NULL, TRUE, FALSE, &bh);
6075
6076   myh = (struct elf_link_hash_entry *) bh;
6077   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6078   myh->forced_local = 1;
6079
6080   s->size += ARM_BX_VENEER_SIZE;
6081   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
6082   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
6083 }
6084
6085
6086 /* Add an entry to the code/data map for section SEC.  */
6087
6088 static void
6089 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
6090 {
6091   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
6092   unsigned int newidx;
6093
6094   if (sec_data->map == NULL)
6095     {
6096       sec_data->map = (elf32_arm_section_map *)
6097           bfd_malloc (sizeof (elf32_arm_section_map));
6098       sec_data->mapcount = 0;
6099       sec_data->mapsize = 1;
6100     }
6101
6102   newidx = sec_data->mapcount++;
6103
6104   if (sec_data->mapcount > sec_data->mapsize)
6105     {
6106       sec_data->mapsize *= 2;
6107       sec_data->map = (elf32_arm_section_map *)
6108           bfd_realloc_or_free (sec_data->map, sec_data->mapsize
6109                                * sizeof (elf32_arm_section_map));
6110     }
6111
6112   if (sec_data->map)
6113     {
6114       sec_data->map[newidx].vma = vma;
6115       sec_data->map[newidx].type = type;
6116     }
6117 }
6118
6119
6120 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
6121    veneers are handled for now.  */
6122
6123 static bfd_vma
6124 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
6125                              elf32_vfp11_erratum_list *branch,
6126                              bfd *branch_bfd,
6127                              asection *branch_sec,
6128                              unsigned int offset)
6129 {
6130   asection *s;
6131   struct elf32_arm_link_hash_table *hash_table;
6132   char *tmp_name;
6133   struct elf_link_hash_entry *myh;
6134   struct bfd_link_hash_entry *bh;
6135   bfd_vma val;
6136   struct _arm_elf_section_data *sec_data;
6137   elf32_vfp11_erratum_list *newerr;
6138
6139   hash_table = elf32_arm_hash_table (link_info);
6140   BFD_ASSERT (hash_table != NULL);
6141   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
6142
6143   s = bfd_get_linker_section
6144     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
6145
6146   sec_data = elf32_arm_section_data (s);
6147
6148   BFD_ASSERT (s != NULL);
6149
6150   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
6151                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
6152
6153   BFD_ASSERT (tmp_name);
6154
6155   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
6156            hash_table->num_vfp11_fixes);
6157
6158   myh = elf_link_hash_lookup
6159     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6160
6161   BFD_ASSERT (myh == NULL);
6162
6163   bh = NULL;
6164   val = hash_table->vfp11_erratum_glue_size;
6165   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
6166                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6167                                     NULL, TRUE, FALSE, &bh);
6168
6169   myh = (struct elf_link_hash_entry *) bh;
6170   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6171   myh->forced_local = 1;
6172
6173   /* Link veneer back to calling location.  */
6174   sec_data->erratumcount += 1;
6175   newerr = (elf32_vfp11_erratum_list *)
6176       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
6177
6178   newerr->type = VFP11_ERRATUM_ARM_VENEER;
6179   newerr->vma = -1;
6180   newerr->u.v.branch = branch;
6181   newerr->u.v.id = hash_table->num_vfp11_fixes;
6182   branch->u.b.veneer = newerr;
6183
6184   newerr->next = sec_data->erratumlist;
6185   sec_data->erratumlist = newerr;
6186
6187   /* A symbol for the return from the veneer.  */
6188   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
6189            hash_table->num_vfp11_fixes);
6190
6191   myh = elf_link_hash_lookup
6192     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6193
6194   if (myh != NULL)
6195     abort ();
6196
6197   bh = NULL;
6198   val = offset + 4;
6199   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
6200                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
6201
6202   myh = (struct elf_link_hash_entry *) bh;
6203   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6204   myh->forced_local = 1;
6205
6206   free (tmp_name);
6207
6208   /* Generate a mapping symbol for the veneer section, and explicitly add an
6209      entry for that symbol to the code/data map for the section.  */
6210   if (hash_table->vfp11_erratum_glue_size == 0)
6211     {
6212       bh = NULL;
6213       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
6214          ever requires this erratum fix.  */
6215       _bfd_generic_link_add_one_symbol (link_info,
6216                                         hash_table->bfd_of_glue_owner, "$a",
6217                                         BSF_LOCAL, s, 0, NULL,
6218                                         TRUE, FALSE, &bh);
6219
6220       myh = (struct elf_link_hash_entry *) bh;
6221       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6222       myh->forced_local = 1;
6223
6224       /* The elf32_arm_init_maps function only cares about symbols from input
6225          BFDs.  We must make a note of this generated mapping symbol
6226          ourselves so that code byteswapping works properly in
6227          elf32_arm_write_section.  */
6228       elf32_arm_section_map_add (s, 'a', 0);
6229     }
6230
6231   s->size += VFP11_ERRATUM_VENEER_SIZE;
6232   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
6233   hash_table->num_vfp11_fixes++;
6234
6235   /* The offset of the veneer.  */
6236   return val;
6237 }
6238
6239 /* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
6240    veneers need to be handled because used only in Cortex-M.  */
6241
6242 static bfd_vma
6243 record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
6244                                  elf32_stm32l4xx_erratum_list *branch,
6245                                  bfd *branch_bfd,
6246                                  asection *branch_sec,
6247                                  unsigned int offset,
6248                                  bfd_size_type veneer_size)
6249 {
6250   asection *s;
6251   struct elf32_arm_link_hash_table *hash_table;
6252   char *tmp_name;
6253   struct elf_link_hash_entry *myh;
6254   struct bfd_link_hash_entry *bh;
6255   bfd_vma val;
6256   struct _arm_elf_section_data *sec_data;
6257   elf32_stm32l4xx_erratum_list *newerr;
6258
6259   hash_table = elf32_arm_hash_table (link_info);
6260   BFD_ASSERT (hash_table != NULL);
6261   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
6262
6263   s = bfd_get_linker_section
6264     (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
6265
6266   BFD_ASSERT (s != NULL);
6267
6268   sec_data = elf32_arm_section_data (s);
6269
6270   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
6271                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
6272
6273   BFD_ASSERT (tmp_name);
6274
6275   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
6276            hash_table->num_stm32l4xx_fixes);
6277
6278   myh = elf_link_hash_lookup
6279     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6280
6281   BFD_ASSERT (myh == NULL);
6282
6283   bh = NULL;
6284   val = hash_table->stm32l4xx_erratum_glue_size;
6285   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
6286                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
6287                                     NULL, TRUE, FALSE, &bh);
6288
6289   myh = (struct elf_link_hash_entry *) bh;
6290   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6291   myh->forced_local = 1;
6292
6293   /* Link veneer back to calling location.  */
6294   sec_data->stm32l4xx_erratumcount += 1;
6295   newerr = (elf32_stm32l4xx_erratum_list *)
6296       bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
6297
6298   newerr->type = STM32L4XX_ERRATUM_VENEER;
6299   newerr->vma = -1;
6300   newerr->u.v.branch = branch;
6301   newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
6302   branch->u.b.veneer = newerr;
6303
6304   newerr->next = sec_data->stm32l4xx_erratumlist;
6305   sec_data->stm32l4xx_erratumlist = newerr;
6306
6307   /* A symbol for the return from the veneer.  */
6308   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
6309            hash_table->num_stm32l4xx_fixes);
6310
6311   myh = elf_link_hash_lookup
6312     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
6313
6314   if (myh != NULL)
6315     abort ();
6316
6317   bh = NULL;
6318   val = offset + 4;
6319   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
6320                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
6321
6322   myh = (struct elf_link_hash_entry *) bh;
6323   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
6324   myh->forced_local = 1;
6325
6326   free (tmp_name);
6327
6328   /* Generate a mapping symbol for the veneer section, and explicitly add an
6329      entry for that symbol to the code/data map for the section.  */
6330   if (hash_table->stm32l4xx_erratum_glue_size == 0)
6331     {
6332       bh = NULL;
6333       /* Creates a THUMB symbol since there is no other choice.  */
6334       _bfd_generic_link_add_one_symbol (link_info,
6335                                         hash_table->bfd_of_glue_owner, "$t",
6336                                         BSF_LOCAL, s, 0, NULL,
6337                                         TRUE, FALSE, &bh);
6338
6339       myh = (struct elf_link_hash_entry *) bh;
6340       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6341       myh->forced_local = 1;
6342
6343       /* The elf32_arm_init_maps function only cares about symbols from input
6344          BFDs.  We must make a note of this generated mapping symbol
6345          ourselves so that code byteswapping works properly in
6346          elf32_arm_write_section.  */
6347       elf32_arm_section_map_add (s, 't', 0);
6348     }
6349
6350   s->size += veneer_size;
6351   hash_table->stm32l4xx_erratum_glue_size += veneer_size;
6352   hash_table->num_stm32l4xx_fixes++;
6353
6354   /* The offset of the veneer.  */
6355   return val;
6356 }
6357
6358 #define ARM_GLUE_SECTION_FLAGS \
6359   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
6360    | SEC_READONLY | SEC_LINKER_CREATED)
6361
6362 /* Create a fake section for use by the ARM backend of the linker.  */
6363
6364 static bfd_boolean
6365 arm_make_glue_section (bfd * abfd, const char * name)
6366 {
6367   asection * sec;
6368
6369   sec = bfd_get_linker_section (abfd, name);
6370   if (sec != NULL)
6371     /* Already made.  */
6372     return TRUE;
6373
6374   sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
6375
6376   if (sec == NULL
6377       || !bfd_set_section_alignment (abfd, sec, 2))
6378     return FALSE;
6379
6380   /* Set the gc mark to prevent the section from being removed by garbage
6381      collection, despite the fact that no relocs refer to this section.  */
6382   sec->gc_mark = 1;
6383
6384   return TRUE;
6385 }
6386
6387 /* Set size of .plt entries.  This function is called from the
6388    linker scripts in ld/emultempl/{armelf}.em.  */
6389
6390 void
6391 bfd_elf32_arm_use_long_plt (void)
6392 {
6393   elf32_arm_use_long_plt_entry = TRUE;
6394 }
6395
6396 /* Add the glue sections to ABFD.  This function is called from the
6397    linker scripts in ld/emultempl/{armelf}.em.  */
6398
6399 bfd_boolean
6400 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
6401                                         struct bfd_link_info *info)
6402 {
6403   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
6404   bfd_boolean dostm32l4xx = globals
6405     && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
6406   bfd_boolean addglue;
6407
6408   /* If we are only performing a partial
6409      link do not bother adding the glue.  */
6410   if (bfd_link_relocatable (info))
6411     return TRUE;
6412
6413   addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
6414     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
6415     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
6416     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
6417
6418   if (!dostm32l4xx)
6419     return addglue;
6420
6421   return addglue
6422     && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
6423 }
6424
6425 /* Select a BFD to be used to hold the sections used by the glue code.
6426    This function is called from the linker scripts in ld/emultempl/
6427    {armelf/pe}.em.  */
6428
6429 bfd_boolean
6430 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
6431 {
6432   struct elf32_arm_link_hash_table *globals;
6433
6434   /* If we are only performing a partial link
6435      do not bother getting a bfd to hold the glue.  */
6436   if (bfd_link_relocatable (info))
6437     return TRUE;
6438
6439   /* Make sure we don't attach the glue sections to a dynamic object.  */
6440   BFD_ASSERT (!(abfd->flags & DYNAMIC));
6441
6442   globals = elf32_arm_hash_table (info);
6443   BFD_ASSERT (globals != NULL);
6444
6445   if (globals->bfd_of_glue_owner != NULL)
6446     return TRUE;
6447
6448   /* Save the bfd for later use.  */
6449   globals->bfd_of_glue_owner = abfd;
6450
6451   return TRUE;
6452 }
6453
6454 static void
6455 check_use_blx (struct elf32_arm_link_hash_table *globals)
6456 {
6457   int cpu_arch;
6458
6459   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
6460                                        Tag_CPU_arch);
6461
6462   if (globals->fix_arm1176)
6463     {
6464       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
6465         globals->use_blx = 1;
6466     }
6467   else
6468     {
6469       if (cpu_arch > TAG_CPU_ARCH_V4T)
6470         globals->use_blx = 1;
6471     }
6472 }
6473
6474 bfd_boolean
6475 bfd_elf32_arm_process_before_allocation (bfd *abfd,
6476                                          struct bfd_link_info *link_info)
6477 {
6478   Elf_Internal_Shdr *symtab_hdr;
6479   Elf_Internal_Rela *internal_relocs = NULL;
6480   Elf_Internal_Rela *irel, *irelend;
6481   bfd_byte *contents = NULL;
6482
6483   asection *sec;
6484   struct elf32_arm_link_hash_table *globals;
6485
6486   /* If we are only performing a partial link do not bother
6487      to construct any glue.  */
6488   if (bfd_link_relocatable (link_info))
6489     return TRUE;
6490
6491   /* Here we have a bfd that is to be included on the link.  We have a
6492      hook to do reloc rummaging, before section sizes are nailed down.  */
6493   globals = elf32_arm_hash_table (link_info);
6494   BFD_ASSERT (globals != NULL);
6495
6496   check_use_blx (globals);
6497
6498   if (globals->byteswap_code && !bfd_big_endian (abfd))
6499     {
6500       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
6501                           abfd);
6502       return FALSE;
6503     }
6504
6505   /* PR 5398: If we have not decided to include any loadable sections in
6506      the output then we will not have a glue owner bfd.  This is OK, it
6507      just means that there is nothing else for us to do here.  */
6508   if (globals->bfd_of_glue_owner == NULL)
6509     return TRUE;
6510
6511   /* Rummage around all the relocs and map the glue vectors.  */
6512   sec = abfd->sections;
6513
6514   if (sec == NULL)
6515     return TRUE;
6516
6517   for (; sec != NULL; sec = sec->next)
6518     {
6519       if (sec->reloc_count == 0)
6520         continue;
6521
6522       if ((sec->flags & SEC_EXCLUDE) != 0)
6523         continue;
6524
6525       symtab_hdr = & elf_symtab_hdr (abfd);
6526
6527       /* Load the relocs.  */
6528       internal_relocs
6529         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
6530
6531       if (internal_relocs == NULL)
6532         goto error_return;
6533
6534       irelend = internal_relocs + sec->reloc_count;
6535       for (irel = internal_relocs; irel < irelend; irel++)
6536         {
6537           long r_type;
6538           unsigned long r_index;
6539
6540           struct elf_link_hash_entry *h;
6541
6542           r_type = ELF32_R_TYPE (irel->r_info);
6543           r_index = ELF32_R_SYM (irel->r_info);
6544
6545           /* These are the only relocation types we care about.  */
6546           if (   r_type != R_ARM_PC24
6547               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
6548             continue;
6549
6550           /* Get the section contents if we haven't done so already.  */
6551           if (contents == NULL)
6552             {
6553               /* Get cached copy if it exists.  */
6554               if (elf_section_data (sec)->this_hdr.contents != NULL)
6555                 contents = elf_section_data (sec)->this_hdr.contents;
6556               else
6557                 {
6558                   /* Go get them off disk.  */
6559                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
6560                     goto error_return;
6561                 }
6562             }
6563
6564           if (r_type == R_ARM_V4BX)
6565             {
6566               int reg;
6567
6568               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
6569               record_arm_bx_glue (link_info, reg);
6570               continue;
6571             }
6572
6573           /* If the relocation is not against a symbol it cannot concern us.  */
6574           h = NULL;
6575
6576           /* We don't care about local symbols.  */
6577           if (r_index < symtab_hdr->sh_info)
6578             continue;
6579
6580           /* This is an external symbol.  */
6581           r_index -= symtab_hdr->sh_info;
6582           h = (struct elf_link_hash_entry *)
6583             elf_sym_hashes (abfd)[r_index];
6584
6585           /* If the relocation is against a static symbol it must be within
6586              the current section and so cannot be a cross ARM/Thumb relocation.  */
6587           if (h == NULL)
6588             continue;
6589
6590           /* If the call will go through a PLT entry then we do not need
6591              glue.  */
6592           if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
6593             continue;
6594
6595           switch (r_type)
6596             {
6597             case R_ARM_PC24:
6598               /* This one is a call from arm code.  We need to look up
6599                  the target of the call.  If it is a thumb target, we
6600                  insert glue.  */
6601               if (h->target_internal == ST_BRANCH_TO_THUMB)
6602                 record_arm_to_thumb_glue (link_info, h);
6603               break;
6604
6605             default:
6606               abort ();
6607             }
6608         }
6609
6610       if (contents != NULL
6611           && elf_section_data (sec)->this_hdr.contents != contents)
6612         free (contents);
6613       contents = NULL;
6614
6615       if (internal_relocs != NULL
6616           && elf_section_data (sec)->relocs != internal_relocs)
6617         free (internal_relocs);
6618       internal_relocs = NULL;
6619     }
6620
6621   return TRUE;
6622
6623 error_return:
6624   if (contents != NULL
6625       && elf_section_data (sec)->this_hdr.contents != contents)
6626     free (contents);
6627   if (internal_relocs != NULL
6628       && elf_section_data (sec)->relocs != internal_relocs)
6629     free (internal_relocs);
6630
6631   return FALSE;
6632 }
6633 #endif
6634
6635
6636 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
6637
6638 void
6639 bfd_elf32_arm_init_maps (bfd *abfd)
6640 {
6641   Elf_Internal_Sym *isymbuf;
6642   Elf_Internal_Shdr *hdr;
6643   unsigned int i, localsyms;
6644
6645   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
6646   if (! is_arm_elf (abfd))
6647     return;
6648
6649   if ((abfd->flags & DYNAMIC) != 0)
6650     return;
6651
6652   hdr = & elf_symtab_hdr (abfd);
6653   localsyms = hdr->sh_info;
6654
6655   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
6656      should contain the number of local symbols, which should come before any
6657      global symbols.  Mapping symbols are always local.  */
6658   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
6659                                   NULL);
6660
6661   /* No internal symbols read?  Skip this BFD.  */
6662   if (isymbuf == NULL)
6663     return;
6664
6665   for (i = 0; i < localsyms; i++)
6666     {
6667       Elf_Internal_Sym *isym = &isymbuf[i];
6668       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6669       const char *name;
6670
6671       if (sec != NULL
6672           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
6673         {
6674           name = bfd_elf_string_from_elf_section (abfd,
6675             hdr->sh_link, isym->st_name);
6676
6677           if (bfd_is_arm_special_symbol_name (name,
6678                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
6679             elf32_arm_section_map_add (sec, name[1], isym->st_value);
6680         }
6681     }
6682 }
6683
6684
6685 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
6686    say what they wanted.  */
6687
6688 void
6689 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
6690 {
6691   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6692   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6693
6694   if (globals == NULL)
6695     return;
6696
6697   if (globals->fix_cortex_a8 == -1)
6698     {
6699       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
6700       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
6701           && (out_attr[Tag_CPU_arch_profile].i == 'A'
6702               || out_attr[Tag_CPU_arch_profile].i == 0))
6703         globals->fix_cortex_a8 = 1;
6704       else
6705         globals->fix_cortex_a8 = 0;
6706     }
6707 }
6708
6709
6710 void
6711 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
6712 {
6713   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6714   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6715
6716   if (globals == NULL)
6717     return;
6718   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
6719   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
6720     {
6721       switch (globals->vfp11_fix)
6722         {
6723         case BFD_ARM_VFP11_FIX_DEFAULT:
6724         case BFD_ARM_VFP11_FIX_NONE:
6725           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6726           break;
6727
6728         default:
6729           /* Give a warning, but do as the user requests anyway.  */
6730           (*_bfd_error_handler) (_("%B: warning: selected VFP11 erratum "
6731             "workaround is not necessary for target architecture"), obfd);
6732         }
6733     }
6734   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
6735     /* For earlier architectures, we might need the workaround, but do not
6736        enable it by default.  If users is running with broken hardware, they
6737        must enable the erratum fix explicitly.  */
6738     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6739 }
6740
6741 void
6742 bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
6743 {
6744   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6745   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6746
6747   if (globals == NULL)
6748     return;
6749
6750   /* We assume only Cortex-M4 may require the fix.  */
6751   if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
6752       || out_attr[Tag_CPU_arch_profile].i != 'M')
6753     {
6754       if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
6755         /* Give a warning, but do as the user requests anyway.  */
6756         (*_bfd_error_handler)
6757           (_("%B: warning: selected STM32L4XX erratum "
6758              "workaround is not necessary for target architecture"), obfd);
6759     }
6760 }
6761
6762 enum bfd_arm_vfp11_pipe
6763 {
6764   VFP11_FMAC,
6765   VFP11_LS,
6766   VFP11_DS,
6767   VFP11_BAD
6768 };
6769
6770 /* Return a VFP register number.  This is encoded as RX:X for single-precision
6771    registers, or X:RX for double-precision registers, where RX is the group of
6772    four bits in the instruction encoding and X is the single extension bit.
6773    RX and X fields are specified using their lowest (starting) bit.  The return
6774    value is:
6775
6776      0...31: single-precision registers s0...s31
6777      32...63: double-precision registers d0...d31.
6778
6779    Although X should be zero for VFP11 (encoding d0...d15 only), we might
6780    encounter VFP3 instructions, so we allow the full range for DP registers.  */
6781
6782 static unsigned int
6783 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
6784                      unsigned int x)
6785 {
6786   if (is_double)
6787     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
6788   else
6789     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
6790 }
6791
6792 /* Set bits in *WMASK according to a register number REG as encoded by
6793    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
6794
6795 static void
6796 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
6797 {
6798   if (reg < 32)
6799     *wmask |= 1 << reg;
6800   else if (reg < 48)
6801     *wmask |= 3 << ((reg - 32) * 2);
6802 }
6803
6804 /* Return TRUE if WMASK overwrites anything in REGS.  */
6805
6806 static bfd_boolean
6807 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
6808 {
6809   int i;
6810
6811   for (i = 0; i < numregs; i++)
6812     {
6813       unsigned int reg = regs[i];
6814
6815       if (reg < 32 && (wmask & (1 << reg)) != 0)
6816         return TRUE;
6817
6818       reg -= 32;
6819
6820       if (reg >= 16)
6821         continue;
6822
6823       if ((wmask & (3 << (reg * 2))) != 0)
6824         return TRUE;
6825     }
6826
6827   return FALSE;
6828 }
6829
6830 /* In this function, we're interested in two things: finding input registers
6831    for VFP data-processing instructions, and finding the set of registers which
6832    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
6833    hold the written set, so FLDM etc. are easy to deal with (we're only
6834    interested in 32 SP registers or 16 dp registers, due to the VFP version
6835    implemented by the chip in question).  DP registers are marked by setting
6836    both SP registers in the write mask).  */
6837
6838 static enum bfd_arm_vfp11_pipe
6839 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
6840                            int *numregs)
6841 {
6842   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
6843   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
6844
6845   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
6846     {
6847       unsigned int pqrs;
6848       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6849       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6850
6851       pqrs = ((insn & 0x00800000) >> 20)
6852            | ((insn & 0x00300000) >> 19)
6853            | ((insn & 0x00000040) >> 6);
6854
6855       switch (pqrs)
6856         {
6857         case 0: /* fmac[sd].  */
6858         case 1: /* fnmac[sd].  */
6859         case 2: /* fmsc[sd].  */
6860         case 3: /* fnmsc[sd].  */
6861           vpipe = VFP11_FMAC;
6862           bfd_arm_vfp11_write_mask (destmask, fd);
6863           regs[0] = fd;
6864           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
6865           regs[2] = fm;
6866           *numregs = 3;
6867           break;
6868
6869         case 4: /* fmul[sd].  */
6870         case 5: /* fnmul[sd].  */
6871         case 6: /* fadd[sd].  */
6872         case 7: /* fsub[sd].  */
6873           vpipe = VFP11_FMAC;
6874           goto vfp_binop;
6875
6876         case 8: /* fdiv[sd].  */
6877           vpipe = VFP11_DS;
6878           vfp_binop:
6879           bfd_arm_vfp11_write_mask (destmask, fd);
6880           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
6881           regs[1] = fm;
6882           *numregs = 2;
6883           break;
6884
6885         case 15: /* extended opcode.  */
6886           {
6887             unsigned int extn = ((insn >> 15) & 0x1e)
6888                               | ((insn >> 7) & 1);
6889
6890             switch (extn)
6891               {
6892               case 0: /* fcpy[sd].  */
6893               case 1: /* fabs[sd].  */
6894               case 2: /* fneg[sd].  */
6895               case 8: /* fcmp[sd].  */
6896               case 9: /* fcmpe[sd].  */
6897               case 10: /* fcmpz[sd].  */
6898               case 11: /* fcmpez[sd].  */
6899               case 16: /* fuito[sd].  */
6900               case 17: /* fsito[sd].  */
6901               case 24: /* ftoui[sd].  */
6902               case 25: /* ftouiz[sd].  */
6903               case 26: /* ftosi[sd].  */
6904               case 27: /* ftosiz[sd].  */
6905                 /* These instructions will not bounce due to underflow.  */
6906                 *numregs = 0;
6907                 vpipe = VFP11_FMAC;
6908                 break;
6909
6910               case 3: /* fsqrt[sd].  */
6911                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
6912                    registers to cause the erratum in previous instructions.  */
6913                 bfd_arm_vfp11_write_mask (destmask, fd);
6914                 vpipe = VFP11_DS;
6915                 break;
6916
6917               case 15: /* fcvt{ds,sd}.  */
6918                 {
6919                   int rnum = 0;
6920
6921                   bfd_arm_vfp11_write_mask (destmask, fd);
6922
6923                   /* Only FCVTSD can underflow.  */
6924                   if ((insn & 0x100) != 0)
6925                     regs[rnum++] = fm;
6926
6927                   *numregs = rnum;
6928
6929                   vpipe = VFP11_FMAC;
6930                 }
6931                 break;
6932
6933               default:
6934                 return VFP11_BAD;
6935               }
6936           }
6937           break;
6938
6939         default:
6940           return VFP11_BAD;
6941         }
6942     }
6943   /* Two-register transfer.  */
6944   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
6945     {
6946       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6947
6948       if ((insn & 0x100000) == 0)
6949         {
6950           if (is_double)
6951             bfd_arm_vfp11_write_mask (destmask, fm);
6952           else
6953             {
6954               bfd_arm_vfp11_write_mask (destmask, fm);
6955               bfd_arm_vfp11_write_mask (destmask, fm + 1);
6956             }
6957         }
6958
6959       vpipe = VFP11_LS;
6960     }
6961   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
6962     {
6963       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6964       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
6965
6966       switch (puw)
6967         {
6968         case 0: /* Two-reg transfer.  We should catch these above.  */
6969           abort ();
6970
6971         case 2: /* fldm[sdx].  */
6972         case 3:
6973         case 5:
6974           {
6975             unsigned int i, offset = insn & 0xff;
6976
6977             if (is_double)
6978               offset >>= 1;
6979
6980             for (i = fd; i < fd + offset; i++)
6981               bfd_arm_vfp11_write_mask (destmask, i);
6982           }
6983           break;
6984
6985         case 4: /* fld[sd].  */
6986         case 6:
6987           bfd_arm_vfp11_write_mask (destmask, fd);
6988           break;
6989
6990         default:
6991           return VFP11_BAD;
6992         }
6993
6994       vpipe = VFP11_LS;
6995     }
6996   /* Single-register transfer. Note L==0.  */
6997   else if ((insn & 0x0f100e10) == 0x0e000a10)
6998     {
6999       unsigned int opcode = (insn >> 21) & 7;
7000       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
7001
7002       switch (opcode)
7003         {
7004         case 0: /* fmsr/fmdlr.  */
7005         case 1: /* fmdhr.  */
7006           /* Mark fmdhr and fmdlr as writing to the whole of the DP
7007              destination register.  I don't know if this is exactly right,
7008              but it is the conservative choice.  */
7009           bfd_arm_vfp11_write_mask (destmask, fn);
7010           break;
7011
7012         case 7: /* fmxr.  */
7013           break;
7014         }
7015
7016       vpipe = VFP11_LS;
7017     }
7018
7019   return vpipe;
7020 }
7021
7022
7023 static int elf32_arm_compare_mapping (const void * a, const void * b);
7024
7025
7026 /* Look for potentially-troublesome code sequences which might trigger the
7027    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
7028    (available from ARM) for details of the erratum.  A short version is
7029    described in ld.texinfo.  */
7030
7031 bfd_boolean
7032 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
7033 {
7034   asection *sec;
7035   bfd_byte *contents = NULL;
7036   int state = 0;
7037   int regs[3], numregs = 0;
7038   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7039   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
7040
7041   if (globals == NULL)
7042     return FALSE;
7043
7044   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
7045      The states transition as follows:
7046
7047        0 -> 1 (vector) or 0 -> 2 (scalar)
7048            A VFP FMAC-pipeline instruction has been seen. Fill
7049            regs[0]..regs[numregs-1] with its input operands. Remember this
7050            instruction in 'first_fmac'.
7051
7052        1 -> 2
7053            Any instruction, except for a VFP instruction which overwrites
7054            regs[*].
7055
7056        1 -> 3 [ -> 0 ]  or
7057        2 -> 3 [ -> 0 ]
7058            A VFP instruction has been seen which overwrites any of regs[*].
7059            We must make a veneer!  Reset state to 0 before examining next
7060            instruction.
7061
7062        2 -> 0
7063            If we fail to match anything in state 2, reset to state 0 and reset
7064            the instruction pointer to the instruction after 'first_fmac'.
7065
7066      If the VFP11 vector mode is in use, there must be at least two unrelated
7067      instructions between anti-dependent VFP11 instructions to properly avoid
7068      triggering the erratum, hence the use of the extra state 1.  */
7069
7070   /* If we are only performing a partial link do not bother
7071      to construct any glue.  */
7072   if (bfd_link_relocatable (link_info))
7073     return TRUE;
7074
7075   /* Skip if this bfd does not correspond to an ELF image.  */
7076   if (! is_arm_elf (abfd))
7077     return TRUE;
7078
7079   /* We should have chosen a fix type by the time we get here.  */
7080   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
7081
7082   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
7083     return TRUE;
7084
7085   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
7086   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
7087     return TRUE;
7088
7089   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7090     {
7091       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
7092       struct _arm_elf_section_data *sec_data;
7093
7094       /* If we don't have executable progbits, we're not interested in this
7095          section.  Also skip if section is to be excluded.  */
7096       if (elf_section_type (sec) != SHT_PROGBITS
7097           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
7098           || (sec->flags & SEC_EXCLUDE) != 0
7099           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7100           || sec->output_section == bfd_abs_section_ptr
7101           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
7102         continue;
7103
7104       sec_data = elf32_arm_section_data (sec);
7105
7106       if (sec_data->mapcount == 0)
7107         continue;
7108
7109       if (elf_section_data (sec)->this_hdr.contents != NULL)
7110         contents = elf_section_data (sec)->this_hdr.contents;
7111       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7112         goto error_return;
7113
7114       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
7115              elf32_arm_compare_mapping);
7116
7117       for (span = 0; span < sec_data->mapcount; span++)
7118         {
7119           unsigned int span_start = sec_data->map[span].vma;
7120           unsigned int span_end = (span == sec_data->mapcount - 1)
7121                                   ? sec->size : sec_data->map[span + 1].vma;
7122           char span_type = sec_data->map[span].type;
7123
7124           /* FIXME: Only ARM mode is supported at present.  We may need to
7125              support Thumb-2 mode also at some point.  */
7126           if (span_type != 'a')
7127             continue;
7128
7129           for (i = span_start; i < span_end;)
7130             {
7131               unsigned int next_i = i + 4;
7132               unsigned int insn = bfd_big_endian (abfd)
7133                 ? (contents[i] << 24)
7134                   | (contents[i + 1] << 16)
7135                   | (contents[i + 2] << 8)
7136                   | contents[i + 3]
7137                 : (contents[i + 3] << 24)
7138                   | (contents[i + 2] << 16)
7139                   | (contents[i + 1] << 8)
7140                   | contents[i];
7141               unsigned int writemask = 0;
7142               enum bfd_arm_vfp11_pipe vpipe;
7143
7144               switch (state)
7145                 {
7146                 case 0:
7147                   vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
7148                                                     &numregs);
7149                   /* I'm assuming the VFP11 erratum can trigger with denorm
7150                      operands on either the FMAC or the DS pipeline. This might
7151                      lead to slightly overenthusiastic veneer insertion.  */
7152                   if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
7153                     {
7154                       state = use_vector ? 1 : 2;
7155                       first_fmac = i;
7156                       veneer_of_insn = insn;
7157                     }
7158                   break;
7159
7160                 case 1:
7161                   {
7162                     int other_regs[3], other_numregs;
7163                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
7164                                                       other_regs,
7165                                                       &other_numregs);
7166                     if (vpipe != VFP11_BAD
7167                         && bfd_arm_vfp11_antidependency (writemask, regs,
7168                                                          numregs))
7169                       state = 3;
7170                     else
7171                       state = 2;
7172                   }
7173                   break;
7174
7175                 case 2:
7176                   {
7177                     int other_regs[3], other_numregs;
7178                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
7179                                                       other_regs,
7180                                                       &other_numregs);
7181                     if (vpipe != VFP11_BAD
7182                         && bfd_arm_vfp11_antidependency (writemask, regs,
7183                                                          numregs))
7184                       state = 3;
7185                     else
7186                       {
7187                         state = 0;
7188                         next_i = first_fmac + 4;
7189                       }
7190                   }
7191                   break;
7192
7193                 case 3:
7194                   abort ();  /* Should be unreachable.  */
7195                 }
7196
7197               if (state == 3)
7198                 {
7199                   elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
7200                       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7201
7202                   elf32_arm_section_data (sec)->erratumcount += 1;
7203
7204                   newerr->u.b.vfp_insn = veneer_of_insn;
7205
7206                   switch (span_type)
7207                     {
7208                     case 'a':
7209                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
7210                       break;
7211
7212                     default:
7213                       abort ();
7214                     }
7215
7216                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
7217                                                first_fmac);
7218
7219                   newerr->vma = -1;
7220
7221                   newerr->next = sec_data->erratumlist;
7222                   sec_data->erratumlist = newerr;
7223
7224                   state = 0;
7225                 }
7226
7227               i = next_i;
7228             }
7229         }
7230
7231       if (contents != NULL
7232           && elf_section_data (sec)->this_hdr.contents != contents)
7233         free (contents);
7234       contents = NULL;
7235     }
7236
7237   return TRUE;
7238
7239 error_return:
7240   if (contents != NULL
7241       && elf_section_data (sec)->this_hdr.contents != contents)
7242     free (contents);
7243
7244   return FALSE;
7245 }
7246
7247 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
7248    after sections have been laid out, using specially-named symbols.  */
7249
7250 void
7251 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
7252                                           struct bfd_link_info *link_info)
7253 {
7254   asection *sec;
7255   struct elf32_arm_link_hash_table *globals;
7256   char *tmp_name;
7257
7258   if (bfd_link_relocatable (link_info))
7259     return;
7260
7261   /* Skip if this bfd does not correspond to an ELF image.  */
7262   if (! is_arm_elf (abfd))
7263     return;
7264
7265   globals = elf32_arm_hash_table (link_info);
7266   if (globals == NULL)
7267     return;
7268
7269   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7270                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7271
7272   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7273     {
7274       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7275       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
7276
7277       for (; errnode != NULL; errnode = errnode->next)
7278         {
7279           struct elf_link_hash_entry *myh;
7280           bfd_vma vma;
7281
7282           switch (errnode->type)
7283             {
7284             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
7285             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
7286               /* Find veneer symbol.  */
7287               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7288                        errnode->u.b.veneer->u.v.id);
7289
7290               myh = elf_link_hash_lookup
7291                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7292
7293               if (myh == NULL)
7294                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
7295                                          "`%s'"), abfd, tmp_name);
7296
7297               vma = myh->root.u.def.section->output_section->vma
7298                     + myh->root.u.def.section->output_offset
7299                     + myh->root.u.def.value;
7300
7301               errnode->u.b.veneer->vma = vma;
7302               break;
7303
7304             case VFP11_ERRATUM_ARM_VENEER:
7305             case VFP11_ERRATUM_THUMB_VENEER:
7306               /* Find return location.  */
7307               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7308                        errnode->u.v.id);
7309
7310               myh = elf_link_hash_lookup
7311                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7312
7313               if (myh == NULL)
7314                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
7315                                          "`%s'"), abfd, tmp_name);
7316
7317               vma = myh->root.u.def.section->output_section->vma
7318                     + myh->root.u.def.section->output_offset
7319                     + myh->root.u.def.value;
7320
7321               errnode->u.v.branch->vma = vma;
7322               break;
7323
7324             default:
7325               abort ();
7326             }
7327         }
7328     }
7329
7330   free (tmp_name);
7331 }
7332
7333 /* Find virtual-memory addresses for STM32L4XX erratum veneers and
7334    return locations after sections have been laid out, using
7335    specially-named symbols.  */
7336
7337 void
7338 bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
7339                                               struct bfd_link_info *link_info)
7340 {
7341   asection *sec;
7342   struct elf32_arm_link_hash_table *globals;
7343   char *tmp_name;
7344
7345   if (bfd_link_relocatable (link_info))
7346     return;
7347
7348   /* Skip if this bfd does not correspond to an ELF image.  */
7349   if (! is_arm_elf (abfd))
7350     return;
7351
7352   globals = elf32_arm_hash_table (link_info);
7353   if (globals == NULL)
7354     return;
7355
7356   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7357                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7358
7359   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7360     {
7361       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7362       elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
7363
7364       for (; errnode != NULL; errnode = errnode->next)
7365         {
7366           struct elf_link_hash_entry *myh;
7367           bfd_vma vma;
7368
7369           switch (errnode->type)
7370             {
7371             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
7372               /* Find veneer symbol.  */
7373               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7374                        errnode->u.b.veneer->u.v.id);
7375
7376               myh = elf_link_hash_lookup
7377                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7378
7379               if (myh == NULL)
7380                 (*_bfd_error_handler) (_("%B: unable to find STM32L4XX veneer "
7381                                          "`%s'"), abfd, tmp_name);
7382
7383               vma = myh->root.u.def.section->output_section->vma
7384                 + myh->root.u.def.section->output_offset
7385                 + myh->root.u.def.value;
7386
7387               errnode->u.b.veneer->vma = vma;
7388               break;
7389
7390             case STM32L4XX_ERRATUM_VENEER:
7391               /* Find return location.  */
7392               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7393                        errnode->u.v.id);
7394
7395               myh = elf_link_hash_lookup
7396                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7397
7398               if (myh == NULL)
7399                 (*_bfd_error_handler) (_("%B: unable to find STM32L4XX veneer "
7400                                          "`%s'"), abfd, tmp_name);
7401
7402               vma = myh->root.u.def.section->output_section->vma
7403                 + myh->root.u.def.section->output_offset
7404                 + myh->root.u.def.value;
7405
7406               errnode->u.v.branch->vma = vma;
7407               break;
7408
7409             default:
7410               abort ();
7411             }
7412         }
7413     }
7414
7415   free (tmp_name);
7416 }
7417
7418 static inline bfd_boolean
7419 is_thumb2_ldmia (const insn32 insn)
7420 {
7421   /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
7422      1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
7423   return (insn & 0xffd02000) == 0xe8900000;
7424 }
7425
7426 static inline bfd_boolean
7427 is_thumb2_ldmdb (const insn32 insn)
7428 {
7429   /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
7430      1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
7431   return (insn & 0xffd02000) == 0xe9100000;
7432 }
7433
7434 static inline bfd_boolean
7435 is_thumb2_vldm (const insn32 insn)
7436 {
7437   /* A6.5 Extension register load or store instruction
7438      A7.7.229
7439      We look only for the 32-bit registers case since the DP (64-bit
7440      registers) are not supported for STM32L4XX
7441      Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
7442      <list> is consecutive 32-bit registers
7443      1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
7444      if P==0 && U==1 && W==1 && Rn=1101 VPOP
7445      if PUW=010 || PUW=011 || PUW=101 VLDM.  */
7446   return
7447     ((insn & 0xfe100f00) == 0xec100a00)
7448     && /* (IA without !).  */
7449     (((((insn << 7) >> 28) & 0xd) == 0x4)
7450      /* (IA with !), includes VPOP (when reg number is SP).  */     
7451      || ((((insn << 7) >> 28) & 0xd) == 0x5)
7452      /* (DB with !).  */
7453      || ((((insn << 7) >> 28) & 0xd) == 0x9));
7454 }
7455
7456 /* STM STM32L4XX erratum : This function assumes that it receives an LDM or
7457    VLDM opcode and:
7458  - computes the number and the mode of memory accesses
7459  - decides if the replacement should be done:
7460    . replaces only if > 8-word accesses
7461    . or (testing purposes only) replaces all accesses.  */
7462
7463 static bfd_boolean
7464 stm32l4xx_need_create_replacing_stub (const insn32 insn,
7465                                       bfd_arm_stm32l4xx_fix stm32l4xx_fix)
7466 {
7467   int nb_regs = 0;
7468
7469   /* The field encoding the register list is the same for both LDMIA
7470      and LDMDB encodings.  */
7471   if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
7472     nb_regs = popcount (insn & 0x0000ffff);
7473   else if (is_thumb2_vldm (insn))
7474    nb_regs = (insn & 0xff);
7475
7476   /* DEFAULT mode accounts for the real bug condition situation,
7477      ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
7478   return
7479     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT) ? nb_regs > 8 :
7480     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL) ? TRUE : FALSE;
7481 }
7482
7483 /* Look for potentially-troublesome code sequences which might trigger
7484    the STM STM32L4XX erratum.  */
7485
7486 bfd_boolean
7487 bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
7488                                       struct bfd_link_info *link_info)
7489 {
7490   asection *sec;
7491   bfd_byte *contents = NULL;
7492   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7493
7494   if (globals == NULL)
7495     return FALSE;
7496
7497   /* If we are only performing a partial link do not bother
7498      to construct any glue.  */
7499   if (bfd_link_relocatable (link_info))
7500     return TRUE;
7501
7502   /* Skip if this bfd does not correspond to an ELF image.  */
7503   if (! is_arm_elf (abfd))
7504     return TRUE;
7505
7506   if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
7507     return TRUE;
7508
7509   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
7510   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
7511     return TRUE;
7512
7513   for (sec = abfd->sections; sec != NULL; sec = sec->next)
7514     {
7515       unsigned int i, span;
7516       struct _arm_elf_section_data *sec_data;
7517
7518       /* If we don't have executable progbits, we're not interested in this
7519          section.  Also skip if section is to be excluded.  */
7520       if (elf_section_type (sec) != SHT_PROGBITS
7521           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
7522           || (sec->flags & SEC_EXCLUDE) != 0
7523           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7524           || sec->output_section == bfd_abs_section_ptr
7525           || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
7526         continue;
7527
7528       sec_data = elf32_arm_section_data (sec);
7529
7530       if (sec_data->mapcount == 0)
7531         continue;
7532
7533       if (elf_section_data (sec)->this_hdr.contents != NULL)
7534         contents = elf_section_data (sec)->this_hdr.contents;
7535       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7536         goto error_return;
7537
7538       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
7539              elf32_arm_compare_mapping);
7540
7541       for (span = 0; span < sec_data->mapcount; span++)
7542         {
7543           unsigned int span_start = sec_data->map[span].vma;
7544           unsigned int span_end = (span == sec_data->mapcount - 1)
7545             ? sec->size : sec_data->map[span + 1].vma;
7546           char span_type = sec_data->map[span].type;
7547           int itblock_current_pos = 0;
7548
7549           /* Only Thumb2 mode need be supported with this CM4 specific
7550              code, we should not encounter any arm mode eg span_type
7551              != 'a'.  */
7552           if (span_type != 't')
7553             continue;
7554
7555           for (i = span_start; i < span_end;)
7556             {
7557               unsigned int insn = bfd_get_16 (abfd, &contents[i]);
7558               bfd_boolean insn_32bit = FALSE;
7559               bfd_boolean is_ldm = FALSE;
7560               bfd_boolean is_vldm = FALSE;
7561               bfd_boolean is_not_last_in_it_block = FALSE;
7562
7563               /* The first 16-bits of all 32-bit thumb2 instructions start
7564                  with opcode[15..13]=0b111 and the encoded op1 can be anything
7565                  except opcode[12..11]!=0b00.
7566                  See 32-bit Thumb instruction encoding.  */
7567               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
7568                 insn_32bit = TRUE;
7569
7570               /* Compute the predicate that tells if the instruction
7571                  is concerned by the IT block
7572                  - Creates an error if there is a ldm that is not
7573                    last in the IT block thus cannot be replaced
7574                  - Otherwise we can create a branch at the end of the
7575                    IT block, it will be controlled naturally by IT
7576                    with the proper pseudo-predicate
7577                  - So the only interesting predicate is the one that
7578                    tells that we are not on the last item of an IT
7579                    block.  */
7580               if (itblock_current_pos != 0)
7581                   is_not_last_in_it_block = !!--itblock_current_pos;
7582
7583               if (insn_32bit)
7584                 {
7585                   /* Load the rest of the insn (in manual-friendly order).  */
7586                   insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
7587                   is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
7588                   is_vldm = is_thumb2_vldm (insn);
7589
7590                   /* Veneers are created for (v)ldm depending on
7591                      option flags and memory accesses conditions; but
7592                      if the instruction is not the last instruction of
7593                      an IT block, we cannot create a jump there, so we
7594                      bail out.  */
7595                     if ((is_ldm || is_vldm) &&
7596                         stm32l4xx_need_create_replacing_stub
7597                         (insn, globals->stm32l4xx_fix))
7598                       {
7599                         if (is_not_last_in_it_block)
7600                           {
7601                             (*_bfd_error_handler)
7602                               /* Note - overlong line used here to allow for translation.  */
7603                               (_("\
7604 %B(%A+0x%lx): error: multiple load detected in non-last IT block instruction : STM32L4XX veneer cannot be generated.\n"
7605                                  "Use gcc option -mrestrict-it to generate only one instruction per IT block.\n"),
7606                                abfd, sec, (long)i);
7607                           }
7608                         else
7609                           {
7610                             elf32_stm32l4xx_erratum_list *newerr =
7611                               (elf32_stm32l4xx_erratum_list *)
7612                               bfd_zmalloc
7613                               (sizeof (elf32_stm32l4xx_erratum_list));
7614
7615                             elf32_arm_section_data (sec)
7616                               ->stm32l4xx_erratumcount += 1;
7617                             newerr->u.b.insn = insn;
7618                             /* We create only thumb branches.  */
7619                             newerr->type =
7620                               STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
7621                             record_stm32l4xx_erratum_veneer
7622                               (link_info, newerr, abfd, sec,
7623                                i,
7624                                is_ldm ?
7625                                STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
7626                                STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
7627                             newerr->vma = -1;
7628                             newerr->next = sec_data->stm32l4xx_erratumlist;
7629                             sec_data->stm32l4xx_erratumlist = newerr;
7630                           }
7631                       }
7632                 }
7633               else
7634                 {
7635                   /* A7.7.37 IT p208
7636                      IT blocks are only encoded in T1
7637                      Encoding T1: IT{x{y{z}}} <firstcond>
7638                      1 0 1 1 - 1 1 1 1 - firstcond - mask
7639                      if mask = '0000' then see 'related encodings'
7640                      We don't deal with UNPREDICTABLE, just ignore these.
7641                      There can be no nested IT blocks so an IT block
7642                      is naturally a new one for which it is worth
7643                      computing its size.  */
7644                   bfd_boolean is_newitblock = ((insn & 0xff00) == 0xbf00) &&
7645                     ((insn & 0x000f) != 0x0000);
7646                   /* If we have a new IT block we compute its size.  */
7647                   if (is_newitblock)
7648                     {
7649                       /* Compute the number of instructions controlled
7650                          by the IT block, it will be used to decide
7651                          whether we are inside an IT block or not.  */
7652                       unsigned int mask = insn & 0x000f;
7653                       itblock_current_pos = 4 - ctz (mask);
7654                     }
7655                 }
7656
7657               i += insn_32bit ? 4 : 2;
7658             }
7659         }
7660
7661       if (contents != NULL
7662           && elf_section_data (sec)->this_hdr.contents != contents)
7663         free (contents);
7664       contents = NULL;
7665     }
7666
7667   return TRUE;
7668
7669 error_return:
7670   if (contents != NULL
7671       && elf_section_data (sec)->this_hdr.contents != contents)
7672     free (contents);
7673
7674   return FALSE;
7675 }
7676
7677 /* Set target relocation values needed during linking.  */
7678
7679 void
7680 bfd_elf32_arm_set_target_relocs (struct bfd *output_bfd,
7681                                  struct bfd_link_info *link_info,
7682                                  int target1_is_rel,
7683                                  char * target2_type,
7684                                  int fix_v4bx,
7685                                  int use_blx,
7686                                  bfd_arm_vfp11_fix vfp11_fix,
7687                                  bfd_arm_stm32l4xx_fix stm32l4xx_fix,
7688                                  int no_enum_warn, int no_wchar_warn,
7689                                  int pic_veneer, int fix_cortex_a8,
7690                                  int fix_arm1176)
7691 {
7692   struct elf32_arm_link_hash_table *globals;
7693
7694   globals = elf32_arm_hash_table (link_info);
7695   if (globals == NULL)
7696     return;
7697
7698   globals->target1_is_rel = target1_is_rel;
7699   if (strcmp (target2_type, "rel") == 0)
7700     globals->target2_reloc = R_ARM_REL32;
7701   else if (strcmp (target2_type, "abs") == 0)
7702     globals->target2_reloc = R_ARM_ABS32;
7703   else if (strcmp (target2_type, "got-rel") == 0)
7704     globals->target2_reloc = R_ARM_GOT_PREL;
7705   else
7706     {
7707       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
7708                           target2_type);
7709     }
7710   globals->fix_v4bx = fix_v4bx;
7711   globals->use_blx |= use_blx;
7712   globals->vfp11_fix = vfp11_fix;
7713   globals->stm32l4xx_fix = stm32l4xx_fix;
7714   globals->pic_veneer = pic_veneer;
7715   globals->fix_cortex_a8 = fix_cortex_a8;
7716   globals->fix_arm1176 = fix_arm1176;
7717
7718   BFD_ASSERT (is_arm_elf (output_bfd));
7719   elf_arm_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
7720   elf_arm_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
7721 }
7722
7723 /* Replace the target offset of a Thumb bl or b.w instruction.  */
7724
7725 static void
7726 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
7727 {
7728   bfd_vma upper;
7729   bfd_vma lower;
7730   int reloc_sign;
7731
7732   BFD_ASSERT ((offset & 1) == 0);
7733
7734   upper = bfd_get_16 (abfd, insn);
7735   lower = bfd_get_16 (abfd, insn + 2);
7736   reloc_sign = (offset < 0) ? 1 : 0;
7737   upper = (upper & ~(bfd_vma) 0x7ff)
7738           | ((offset >> 12) & 0x3ff)
7739           | (reloc_sign << 10);
7740   lower = (lower & ~(bfd_vma) 0x2fff)
7741           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
7742           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
7743           | ((offset >> 1) & 0x7ff);
7744   bfd_put_16 (abfd, upper, insn);
7745   bfd_put_16 (abfd, lower, insn + 2);
7746 }
7747
7748 /* Thumb code calling an ARM function.  */
7749
7750 static int
7751 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
7752                          const char *           name,
7753                          bfd *                  input_bfd,
7754                          bfd *                  output_bfd,
7755                          asection *             input_section,
7756                          bfd_byte *             hit_data,
7757                          asection *             sym_sec,
7758                          bfd_vma                offset,
7759                          bfd_signed_vma         addend,
7760                          bfd_vma                val,
7761                          char **error_message)
7762 {
7763   asection * s = 0;
7764   bfd_vma my_offset;
7765   long int ret_offset;
7766   struct elf_link_hash_entry * myh;
7767   struct elf32_arm_link_hash_table * globals;
7768
7769   myh = find_thumb_glue (info, name, error_message);
7770   if (myh == NULL)
7771     return FALSE;
7772
7773   globals = elf32_arm_hash_table (info);
7774   BFD_ASSERT (globals != NULL);
7775   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7776
7777   my_offset = myh->root.u.def.value;
7778
7779   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
7780                               THUMB2ARM_GLUE_SECTION_NAME);
7781
7782   BFD_ASSERT (s != NULL);
7783   BFD_ASSERT (s->contents != NULL);
7784   BFD_ASSERT (s->output_section != NULL);
7785
7786   if ((my_offset & 0x01) == 0x01)
7787     {
7788       if (sym_sec != NULL
7789           && sym_sec->owner != NULL
7790           && !INTERWORK_FLAG (sym_sec->owner))
7791         {
7792           (*_bfd_error_handler)
7793             (_("%B(%s): warning: interworking not enabled.\n"
7794                "  first occurrence: %B: Thumb call to ARM"),
7795              sym_sec->owner, input_bfd, name);
7796
7797           return FALSE;
7798         }
7799
7800       --my_offset;
7801       myh->root.u.def.value = my_offset;
7802
7803       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
7804                       s->contents + my_offset);
7805
7806       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
7807                       s->contents + my_offset + 2);
7808
7809       ret_offset =
7810         /* Address of destination of the stub.  */
7811         ((bfd_signed_vma) val)
7812         - ((bfd_signed_vma)
7813            /* Offset from the start of the current section
7814               to the start of the stubs.  */
7815            (s->output_offset
7816             /* Offset of the start of this stub from the start of the stubs.  */
7817             + my_offset
7818             /* Address of the start of the current section.  */
7819             + s->output_section->vma)
7820            /* The branch instruction is 4 bytes into the stub.  */
7821            + 4
7822            /* ARM branches work from the pc of the instruction + 8.  */
7823            + 8);
7824
7825       put_arm_insn (globals, output_bfd,
7826                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
7827                     s->contents + my_offset + 4);
7828     }
7829
7830   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
7831
7832   /* Now go back and fix up the original BL insn to point to here.  */
7833   ret_offset =
7834     /* Address of where the stub is located.  */
7835     (s->output_section->vma + s->output_offset + my_offset)
7836      /* Address of where the BL is located.  */
7837     - (input_section->output_section->vma + input_section->output_offset
7838        + offset)
7839     /* Addend in the relocation.  */
7840     - addend
7841     /* Biassing for PC-relative addressing.  */
7842     - 8;
7843
7844   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
7845
7846   return TRUE;
7847 }
7848
7849 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
7850
7851 static struct elf_link_hash_entry *
7852 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
7853                              const char *           name,
7854                              bfd *                  input_bfd,
7855                              bfd *                  output_bfd,
7856                              asection *             sym_sec,
7857                              bfd_vma                val,
7858                              asection *             s,
7859                              char **                error_message)
7860 {
7861   bfd_vma my_offset;
7862   long int ret_offset;
7863   struct elf_link_hash_entry * myh;
7864   struct elf32_arm_link_hash_table * globals;
7865
7866   myh = find_arm_glue (info, name, error_message);
7867   if (myh == NULL)
7868     return NULL;
7869
7870   globals = elf32_arm_hash_table (info);
7871   BFD_ASSERT (globals != NULL);
7872   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7873
7874   my_offset = myh->root.u.def.value;
7875
7876   if ((my_offset & 0x01) == 0x01)
7877     {
7878       if (sym_sec != NULL
7879           && sym_sec->owner != NULL
7880           && !INTERWORK_FLAG (sym_sec->owner))
7881         {
7882           (*_bfd_error_handler)
7883             (_("%B(%s): warning: interworking not enabled.\n"
7884                "  first occurrence: %B: arm call to thumb"),
7885              sym_sec->owner, input_bfd, name);
7886         }
7887
7888       --my_offset;
7889       myh->root.u.def.value = my_offset;
7890
7891       if (bfd_link_pic (info)
7892           || globals->root.is_relocatable_executable
7893           || globals->pic_veneer)
7894         {
7895           /* For relocatable objects we can't use absolute addresses,
7896              so construct the address from a relative offset.  */
7897           /* TODO: If the offset is small it's probably worth
7898              constructing the address with adds.  */
7899           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
7900                         s->contents + my_offset);
7901           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
7902                         s->contents + my_offset + 4);
7903           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
7904                         s->contents + my_offset + 8);
7905           /* Adjust the offset by 4 for the position of the add,
7906              and 8 for the pipeline offset.  */
7907           ret_offset = (val - (s->output_offset
7908                                + s->output_section->vma
7909                                + my_offset + 12))
7910                        | 1;
7911           bfd_put_32 (output_bfd, ret_offset,
7912                       s->contents + my_offset + 12);
7913         }
7914       else if (globals->use_blx)
7915         {
7916           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
7917                         s->contents + my_offset);
7918
7919           /* It's a thumb address.  Add the low order bit.  */
7920           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
7921                       s->contents + my_offset + 4);
7922         }
7923       else
7924         {
7925           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
7926                         s->contents + my_offset);
7927
7928           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
7929                         s->contents + my_offset + 4);
7930
7931           /* It's a thumb address.  Add the low order bit.  */
7932           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
7933                       s->contents + my_offset + 8);
7934
7935           my_offset += 12;
7936         }
7937     }
7938
7939   BFD_ASSERT (my_offset <= globals->arm_glue_size);
7940
7941   return myh;
7942 }
7943
7944 /* Arm code calling a Thumb function.  */
7945
7946 static int
7947 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
7948                          const char *           name,
7949                          bfd *                  input_bfd,
7950                          bfd *                  output_bfd,
7951                          asection *             input_section,
7952                          bfd_byte *             hit_data,
7953                          asection *             sym_sec,
7954                          bfd_vma                offset,
7955                          bfd_signed_vma         addend,
7956                          bfd_vma                val,
7957                          char **error_message)
7958 {
7959   unsigned long int tmp;
7960   bfd_vma my_offset;
7961   asection * s;
7962   long int ret_offset;
7963   struct elf_link_hash_entry * myh;
7964   struct elf32_arm_link_hash_table * globals;
7965
7966   globals = elf32_arm_hash_table (info);
7967   BFD_ASSERT (globals != NULL);
7968   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7969
7970   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
7971                               ARM2THUMB_GLUE_SECTION_NAME);
7972   BFD_ASSERT (s != NULL);
7973   BFD_ASSERT (s->contents != NULL);
7974   BFD_ASSERT (s->output_section != NULL);
7975
7976   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
7977                                      sym_sec, val, s, error_message);
7978   if (!myh)
7979     return FALSE;
7980
7981   my_offset = myh->root.u.def.value;
7982   tmp = bfd_get_32 (input_bfd, hit_data);
7983   tmp = tmp & 0xFF000000;
7984
7985   /* Somehow these are both 4 too far, so subtract 8.  */
7986   ret_offset = (s->output_offset
7987                 + my_offset
7988                 + s->output_section->vma
7989                 - (input_section->output_offset
7990                    + input_section->output_section->vma
7991                    + offset + addend)
7992                 - 8);
7993
7994   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
7995
7996   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
7997
7998   return TRUE;
7999 }
8000
8001 /* Populate Arm stub for an exported Thumb function.  */
8002
8003 static bfd_boolean
8004 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
8005 {
8006   struct bfd_link_info * info = (struct bfd_link_info *) inf;
8007   asection * s;
8008   struct elf_link_hash_entry * myh;
8009   struct elf32_arm_link_hash_entry *eh;
8010   struct elf32_arm_link_hash_table * globals;
8011   asection *sec;
8012   bfd_vma val;
8013   char *error_message;
8014
8015   eh = elf32_arm_hash_entry (h);
8016   /* Allocate stubs for exported Thumb functions on v4t.  */
8017   if (eh->export_glue == NULL)
8018     return TRUE;
8019
8020   globals = elf32_arm_hash_table (info);
8021   BFD_ASSERT (globals != NULL);
8022   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
8023
8024   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
8025                               ARM2THUMB_GLUE_SECTION_NAME);
8026   BFD_ASSERT (s != NULL);
8027   BFD_ASSERT (s->contents != NULL);
8028   BFD_ASSERT (s->output_section != NULL);
8029
8030   sec = eh->export_glue->root.u.def.section;
8031
8032   BFD_ASSERT (sec->output_section != NULL);
8033
8034   val = eh->export_glue->root.u.def.value + sec->output_offset
8035         + sec->output_section->vma;
8036
8037   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
8038                                      h->root.u.def.section->owner,
8039                                      globals->obfd, sec, val, s,
8040                                      &error_message);
8041   BFD_ASSERT (myh);
8042   return TRUE;
8043 }
8044
8045 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
8046
8047 static bfd_vma
8048 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
8049 {
8050   bfd_byte *p;
8051   bfd_vma glue_addr;
8052   asection *s;
8053   struct elf32_arm_link_hash_table *globals;
8054
8055   globals = elf32_arm_hash_table (info);
8056   BFD_ASSERT (globals != NULL);
8057   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
8058
8059   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
8060                               ARM_BX_GLUE_SECTION_NAME);
8061   BFD_ASSERT (s != NULL);
8062   BFD_ASSERT (s->contents != NULL);
8063   BFD_ASSERT (s->output_section != NULL);
8064
8065   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
8066
8067   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
8068
8069   if ((globals->bx_glue_offset[reg] & 1) == 0)
8070     {
8071       p = s->contents + glue_addr;
8072       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
8073       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
8074       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
8075       globals->bx_glue_offset[reg] |= 1;
8076     }
8077
8078   return glue_addr + s->output_section->vma + s->output_offset;
8079 }
8080
8081 /* Generate Arm stubs for exported Thumb symbols.  */
8082 static void
8083 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
8084                                   struct bfd_link_info *link_info)
8085 {
8086   struct elf32_arm_link_hash_table * globals;
8087
8088   if (link_info == NULL)
8089     /* Ignore this if we are not called by the ELF backend linker.  */
8090     return;
8091
8092   globals = elf32_arm_hash_table (link_info);
8093   if (globals == NULL)
8094     return;
8095
8096   /* If blx is available then exported Thumb symbols are OK and there is
8097      nothing to do.  */
8098   if (globals->use_blx)
8099     return;
8100
8101   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
8102                           link_info);
8103 }
8104
8105 /* Reserve space for COUNT dynamic relocations in relocation selection
8106    SRELOC.  */
8107
8108 static void
8109 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
8110                               bfd_size_type count)
8111 {
8112   struct elf32_arm_link_hash_table *htab;
8113
8114   htab = elf32_arm_hash_table (info);
8115   BFD_ASSERT (htab->root.dynamic_sections_created);
8116   if (sreloc == NULL)
8117     abort ();
8118   sreloc->size += RELOC_SIZE (htab) * count;
8119 }
8120
8121 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
8122    dynamic, the relocations should go in SRELOC, otherwise they should
8123    go in the special .rel.iplt section.  */
8124
8125 static void
8126 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
8127                             bfd_size_type count)
8128 {
8129   struct elf32_arm_link_hash_table *htab;
8130
8131   htab = elf32_arm_hash_table (info);
8132   if (!htab->root.dynamic_sections_created)
8133     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
8134   else
8135     {
8136       BFD_ASSERT (sreloc != NULL);
8137       sreloc->size += RELOC_SIZE (htab) * count;
8138     }
8139 }
8140
8141 /* Add relocation REL to the end of relocation section SRELOC.  */
8142
8143 static void
8144 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
8145                         asection *sreloc, Elf_Internal_Rela *rel)
8146 {
8147   bfd_byte *loc;
8148   struct elf32_arm_link_hash_table *htab;
8149
8150   htab = elf32_arm_hash_table (info);
8151   if (!htab->root.dynamic_sections_created
8152       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
8153     sreloc = htab->root.irelplt;
8154   if (sreloc == NULL)
8155     abort ();
8156   loc = sreloc->contents;
8157   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
8158   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
8159     abort ();
8160   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
8161 }
8162
8163 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
8164    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
8165    to .plt.  */
8166
8167 static void
8168 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
8169                               bfd_boolean is_iplt_entry,
8170                               union gotplt_union *root_plt,
8171                               struct arm_plt_info *arm_plt)
8172 {
8173   struct elf32_arm_link_hash_table *htab;
8174   asection *splt;
8175   asection *sgotplt;
8176
8177   htab = elf32_arm_hash_table (info);
8178
8179   if (is_iplt_entry)
8180     {
8181       splt = htab->root.iplt;
8182       sgotplt = htab->root.igotplt;
8183
8184       /* NaCl uses a special first entry in .iplt too.  */
8185       if (htab->nacl_p && splt->size == 0)
8186         splt->size += htab->plt_header_size;
8187
8188       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
8189       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
8190     }
8191   else
8192     {
8193       splt = htab->root.splt;
8194       sgotplt = htab->root.sgotplt;
8195
8196       /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
8197       elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
8198
8199       /* If this is the first .plt entry, make room for the special
8200          first entry.  */
8201       if (splt->size == 0)
8202         splt->size += htab->plt_header_size;
8203
8204       htab->next_tls_desc_index++;
8205     }
8206
8207   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
8208   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
8209     splt->size += PLT_THUMB_STUB_SIZE;
8210   root_plt->offset = splt->size;
8211   splt->size += htab->plt_entry_size;
8212
8213   if (!htab->symbian_p)
8214     {
8215       /* We also need to make an entry in the .got.plt section, which
8216          will be placed in the .got section by the linker script.  */
8217       if (is_iplt_entry)
8218         arm_plt->got_offset = sgotplt->size;
8219       else
8220         arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
8221       sgotplt->size += 4;
8222     }
8223 }
8224
8225 static bfd_vma
8226 arm_movw_immediate (bfd_vma value)
8227 {
8228   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
8229 }
8230
8231 static bfd_vma
8232 arm_movt_immediate (bfd_vma value)
8233 {
8234   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
8235 }
8236
8237 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
8238    the entry lives in .iplt and resolves to (*SYM_VALUE)().
8239    Otherwise, DYNINDX is the index of the symbol in the dynamic
8240    symbol table and SYM_VALUE is undefined.
8241
8242    ROOT_PLT points to the offset of the PLT entry from the start of its
8243    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
8244    bookkeeping information.
8245
8246    Returns FALSE if there was a problem.  */
8247
8248 static bfd_boolean
8249 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
8250                               union gotplt_union *root_plt,
8251                               struct arm_plt_info *arm_plt,
8252                               int dynindx, bfd_vma sym_value)
8253 {
8254   struct elf32_arm_link_hash_table *htab;
8255   asection *sgot;
8256   asection *splt;
8257   asection *srel;
8258   bfd_byte *loc;
8259   bfd_vma plt_index;
8260   Elf_Internal_Rela rel;
8261   bfd_vma plt_header_size;
8262   bfd_vma got_header_size;
8263
8264   htab = elf32_arm_hash_table (info);
8265
8266   /* Pick the appropriate sections and sizes.  */
8267   if (dynindx == -1)
8268     {
8269       splt = htab->root.iplt;
8270       sgot = htab->root.igotplt;
8271       srel = htab->root.irelplt;
8272
8273       /* There are no reserved entries in .igot.plt, and no special
8274          first entry in .iplt.  */
8275       got_header_size = 0;
8276       plt_header_size = 0;
8277     }
8278   else
8279     {
8280       splt = htab->root.splt;
8281       sgot = htab->root.sgotplt;
8282       srel = htab->root.srelplt;
8283
8284       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
8285       plt_header_size = htab->plt_header_size;
8286     }
8287   BFD_ASSERT (splt != NULL && srel != NULL);
8288
8289   /* Fill in the entry in the procedure linkage table.  */
8290   if (htab->symbian_p)
8291     {
8292       BFD_ASSERT (dynindx >= 0);
8293       put_arm_insn (htab, output_bfd,
8294                     elf32_arm_symbian_plt_entry[0],
8295                     splt->contents + root_plt->offset);
8296       bfd_put_32 (output_bfd,
8297                   elf32_arm_symbian_plt_entry[1],
8298                   splt->contents + root_plt->offset + 4);
8299
8300       /* Fill in the entry in the .rel.plt section.  */
8301       rel.r_offset = (splt->output_section->vma
8302                       + splt->output_offset
8303                       + root_plt->offset + 4);
8304       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
8305
8306       /* Get the index in the procedure linkage table which
8307          corresponds to this symbol.  This is the index of this symbol
8308          in all the symbols for which we are making plt entries.  The
8309          first entry in the procedure linkage table is reserved.  */
8310       plt_index = ((root_plt->offset - plt_header_size)
8311                    / htab->plt_entry_size);
8312     }
8313   else
8314     {
8315       bfd_vma got_offset, got_address, plt_address;
8316       bfd_vma got_displacement, initial_got_entry;
8317       bfd_byte * ptr;
8318
8319       BFD_ASSERT (sgot != NULL);
8320
8321       /* Get the offset into the .(i)got.plt table of the entry that
8322          corresponds to this function.  */
8323       got_offset = (arm_plt->got_offset & -2);
8324
8325       /* Get the index in the procedure linkage table which
8326          corresponds to this symbol.  This is the index of this symbol
8327          in all the symbols for which we are making plt entries.
8328          After the reserved .got.plt entries, all symbols appear in
8329          the same order as in .plt.  */
8330       plt_index = (got_offset - got_header_size) / 4;
8331
8332       /* Calculate the address of the GOT entry.  */
8333       got_address = (sgot->output_section->vma
8334                      + sgot->output_offset
8335                      + got_offset);
8336
8337       /* ...and the address of the PLT entry.  */
8338       plt_address = (splt->output_section->vma
8339                      + splt->output_offset
8340                      + root_plt->offset);
8341
8342       ptr = splt->contents + root_plt->offset;
8343       if (htab->vxworks_p && bfd_link_pic (info))
8344         {
8345           unsigned int i;
8346           bfd_vma val;
8347
8348           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
8349             {
8350               val = elf32_arm_vxworks_shared_plt_entry[i];
8351               if (i == 2)
8352                 val |= got_address - sgot->output_section->vma;
8353               if (i == 5)
8354                 val |= plt_index * RELOC_SIZE (htab);
8355               if (i == 2 || i == 5)
8356                 bfd_put_32 (output_bfd, val, ptr);
8357               else
8358                 put_arm_insn (htab, output_bfd, val, ptr);
8359             }
8360         }
8361       else if (htab->vxworks_p)
8362         {
8363           unsigned int i;
8364           bfd_vma val;
8365
8366           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
8367             {
8368               val = elf32_arm_vxworks_exec_plt_entry[i];
8369               if (i == 2)
8370                 val |= got_address;
8371               if (i == 4)
8372                 val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
8373               if (i == 5)
8374                 val |= plt_index * RELOC_SIZE (htab);
8375               if (i == 2 || i == 5)
8376                 bfd_put_32 (output_bfd, val, ptr);
8377               else
8378                 put_arm_insn (htab, output_bfd, val, ptr);
8379             }
8380
8381           loc = (htab->srelplt2->contents
8382                  + (plt_index * 2 + 1) * RELOC_SIZE (htab));
8383
8384           /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
8385              referencing the GOT for this PLT entry.  */
8386           rel.r_offset = plt_address + 8;
8387           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
8388           rel.r_addend = got_offset;
8389           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8390           loc += RELOC_SIZE (htab);
8391
8392           /* Create the R_ARM_ABS32 relocation referencing the
8393              beginning of the PLT for this GOT entry.  */
8394           rel.r_offset = got_address;
8395           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
8396           rel.r_addend = 0;
8397           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8398         }
8399       else if (htab->nacl_p)
8400         {
8401           /* Calculate the displacement between the PLT slot and the
8402              common tail that's part of the special initial PLT slot.  */
8403           int32_t tail_displacement
8404             = ((splt->output_section->vma + splt->output_offset
8405                 + ARM_NACL_PLT_TAIL_OFFSET)
8406                - (plt_address + htab->plt_entry_size + 4));
8407           BFD_ASSERT ((tail_displacement & 3) == 0);
8408           tail_displacement >>= 2;
8409
8410           BFD_ASSERT ((tail_displacement & 0xff000000) == 0
8411                       || (-tail_displacement & 0xff000000) == 0);
8412
8413           /* Calculate the displacement between the PLT slot and the entry
8414              in the GOT.  The offset accounts for the value produced by
8415              adding to pc in the penultimate instruction of the PLT stub.  */
8416           got_displacement = (got_address
8417                               - (plt_address + htab->plt_entry_size));
8418
8419           /* NaCl does not support interworking at all.  */
8420           BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
8421
8422           put_arm_insn (htab, output_bfd,
8423                         elf32_arm_nacl_plt_entry[0]
8424                         | arm_movw_immediate (got_displacement),
8425                         ptr + 0);
8426           put_arm_insn (htab, output_bfd,
8427                         elf32_arm_nacl_plt_entry[1]
8428                         | arm_movt_immediate (got_displacement),
8429                         ptr + 4);
8430           put_arm_insn (htab, output_bfd,
8431                         elf32_arm_nacl_plt_entry[2],
8432                         ptr + 8);
8433           put_arm_insn (htab, output_bfd,
8434                         elf32_arm_nacl_plt_entry[3]
8435                         | (tail_displacement & 0x00ffffff),
8436                         ptr + 12);
8437         }
8438       else if (using_thumb_only (htab))
8439         {
8440           /* PR ld/16017: Generate thumb only PLT entries.  */
8441           if (!using_thumb2 (htab))
8442             {
8443               /* FIXME: We ought to be able to generate thumb-1 PLT
8444                  instructions...  */
8445               _bfd_error_handler (_("%B: Warning: thumb-1 mode PLT generation not currently supported"),
8446                                   output_bfd);
8447               return FALSE;
8448             }
8449
8450           /* Calculate the displacement between the PLT slot and the entry in
8451              the GOT.  The 12-byte offset accounts for the value produced by
8452              adding to pc in the 3rd instruction of the PLT stub.  */
8453           got_displacement = got_address - (plt_address + 12);
8454
8455           /* As we are using 32 bit instructions we have to use 'put_arm_insn'
8456              instead of 'put_thumb_insn'.  */
8457           put_arm_insn (htab, output_bfd,
8458                         elf32_thumb2_plt_entry[0]
8459                         | ((got_displacement & 0x000000ff) << 16)
8460                         | ((got_displacement & 0x00000700) << 20)
8461                         | ((got_displacement & 0x00000800) >>  1)
8462                         | ((got_displacement & 0x0000f000) >> 12),
8463                         ptr + 0);
8464           put_arm_insn (htab, output_bfd,
8465                         elf32_thumb2_plt_entry[1]
8466                         | ((got_displacement & 0x00ff0000)      )
8467                         | ((got_displacement & 0x07000000) <<  4)
8468                         | ((got_displacement & 0x08000000) >> 17)
8469                         | ((got_displacement & 0xf0000000) >> 28),
8470                         ptr + 4);
8471           put_arm_insn (htab, output_bfd,
8472                         elf32_thumb2_plt_entry[2],
8473                         ptr + 8);
8474           put_arm_insn (htab, output_bfd,
8475                         elf32_thumb2_plt_entry[3],
8476                         ptr + 12);
8477         }
8478       else
8479         {
8480           /* Calculate the displacement between the PLT slot and the
8481              entry in the GOT.  The eight-byte offset accounts for the
8482              value produced by adding to pc in the first instruction
8483              of the PLT stub.  */
8484           got_displacement = got_address - (plt_address + 8);
8485
8486           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
8487             {
8488               put_thumb_insn (htab, output_bfd,
8489                               elf32_arm_plt_thumb_stub[0], ptr - 4);
8490               put_thumb_insn (htab, output_bfd,
8491                               elf32_arm_plt_thumb_stub[1], ptr - 2);
8492             }
8493
8494           if (!elf32_arm_use_long_plt_entry)
8495             {
8496               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
8497
8498               put_arm_insn (htab, output_bfd,
8499                             elf32_arm_plt_entry_short[0]
8500                             | ((got_displacement & 0x0ff00000) >> 20),
8501                             ptr + 0);
8502               put_arm_insn (htab, output_bfd,
8503                             elf32_arm_plt_entry_short[1]
8504                             | ((got_displacement & 0x000ff000) >> 12),
8505                             ptr+ 4);
8506               put_arm_insn (htab, output_bfd,
8507                             elf32_arm_plt_entry_short[2]
8508                             | (got_displacement & 0x00000fff),
8509                             ptr + 8);
8510 #ifdef FOUR_WORD_PLT
8511               bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
8512 #endif
8513             }
8514           else
8515             {
8516               put_arm_insn (htab, output_bfd,
8517                             elf32_arm_plt_entry_long[0]
8518                             | ((got_displacement & 0xf0000000) >> 28),
8519                             ptr + 0);
8520               put_arm_insn (htab, output_bfd,
8521                             elf32_arm_plt_entry_long[1]
8522                             | ((got_displacement & 0x0ff00000) >> 20),
8523                             ptr + 4);
8524               put_arm_insn (htab, output_bfd,
8525                             elf32_arm_plt_entry_long[2]
8526                             | ((got_displacement & 0x000ff000) >> 12),
8527                             ptr+ 8);
8528               put_arm_insn (htab, output_bfd,
8529                             elf32_arm_plt_entry_long[3]
8530                             | (got_displacement & 0x00000fff),
8531                             ptr + 12);
8532             }
8533         }
8534
8535       /* Fill in the entry in the .rel(a).(i)plt section.  */
8536       rel.r_offset = got_address;
8537       rel.r_addend = 0;
8538       if (dynindx == -1)
8539         {
8540           /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
8541              The dynamic linker or static executable then calls SYM_VALUE
8542              to determine the correct run-time value of the .igot.plt entry.  */
8543           rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
8544           initial_got_entry = sym_value;
8545         }
8546       else
8547         {
8548           rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
8549           initial_got_entry = (splt->output_section->vma
8550                                + splt->output_offset);
8551         }
8552
8553       /* Fill in the entry in the global offset table.  */
8554       bfd_put_32 (output_bfd, initial_got_entry,
8555                   sgot->contents + got_offset);
8556     }
8557
8558   if (dynindx == -1)
8559     elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
8560   else
8561     {
8562       loc = srel->contents + plt_index * RELOC_SIZE (htab);
8563       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8564     }
8565
8566   return TRUE;
8567 }
8568
8569 /* Some relocations map to different relocations depending on the
8570    target.  Return the real relocation.  */
8571
8572 static int
8573 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
8574                      int r_type)
8575 {
8576   switch (r_type)
8577     {
8578     case R_ARM_TARGET1:
8579       if (globals->target1_is_rel)
8580         return R_ARM_REL32;
8581       else
8582         return R_ARM_ABS32;
8583
8584     case R_ARM_TARGET2:
8585       return globals->target2_reloc;
8586
8587     default:
8588       return r_type;
8589     }
8590 }
8591
8592 /* Return the base VMA address which should be subtracted from real addresses
8593    when resolving @dtpoff relocation.
8594    This is PT_TLS segment p_vaddr.  */
8595
8596 static bfd_vma
8597 dtpoff_base (struct bfd_link_info *info)
8598 {
8599   /* If tls_sec is NULL, we should have signalled an error already.  */
8600   if (elf_hash_table (info)->tls_sec == NULL)
8601     return 0;
8602   return elf_hash_table (info)->tls_sec->vma;
8603 }
8604
8605 /* Return the relocation value for @tpoff relocation
8606    if STT_TLS virtual address is ADDRESS.  */
8607
8608 static bfd_vma
8609 tpoff (struct bfd_link_info *info, bfd_vma address)
8610 {
8611   struct elf_link_hash_table *htab = elf_hash_table (info);
8612   bfd_vma base;
8613
8614   /* If tls_sec is NULL, we should have signalled an error already.  */
8615   if (htab->tls_sec == NULL)
8616     return 0;
8617   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
8618   return address - htab->tls_sec->vma + base;
8619 }
8620
8621 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
8622    VALUE is the relocation value.  */
8623
8624 static bfd_reloc_status_type
8625 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
8626 {
8627   if (value > 0xfff)
8628     return bfd_reloc_overflow;
8629
8630   value |= bfd_get_32 (abfd, data) & 0xfffff000;
8631   bfd_put_32 (abfd, value, data);
8632   return bfd_reloc_ok;
8633 }
8634
8635 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
8636    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
8637    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
8638
8639    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
8640    is to then call final_link_relocate.  Return other values in the
8641    case of error.
8642
8643    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
8644    the pre-relaxed code.  It would be nice if the relocs were updated
8645    to match the optimization.   */
8646
8647 static bfd_reloc_status_type
8648 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
8649                      bfd *input_bfd, asection *input_sec, bfd_byte *contents,
8650                      Elf_Internal_Rela *rel, unsigned long is_local)
8651 {
8652   unsigned long insn;
8653
8654   switch (ELF32_R_TYPE (rel->r_info))
8655     {
8656     default:
8657       return bfd_reloc_notsupported;
8658
8659     case R_ARM_TLS_GOTDESC:
8660       if (is_local)
8661         insn = 0;
8662       else
8663         {
8664           insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
8665           if (insn & 1)
8666             insn -= 5; /* THUMB */
8667           else
8668             insn -= 8; /* ARM */
8669         }
8670       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8671       return bfd_reloc_continue;
8672
8673     case R_ARM_THM_TLS_DESCSEQ:
8674       /* Thumb insn.  */
8675       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
8676       if ((insn & 0xff78) == 0x4478)      /* add rx, pc */
8677         {
8678           if (is_local)
8679             /* nop */
8680             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8681         }
8682       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
8683         {
8684           if (is_local)
8685             /* nop */
8686             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8687           else
8688             /* ldr rx,[ry] */
8689             bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
8690         }
8691       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
8692         {
8693           if (is_local)
8694             /* nop */
8695             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
8696           else
8697             /* mov r0, rx */
8698             bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
8699                         contents + rel->r_offset);
8700         }
8701       else
8702         {
8703           if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
8704             /* It's a 32 bit instruction, fetch the rest of it for
8705                error generation.  */
8706             insn = (insn << 16)
8707               | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
8708           (*_bfd_error_handler)
8709             (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' in TLS trampoline"),
8710              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
8711           return bfd_reloc_notsupported;
8712         }
8713       break;
8714
8715     case R_ARM_TLS_DESCSEQ:
8716       /* arm insn.  */
8717       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
8718       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
8719         {
8720           if (is_local)
8721             /* mov rx, ry */
8722             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
8723                         contents + rel->r_offset);
8724         }
8725       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
8726         {
8727           if (is_local)
8728             /* nop */
8729             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
8730           else
8731             /* ldr rx,[ry] */
8732             bfd_put_32 (input_bfd, insn & 0xfffff000,
8733                         contents + rel->r_offset);
8734         }
8735       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
8736         {
8737           if (is_local)
8738             /* nop */
8739             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
8740           else
8741             /* mov r0, rx */
8742             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
8743                         contents + rel->r_offset);
8744         }
8745       else
8746         {
8747           (*_bfd_error_handler)
8748             (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' in TLS trampoline"),
8749              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
8750           return bfd_reloc_notsupported;
8751         }
8752       break;
8753
8754     case R_ARM_TLS_CALL:
8755       /* GD->IE relaxation, turn the instruction into 'nop' or
8756          'ldr r0, [pc,r0]'  */
8757       insn = is_local ? 0xe1a00000 : 0xe79f0000;
8758       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8759       break;
8760
8761     case R_ARM_THM_TLS_CALL:
8762       /* GD->IE relaxation.  */
8763       if (!is_local)
8764         /* add r0,pc; ldr r0, [r0]  */
8765         insn = 0x44786800;
8766       else if (arch_has_thumb2_nop (globals))
8767         /* nop.w */
8768         insn = 0xf3af8000;
8769       else
8770         /* nop; nop */
8771         insn = 0xbf00bf00;
8772
8773       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
8774       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
8775       break;
8776     }
8777   return bfd_reloc_ok;
8778 }
8779
8780 /* For a given value of n, calculate the value of G_n as required to
8781    deal with group relocations.  We return it in the form of an
8782    encoded constant-and-rotation, together with the final residual.  If n is
8783    specified as less than zero, then final_residual is filled with the
8784    input value and no further action is performed.  */
8785
8786 static bfd_vma
8787 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
8788 {
8789   int current_n;
8790   bfd_vma g_n;
8791   bfd_vma encoded_g_n = 0;
8792   bfd_vma residual = value; /* Also known as Y_n.  */
8793
8794   for (current_n = 0; current_n <= n; current_n++)
8795     {
8796       int shift;
8797
8798       /* Calculate which part of the value to mask.  */
8799       if (residual == 0)
8800         shift = 0;
8801       else
8802         {
8803           int msb;
8804
8805           /* Determine the most significant bit in the residual and
8806              align the resulting value to a 2-bit boundary.  */
8807           for (msb = 30; msb >= 0; msb -= 2)
8808             if (residual & (3 << msb))
8809               break;
8810
8811           /* The desired shift is now (msb - 6), or zero, whichever
8812              is the greater.  */
8813           shift = msb - 6;
8814           if (shift < 0)
8815             shift = 0;
8816         }
8817
8818       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
8819       g_n = residual & (0xff << shift);
8820       encoded_g_n = (g_n >> shift)
8821                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
8822
8823       /* Calculate the residual for the next time around.  */
8824       residual &= ~g_n;
8825     }
8826
8827   *final_residual = residual;
8828
8829   return encoded_g_n;
8830 }
8831
8832 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
8833    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
8834
8835 static int
8836 identify_add_or_sub (bfd_vma insn)
8837 {
8838   int opcode = insn & 0x1e00000;
8839
8840   if (opcode == 1 << 23) /* ADD */
8841     return 1;
8842
8843   if (opcode == 1 << 22) /* SUB */
8844     return -1;
8845
8846   return 0;
8847 }
8848
8849 /* Perform a relocation as part of a final link.  */
8850
8851 static bfd_reloc_status_type
8852 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
8853                                bfd *                        input_bfd,
8854                                bfd *                        output_bfd,
8855                                asection *                   input_section,
8856                                bfd_byte *                   contents,
8857                                Elf_Internal_Rela *          rel,
8858                                bfd_vma                      value,
8859                                struct bfd_link_info *       info,
8860                                asection *                   sym_sec,
8861                                const char *                 sym_name,
8862                                unsigned char                st_type,
8863                                enum arm_st_branch_type      branch_type,
8864                                struct elf_link_hash_entry * h,
8865                                bfd_boolean *                unresolved_reloc_p,
8866                                char **                      error_message)
8867 {
8868   unsigned long                 r_type = howto->type;
8869   unsigned long                 r_symndx;
8870   bfd_byte *                    hit_data = contents + rel->r_offset;
8871   bfd_vma *                     local_got_offsets;
8872   bfd_vma *                     local_tlsdesc_gotents;
8873   asection *                    sgot;
8874   asection *                    splt;
8875   asection *                    sreloc = NULL;
8876   asection *                    srelgot;
8877   bfd_vma                       addend;
8878   bfd_signed_vma                signed_addend;
8879   unsigned char                 dynreloc_st_type;
8880   bfd_vma                       dynreloc_value;
8881   struct elf32_arm_link_hash_table * globals;
8882   struct elf32_arm_link_hash_entry *eh;
8883   union gotplt_union           *root_plt;
8884   struct arm_plt_info          *arm_plt;
8885   bfd_vma                       plt_offset;
8886   bfd_vma                       gotplt_offset;
8887   bfd_boolean                   has_iplt_entry;
8888
8889   globals = elf32_arm_hash_table (info);
8890   if (globals == NULL)
8891     return bfd_reloc_notsupported;
8892
8893   BFD_ASSERT (is_arm_elf (input_bfd));
8894
8895   /* Some relocation types map to different relocations depending on the
8896      target.  We pick the right one here.  */
8897   r_type = arm_real_reloc_type (globals, r_type);
8898
8899   /* It is possible to have linker relaxations on some TLS access
8900      models.  Update our information here.  */
8901   r_type = elf32_arm_tls_transition (info, r_type, h);
8902
8903   if (r_type != howto->type)
8904     howto = elf32_arm_howto_from_type (r_type);
8905
8906   eh = (struct elf32_arm_link_hash_entry *) h;
8907   sgot = globals->root.sgot;
8908   local_got_offsets = elf_local_got_offsets (input_bfd);
8909   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
8910
8911   if (globals->root.dynamic_sections_created)
8912     srelgot = globals->root.srelgot;
8913   else
8914     srelgot = NULL;
8915
8916   r_symndx = ELF32_R_SYM (rel->r_info);
8917
8918   if (globals->use_rel)
8919     {
8920       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
8921
8922       if (addend & ((howto->src_mask + 1) >> 1))
8923         {
8924           signed_addend = -1;
8925           signed_addend &= ~ howto->src_mask;
8926           signed_addend |= addend;
8927         }
8928       else
8929         signed_addend = addend;
8930     }
8931   else
8932     addend = signed_addend = rel->r_addend;
8933
8934   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
8935      are resolving a function call relocation.  */
8936   if (using_thumb_only (globals)
8937       && (r_type == R_ARM_THM_CALL
8938           || r_type == R_ARM_THM_JUMP24)
8939       && branch_type == ST_BRANCH_TO_ARM)
8940     branch_type = ST_BRANCH_TO_THUMB;
8941
8942   /* Record the symbol information that should be used in dynamic
8943      relocations.  */
8944   dynreloc_st_type = st_type;
8945   dynreloc_value = value;
8946   if (branch_type == ST_BRANCH_TO_THUMB)
8947     dynreloc_value |= 1;
8948
8949   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
8950      VALUE appropriately for relocations that we resolve at link time.  */
8951   has_iplt_entry = FALSE;
8952   if (elf32_arm_get_plt_info (input_bfd, eh, r_symndx, &root_plt, &arm_plt)
8953       && root_plt->offset != (bfd_vma) -1)
8954     {
8955       plt_offset = root_plt->offset;
8956       gotplt_offset = arm_plt->got_offset;
8957
8958       if (h == NULL || eh->is_iplt)
8959         {
8960           has_iplt_entry = TRUE;
8961           splt = globals->root.iplt;
8962
8963           /* Populate .iplt entries here, because not all of them will
8964              be seen by finish_dynamic_symbol.  The lower bit is set if
8965              we have already populated the entry.  */
8966           if (plt_offset & 1)
8967             plt_offset--;
8968           else
8969             {
8970               if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
8971                                                 -1, dynreloc_value))
8972                 root_plt->offset |= 1;
8973               else
8974                 return bfd_reloc_notsupported;
8975             }
8976
8977           /* Static relocations always resolve to the .iplt entry.  */
8978           st_type = STT_FUNC;
8979           value = (splt->output_section->vma
8980                    + splt->output_offset
8981                    + plt_offset);
8982           branch_type = ST_BRANCH_TO_ARM;
8983
8984           /* If there are non-call relocations that resolve to the .iplt
8985              entry, then all dynamic ones must too.  */
8986           if (arm_plt->noncall_refcount != 0)
8987             {
8988               dynreloc_st_type = st_type;
8989               dynreloc_value = value;
8990             }
8991         }
8992       else
8993         /* We populate the .plt entry in finish_dynamic_symbol.  */
8994         splt = globals->root.splt;
8995     }
8996   else
8997     {
8998       splt = NULL;
8999       plt_offset = (bfd_vma) -1;
9000       gotplt_offset = (bfd_vma) -1;
9001     }
9002
9003   switch (r_type)
9004     {
9005     case R_ARM_NONE:
9006       /* We don't need to find a value for this symbol.  It's just a
9007          marker.  */
9008       *unresolved_reloc_p = FALSE;
9009       return bfd_reloc_ok;
9010
9011     case R_ARM_ABS12:
9012       if (!globals->vxworks_p)
9013         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
9014
9015     case R_ARM_PC24:
9016     case R_ARM_ABS32:
9017     case R_ARM_ABS32_NOI:
9018     case R_ARM_REL32:
9019     case R_ARM_REL32_NOI:
9020     case R_ARM_CALL:
9021     case R_ARM_JUMP24:
9022     case R_ARM_XPC25:
9023     case R_ARM_PREL31:
9024     case R_ARM_PLT32:
9025       /* Handle relocations which should use the PLT entry.  ABS32/REL32
9026          will use the symbol's value, which may point to a PLT entry, but we
9027          don't need to handle that here.  If we created a PLT entry, all
9028          branches in this object should go to it, except if the PLT is too
9029          far away, in which case a long branch stub should be inserted.  */
9030       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
9031            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
9032            && r_type != R_ARM_CALL
9033            && r_type != R_ARM_JUMP24
9034            && r_type != R_ARM_PLT32)
9035           && plt_offset != (bfd_vma) -1)
9036         {
9037           /* If we've created a .plt section, and assigned a PLT entry
9038              to this function, it must either be a STT_GNU_IFUNC reference
9039              or not be known to bind locally.  In other cases, we should
9040              have cleared the PLT entry by now.  */
9041           BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
9042
9043           value = (splt->output_section->vma
9044                    + splt->output_offset
9045                    + plt_offset);
9046           *unresolved_reloc_p = FALSE;
9047           return _bfd_final_link_relocate (howto, input_bfd, input_section,
9048                                            contents, rel->r_offset, value,
9049                                            rel->r_addend);
9050         }
9051
9052       /* When generating a shared object or relocatable executable, these
9053          relocations are copied into the output file to be resolved at
9054          run time.  */
9055       if ((bfd_link_pic (info)
9056            || globals->root.is_relocatable_executable)
9057           && (input_section->flags & SEC_ALLOC)
9058           && !(globals->vxworks_p
9059                && strcmp (input_section->output_section->name,
9060                           ".tls_vars") == 0)
9061           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
9062               || !SYMBOL_CALLS_LOCAL (info, h))
9063           && !(input_bfd == globals->stub_bfd
9064                && strstr (input_section->name, STUB_SUFFIX))
9065           && (h == NULL
9066               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9067               || h->root.type != bfd_link_hash_undefweak)
9068           && r_type != R_ARM_PC24
9069           && r_type != R_ARM_CALL
9070           && r_type != R_ARM_JUMP24
9071           && r_type != R_ARM_PREL31
9072           && r_type != R_ARM_PLT32)
9073         {
9074           Elf_Internal_Rela outrel;
9075           bfd_boolean skip, relocate;
9076
9077           if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
9078               && !h->def_regular)
9079             {
9080               char *v = _("shared object");
9081
9082               if (bfd_link_executable (info))
9083                 v = _("PIE executable");
9084
9085               (*_bfd_error_handler)
9086                 (_("%B: relocation %s against external or undefined symbol `%s'"
9087                    " can not be used when making a %s; recompile with -fPIC"), input_bfd,
9088                  elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
9089               return bfd_reloc_notsupported;
9090             }
9091
9092           *unresolved_reloc_p = FALSE;
9093
9094           if (sreloc == NULL && globals->root.dynamic_sections_created)
9095             {
9096               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
9097                                                            ! globals->use_rel);
9098
9099               if (sreloc == NULL)
9100                 return bfd_reloc_notsupported;
9101             }
9102
9103           skip = FALSE;
9104           relocate = FALSE;
9105
9106           outrel.r_addend = addend;
9107           outrel.r_offset =
9108             _bfd_elf_section_offset (output_bfd, info, input_section,
9109                                      rel->r_offset);
9110           if (outrel.r_offset == (bfd_vma) -1)
9111             skip = TRUE;
9112           else if (outrel.r_offset == (bfd_vma) -2)
9113             skip = TRUE, relocate = TRUE;
9114           outrel.r_offset += (input_section->output_section->vma
9115                               + input_section->output_offset);
9116
9117           if (skip)
9118             memset (&outrel, 0, sizeof outrel);
9119           else if (h != NULL
9120                    && h->dynindx != -1
9121                    && (!bfd_link_pic (info)
9122                        || !SYMBOLIC_BIND (info, h)
9123                        || !h->def_regular))
9124             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
9125           else
9126             {
9127               int symbol;
9128
9129               /* This symbol is local, or marked to become local.  */
9130               BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI);
9131               if (globals->symbian_p)
9132                 {
9133                   asection *osec;
9134
9135                   /* On Symbian OS, the data segment and text segement
9136                      can be relocated independently.  Therefore, we
9137                      must indicate the segment to which this
9138                      relocation is relative.  The BPABI allows us to
9139                      use any symbol in the right segment; we just use
9140                      the section symbol as it is convenient.  (We
9141                      cannot use the symbol given by "h" directly as it
9142                      will not appear in the dynamic symbol table.)
9143
9144                      Note that the dynamic linker ignores the section
9145                      symbol value, so we don't subtract osec->vma
9146                      from the emitted reloc addend.  */
9147                   if (sym_sec)
9148                     osec = sym_sec->output_section;
9149                   else
9150                     osec = input_section->output_section;
9151                   symbol = elf_section_data (osec)->dynindx;
9152                   if (symbol == 0)
9153                     {
9154                       struct elf_link_hash_table *htab = elf_hash_table (info);
9155
9156                       if ((osec->flags & SEC_READONLY) == 0
9157                           && htab->data_index_section != NULL)
9158                         osec = htab->data_index_section;
9159                       else
9160                         osec = htab->text_index_section;
9161                       symbol = elf_section_data (osec)->dynindx;
9162                     }
9163                   BFD_ASSERT (symbol != 0);
9164                 }
9165               else
9166                 /* On SVR4-ish systems, the dynamic loader cannot
9167                    relocate the text and data segments independently,
9168                    so the symbol does not matter.  */
9169                 symbol = 0;
9170               if (dynreloc_st_type == STT_GNU_IFUNC)
9171                 /* We have an STT_GNU_IFUNC symbol that doesn't resolve
9172                    to the .iplt entry.  Instead, every non-call reference
9173                    must use an R_ARM_IRELATIVE relocation to obtain the
9174                    correct run-time address.  */
9175                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
9176               else
9177                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
9178               if (globals->use_rel)
9179                 relocate = TRUE;
9180               else
9181                 outrel.r_addend += dynreloc_value;
9182             }
9183
9184           elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
9185
9186           /* If this reloc is against an external symbol, we do not want to
9187              fiddle with the addend.  Otherwise, we need to include the symbol
9188              value so that it becomes an addend for the dynamic reloc.  */
9189           if (! relocate)
9190             return bfd_reloc_ok;
9191
9192           return _bfd_final_link_relocate (howto, input_bfd, input_section,
9193                                            contents, rel->r_offset,
9194                                            dynreloc_value, (bfd_vma) 0);
9195         }
9196       else switch (r_type)
9197         {
9198         case R_ARM_ABS12:
9199           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
9200
9201         case R_ARM_XPC25:         /* Arm BLX instruction.  */
9202         case R_ARM_CALL:
9203         case R_ARM_JUMP24:
9204         case R_ARM_PC24:          /* Arm B/BL instruction.  */
9205         case R_ARM_PLT32:
9206           {
9207           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
9208
9209           if (r_type == R_ARM_XPC25)
9210             {
9211               /* Check for Arm calling Arm function.  */
9212               /* FIXME: Should we translate the instruction into a BL
9213                  instruction instead ?  */
9214               if (branch_type != ST_BRANCH_TO_THUMB)
9215                 (*_bfd_error_handler)
9216                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
9217                    input_bfd,
9218                    h ? h->root.root.string : "(local)");
9219             }
9220           else if (r_type == R_ARM_PC24)
9221             {
9222               /* Check for Arm calling Thumb function.  */
9223               if (branch_type == ST_BRANCH_TO_THUMB)
9224                 {
9225                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
9226                                                output_bfd, input_section,
9227                                                hit_data, sym_sec, rel->r_offset,
9228                                                signed_addend, value,
9229                                                error_message))
9230                     return bfd_reloc_ok;
9231                   else
9232                     return bfd_reloc_dangerous;
9233                 }
9234             }
9235
9236           /* Check if a stub has to be inserted because the
9237              destination is too far or we are changing mode.  */
9238           if (   r_type == R_ARM_CALL
9239               || r_type == R_ARM_JUMP24
9240               || r_type == R_ARM_PLT32)
9241             {
9242               enum elf32_arm_stub_type stub_type = arm_stub_none;
9243               struct elf32_arm_link_hash_entry *hash;
9244
9245               hash = (struct elf32_arm_link_hash_entry *) h;
9246               stub_type = arm_type_of_stub (info, input_section, rel,
9247                                             st_type, &branch_type,
9248                                             hash, value, sym_sec,
9249                                             input_bfd, sym_name);
9250
9251               if (stub_type != arm_stub_none)
9252                 {
9253                   /* The target is out of reach, so redirect the
9254                      branch to the local stub for this function.  */
9255                   stub_entry = elf32_arm_get_stub_entry (input_section,
9256                                                          sym_sec, h,
9257                                                          rel, globals,
9258                                                          stub_type);
9259                   {
9260                     if (stub_entry != NULL)
9261                       value = (stub_entry->stub_offset
9262                                + stub_entry->stub_sec->output_offset
9263                                + stub_entry->stub_sec->output_section->vma);
9264
9265                     if (plt_offset != (bfd_vma) -1)
9266                       *unresolved_reloc_p = FALSE;
9267                   }
9268                 }
9269               else
9270                 {
9271                   /* If the call goes through a PLT entry, make sure to
9272                      check distance to the right destination address.  */
9273                   if (plt_offset != (bfd_vma) -1)
9274                     {
9275                       value = (splt->output_section->vma
9276                                + splt->output_offset
9277                                + plt_offset);
9278                       *unresolved_reloc_p = FALSE;
9279                       /* The PLT entry is in ARM mode, regardless of the
9280                          target function.  */
9281                       branch_type = ST_BRANCH_TO_ARM;
9282                     }
9283                 }
9284             }
9285
9286           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
9287              where:
9288               S is the address of the symbol in the relocation.
9289               P is address of the instruction being relocated.
9290               A is the addend (extracted from the instruction) in bytes.
9291
9292              S is held in 'value'.
9293              P is the base address of the section containing the
9294                instruction plus the offset of the reloc into that
9295                section, ie:
9296                  (input_section->output_section->vma +
9297                   input_section->output_offset +
9298                   rel->r_offset).
9299              A is the addend, converted into bytes, ie:
9300                  (signed_addend * 4)
9301
9302              Note: None of these operations have knowledge of the pipeline
9303              size of the processor, thus it is up to the assembler to
9304              encode this information into the addend.  */
9305           value -= (input_section->output_section->vma
9306                     + input_section->output_offset);
9307           value -= rel->r_offset;
9308           if (globals->use_rel)
9309             value += (signed_addend << howto->size);
9310           else
9311             /* RELA addends do not have to be adjusted by howto->size.  */
9312             value += signed_addend;
9313
9314           signed_addend = value;
9315           signed_addend >>= howto->rightshift;
9316
9317           /* A branch to an undefined weak symbol is turned into a jump to
9318              the next instruction unless a PLT entry will be created.
9319              Do the same for local undefined symbols (but not for STN_UNDEF).
9320              The jump to the next instruction is optimized as a NOP depending
9321              on the architecture.  */
9322           if (h ? (h->root.type == bfd_link_hash_undefweak
9323                    && plt_offset == (bfd_vma) -1)
9324               : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
9325             {
9326               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
9327
9328               if (arch_has_arm_nop (globals))
9329                 value |= 0x0320f000;
9330               else
9331                 value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
9332             }
9333           else
9334             {
9335               /* Perform a signed range check.  */
9336               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
9337                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
9338                 return bfd_reloc_overflow;
9339
9340               addend = (value & 2);
9341
9342               value = (signed_addend & howto->dst_mask)
9343                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
9344
9345               if (r_type == R_ARM_CALL)
9346                 {
9347                   /* Set the H bit in the BLX instruction.  */
9348                   if (branch_type == ST_BRANCH_TO_THUMB)
9349                     {
9350                       if (addend)
9351                         value |= (1 << 24);
9352                       else
9353                         value &= ~(bfd_vma)(1 << 24);
9354                     }
9355
9356                   /* Select the correct instruction (BL or BLX).  */
9357                   /* Only if we are not handling a BL to a stub. In this
9358                      case, mode switching is performed by the stub.  */
9359                   if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
9360                     value |= (1 << 28);
9361                   else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
9362                     {
9363                       value &= ~(bfd_vma)(1 << 28);
9364                       value |= (1 << 24);
9365                     }
9366                 }
9367             }
9368           }
9369           break;
9370
9371         case R_ARM_ABS32:
9372           value += addend;
9373           if (branch_type == ST_BRANCH_TO_THUMB)
9374             value |= 1;
9375           break;
9376
9377         case R_ARM_ABS32_NOI:
9378           value += addend;
9379           break;
9380
9381         case R_ARM_REL32:
9382           value += addend;
9383           if (branch_type == ST_BRANCH_TO_THUMB)
9384             value |= 1;
9385           value -= (input_section->output_section->vma
9386                     + input_section->output_offset + rel->r_offset);
9387           break;
9388
9389         case R_ARM_REL32_NOI:
9390           value += addend;
9391           value -= (input_section->output_section->vma
9392                     + input_section->output_offset + rel->r_offset);
9393           break;
9394
9395         case R_ARM_PREL31:
9396           value -= (input_section->output_section->vma
9397                     + input_section->output_offset + rel->r_offset);
9398           value += signed_addend;
9399           if (! h || h->root.type != bfd_link_hash_undefweak)
9400             {
9401               /* Check for overflow.  */
9402               if ((value ^ (value >> 1)) & (1 << 30))
9403                 return bfd_reloc_overflow;
9404             }
9405           value &= 0x7fffffff;
9406           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
9407           if (branch_type == ST_BRANCH_TO_THUMB)
9408             value |= 1;
9409           break;
9410         }
9411
9412       bfd_put_32 (input_bfd, value, hit_data);
9413       return bfd_reloc_ok;
9414
9415     case R_ARM_ABS8:
9416       /* PR 16202: Refectch the addend using the correct size.  */
9417       if (globals->use_rel)
9418         addend = bfd_get_8 (input_bfd, hit_data);
9419       value += addend;
9420
9421       /* There is no way to tell whether the user intended to use a signed or
9422          unsigned addend.  When checking for overflow we accept either,
9423          as specified by the AAELF.  */
9424       if ((long) value > 0xff || (long) value < -0x80)
9425         return bfd_reloc_overflow;
9426
9427       bfd_put_8 (input_bfd, value, hit_data);
9428       return bfd_reloc_ok;
9429
9430     case R_ARM_ABS16:
9431       /* PR 16202: Refectch the addend using the correct size.  */
9432       if (globals->use_rel)
9433         addend = bfd_get_16 (input_bfd, hit_data);
9434       value += addend;
9435
9436       /* See comment for R_ARM_ABS8.  */
9437       if ((long) value > 0xffff || (long) value < -0x8000)
9438         return bfd_reloc_overflow;
9439
9440       bfd_put_16 (input_bfd, value, hit_data);
9441       return bfd_reloc_ok;
9442
9443     case R_ARM_THM_ABS5:
9444       /* Support ldr and str instructions for the thumb.  */
9445       if (globals->use_rel)
9446         {
9447           /* Need to refetch addend.  */
9448           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
9449           /* ??? Need to determine shift amount from operand size.  */
9450           addend >>= howto->rightshift;
9451         }
9452       value += addend;
9453
9454       /* ??? Isn't value unsigned?  */
9455       if ((long) value > 0x1f || (long) value < -0x10)
9456         return bfd_reloc_overflow;
9457
9458       /* ??? Value needs to be properly shifted into place first.  */
9459       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
9460       bfd_put_16 (input_bfd, value, hit_data);
9461       return bfd_reloc_ok;
9462
9463     case R_ARM_THM_ALU_PREL_11_0:
9464       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
9465       {
9466         bfd_vma insn;
9467         bfd_signed_vma relocation;
9468
9469         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
9470              | bfd_get_16 (input_bfd, hit_data + 2);
9471
9472         if (globals->use_rel)
9473           {
9474             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
9475                           | ((insn & (1 << 26)) >> 15);
9476             if (insn & 0xf00000)
9477               signed_addend = -signed_addend;
9478           }
9479
9480         relocation = value + signed_addend;
9481         relocation -= Pa (input_section->output_section->vma
9482                           + input_section->output_offset
9483                           + rel->r_offset);
9484
9485         value = relocation;
9486
9487         if (value >= 0x1000)
9488           return bfd_reloc_overflow;
9489
9490         insn = (insn & 0xfb0f8f00) | (value & 0xff)
9491              | ((value & 0x700) << 4)
9492              | ((value & 0x800) << 15);
9493         if (relocation < 0)
9494           insn |= 0xa00000;
9495
9496         bfd_put_16 (input_bfd, insn >> 16, hit_data);
9497         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
9498
9499         return bfd_reloc_ok;
9500       }
9501
9502     case R_ARM_THM_PC8:
9503       /* PR 10073:  This reloc is not generated by the GNU toolchain,
9504          but it is supported for compatibility with third party libraries
9505          generated by other compilers, specifically the ARM/IAR.  */
9506       {
9507         bfd_vma insn;
9508         bfd_signed_vma relocation;
9509
9510         insn = bfd_get_16 (input_bfd, hit_data);
9511
9512         if (globals->use_rel)
9513           addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
9514
9515         relocation = value + addend;
9516         relocation -= Pa (input_section->output_section->vma
9517                           + input_section->output_offset
9518                           + rel->r_offset);
9519
9520         value = relocation;
9521
9522         /* We do not check for overflow of this reloc.  Although strictly
9523            speaking this is incorrect, it appears to be necessary in order
9524            to work with IAR generated relocs.  Since GCC and GAS do not
9525            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
9526            a problem for them.  */
9527         value &= 0x3fc;
9528
9529         insn = (insn & 0xff00) | (value >> 2);
9530
9531         bfd_put_16 (input_bfd, insn, hit_data);
9532
9533         return bfd_reloc_ok;
9534       }
9535
9536     case R_ARM_THM_PC12:
9537       /* Corresponds to: ldr.w reg, [pc, #offset].  */
9538       {
9539         bfd_vma insn;
9540         bfd_signed_vma relocation;
9541
9542         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
9543              | bfd_get_16 (input_bfd, hit_data + 2);
9544
9545         if (globals->use_rel)
9546           {
9547             signed_addend = insn & 0xfff;
9548             if (!(insn & (1 << 23)))
9549               signed_addend = -signed_addend;
9550           }
9551
9552         relocation = value + signed_addend;
9553         relocation -= Pa (input_section->output_section->vma
9554                           + input_section->output_offset
9555                           + rel->r_offset);
9556
9557         value = relocation;
9558
9559         if (value >= 0x1000)
9560           return bfd_reloc_overflow;
9561
9562         insn = (insn & 0xff7ff000) | value;
9563         if (relocation >= 0)
9564           insn |= (1 << 23);
9565
9566         bfd_put_16 (input_bfd, insn >> 16, hit_data);
9567         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
9568
9569         return bfd_reloc_ok;
9570       }
9571
9572     case R_ARM_THM_XPC22:
9573     case R_ARM_THM_CALL:
9574     case R_ARM_THM_JUMP24:
9575       /* Thumb BL (branch long instruction).  */
9576       {
9577         bfd_vma relocation;
9578         bfd_vma reloc_sign;
9579         bfd_boolean overflow = FALSE;
9580         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
9581         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
9582         bfd_signed_vma reloc_signed_max;
9583         bfd_signed_vma reloc_signed_min;
9584         bfd_vma check;
9585         bfd_signed_vma signed_check;
9586         int bitsize;
9587         const int thumb2 = using_thumb2 (globals);
9588
9589         /* A branch to an undefined weak symbol is turned into a jump to
9590            the next instruction unless a PLT entry will be created.
9591            The jump to the next instruction is optimized as a NOP.W for
9592            Thumb-2 enabled architectures.  */
9593         if (h && h->root.type == bfd_link_hash_undefweak
9594             && plt_offset == (bfd_vma) -1)
9595           {
9596             if (arch_has_thumb2_nop (globals))
9597               {
9598                 bfd_put_16 (input_bfd, 0xf3af, hit_data);
9599                 bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
9600               }
9601             else
9602               {
9603                 bfd_put_16 (input_bfd, 0xe000, hit_data);
9604                 bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
9605               }
9606             return bfd_reloc_ok;
9607           }
9608
9609         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
9610            with Thumb-1) involving the J1 and J2 bits.  */
9611         if (globals->use_rel)
9612           {
9613             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
9614             bfd_vma upper = upper_insn & 0x3ff;
9615             bfd_vma lower = lower_insn & 0x7ff;
9616             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
9617             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
9618             bfd_vma i1 = j1 ^ s ? 0 : 1;
9619             bfd_vma i2 = j2 ^ s ? 0 : 1;
9620
9621             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
9622             /* Sign extend.  */
9623             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
9624
9625             signed_addend = addend;
9626           }
9627
9628         if (r_type == R_ARM_THM_XPC22)
9629           {
9630             /* Check for Thumb to Thumb call.  */
9631             /* FIXME: Should we translate the instruction into a BL
9632                instruction instead ?  */
9633             if (branch_type == ST_BRANCH_TO_THUMB)
9634               (*_bfd_error_handler)
9635                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
9636                  input_bfd,
9637                  h ? h->root.root.string : "(local)");
9638           }
9639         else
9640           {
9641             /* If it is not a call to Thumb, assume call to Arm.
9642                If it is a call relative to a section name, then it is not a
9643                function call at all, but rather a long jump.  Calls through
9644                the PLT do not require stubs.  */
9645             if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
9646               {
9647                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
9648                   {
9649                     /* Convert BL to BLX.  */
9650                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
9651                   }
9652                 else if ((   r_type != R_ARM_THM_CALL)
9653                          && (r_type != R_ARM_THM_JUMP24))
9654                   {
9655                     if (elf32_thumb_to_arm_stub
9656                         (info, sym_name, input_bfd, output_bfd, input_section,
9657                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
9658                          error_message))
9659                       return bfd_reloc_ok;
9660                     else
9661                       return bfd_reloc_dangerous;
9662                   }
9663               }
9664             else if (branch_type == ST_BRANCH_TO_THUMB
9665                      && globals->use_blx
9666                      && r_type == R_ARM_THM_CALL)
9667               {
9668                 /* Make sure this is a BL.  */
9669                 lower_insn |= 0x1800;
9670               }
9671           }
9672
9673         enum elf32_arm_stub_type stub_type = arm_stub_none;
9674         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
9675           {
9676             /* Check if a stub has to be inserted because the destination
9677                is too far.  */
9678             struct elf32_arm_stub_hash_entry *stub_entry;
9679             struct elf32_arm_link_hash_entry *hash;
9680
9681             hash = (struct elf32_arm_link_hash_entry *) h;
9682
9683             stub_type = arm_type_of_stub (info, input_section, rel,
9684                                           st_type, &branch_type,
9685                                           hash, value, sym_sec,
9686                                           input_bfd, sym_name);
9687
9688             if (stub_type != arm_stub_none)
9689               {
9690                 /* The target is out of reach or we are changing modes, so
9691                    redirect the branch to the local stub for this
9692                    function.  */
9693                 stub_entry = elf32_arm_get_stub_entry (input_section,
9694                                                        sym_sec, h,
9695                                                        rel, globals,
9696                                                        stub_type);
9697                 if (stub_entry != NULL)
9698                   {
9699                     value = (stub_entry->stub_offset
9700                              + stub_entry->stub_sec->output_offset
9701                              + stub_entry->stub_sec->output_section->vma);
9702
9703                     if (plt_offset != (bfd_vma) -1)
9704                       *unresolved_reloc_p = FALSE;
9705                   }
9706
9707                 /* If this call becomes a call to Arm, force BLX.  */
9708                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
9709                   {
9710                     if ((stub_entry
9711                          && !arm_stub_is_thumb (stub_entry->stub_type))
9712                         || branch_type != ST_BRANCH_TO_THUMB)
9713                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
9714                   }
9715               }
9716           }
9717
9718         /* Handle calls via the PLT.  */
9719         if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
9720           {
9721             value = (splt->output_section->vma
9722                      + splt->output_offset
9723                      + plt_offset);
9724
9725             if (globals->use_blx
9726                 && r_type == R_ARM_THM_CALL
9727                 && ! using_thumb_only (globals))
9728               {
9729                 /* If the Thumb BLX instruction is available, convert
9730                    the BL to a BLX instruction to call the ARM-mode
9731                    PLT entry.  */
9732                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
9733                 branch_type = ST_BRANCH_TO_ARM;
9734               }
9735             else
9736               {
9737                 if (! using_thumb_only (globals))
9738                   /* Target the Thumb stub before the ARM PLT entry.  */
9739                   value -= PLT_THUMB_STUB_SIZE;
9740                 branch_type = ST_BRANCH_TO_THUMB;
9741               }
9742             *unresolved_reloc_p = FALSE;
9743           }
9744
9745         relocation = value + signed_addend;
9746
9747         relocation -= (input_section->output_section->vma
9748                        + input_section->output_offset
9749                        + rel->r_offset);
9750
9751         check = relocation >> howto->rightshift;
9752
9753         /* If this is a signed value, the rightshift just dropped
9754            leading 1 bits (assuming twos complement).  */
9755         if ((bfd_signed_vma) relocation >= 0)
9756           signed_check = check;
9757         else
9758           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
9759
9760         /* Calculate the permissable maximum and minimum values for
9761            this relocation according to whether we're relocating for
9762            Thumb-2 or not.  */
9763         bitsize = howto->bitsize;
9764         if (!thumb2)
9765           bitsize -= 2;
9766         reloc_signed_max = (1 << (bitsize - 1)) - 1;
9767         reloc_signed_min = ~reloc_signed_max;
9768
9769         /* Assumes two's complement.  */
9770         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9771           overflow = TRUE;
9772
9773         if ((lower_insn & 0x5000) == 0x4000)
9774           /* For a BLX instruction, make sure that the relocation is rounded up
9775              to a word boundary.  This follows the semantics of the instruction
9776              which specifies that bit 1 of the target address will come from bit
9777              1 of the base address.  */
9778           relocation = (relocation + 2) & ~ 3;
9779
9780         /* Put RELOCATION back into the insn.  Assumes two's complement.
9781            We use the Thumb-2 encoding, which is safe even if dealing with
9782            a Thumb-1 instruction by virtue of our overflow check above.  */
9783         reloc_sign = (signed_check < 0) ? 1 : 0;
9784         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
9785                      | ((relocation >> 12) & 0x3ff)
9786                      | (reloc_sign << 10);
9787         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
9788                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
9789                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
9790                      | ((relocation >> 1) & 0x7ff);
9791
9792         /* Put the relocated value back in the object file:  */
9793         bfd_put_16 (input_bfd, upper_insn, hit_data);
9794         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9795
9796         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
9797       }
9798       break;
9799
9800     case R_ARM_THM_JUMP19:
9801       /* Thumb32 conditional branch instruction.  */
9802       {
9803         bfd_vma relocation;
9804         bfd_boolean overflow = FALSE;
9805         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
9806         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
9807         bfd_signed_vma reloc_signed_max = 0xffffe;
9808         bfd_signed_vma reloc_signed_min = -0x100000;
9809         bfd_signed_vma signed_check;
9810         enum elf32_arm_stub_type stub_type = arm_stub_none;
9811         struct elf32_arm_stub_hash_entry *stub_entry;
9812         struct elf32_arm_link_hash_entry *hash;
9813
9814         /* Need to refetch the addend, reconstruct the top three bits,
9815            and squish the two 11 bit pieces together.  */
9816         if (globals->use_rel)
9817           {
9818             bfd_vma S     = (upper_insn & 0x0400) >> 10;
9819             bfd_vma upper = (upper_insn & 0x003f);
9820             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
9821             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
9822             bfd_vma lower = (lower_insn & 0x07ff);
9823
9824             upper |= J1 << 6;
9825             upper |= J2 << 7;
9826             upper |= (!S) << 8;
9827             upper -= 0x0100; /* Sign extend.  */
9828
9829             addend = (upper << 12) | (lower << 1);
9830             signed_addend = addend;
9831           }
9832
9833         /* Handle calls via the PLT.  */
9834         if (plt_offset != (bfd_vma) -1)
9835           {
9836             value = (splt->output_section->vma
9837                      + splt->output_offset
9838                      + plt_offset);
9839             /* Target the Thumb stub before the ARM PLT entry.  */
9840             value -= PLT_THUMB_STUB_SIZE;
9841             *unresolved_reloc_p = FALSE;
9842           }
9843
9844         hash = (struct elf32_arm_link_hash_entry *)h;
9845
9846         stub_type = arm_type_of_stub (info, input_section, rel,
9847                                       st_type, &branch_type,
9848                                       hash, value, sym_sec,
9849                                       input_bfd, sym_name);
9850         if (stub_type != arm_stub_none)
9851           {
9852             stub_entry = elf32_arm_get_stub_entry (input_section,
9853                                                    sym_sec, h,
9854                                                    rel, globals,
9855                                                    stub_type);
9856             if (stub_entry != NULL)
9857               {
9858                 value = (stub_entry->stub_offset
9859                         + stub_entry->stub_sec->output_offset
9860                         + stub_entry->stub_sec->output_section->vma);
9861               }
9862           }
9863
9864         relocation = value + signed_addend;
9865         relocation -= (input_section->output_section->vma
9866                        + input_section->output_offset
9867                        + rel->r_offset);
9868         signed_check = (bfd_signed_vma) relocation;
9869
9870         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9871           overflow = TRUE;
9872
9873         /* Put RELOCATION back into the insn.  */
9874         {
9875           bfd_vma S  = (relocation & 0x00100000) >> 20;
9876           bfd_vma J2 = (relocation & 0x00080000) >> 19;
9877           bfd_vma J1 = (relocation & 0x00040000) >> 18;
9878           bfd_vma hi = (relocation & 0x0003f000) >> 12;
9879           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
9880
9881           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
9882           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
9883         }
9884
9885         /* Put the relocated value back in the object file:  */
9886         bfd_put_16 (input_bfd, upper_insn, hit_data);
9887         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9888
9889         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
9890       }
9891
9892     case R_ARM_THM_JUMP11:
9893     case R_ARM_THM_JUMP8:
9894     case R_ARM_THM_JUMP6:
9895       /* Thumb B (branch) instruction).  */
9896       {
9897         bfd_signed_vma relocation;
9898         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
9899         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
9900         bfd_signed_vma signed_check;
9901
9902         /* CZB cannot jump backward.  */
9903         if (r_type == R_ARM_THM_JUMP6)
9904           reloc_signed_min = 0;
9905
9906         if (globals->use_rel)
9907           {
9908             /* Need to refetch addend.  */
9909             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
9910             if (addend & ((howto->src_mask + 1) >> 1))
9911               {
9912                 signed_addend = -1;
9913                 signed_addend &= ~ howto->src_mask;
9914                 signed_addend |= addend;
9915               }
9916             else
9917               signed_addend = addend;
9918             /* The value in the insn has been right shifted.  We need to
9919                undo this, so that we can perform the address calculation
9920                in terms of bytes.  */
9921             signed_addend <<= howto->rightshift;
9922           }
9923         relocation = value + signed_addend;
9924
9925         relocation -= (input_section->output_section->vma
9926                        + input_section->output_offset
9927                        + rel->r_offset);
9928
9929         relocation >>= howto->rightshift;
9930         signed_check = relocation;
9931
9932         if (r_type == R_ARM_THM_JUMP6)
9933           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
9934         else
9935           relocation &= howto->dst_mask;
9936         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
9937
9938         bfd_put_16 (input_bfd, relocation, hit_data);
9939
9940         /* Assumes two's complement.  */
9941         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9942           return bfd_reloc_overflow;
9943
9944         return bfd_reloc_ok;
9945       }
9946
9947     case R_ARM_ALU_PCREL7_0:
9948     case R_ARM_ALU_PCREL15_8:
9949     case R_ARM_ALU_PCREL23_15:
9950       {
9951         bfd_vma insn;
9952         bfd_vma relocation;
9953
9954         insn = bfd_get_32 (input_bfd, hit_data);
9955         if (globals->use_rel)
9956           {
9957             /* Extract the addend.  */
9958             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
9959             signed_addend = addend;
9960           }
9961         relocation = value + signed_addend;
9962
9963         relocation -= (input_section->output_section->vma
9964                        + input_section->output_offset
9965                        + rel->r_offset);
9966         insn = (insn & ~0xfff)
9967                | ((howto->bitpos << 7) & 0xf00)
9968                | ((relocation >> howto->bitpos) & 0xff);
9969         bfd_put_32 (input_bfd, value, hit_data);
9970       }
9971       return bfd_reloc_ok;
9972
9973     case R_ARM_GNU_VTINHERIT:
9974     case R_ARM_GNU_VTENTRY:
9975       return bfd_reloc_ok;
9976
9977     case R_ARM_GOTOFF32:
9978       /* Relocation is relative to the start of the
9979          global offset table.  */
9980
9981       BFD_ASSERT (sgot != NULL);
9982       if (sgot == NULL)
9983         return bfd_reloc_notsupported;
9984
9985       /* If we are addressing a Thumb function, we need to adjust the
9986          address by one, so that attempts to call the function pointer will
9987          correctly interpret it as Thumb code.  */
9988       if (branch_type == ST_BRANCH_TO_THUMB)
9989         value += 1;
9990
9991       /* Note that sgot->output_offset is not involved in this
9992          calculation.  We always want the start of .got.  If we
9993          define _GLOBAL_OFFSET_TABLE in a different way, as is
9994          permitted by the ABI, we might have to change this
9995          calculation.  */
9996       value -= sgot->output_section->vma;
9997       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9998                                        contents, rel->r_offset, value,
9999                                        rel->r_addend);
10000
10001     case R_ARM_GOTPC:
10002       /* Use global offset table as symbol value.  */
10003       BFD_ASSERT (sgot != NULL);
10004
10005       if (sgot == NULL)
10006         return bfd_reloc_notsupported;
10007
10008       *unresolved_reloc_p = FALSE;
10009       value = sgot->output_section->vma;
10010       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10011                                        contents, rel->r_offset, value,
10012                                        rel->r_addend);
10013
10014     case R_ARM_GOT32:
10015     case R_ARM_GOT_PREL:
10016       /* Relocation is to the entry for this symbol in the
10017          global offset table.  */
10018       if (sgot == NULL)
10019         return bfd_reloc_notsupported;
10020
10021       if (dynreloc_st_type == STT_GNU_IFUNC
10022           && plt_offset != (bfd_vma) -1
10023           && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
10024         {
10025           /* We have a relocation against a locally-binding STT_GNU_IFUNC
10026              symbol, and the relocation resolves directly to the runtime
10027              target rather than to the .iplt entry.  This means that any
10028              .got entry would be the same value as the .igot.plt entry,
10029              so there's no point creating both.  */
10030           sgot = globals->root.igotplt;
10031           value = sgot->output_offset + gotplt_offset;
10032         }
10033       else if (h != NULL)
10034         {
10035           bfd_vma off;
10036
10037           off = h->got.offset;
10038           BFD_ASSERT (off != (bfd_vma) -1);
10039           if ((off & 1) != 0)
10040             {
10041               /* We have already processsed one GOT relocation against
10042                  this symbol.  */
10043               off &= ~1;
10044               if (globals->root.dynamic_sections_created
10045                   && !SYMBOL_REFERENCES_LOCAL (info, h))
10046                 *unresolved_reloc_p = FALSE;
10047             }
10048           else
10049             {
10050               Elf_Internal_Rela outrel;
10051
10052               if (h->dynindx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
10053                 {
10054                   /* If the symbol doesn't resolve locally in a static
10055                      object, we have an undefined reference.  If the
10056                      symbol doesn't resolve locally in a dynamic object,
10057                      it should be resolved by the dynamic linker.  */
10058                   if (globals->root.dynamic_sections_created)
10059                     {
10060                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
10061                       *unresolved_reloc_p = FALSE;
10062                     }
10063                   else
10064                     outrel.r_info = 0;
10065                   outrel.r_addend = 0;
10066                 }
10067               else
10068                 {
10069                   if (dynreloc_st_type == STT_GNU_IFUNC)
10070                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
10071                   else if (bfd_link_pic (info) &&
10072                            (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10073                             || h->root.type != bfd_link_hash_undefweak))
10074                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
10075                   else
10076                     outrel.r_info = 0;
10077                   outrel.r_addend = dynreloc_value;
10078                 }
10079
10080               /* The GOT entry is initialized to zero by default.
10081                  See if we should install a different value.  */
10082               if (outrel.r_addend != 0
10083                   && (outrel.r_info == 0 || globals->use_rel))
10084                 {
10085                   bfd_put_32 (output_bfd, outrel.r_addend,
10086                               sgot->contents + off);
10087                   outrel.r_addend = 0;
10088                 }
10089
10090               if (outrel.r_info != 0)
10091                 {
10092                   outrel.r_offset = (sgot->output_section->vma
10093                                      + sgot->output_offset
10094                                      + off);
10095                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10096                 }
10097               h->got.offset |= 1;
10098             }
10099           value = sgot->output_offset + off;
10100         }
10101       else
10102         {
10103           bfd_vma off;
10104
10105           BFD_ASSERT (local_got_offsets != NULL &&
10106                       local_got_offsets[r_symndx] != (bfd_vma) -1);
10107
10108           off = local_got_offsets[r_symndx];
10109
10110           /* The offset must always be a multiple of 4.  We use the
10111              least significant bit to record whether we have already
10112              generated the necessary reloc.  */
10113           if ((off & 1) != 0)
10114             off &= ~1;
10115           else
10116             {
10117               if (globals->use_rel)
10118                 bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
10119
10120               if (bfd_link_pic (info) || dynreloc_st_type == STT_GNU_IFUNC)
10121                 {
10122                   Elf_Internal_Rela outrel;
10123
10124                   outrel.r_addend = addend + dynreloc_value;
10125                   outrel.r_offset = (sgot->output_section->vma
10126                                      + sgot->output_offset
10127                                      + off);
10128                   if (dynreloc_st_type == STT_GNU_IFUNC)
10129                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
10130                   else
10131                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
10132                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10133                 }
10134
10135               local_got_offsets[r_symndx] |= 1;
10136             }
10137
10138           value = sgot->output_offset + off;
10139         }
10140       if (r_type != R_ARM_GOT32)
10141         value += sgot->output_section->vma;
10142
10143       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10144                                        contents, rel->r_offset, value,
10145                                        rel->r_addend);
10146
10147     case R_ARM_TLS_LDO32:
10148       value = value - dtpoff_base (info);
10149
10150       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10151                                        contents, rel->r_offset, value,
10152                                        rel->r_addend);
10153
10154     case R_ARM_TLS_LDM32:
10155       {
10156         bfd_vma off;
10157
10158         if (sgot == NULL)
10159           abort ();
10160
10161         off = globals->tls_ldm_got.offset;
10162
10163         if ((off & 1) != 0)
10164           off &= ~1;
10165         else
10166           {
10167             /* If we don't know the module number, create a relocation
10168                for it.  */
10169             if (bfd_link_pic (info))
10170               {
10171                 Elf_Internal_Rela outrel;
10172
10173                 if (srelgot == NULL)
10174                   abort ();
10175
10176                 outrel.r_addend = 0;
10177                 outrel.r_offset = (sgot->output_section->vma
10178                                    + sgot->output_offset + off);
10179                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
10180
10181                 if (globals->use_rel)
10182                   bfd_put_32 (output_bfd, outrel.r_addend,
10183                               sgot->contents + off);
10184
10185                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10186               }
10187             else
10188               bfd_put_32 (output_bfd, 1, sgot->contents + off);
10189
10190             globals->tls_ldm_got.offset |= 1;
10191           }
10192
10193         value = sgot->output_section->vma + sgot->output_offset + off
10194           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
10195
10196         return _bfd_final_link_relocate (howto, input_bfd, input_section,
10197                                          contents, rel->r_offset, value,
10198                                          rel->r_addend);
10199       }
10200
10201     case R_ARM_TLS_CALL:
10202     case R_ARM_THM_TLS_CALL:
10203     case R_ARM_TLS_GD32:
10204     case R_ARM_TLS_IE32:
10205     case R_ARM_TLS_GOTDESC:
10206     case R_ARM_TLS_DESCSEQ:
10207     case R_ARM_THM_TLS_DESCSEQ:
10208       {
10209         bfd_vma off, offplt;
10210         int indx = 0;
10211         char tls_type;
10212
10213         BFD_ASSERT (sgot != NULL);
10214
10215         if (h != NULL)
10216           {
10217             bfd_boolean dyn;
10218             dyn = globals->root.dynamic_sections_created;
10219             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
10220                                                  bfd_link_pic (info),
10221                                                  h)
10222                 && (!bfd_link_pic (info)
10223                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
10224               {
10225                 *unresolved_reloc_p = FALSE;
10226                 indx = h->dynindx;
10227               }
10228             off = h->got.offset;
10229             offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
10230             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
10231           }
10232         else
10233           {
10234             BFD_ASSERT (local_got_offsets != NULL);
10235             off = local_got_offsets[r_symndx];
10236             offplt = local_tlsdesc_gotents[r_symndx];
10237             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
10238           }
10239
10240         /* Linker relaxations happens from one of the
10241            R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
10242         if (ELF32_R_TYPE(rel->r_info) != r_type)
10243           tls_type = GOT_TLS_IE;
10244
10245         BFD_ASSERT (tls_type != GOT_UNKNOWN);
10246
10247         if ((off & 1) != 0)
10248           off &= ~1;
10249         else
10250           {
10251             bfd_boolean need_relocs = FALSE;
10252             Elf_Internal_Rela outrel;
10253             int cur_off = off;
10254
10255             /* The GOT entries have not been initialized yet.  Do it
10256                now, and emit any relocations.  If both an IE GOT and a
10257                GD GOT are necessary, we emit the GD first.  */
10258
10259             if ((bfd_link_pic (info) || indx != 0)
10260                 && (h == NULL
10261                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10262                     || h->root.type != bfd_link_hash_undefweak))
10263               {
10264                 need_relocs = TRUE;
10265                 BFD_ASSERT (srelgot != NULL);
10266               }
10267
10268             if (tls_type & GOT_TLS_GDESC)
10269               {
10270                 bfd_byte *loc;
10271
10272                 /* We should have relaxed, unless this is an undefined
10273                    weak symbol.  */
10274                 BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
10275                             || bfd_link_pic (info));
10276                 BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
10277                             <= globals->root.sgotplt->size);
10278
10279                 outrel.r_addend = 0;
10280                 outrel.r_offset = (globals->root.sgotplt->output_section->vma
10281                                    + globals->root.sgotplt->output_offset
10282                                    + offplt
10283                                    + globals->sgotplt_jump_table_size);
10284
10285                 outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
10286                 sreloc = globals->root.srelplt;
10287                 loc = sreloc->contents;
10288                 loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
10289                 BFD_ASSERT (loc + RELOC_SIZE (globals)
10290                            <= sreloc->contents + sreloc->size);
10291
10292                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
10293
10294                 /* For globals, the first word in the relocation gets
10295                    the relocation index and the top bit set, or zero,
10296                    if we're binding now.  For locals, it gets the
10297                    symbol's offset in the tls section.  */
10298                 bfd_put_32 (output_bfd,
10299                             !h ? value - elf_hash_table (info)->tls_sec->vma
10300                             : info->flags & DF_BIND_NOW ? 0
10301                             : 0x80000000 | ELF32_R_SYM (outrel.r_info),
10302                             globals->root.sgotplt->contents + offplt
10303                             + globals->sgotplt_jump_table_size);
10304
10305                 /* Second word in the relocation is always zero.  */
10306                 bfd_put_32 (output_bfd, 0,
10307                             globals->root.sgotplt->contents + offplt
10308                             + globals->sgotplt_jump_table_size + 4);
10309               }
10310             if (tls_type & GOT_TLS_GD)
10311               {
10312                 if (need_relocs)
10313                   {
10314                     outrel.r_addend = 0;
10315                     outrel.r_offset = (sgot->output_section->vma
10316                                        + sgot->output_offset
10317                                        + cur_off);
10318                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
10319
10320                     if (globals->use_rel)
10321                       bfd_put_32 (output_bfd, outrel.r_addend,
10322                                   sgot->contents + cur_off);
10323
10324                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10325
10326                     if (indx == 0)
10327                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
10328                                   sgot->contents + cur_off + 4);
10329                     else
10330                       {
10331                         outrel.r_addend = 0;
10332                         outrel.r_info = ELF32_R_INFO (indx,
10333                                                       R_ARM_TLS_DTPOFF32);
10334                         outrel.r_offset += 4;
10335
10336                         if (globals->use_rel)
10337                           bfd_put_32 (output_bfd, outrel.r_addend,
10338                                       sgot->contents + cur_off + 4);
10339
10340                         elf32_arm_add_dynreloc (output_bfd, info,
10341                                                 srelgot, &outrel);
10342                       }
10343                   }
10344                 else
10345                   {
10346                     /* If we are not emitting relocations for a
10347                        general dynamic reference, then we must be in a
10348                        static link or an executable link with the
10349                        symbol binding locally.  Mark it as belonging
10350                        to module 1, the executable.  */
10351                     bfd_put_32 (output_bfd, 1,
10352                                 sgot->contents + cur_off);
10353                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
10354                                 sgot->contents + cur_off + 4);
10355                   }
10356
10357                 cur_off += 8;
10358               }
10359
10360             if (tls_type & GOT_TLS_IE)
10361               {
10362                 if (need_relocs)
10363                   {
10364                     if (indx == 0)
10365                       outrel.r_addend = value - dtpoff_base (info);
10366                     else
10367                       outrel.r_addend = 0;
10368                     outrel.r_offset = (sgot->output_section->vma
10369                                        + sgot->output_offset
10370                                        + cur_off);
10371                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
10372
10373                     if (globals->use_rel)
10374                       bfd_put_32 (output_bfd, outrel.r_addend,
10375                                   sgot->contents + cur_off);
10376
10377                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
10378                   }
10379                 else
10380                   bfd_put_32 (output_bfd, tpoff (info, value),
10381                               sgot->contents + cur_off);
10382                 cur_off += 4;
10383               }
10384
10385             if (h != NULL)
10386               h->got.offset |= 1;
10387             else
10388               local_got_offsets[r_symndx] |= 1;
10389           }
10390
10391         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
10392           off += 8;
10393         else if (tls_type & GOT_TLS_GDESC)
10394           off = offplt;
10395
10396         if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
10397             || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
10398           {
10399             bfd_signed_vma offset;
10400             /* TLS stubs are arm mode.  The original symbol is a
10401                data object, so branch_type is bogus.  */
10402             branch_type = ST_BRANCH_TO_ARM;
10403             enum elf32_arm_stub_type stub_type
10404               = arm_type_of_stub (info, input_section, rel,
10405                                   st_type, &branch_type,
10406                                   (struct elf32_arm_link_hash_entry *)h,
10407                                   globals->tls_trampoline, globals->root.splt,
10408                                   input_bfd, sym_name);
10409
10410             if (stub_type != arm_stub_none)
10411               {
10412                 struct elf32_arm_stub_hash_entry *stub_entry
10413                   = elf32_arm_get_stub_entry
10414                   (input_section, globals->root.splt, 0, rel,
10415                    globals, stub_type);
10416                 offset = (stub_entry->stub_offset
10417                           + stub_entry->stub_sec->output_offset
10418                           + stub_entry->stub_sec->output_section->vma);
10419               }
10420             else
10421               offset = (globals->root.splt->output_section->vma
10422                         + globals->root.splt->output_offset
10423                         + globals->tls_trampoline);
10424
10425             if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
10426               {
10427                 unsigned long inst;
10428
10429                 offset -= (input_section->output_section->vma
10430                            + input_section->output_offset
10431                            + rel->r_offset + 8);
10432
10433                 inst = offset >> 2;
10434                 inst &= 0x00ffffff;
10435                 value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
10436               }
10437             else
10438               {
10439                 /* Thumb blx encodes the offset in a complicated
10440                    fashion.  */
10441                 unsigned upper_insn, lower_insn;
10442                 unsigned neg;
10443
10444                 offset -= (input_section->output_section->vma
10445                            + input_section->output_offset
10446                            + rel->r_offset + 4);
10447
10448                 if (stub_type != arm_stub_none
10449                     && arm_stub_is_thumb (stub_type))
10450                   {
10451                     lower_insn = 0xd000;
10452                   }
10453                 else
10454                   {
10455                     lower_insn = 0xc000;
10456                     /* Round up the offset to a word boundary.  */
10457                     offset = (offset + 2) & ~2;
10458                   }
10459
10460                 neg = offset < 0;
10461                 upper_insn = (0xf000
10462                               | ((offset >> 12) & 0x3ff)
10463                               | (neg << 10));
10464                 lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
10465                               | (((!((offset >> 22) & 1)) ^ neg) << 11)
10466                               | ((offset >> 1) & 0x7ff);
10467                 bfd_put_16 (input_bfd, upper_insn, hit_data);
10468                 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
10469                 return bfd_reloc_ok;
10470               }
10471           }
10472         /* These relocations needs special care, as besides the fact
10473            they point somewhere in .gotplt, the addend must be
10474            adjusted accordingly depending on the type of instruction
10475            we refer to.  */
10476         else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
10477           {
10478             unsigned long data, insn;
10479             unsigned thumb;
10480
10481             data = bfd_get_32 (input_bfd, hit_data);
10482             thumb = data & 1;
10483             data &= ~1u;
10484
10485             if (thumb)
10486               {
10487                 insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
10488                 if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
10489                   insn = (insn << 16)
10490                     | bfd_get_16 (input_bfd,
10491                                   contents + rel->r_offset - data + 2);
10492                 if ((insn & 0xf800c000) == 0xf000c000)
10493                   /* bl/blx */
10494                   value = -6;
10495                 else if ((insn & 0xffffff00) == 0x4400)
10496                   /* add */
10497                   value = -5;
10498                 else
10499                   {
10500                     (*_bfd_error_handler)
10501                       (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' referenced by TLS_GOTDESC"),
10502                        input_bfd, input_section,
10503                        (unsigned long)rel->r_offset, insn);
10504                     return bfd_reloc_notsupported;
10505                   }
10506               }
10507             else
10508               {
10509                 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
10510
10511                 switch (insn >> 24)
10512                   {
10513                   case 0xeb:  /* bl */
10514                   case 0xfa:  /* blx */
10515                     value = -4;
10516                     break;
10517
10518                   case 0xe0:    /* add */
10519                     value = -8;
10520                     break;
10521
10522                   default:
10523                     (*_bfd_error_handler)
10524                       (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' referenced by TLS_GOTDESC"),
10525                        input_bfd, input_section,
10526                        (unsigned long)rel->r_offset, insn);
10527                     return bfd_reloc_notsupported;
10528                   }
10529               }
10530
10531             value += ((globals->root.sgotplt->output_section->vma
10532                        + globals->root.sgotplt->output_offset + off)
10533                       - (input_section->output_section->vma
10534                          + input_section->output_offset
10535                          + rel->r_offset)
10536                       + globals->sgotplt_jump_table_size);
10537           }
10538         else
10539           value = ((globals->root.sgot->output_section->vma
10540                     + globals->root.sgot->output_offset + off)
10541                    - (input_section->output_section->vma
10542                       + input_section->output_offset + rel->r_offset));
10543
10544         return _bfd_final_link_relocate (howto, input_bfd, input_section,
10545                                          contents, rel->r_offset, value,
10546                                          rel->r_addend);
10547       }
10548
10549     case R_ARM_TLS_LE32:
10550       if (bfd_link_dll (info))
10551         {
10552           (*_bfd_error_handler)
10553             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
10554              input_bfd, input_section,
10555              (long) rel->r_offset, howto->name);
10556           return bfd_reloc_notsupported;
10557         }
10558       else
10559         value = tpoff (info, value);
10560
10561       return _bfd_final_link_relocate (howto, input_bfd, input_section,
10562                                        contents, rel->r_offset, value,
10563                                        rel->r_addend);
10564
10565     case R_ARM_V4BX:
10566       if (globals->fix_v4bx)
10567         {
10568           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10569
10570           /* Ensure that we have a BX instruction.  */
10571           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
10572
10573           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
10574             {
10575               /* Branch to veneer.  */
10576               bfd_vma glue_addr;
10577               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
10578               glue_addr -= input_section->output_section->vma
10579                            + input_section->output_offset
10580                            + rel->r_offset + 8;
10581               insn = (insn & 0xf0000000) | 0x0a000000
10582                      | ((glue_addr >> 2) & 0x00ffffff);
10583             }
10584           else
10585             {
10586               /* Preserve Rm (lowest four bits) and the condition code
10587                  (highest four bits). Other bits encode MOV PC,Rm.  */
10588               insn = (insn & 0xf000000f) | 0x01a0f000;
10589             }
10590
10591           bfd_put_32 (input_bfd, insn, hit_data);
10592         }
10593       return bfd_reloc_ok;
10594
10595     case R_ARM_MOVW_ABS_NC:
10596     case R_ARM_MOVT_ABS:
10597     case R_ARM_MOVW_PREL_NC:
10598     case R_ARM_MOVT_PREL:
10599     /* Until we properly support segment-base-relative addressing then
10600        we assume the segment base to be zero, as for the group relocations.
10601        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
10602        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
10603     case R_ARM_MOVW_BREL_NC:
10604     case R_ARM_MOVW_BREL:
10605     case R_ARM_MOVT_BREL:
10606       {
10607         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10608
10609         if (globals->use_rel)
10610           {
10611             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
10612             signed_addend = (addend ^ 0x8000) - 0x8000;
10613           }
10614
10615         value += signed_addend;
10616
10617         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
10618           value -= (input_section->output_section->vma
10619                     + input_section->output_offset + rel->r_offset);
10620
10621         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
10622           return bfd_reloc_overflow;
10623
10624         if (branch_type == ST_BRANCH_TO_THUMB)
10625           value |= 1;
10626
10627         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
10628             || r_type == R_ARM_MOVT_BREL)
10629           value >>= 16;
10630
10631         insn &= 0xfff0f000;
10632         insn |= value & 0xfff;
10633         insn |= (value & 0xf000) << 4;
10634         bfd_put_32 (input_bfd, insn, hit_data);
10635       }
10636       return bfd_reloc_ok;
10637
10638     case R_ARM_THM_MOVW_ABS_NC:
10639     case R_ARM_THM_MOVT_ABS:
10640     case R_ARM_THM_MOVW_PREL_NC:
10641     case R_ARM_THM_MOVT_PREL:
10642     /* Until we properly support segment-base-relative addressing then
10643        we assume the segment base to be zero, as for the above relocations.
10644        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
10645        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
10646        as R_ARM_THM_MOVT_ABS.  */
10647     case R_ARM_THM_MOVW_BREL_NC:
10648     case R_ARM_THM_MOVW_BREL:
10649     case R_ARM_THM_MOVT_BREL:
10650       {
10651         bfd_vma insn;
10652
10653         insn = bfd_get_16 (input_bfd, hit_data) << 16;
10654         insn |= bfd_get_16 (input_bfd, hit_data + 2);
10655
10656         if (globals->use_rel)
10657           {
10658             addend = ((insn >> 4)  & 0xf000)
10659                    | ((insn >> 15) & 0x0800)
10660                    | ((insn >> 4)  & 0x0700)
10661                    | (insn         & 0x00ff);
10662             signed_addend = (addend ^ 0x8000) - 0x8000;
10663           }
10664
10665         value += signed_addend;
10666
10667         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
10668           value -= (input_section->output_section->vma
10669                     + input_section->output_offset + rel->r_offset);
10670
10671         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
10672           return bfd_reloc_overflow;
10673
10674         if (branch_type == ST_BRANCH_TO_THUMB)
10675           value |= 1;
10676
10677         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
10678             || r_type == R_ARM_THM_MOVT_BREL)
10679           value >>= 16;
10680
10681         insn &= 0xfbf08f00;
10682         insn |= (value & 0xf000) << 4;
10683         insn |= (value & 0x0800) << 15;
10684         insn |= (value & 0x0700) << 4;
10685         insn |= (value & 0x00ff);
10686
10687         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10688         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10689       }
10690       return bfd_reloc_ok;
10691
10692     case R_ARM_ALU_PC_G0_NC:
10693     case R_ARM_ALU_PC_G1_NC:
10694     case R_ARM_ALU_PC_G0:
10695     case R_ARM_ALU_PC_G1:
10696     case R_ARM_ALU_PC_G2:
10697     case R_ARM_ALU_SB_G0_NC:
10698     case R_ARM_ALU_SB_G1_NC:
10699     case R_ARM_ALU_SB_G0:
10700     case R_ARM_ALU_SB_G1:
10701     case R_ARM_ALU_SB_G2:
10702       {
10703         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10704         bfd_vma pc = input_section->output_section->vma
10705                      + input_section->output_offset + rel->r_offset;
10706         /* sb is the origin of the *segment* containing the symbol.  */
10707         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10708         bfd_vma residual;
10709         bfd_vma g_n;
10710         bfd_signed_vma signed_value;
10711         int group = 0;
10712
10713         /* Determine which group of bits to select.  */
10714         switch (r_type)
10715           {
10716           case R_ARM_ALU_PC_G0_NC:
10717           case R_ARM_ALU_PC_G0:
10718           case R_ARM_ALU_SB_G0_NC:
10719           case R_ARM_ALU_SB_G0:
10720             group = 0;
10721             break;
10722
10723           case R_ARM_ALU_PC_G1_NC:
10724           case R_ARM_ALU_PC_G1:
10725           case R_ARM_ALU_SB_G1_NC:
10726           case R_ARM_ALU_SB_G1:
10727             group = 1;
10728             break;
10729
10730           case R_ARM_ALU_PC_G2:
10731           case R_ARM_ALU_SB_G2:
10732             group = 2;
10733             break;
10734
10735           default:
10736             abort ();
10737           }
10738
10739         /* If REL, extract the addend from the insn.  If RELA, it will
10740            have already been fetched for us.  */
10741         if (globals->use_rel)
10742           {
10743             int negative;
10744             bfd_vma constant = insn & 0xff;
10745             bfd_vma rotation = (insn & 0xf00) >> 8;
10746
10747             if (rotation == 0)
10748               signed_addend = constant;
10749             else
10750               {
10751                 /* Compensate for the fact that in the instruction, the
10752                    rotation is stored in multiples of 2 bits.  */
10753                 rotation *= 2;
10754
10755                 /* Rotate "constant" right by "rotation" bits.  */
10756                 signed_addend = (constant >> rotation) |
10757                                 (constant << (8 * sizeof (bfd_vma) - rotation));
10758               }
10759
10760             /* Determine if the instruction is an ADD or a SUB.
10761                (For REL, this determines the sign of the addend.)  */
10762             negative = identify_add_or_sub (insn);
10763             if (negative == 0)
10764               {
10765                 (*_bfd_error_handler)
10766                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
10767                   input_bfd, input_section,
10768                   (long) rel->r_offset, howto->name);
10769                 return bfd_reloc_overflow;
10770               }
10771
10772             signed_addend *= negative;
10773           }
10774
10775         /* Compute the value (X) to go in the place.  */
10776         if (r_type == R_ARM_ALU_PC_G0_NC
10777             || r_type == R_ARM_ALU_PC_G1_NC
10778             || r_type == R_ARM_ALU_PC_G0
10779             || r_type == R_ARM_ALU_PC_G1
10780             || r_type == R_ARM_ALU_PC_G2)
10781           /* PC relative.  */
10782           signed_value = value - pc + signed_addend;
10783         else
10784           /* Section base relative.  */
10785           signed_value = value - sb + signed_addend;
10786
10787         /* If the target symbol is a Thumb function, then set the
10788            Thumb bit in the address.  */
10789         if (branch_type == ST_BRANCH_TO_THUMB)
10790           signed_value |= 1;
10791
10792         /* Calculate the value of the relevant G_n, in encoded
10793            constant-with-rotation format.  */
10794         g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10795                                           group, &residual);
10796
10797         /* Check for overflow if required.  */
10798         if ((r_type == R_ARM_ALU_PC_G0
10799              || r_type == R_ARM_ALU_PC_G1
10800              || r_type == R_ARM_ALU_PC_G2
10801              || r_type == R_ARM_ALU_SB_G0
10802              || r_type == R_ARM_ALU_SB_G1
10803              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
10804           {
10805             (*_bfd_error_handler)
10806               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10807               input_bfd, input_section,
10808                (long) rel->r_offset, signed_value < 0 ? - signed_value : signed_value,
10809                howto->name);
10810             return bfd_reloc_overflow;
10811           }
10812
10813         /* Mask out the value and the ADD/SUB part of the opcode; take care
10814            not to destroy the S bit.  */
10815         insn &= 0xff1ff000;
10816
10817         /* Set the opcode according to whether the value to go in the
10818            place is negative.  */
10819         if (signed_value < 0)
10820           insn |= 1 << 22;
10821         else
10822           insn |= 1 << 23;
10823
10824         /* Encode the offset.  */
10825         insn |= g_n;
10826
10827         bfd_put_32 (input_bfd, insn, hit_data);
10828       }
10829       return bfd_reloc_ok;
10830
10831     case R_ARM_LDR_PC_G0:
10832     case R_ARM_LDR_PC_G1:
10833     case R_ARM_LDR_PC_G2:
10834     case R_ARM_LDR_SB_G0:
10835     case R_ARM_LDR_SB_G1:
10836     case R_ARM_LDR_SB_G2:
10837       {
10838         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10839         bfd_vma pc = input_section->output_section->vma
10840                      + input_section->output_offset + rel->r_offset;
10841         /* sb is the origin of the *segment* containing the symbol.  */
10842         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10843         bfd_vma residual;
10844         bfd_signed_vma signed_value;
10845         int group = 0;
10846
10847         /* Determine which groups of bits to calculate.  */
10848         switch (r_type)
10849           {
10850           case R_ARM_LDR_PC_G0:
10851           case R_ARM_LDR_SB_G0:
10852             group = 0;
10853             break;
10854
10855           case R_ARM_LDR_PC_G1:
10856           case R_ARM_LDR_SB_G1:
10857             group = 1;
10858             break;
10859
10860           case R_ARM_LDR_PC_G2:
10861           case R_ARM_LDR_SB_G2:
10862             group = 2;
10863             break;
10864
10865           default:
10866             abort ();
10867           }
10868
10869         /* If REL, extract the addend from the insn.  If RELA, it will
10870            have already been fetched for us.  */
10871         if (globals->use_rel)
10872           {
10873             int negative = (insn & (1 << 23)) ? 1 : -1;
10874             signed_addend = negative * (insn & 0xfff);
10875           }
10876
10877         /* Compute the value (X) to go in the place.  */
10878         if (r_type == R_ARM_LDR_PC_G0
10879             || r_type == R_ARM_LDR_PC_G1
10880             || r_type == R_ARM_LDR_PC_G2)
10881           /* PC relative.  */
10882           signed_value = value - pc + signed_addend;
10883         else
10884           /* Section base relative.  */
10885           signed_value = value - sb + signed_addend;
10886
10887         /* Calculate the value of the relevant G_{n-1} to obtain
10888            the residual at that stage.  */
10889         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10890                                     group - 1, &residual);
10891
10892         /* Check for overflow.  */
10893         if (residual >= 0x1000)
10894           {
10895             (*_bfd_error_handler)
10896               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10897                input_bfd, input_section,
10898                (long) rel->r_offset, labs (signed_value), howto->name);
10899             return bfd_reloc_overflow;
10900           }
10901
10902         /* Mask out the value and U bit.  */
10903         insn &= 0xff7ff000;
10904
10905         /* Set the U bit if the value to go in the place is non-negative.  */
10906         if (signed_value >= 0)
10907           insn |= 1 << 23;
10908
10909         /* Encode the offset.  */
10910         insn |= residual;
10911
10912         bfd_put_32 (input_bfd, insn, hit_data);
10913       }
10914       return bfd_reloc_ok;
10915
10916     case R_ARM_LDRS_PC_G0:
10917     case R_ARM_LDRS_PC_G1:
10918     case R_ARM_LDRS_PC_G2:
10919     case R_ARM_LDRS_SB_G0:
10920     case R_ARM_LDRS_SB_G1:
10921     case R_ARM_LDRS_SB_G2:
10922       {
10923         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10924         bfd_vma pc = input_section->output_section->vma
10925                      + input_section->output_offset + rel->r_offset;
10926         /* sb is the origin of the *segment* containing the symbol.  */
10927         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
10928         bfd_vma residual;
10929         bfd_signed_vma signed_value;
10930         int group = 0;
10931
10932         /* Determine which groups of bits to calculate.  */
10933         switch (r_type)
10934           {
10935           case R_ARM_LDRS_PC_G0:
10936           case R_ARM_LDRS_SB_G0:
10937             group = 0;
10938             break;
10939
10940           case R_ARM_LDRS_PC_G1:
10941           case R_ARM_LDRS_SB_G1:
10942             group = 1;
10943             break;
10944
10945           case R_ARM_LDRS_PC_G2:
10946           case R_ARM_LDRS_SB_G2:
10947             group = 2;
10948             break;
10949
10950           default:
10951             abort ();
10952           }
10953
10954         /* If REL, extract the addend from the insn.  If RELA, it will
10955            have already been fetched for us.  */
10956         if (globals->use_rel)
10957           {
10958             int negative = (insn & (1 << 23)) ? 1 : -1;
10959             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
10960           }
10961
10962         /* Compute the value (X) to go in the place.  */
10963         if (r_type == R_ARM_LDRS_PC_G0
10964             || r_type == R_ARM_LDRS_PC_G1
10965             || r_type == R_ARM_LDRS_PC_G2)
10966           /* PC relative.  */
10967           signed_value = value - pc + signed_addend;
10968         else
10969           /* Section base relative.  */
10970           signed_value = value - sb + signed_addend;
10971
10972         /* Calculate the value of the relevant G_{n-1} to obtain
10973            the residual at that stage.  */
10974         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
10975                                     group - 1, &residual);
10976
10977         /* Check for overflow.  */
10978         if (residual >= 0x100)
10979           {
10980             (*_bfd_error_handler)
10981               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10982                input_bfd, input_section,
10983                (long) rel->r_offset, labs (signed_value), howto->name);
10984             return bfd_reloc_overflow;
10985           }
10986
10987         /* Mask out the value and U bit.  */
10988         insn &= 0xff7ff0f0;
10989
10990         /* Set the U bit if the value to go in the place is non-negative.  */
10991         if (signed_value >= 0)
10992           insn |= 1 << 23;
10993
10994         /* Encode the offset.  */
10995         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
10996
10997         bfd_put_32 (input_bfd, insn, hit_data);
10998       }
10999       return bfd_reloc_ok;
11000
11001     case R_ARM_LDC_PC_G0:
11002     case R_ARM_LDC_PC_G1:
11003     case R_ARM_LDC_PC_G2:
11004     case R_ARM_LDC_SB_G0:
11005     case R_ARM_LDC_SB_G1:
11006     case R_ARM_LDC_SB_G2:
11007       {
11008         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
11009         bfd_vma pc = input_section->output_section->vma
11010                      + input_section->output_offset + rel->r_offset;
11011         /* sb is the origin of the *segment* containing the symbol.  */
11012         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
11013         bfd_vma residual;
11014         bfd_signed_vma signed_value;
11015         int group = 0;
11016
11017         /* Determine which groups of bits to calculate.  */
11018         switch (r_type)
11019           {
11020           case R_ARM_LDC_PC_G0:
11021           case R_ARM_LDC_SB_G0:
11022             group = 0;
11023             break;
11024
11025           case R_ARM_LDC_PC_G1:
11026           case R_ARM_LDC_SB_G1:
11027             group = 1;
11028             break;
11029
11030           case R_ARM_LDC_PC_G2:
11031           case R_ARM_LDC_SB_G2:
11032             group = 2;
11033             break;
11034
11035           default:
11036             abort ();
11037           }
11038
11039         /* If REL, extract the addend from the insn.  If RELA, it will
11040            have already been fetched for us.  */
11041         if (globals->use_rel)
11042           {
11043             int negative = (insn & (1 << 23)) ? 1 : -1;
11044             signed_addend = negative * ((insn & 0xff) << 2);
11045           }
11046
11047         /* Compute the value (X) to go in the place.  */
11048         if (r_type == R_ARM_LDC_PC_G0
11049             || r_type == R_ARM_LDC_PC_G1
11050             || r_type == R_ARM_LDC_PC_G2)
11051           /* PC relative.  */
11052           signed_value = value - pc + signed_addend;
11053         else
11054           /* Section base relative.  */
11055           signed_value = value - sb + signed_addend;
11056
11057         /* Calculate the value of the relevant G_{n-1} to obtain
11058            the residual at that stage.  */
11059         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
11060                                     group - 1, &residual);
11061
11062         /* Check for overflow.  (The absolute value to go in the place must be
11063            divisible by four and, after having been divided by four, must
11064            fit in eight bits.)  */
11065         if ((residual & 0x3) != 0 || residual >= 0x400)
11066           {
11067             (*_bfd_error_handler)
11068               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
11069               input_bfd, input_section,
11070               (long) rel->r_offset, labs (signed_value), howto->name);
11071             return bfd_reloc_overflow;
11072           }
11073
11074         /* Mask out the value and U bit.  */
11075         insn &= 0xff7fff00;
11076
11077         /* Set the U bit if the value to go in the place is non-negative.  */
11078         if (signed_value >= 0)
11079           insn |= 1 << 23;
11080
11081         /* Encode the offset.  */
11082         insn |= residual >> 2;
11083
11084         bfd_put_32 (input_bfd, insn, hit_data);
11085       }
11086       return bfd_reloc_ok;
11087
11088     case R_ARM_THM_ALU_ABS_G0_NC:
11089     case R_ARM_THM_ALU_ABS_G1_NC:
11090     case R_ARM_THM_ALU_ABS_G2_NC:
11091     case R_ARM_THM_ALU_ABS_G3_NC:
11092         {
11093             const int shift_array[4] = {0, 8, 16, 24};
11094             bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
11095             bfd_vma addr = value;
11096             int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
11097
11098             /* Compute address.  */
11099             if (globals->use_rel)
11100                 signed_addend = insn & 0xff;
11101             addr += signed_addend;
11102             if (branch_type == ST_BRANCH_TO_THUMB)
11103                 addr |= 1;
11104             /* Clean imm8 insn.  */
11105             insn &= 0xff00;
11106             /* And update with correct part of address.  */
11107             insn |= (addr >> shift) & 0xff;
11108             /* Update insn.  */
11109             bfd_put_16 (input_bfd, insn, hit_data);
11110         }
11111
11112         *unresolved_reloc_p = FALSE;
11113         return bfd_reloc_ok;
11114
11115     default:
11116       return bfd_reloc_notsupported;
11117     }
11118 }
11119
11120 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
11121 static void
11122 arm_add_to_rel (bfd *              abfd,
11123                 bfd_byte *         address,
11124                 reloc_howto_type * howto,
11125                 bfd_signed_vma     increment)
11126 {
11127   bfd_signed_vma addend;
11128
11129   if (howto->type == R_ARM_THM_CALL
11130       || howto->type == R_ARM_THM_JUMP24)
11131     {
11132       int upper_insn, lower_insn;
11133       int upper, lower;
11134
11135       upper_insn = bfd_get_16 (abfd, address);
11136       lower_insn = bfd_get_16 (abfd, address + 2);
11137       upper = upper_insn & 0x7ff;
11138       lower = lower_insn & 0x7ff;
11139
11140       addend = (upper << 12) | (lower << 1);
11141       addend += increment;
11142       addend >>= 1;
11143
11144       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
11145       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
11146
11147       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
11148       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
11149     }
11150   else
11151     {
11152       bfd_vma        contents;
11153
11154       contents = bfd_get_32 (abfd, address);
11155
11156       /* Get the (signed) value from the instruction.  */
11157       addend = contents & howto->src_mask;
11158       if (addend & ((howto->src_mask + 1) >> 1))
11159         {
11160           bfd_signed_vma mask;
11161
11162           mask = -1;
11163           mask &= ~ howto->src_mask;
11164           addend |= mask;
11165         }
11166
11167       /* Add in the increment, (which is a byte value).  */
11168       switch (howto->type)
11169         {
11170         default:
11171           addend += increment;
11172           break;
11173
11174         case R_ARM_PC24:
11175         case R_ARM_PLT32:
11176         case R_ARM_CALL:
11177         case R_ARM_JUMP24:
11178           addend <<= howto->size;
11179           addend += increment;
11180
11181           /* Should we check for overflow here ?  */
11182
11183           /* Drop any undesired bits.  */
11184           addend >>= howto->rightshift;
11185           break;
11186         }
11187
11188       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
11189
11190       bfd_put_32 (abfd, contents, address);
11191     }
11192 }
11193
11194 #define IS_ARM_TLS_RELOC(R_TYPE)        \
11195   ((R_TYPE) == R_ARM_TLS_GD32           \
11196    || (R_TYPE) == R_ARM_TLS_LDO32       \
11197    || (R_TYPE) == R_ARM_TLS_LDM32       \
11198    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
11199    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
11200    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
11201    || (R_TYPE) == R_ARM_TLS_LE32        \
11202    || (R_TYPE) == R_ARM_TLS_IE32        \
11203    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
11204
11205 /* Specific set of relocations for the gnu tls dialect.  */
11206 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)    \
11207   ((R_TYPE) == R_ARM_TLS_GOTDESC        \
11208    || (R_TYPE) == R_ARM_TLS_CALL        \
11209    || (R_TYPE) == R_ARM_THM_TLS_CALL    \
11210    || (R_TYPE) == R_ARM_TLS_DESCSEQ     \
11211    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
11212
11213 /* Relocate an ARM ELF section.  */
11214
11215 static bfd_boolean
11216 elf32_arm_relocate_section (bfd *                  output_bfd,
11217                             struct bfd_link_info * info,
11218                             bfd *                  input_bfd,
11219                             asection *             input_section,
11220                             bfd_byte *             contents,
11221                             Elf_Internal_Rela *    relocs,
11222                             Elf_Internal_Sym *     local_syms,
11223                             asection **            local_sections)
11224 {
11225   Elf_Internal_Shdr *symtab_hdr;
11226   struct elf_link_hash_entry **sym_hashes;
11227   Elf_Internal_Rela *rel;
11228   Elf_Internal_Rela *relend;
11229   const char *name;
11230   struct elf32_arm_link_hash_table * globals;
11231
11232   globals = elf32_arm_hash_table (info);
11233   if (globals == NULL)
11234     return FALSE;
11235
11236   symtab_hdr = & elf_symtab_hdr (input_bfd);
11237   sym_hashes = elf_sym_hashes (input_bfd);
11238
11239   rel = relocs;
11240   relend = relocs + input_section->reloc_count;
11241   for (; rel < relend; rel++)
11242     {
11243       int                          r_type;
11244       reloc_howto_type *           howto;
11245       unsigned long                r_symndx;
11246       Elf_Internal_Sym *           sym;
11247       asection *                   sec;
11248       struct elf_link_hash_entry * h;
11249       bfd_vma                      relocation;
11250       bfd_reloc_status_type        r;
11251       arelent                      bfd_reloc;
11252       char                         sym_type;
11253       bfd_boolean                  unresolved_reloc = FALSE;
11254       char *error_message = NULL;
11255
11256       r_symndx = ELF32_R_SYM (rel->r_info);
11257       r_type   = ELF32_R_TYPE (rel->r_info);
11258       r_type   = arm_real_reloc_type (globals, r_type);
11259
11260       if (   r_type == R_ARM_GNU_VTENTRY
11261           || r_type == R_ARM_GNU_VTINHERIT)
11262         continue;
11263
11264       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
11265       howto = bfd_reloc.howto;
11266
11267       h = NULL;
11268       sym = NULL;
11269       sec = NULL;
11270
11271       if (r_symndx < symtab_hdr->sh_info)
11272         {
11273           sym = local_syms + r_symndx;
11274           sym_type = ELF32_ST_TYPE (sym->st_info);
11275           sec = local_sections[r_symndx];
11276
11277           /* An object file might have a reference to a local
11278              undefined symbol.  This is a daft object file, but we
11279              should at least do something about it.  V4BX & NONE
11280              relocations do not use the symbol and are explicitly
11281              allowed to use the undefined symbol, so allow those.
11282              Likewise for relocations against STN_UNDEF.  */
11283           if (r_type != R_ARM_V4BX
11284               && r_type != R_ARM_NONE
11285               && r_symndx != STN_UNDEF
11286               && bfd_is_und_section (sec)
11287               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
11288             {
11289               if (!info->callbacks->undefined_symbol
11290                   (info, bfd_elf_string_from_elf_section
11291                    (input_bfd, symtab_hdr->sh_link, sym->st_name),
11292                    input_bfd, input_section,
11293                    rel->r_offset, TRUE))
11294                 return FALSE;
11295             }
11296
11297           if (globals->use_rel)
11298             {
11299               relocation = (sec->output_section->vma
11300                             + sec->output_offset
11301                             + sym->st_value);
11302               if (!bfd_link_relocatable (info)
11303                   && (sec->flags & SEC_MERGE)
11304                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11305                 {
11306                   asection *msec;
11307                   bfd_vma addend, value;
11308
11309                   switch (r_type)
11310                     {
11311                     case R_ARM_MOVW_ABS_NC:
11312                     case R_ARM_MOVT_ABS:
11313                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
11314                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
11315                       addend = (addend ^ 0x8000) - 0x8000;
11316                       break;
11317
11318                     case R_ARM_THM_MOVW_ABS_NC:
11319                     case R_ARM_THM_MOVT_ABS:
11320                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
11321                               << 16;
11322                       value |= bfd_get_16 (input_bfd,
11323                                            contents + rel->r_offset + 2);
11324                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
11325                                | ((value & 0x04000000) >> 15);
11326                       addend = (addend ^ 0x8000) - 0x8000;
11327                       break;
11328
11329                     default:
11330                       if (howto->rightshift
11331                           || (howto->src_mask & (howto->src_mask + 1)))
11332                         {
11333                           (*_bfd_error_handler)
11334                             (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
11335                              input_bfd, input_section,
11336                              (long) rel->r_offset, howto->name);
11337                           return FALSE;
11338                         }
11339
11340                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
11341
11342                       /* Get the (signed) value from the instruction.  */
11343                       addend = value & howto->src_mask;
11344                       if (addend & ((howto->src_mask + 1) >> 1))
11345                         {
11346                           bfd_signed_vma mask;
11347
11348                           mask = -1;
11349                           mask &= ~ howto->src_mask;
11350                           addend |= mask;
11351                         }
11352                       break;
11353                     }
11354
11355                   msec = sec;
11356                   addend =
11357                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
11358                     - relocation;
11359                   addend += msec->output_section->vma + msec->output_offset;
11360
11361                   /* Cases here must match those in the preceding
11362                      switch statement.  */
11363                   switch (r_type)
11364                     {
11365                     case R_ARM_MOVW_ABS_NC:
11366                     case R_ARM_MOVT_ABS:
11367                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
11368                               | (addend & 0xfff);
11369                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
11370                       break;
11371
11372                     case R_ARM_THM_MOVW_ABS_NC:
11373                     case R_ARM_THM_MOVT_ABS:
11374                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
11375                               | (addend & 0xff) | ((addend & 0x0800) << 15);
11376                       bfd_put_16 (input_bfd, value >> 16,
11377                                   contents + rel->r_offset);
11378                       bfd_put_16 (input_bfd, value,
11379                                   contents + rel->r_offset + 2);
11380                       break;
11381
11382                     default:
11383                       value = (value & ~ howto->dst_mask)
11384                               | (addend & howto->dst_mask);
11385                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
11386                       break;
11387                     }
11388                 }
11389             }
11390           else
11391             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11392         }
11393       else
11394         {
11395           bfd_boolean warned, ignored;
11396
11397           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11398                                    r_symndx, symtab_hdr, sym_hashes,
11399                                    h, sec, relocation,
11400                                    unresolved_reloc, warned, ignored);
11401
11402           sym_type = h->type;
11403         }
11404
11405       if (sec != NULL && discarded_section (sec))
11406         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11407                                          rel, 1, relend, howto, 0, contents);
11408
11409       if (bfd_link_relocatable (info))
11410         {
11411           /* This is a relocatable link.  We don't have to change
11412              anything, unless the reloc is against a section symbol,
11413              in which case we have to adjust according to where the
11414              section symbol winds up in the output section.  */
11415           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11416             {
11417               if (globals->use_rel)
11418                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
11419                                 howto, (bfd_signed_vma) sec->output_offset);
11420               else
11421                 rel->r_addend += sec->output_offset;
11422             }
11423           continue;
11424         }
11425
11426       if (h != NULL)
11427         name = h->root.root.string;
11428       else
11429         {
11430           name = (bfd_elf_string_from_elf_section
11431                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
11432           if (name == NULL || *name == '\0')
11433             name = bfd_section_name (input_bfd, sec);
11434         }
11435
11436       if (r_symndx != STN_UNDEF
11437           && r_type != R_ARM_NONE
11438           && (h == NULL
11439               || h->root.type == bfd_link_hash_defined
11440               || h->root.type == bfd_link_hash_defweak)
11441           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
11442         {
11443           (*_bfd_error_handler)
11444             ((sym_type == STT_TLS
11445               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11446               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
11447              input_bfd,
11448              input_section,
11449              (long) rel->r_offset,
11450              howto->name,
11451              name);
11452         }
11453
11454       /* We call elf32_arm_final_link_relocate unless we're completely
11455          done, i.e., the relaxation produced the final output we want,
11456          and we won't let anybody mess with it. Also, we have to do
11457          addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
11458          both in relaxed and non-relaxed cases.  */
11459      if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
11460          || (IS_ARM_TLS_GNU_RELOC (r_type)
11461              && !((h ? elf32_arm_hash_entry (h)->tls_type :
11462                    elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
11463                   & GOT_TLS_GDESC)))
11464        {
11465          r = elf32_arm_tls_relax (globals, input_bfd, input_section,
11466                                   contents, rel, h == NULL);
11467          /* This may have been marked unresolved because it came from
11468             a shared library.  But we've just dealt with that.  */
11469          unresolved_reloc = 0;
11470        }
11471      else
11472        r = bfd_reloc_continue;
11473
11474      if (r == bfd_reloc_continue)
11475        r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
11476                                           input_section, contents, rel,
11477                                           relocation, info, sec, name, sym_type,
11478                                           (h ? h->target_internal
11479                                            : ARM_SYM_BRANCH_TYPE (sym)), h,
11480                                           &unresolved_reloc, &error_message);
11481
11482       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11483          because such sections are not SEC_ALLOC and thus ld.so will
11484          not process them.  */
11485       if (unresolved_reloc
11486           && !((input_section->flags & SEC_DEBUGGING) != 0
11487                && h->def_dynamic)
11488           && _bfd_elf_section_offset (output_bfd, info, input_section,
11489                                       rel->r_offset) != (bfd_vma) -1)
11490         {
11491           (*_bfd_error_handler)
11492             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11493              input_bfd,
11494              input_section,
11495              (long) rel->r_offset,
11496              howto->name,
11497              h->root.root.string);
11498           return FALSE;
11499         }
11500
11501       if (r != bfd_reloc_ok)
11502         {
11503           switch (r)
11504             {
11505             case bfd_reloc_overflow:
11506               /* If the overflowing reloc was to an undefined symbol,
11507                  we have already printed one error message and there
11508                  is no point complaining again.  */
11509               if ((! h ||
11510                    h->root.type != bfd_link_hash_undefined)
11511                   && (!((*info->callbacks->reloc_overflow)
11512                         (info, (h ? &h->root : NULL), name, howto->name,
11513                          (bfd_vma) 0, input_bfd, input_section,
11514                          rel->r_offset))))
11515                   return FALSE;
11516               break;
11517
11518             case bfd_reloc_undefined:
11519               if (!((*info->callbacks->undefined_symbol)
11520                     (info, name, input_bfd, input_section,
11521                      rel->r_offset, TRUE)))
11522                 return FALSE;
11523               break;
11524
11525             case bfd_reloc_outofrange:
11526               error_message = _("out of range");
11527               goto common_error;
11528
11529             case bfd_reloc_notsupported:
11530               error_message = _("unsupported relocation");
11531               goto common_error;
11532
11533             case bfd_reloc_dangerous:
11534               /* error_message should already be set.  */
11535               goto common_error;
11536
11537             default:
11538               error_message = _("unknown error");
11539               /* Fall through.  */
11540
11541             common_error:
11542               BFD_ASSERT (error_message != NULL);
11543               if (!((*info->callbacks->reloc_dangerous)
11544                     (info, error_message, input_bfd, input_section,
11545                      rel->r_offset)))
11546                 return FALSE;
11547               break;
11548             }
11549         }
11550     }
11551
11552   return TRUE;
11553 }
11554
11555 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
11556    adds the edit to the start of the list.  (The list must be built in order of
11557    ascending TINDEX: the function's callers are primarily responsible for
11558    maintaining that condition).  */
11559
11560 static void
11561 add_unwind_table_edit (arm_unwind_table_edit **head,
11562                        arm_unwind_table_edit **tail,
11563                        arm_unwind_edit_type type,
11564                        asection *linked_section,
11565                        unsigned int tindex)
11566 {
11567   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
11568       xmalloc (sizeof (arm_unwind_table_edit));
11569
11570   new_edit->type = type;
11571   new_edit->linked_section = linked_section;
11572   new_edit->index = tindex;
11573
11574   if (tindex > 0)
11575     {
11576       new_edit->next = NULL;
11577
11578       if (*tail)
11579         (*tail)->next = new_edit;
11580
11581       (*tail) = new_edit;
11582
11583       if (!*head)
11584         (*head) = new_edit;
11585     }
11586   else
11587     {
11588       new_edit->next = *head;
11589
11590       if (!*tail)
11591         *tail = new_edit;
11592
11593       *head = new_edit;
11594     }
11595 }
11596
11597 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
11598
11599 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
11600 static void
11601 adjust_exidx_size(asection *exidx_sec, int adjust)
11602 {
11603   asection *out_sec;
11604
11605   if (!exidx_sec->rawsize)
11606     exidx_sec->rawsize = exidx_sec->size;
11607
11608   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
11609   out_sec = exidx_sec->output_section;
11610   /* Adjust size of output section.  */
11611   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
11612 }
11613
11614 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
11615 static void
11616 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
11617 {
11618   struct _arm_elf_section_data *exidx_arm_data;
11619
11620   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
11621   add_unwind_table_edit (
11622     &exidx_arm_data->u.exidx.unwind_edit_list,
11623     &exidx_arm_data->u.exidx.unwind_edit_tail,
11624     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
11625
11626   exidx_arm_data->additional_reloc_count++;
11627
11628   adjust_exidx_size(exidx_sec, 8);
11629 }
11630
11631 /* Scan .ARM.exidx tables, and create a list describing edits which should be
11632    made to those tables, such that:
11633
11634      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
11635      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
11636         codes which have been inlined into the index).
11637
11638    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
11639
11640    The edits are applied when the tables are written
11641    (in elf32_arm_write_section).  */
11642
11643 bfd_boolean
11644 elf32_arm_fix_exidx_coverage (asection **text_section_order,
11645                               unsigned int num_text_sections,
11646                               struct bfd_link_info *info,
11647                               bfd_boolean merge_exidx_entries)
11648 {
11649   bfd *inp;
11650   unsigned int last_second_word = 0, i;
11651   asection *last_exidx_sec = NULL;
11652   asection *last_text_sec = NULL;
11653   int last_unwind_type = -1;
11654
11655   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
11656      text sections.  */
11657   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
11658     {
11659       asection *sec;
11660
11661       for (sec = inp->sections; sec != NULL; sec = sec->next)
11662         {
11663           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
11664           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
11665
11666           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
11667             continue;
11668
11669           if (elf_sec->linked_to)
11670             {
11671               Elf_Internal_Shdr *linked_hdr
11672                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
11673               struct _arm_elf_section_data *linked_sec_arm_data
11674                 = get_arm_elf_section_data (linked_hdr->bfd_section);
11675
11676               if (linked_sec_arm_data == NULL)
11677                 continue;
11678
11679               /* Link this .ARM.exidx section back from the text section it
11680                  describes.  */
11681               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
11682             }
11683         }
11684     }
11685
11686   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
11687      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
11688      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
11689
11690   for (i = 0; i < num_text_sections; i++)
11691     {
11692       asection *sec = text_section_order[i];
11693       asection *exidx_sec;
11694       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
11695       struct _arm_elf_section_data *exidx_arm_data;
11696       bfd_byte *contents = NULL;
11697       int deleted_exidx_bytes = 0;
11698       bfd_vma j;
11699       arm_unwind_table_edit *unwind_edit_head = NULL;
11700       arm_unwind_table_edit *unwind_edit_tail = NULL;
11701       Elf_Internal_Shdr *hdr;
11702       bfd *ibfd;
11703
11704       if (arm_data == NULL)
11705         continue;
11706
11707       exidx_sec = arm_data->u.text.arm_exidx_sec;
11708       if (exidx_sec == NULL)
11709         {
11710           /* Section has no unwind data.  */
11711           if (last_unwind_type == 0 || !last_exidx_sec)
11712             continue;
11713
11714           /* Ignore zero sized sections.  */
11715           if (sec->size == 0)
11716             continue;
11717
11718           insert_cantunwind_after(last_text_sec, last_exidx_sec);
11719           last_unwind_type = 0;
11720           continue;
11721         }
11722
11723       /* Skip /DISCARD/ sections.  */
11724       if (bfd_is_abs_section (exidx_sec->output_section))
11725         continue;
11726
11727       hdr = &elf_section_data (exidx_sec)->this_hdr;
11728       if (hdr->sh_type != SHT_ARM_EXIDX)
11729         continue;
11730
11731       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
11732       if (exidx_arm_data == NULL)
11733         continue;
11734
11735       ibfd = exidx_sec->owner;
11736
11737       if (hdr->contents != NULL)
11738         contents = hdr->contents;
11739       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
11740         /* An error?  */
11741         continue;
11742
11743       for (j = 0; j < hdr->sh_size; j += 8)
11744         {
11745           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
11746           int unwind_type;
11747           int elide = 0;
11748
11749           /* An EXIDX_CANTUNWIND entry.  */
11750           if (second_word == 1)
11751             {
11752               if (last_unwind_type == 0)
11753                 elide = 1;
11754               unwind_type = 0;
11755             }
11756           /* Inlined unwinding data.  Merge if equal to previous.  */
11757           else if ((second_word & 0x80000000) != 0)
11758             {
11759               if (merge_exidx_entries
11760                    && last_second_word == second_word && last_unwind_type == 1)
11761                 elide = 1;
11762               unwind_type = 1;
11763               last_second_word = second_word;
11764             }
11765           /* Normal table entry.  In theory we could merge these too,
11766              but duplicate entries are likely to be much less common.  */
11767           else
11768             unwind_type = 2;
11769
11770           if (elide && !bfd_link_relocatable (info))
11771             {
11772               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
11773                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
11774
11775               deleted_exidx_bytes += 8;
11776             }
11777
11778           last_unwind_type = unwind_type;
11779         }
11780
11781       /* Free contents if we allocated it ourselves.  */
11782       if (contents != hdr->contents)
11783         free (contents);
11784
11785       /* Record edits to be applied later (in elf32_arm_write_section).  */
11786       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
11787       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
11788
11789       if (deleted_exidx_bytes > 0)
11790         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
11791
11792       last_exidx_sec = exidx_sec;
11793       last_text_sec = sec;
11794     }
11795
11796   /* Add terminating CANTUNWIND entry.  */
11797   if (!bfd_link_relocatable (info) && last_exidx_sec
11798       && last_unwind_type != 0)
11799     insert_cantunwind_after(last_text_sec, last_exidx_sec);
11800
11801   return TRUE;
11802 }
11803
11804 static bfd_boolean
11805 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
11806                                bfd *ibfd, const char *name)
11807 {
11808   asection *sec, *osec;
11809
11810   sec = bfd_get_linker_section (ibfd, name);
11811   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
11812     return TRUE;
11813
11814   osec = sec->output_section;
11815   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
11816     return TRUE;
11817
11818   if (! bfd_set_section_contents (obfd, osec, sec->contents,
11819                                   sec->output_offset, sec->size))
11820     return FALSE;
11821
11822   return TRUE;
11823 }
11824
11825 static bfd_boolean
11826 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
11827 {
11828   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
11829   asection *sec, *osec;
11830
11831   if (globals == NULL)
11832     return FALSE;
11833
11834   /* Invoke the regular ELF backend linker to do all the work.  */
11835   if (!bfd_elf_final_link (abfd, info))
11836     return FALSE;
11837
11838   /* Process stub sections (eg BE8 encoding, ...).  */
11839   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
11840   unsigned int i;
11841   for (i=0; i<htab->top_id; i++)
11842     {
11843       sec = htab->stub_group[i].stub_sec;
11844       /* Only process it once, in its link_sec slot.  */
11845       if (sec && i == htab->stub_group[i].link_sec->id)
11846         {
11847           osec = sec->output_section;
11848           elf32_arm_write_section (abfd, info, sec, sec->contents);
11849           if (! bfd_set_section_contents (abfd, osec, sec->contents,
11850                                           sec->output_offset, sec->size))
11851             return FALSE;
11852         }
11853     }
11854
11855   /* Write out any glue sections now that we have created all the
11856      stubs.  */
11857   if (globals->bfd_of_glue_owner != NULL)
11858     {
11859       if (! elf32_arm_output_glue_section (info, abfd,
11860                                            globals->bfd_of_glue_owner,
11861                                            ARM2THUMB_GLUE_SECTION_NAME))
11862         return FALSE;
11863
11864       if (! elf32_arm_output_glue_section (info, abfd,
11865                                            globals->bfd_of_glue_owner,
11866                                            THUMB2ARM_GLUE_SECTION_NAME))
11867         return FALSE;
11868
11869       if (! elf32_arm_output_glue_section (info, abfd,
11870                                            globals->bfd_of_glue_owner,
11871                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
11872         return FALSE;
11873
11874       if (! elf32_arm_output_glue_section (info, abfd,
11875                                            globals->bfd_of_glue_owner,
11876                                            STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
11877         return FALSE;
11878
11879       if (! elf32_arm_output_glue_section (info, abfd,
11880                                            globals->bfd_of_glue_owner,
11881                                            ARM_BX_GLUE_SECTION_NAME))
11882         return FALSE;
11883     }
11884
11885   return TRUE;
11886 }
11887
11888 /* Return a best guess for the machine number based on the attributes.  */
11889
11890 static unsigned int
11891 bfd_arm_get_mach_from_attributes (bfd * abfd)
11892 {
11893   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
11894
11895   switch (arch)
11896     {
11897     case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
11898     case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
11899     case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
11900
11901     case TAG_CPU_ARCH_V5TE:
11902       {
11903         char * name;
11904
11905         BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
11906         name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
11907
11908         if (name)
11909           {
11910             if (strcmp (name, "IWMMXT2") == 0)
11911               return bfd_mach_arm_iWMMXt2;
11912
11913             if (strcmp (name, "IWMMXT") == 0)
11914               return bfd_mach_arm_iWMMXt;
11915
11916             if (strcmp (name, "XSCALE") == 0)
11917               {
11918                 int wmmx;
11919
11920                 BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
11921                 wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
11922                 switch (wmmx)
11923                   {
11924                   case 1: return bfd_mach_arm_iWMMXt;
11925                   case 2: return bfd_mach_arm_iWMMXt2;
11926                   default: return bfd_mach_arm_XScale;
11927                   }
11928               }
11929           }
11930
11931         return bfd_mach_arm_5TE;
11932       }
11933
11934     default:
11935       return bfd_mach_arm_unknown;
11936     }
11937 }
11938
11939 /* Set the right machine number.  */
11940
11941 static bfd_boolean
11942 elf32_arm_object_p (bfd *abfd)
11943 {
11944   unsigned int mach;
11945
11946   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
11947
11948   if (mach == bfd_mach_arm_unknown)
11949     {
11950       if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
11951         mach = bfd_mach_arm_ep9312;
11952       else
11953         mach = bfd_arm_get_mach_from_attributes (abfd);
11954     }
11955
11956   bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
11957   return TRUE;
11958 }
11959
11960 /* Function to keep ARM specific flags in the ELF header.  */
11961
11962 static bfd_boolean
11963 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
11964 {
11965   if (elf_flags_init (abfd)
11966       && elf_elfheader (abfd)->e_flags != flags)
11967     {
11968       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
11969         {
11970           if (flags & EF_ARM_INTERWORK)
11971             (*_bfd_error_handler)
11972               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
11973                abfd);
11974           else
11975             _bfd_error_handler
11976               (_("Warning: Clearing the interworking flag of %B due to outside request"),
11977                abfd);
11978         }
11979     }
11980   else
11981     {
11982       elf_elfheader (abfd)->e_flags = flags;
11983       elf_flags_init (abfd) = TRUE;
11984     }
11985
11986   return TRUE;
11987 }
11988
11989 /* Copy backend specific data from one object module to another.  */
11990
11991 static bfd_boolean
11992 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
11993 {
11994   flagword in_flags;
11995   flagword out_flags;
11996
11997   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
11998     return TRUE;
11999
12000   in_flags  = elf_elfheader (ibfd)->e_flags;
12001   out_flags = elf_elfheader (obfd)->e_flags;
12002
12003   if (elf_flags_init (obfd)
12004       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
12005       && in_flags != out_flags)
12006     {
12007       /* Cannot mix APCS26 and APCS32 code.  */
12008       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
12009         return FALSE;
12010
12011       /* Cannot mix float APCS and non-float APCS code.  */
12012       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
12013         return FALSE;
12014
12015       /* If the src and dest have different interworking flags
12016          then turn off the interworking bit.  */
12017       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
12018         {
12019           if (out_flags & EF_ARM_INTERWORK)
12020             _bfd_error_handler
12021               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
12022                obfd, ibfd);
12023
12024           in_flags &= ~EF_ARM_INTERWORK;
12025         }
12026
12027       /* Likewise for PIC, though don't warn for this case.  */
12028       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
12029         in_flags &= ~EF_ARM_PIC;
12030     }
12031
12032   elf_elfheader (obfd)->e_flags = in_flags;
12033   elf_flags_init (obfd) = TRUE;
12034
12035   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
12036 }
12037
12038 /* Values for Tag_ABI_PCS_R9_use.  */
12039 enum
12040 {
12041   AEABI_R9_V6,
12042   AEABI_R9_SB,
12043   AEABI_R9_TLS,
12044   AEABI_R9_unused
12045 };
12046
12047 /* Values for Tag_ABI_PCS_RW_data.  */
12048 enum
12049 {
12050   AEABI_PCS_RW_data_absolute,
12051   AEABI_PCS_RW_data_PCrel,
12052   AEABI_PCS_RW_data_SBrel,
12053   AEABI_PCS_RW_data_unused
12054 };
12055
12056 /* Values for Tag_ABI_enum_size.  */
12057 enum
12058 {
12059   AEABI_enum_unused,
12060   AEABI_enum_short,
12061   AEABI_enum_wide,
12062   AEABI_enum_forced_wide
12063 };
12064
12065 /* Determine whether an object attribute tag takes an integer, a
12066    string or both.  */
12067
12068 static int
12069 elf32_arm_obj_attrs_arg_type (int tag)
12070 {
12071   if (tag == Tag_compatibility)
12072     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
12073   else if (tag == Tag_nodefaults)
12074     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
12075   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
12076     return ATTR_TYPE_FLAG_STR_VAL;
12077   else if (tag < 32)
12078     return ATTR_TYPE_FLAG_INT_VAL;
12079   else
12080     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
12081 }
12082
12083 /* The ABI defines that Tag_conformance should be emitted first, and that
12084    Tag_nodefaults should be second (if either is defined).  This sets those
12085    two positions, and bumps up the position of all the remaining tags to
12086    compensate.  */
12087 static int
12088 elf32_arm_obj_attrs_order (int num)
12089 {
12090   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
12091     return Tag_conformance;
12092   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
12093     return Tag_nodefaults;
12094   if ((num - 2) < Tag_nodefaults)
12095     return num - 2;
12096   if ((num - 1) < Tag_conformance)
12097     return num - 1;
12098   return num;
12099 }
12100
12101 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
12102 static bfd_boolean
12103 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
12104 {
12105   if ((tag & 127) < 64)
12106     {
12107       _bfd_error_handler
12108         (_("%B: Unknown mandatory EABI object attribute %d"),
12109          abfd, tag);
12110       bfd_set_error (bfd_error_bad_value);
12111       return FALSE;
12112     }
12113   else
12114     {
12115       _bfd_error_handler
12116         (_("Warning: %B: Unknown EABI object attribute %d"),
12117          abfd, tag);
12118       return TRUE;
12119     }
12120 }
12121
12122 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
12123    Returns -1 if no architecture could be read.  */
12124
12125 static int
12126 get_secondary_compatible_arch (bfd *abfd)
12127 {
12128   obj_attribute *attr =
12129     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
12130
12131   /* Note: the tag and its argument below are uleb128 values, though
12132      currently-defined values fit in one byte for each.  */
12133   if (attr->s
12134       && attr->s[0] == Tag_CPU_arch
12135       && (attr->s[1] & 128) != 128
12136       && attr->s[2] == 0)
12137    return attr->s[1];
12138
12139   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
12140   return -1;
12141 }
12142
12143 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
12144    The tag is removed if ARCH is -1.  */
12145
12146 static void
12147 set_secondary_compatible_arch (bfd *abfd, int arch)
12148 {
12149   obj_attribute *attr =
12150     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
12151
12152   if (arch == -1)
12153     {
12154       attr->s = NULL;
12155       return;
12156     }
12157
12158   /* Note: the tag and its argument below are uleb128 values, though
12159      currently-defined values fit in one byte for each.  */
12160   if (!attr->s)
12161     attr->s = (char *) bfd_alloc (abfd, 3);
12162   attr->s[0] = Tag_CPU_arch;
12163   attr->s[1] = arch;
12164   attr->s[2] = '\0';
12165 }
12166
12167 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
12168    into account.  */
12169
12170 static int
12171 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
12172                       int newtag, int secondary_compat)
12173 {
12174 #define T(X) TAG_CPU_ARCH_##X
12175   int tagl, tagh, result;
12176   const int v6t2[] =
12177     {
12178       T(V6T2),   /* PRE_V4.  */
12179       T(V6T2),   /* V4.  */
12180       T(V6T2),   /* V4T.  */
12181       T(V6T2),   /* V5T.  */
12182       T(V6T2),   /* V5TE.  */
12183       T(V6T2),   /* V5TEJ.  */
12184       T(V6T2),   /* V6.  */
12185       T(V7),     /* V6KZ.  */
12186       T(V6T2)    /* V6T2.  */
12187     };
12188   const int v6k[] =
12189     {
12190       T(V6K),    /* PRE_V4.  */
12191       T(V6K),    /* V4.  */
12192       T(V6K),    /* V4T.  */
12193       T(V6K),    /* V5T.  */
12194       T(V6K),    /* V5TE.  */
12195       T(V6K),    /* V5TEJ.  */
12196       T(V6K),    /* V6.  */
12197       T(V6KZ),   /* V6KZ.  */
12198       T(V7),     /* V6T2.  */
12199       T(V6K)     /* V6K.  */
12200     };
12201   const int v7[] =
12202     {
12203       T(V7),     /* PRE_V4.  */
12204       T(V7),     /* V4.  */
12205       T(V7),     /* V4T.  */
12206       T(V7),     /* V5T.  */
12207       T(V7),     /* V5TE.  */
12208       T(V7),     /* V5TEJ.  */
12209       T(V7),     /* V6.  */
12210       T(V7),     /* V6KZ.  */
12211       T(V7),     /* V6T2.  */
12212       T(V7),     /* V6K.  */
12213       T(V7)      /* V7.  */
12214     };
12215   const int v6_m[] =
12216     {
12217       -1,        /* PRE_V4.  */
12218       -1,        /* V4.  */
12219       T(V6K),    /* V4T.  */
12220       T(V6K),    /* V5T.  */
12221       T(V6K),    /* V5TE.  */
12222       T(V6K),    /* V5TEJ.  */
12223       T(V6K),    /* V6.  */
12224       T(V6KZ),   /* V6KZ.  */
12225       T(V7),     /* V6T2.  */
12226       T(V6K),    /* V6K.  */
12227       T(V7),     /* V7.  */
12228       T(V6_M)    /* V6_M.  */
12229     };
12230   const int v6s_m[] =
12231     {
12232       -1,        /* PRE_V4.  */
12233       -1,        /* V4.  */
12234       T(V6K),    /* V4T.  */
12235       T(V6K),    /* V5T.  */
12236       T(V6K),    /* V5TE.  */
12237       T(V6K),    /* V5TEJ.  */
12238       T(V6K),    /* V6.  */
12239       T(V6KZ),   /* V6KZ.  */
12240       T(V7),     /* V6T2.  */
12241       T(V6K),    /* V6K.  */
12242       T(V7),     /* V7.  */
12243       T(V6S_M),  /* V6_M.  */
12244       T(V6S_M)   /* V6S_M.  */
12245     };
12246   const int v7e_m[] =
12247     {
12248       -1,        /* PRE_V4.  */
12249       -1,        /* V4.  */
12250       T(V7E_M),  /* V4T.  */
12251       T(V7E_M),  /* V5T.  */
12252       T(V7E_M),  /* V5TE.  */
12253       T(V7E_M),  /* V5TEJ.  */
12254       T(V7E_M),  /* V6.  */
12255       T(V7E_M),  /* V6KZ.  */
12256       T(V7E_M),  /* V6T2.  */
12257       T(V7E_M),  /* V6K.  */
12258       T(V7E_M),  /* V7.  */
12259       T(V7E_M),  /* V6_M.  */
12260       T(V7E_M),  /* V6S_M.  */
12261       T(V7E_M)   /* V7E_M.  */
12262     };
12263   const int v8[] =
12264     {
12265       T(V8),            /* PRE_V4.  */
12266       T(V8),            /* V4.  */
12267       T(V8),            /* V4T.  */
12268       T(V8),            /* V5T.  */
12269       T(V8),            /* V5TE.  */
12270       T(V8),            /* V5TEJ.  */
12271       T(V8),            /* V6.  */
12272       T(V8),            /* V6KZ.  */
12273       T(V8),            /* V6T2.  */
12274       T(V8),            /* V6K.  */
12275       T(V8),            /* V7.  */
12276       T(V8),            /* V6_M.  */
12277       T(V8),            /* V6S_M.  */
12278       T(V8),            /* V7E_M.  */
12279       T(V8)             /* V8.  */
12280     };
12281   const int v8m_baseline[] =
12282     {
12283       -1,               /* PRE_V4.  */
12284       -1,               /* V4.  */
12285       -1,               /* V4T.  */
12286       -1,               /* V5T.  */
12287       -1,               /* V5TE.  */
12288       -1,               /* V5TEJ.  */
12289       -1,               /* V6.  */
12290       -1,               /* V6KZ.  */
12291       -1,               /* V6T2.  */
12292       -1,               /* V6K.  */
12293       -1,               /* V7.  */
12294       T(V8M_BASE),      /* V6_M.  */
12295       T(V8M_BASE),      /* V6S_M.  */
12296       -1,               /* V7E_M.  */
12297       -1,               /* V8.  */
12298       -1,
12299       T(V8M_BASE)       /* V8-M BASELINE.  */
12300     };
12301   const int v8m_mainline[] =
12302     {
12303       -1,               /* PRE_V4.  */
12304       -1,               /* V4.  */
12305       -1,               /* V4T.  */
12306       -1,               /* V5T.  */
12307       -1,               /* V5TE.  */
12308       -1,               /* V5TEJ.  */
12309       -1,               /* V6.  */
12310       -1,               /* V6KZ.  */
12311       -1,               /* V6T2.  */
12312       -1,               /* V6K.  */
12313       T(V8M_MAIN),      /* V7.  */
12314       T(V8M_MAIN),      /* V6_M.  */
12315       T(V8M_MAIN),      /* V6S_M.  */
12316       T(V8M_MAIN),      /* V7E_M.  */
12317       -1,               /* V8.  */
12318       -1,
12319       T(V8M_MAIN),      /* V8-M BASELINE.  */
12320       T(V8M_MAIN)       /* V8-M MAINLINE.  */
12321     };
12322   const int v4t_plus_v6_m[] =
12323     {
12324       -1,               /* PRE_V4.  */
12325       -1,               /* V4.  */
12326       T(V4T),           /* V4T.  */
12327       T(V5T),           /* V5T.  */
12328       T(V5TE),          /* V5TE.  */
12329       T(V5TEJ),         /* V5TEJ.  */
12330       T(V6),            /* V6.  */
12331       T(V6KZ),          /* V6KZ.  */
12332       T(V6T2),          /* V6T2.  */
12333       T(V6K),           /* V6K.  */
12334       T(V7),            /* V7.  */
12335       T(V6_M),          /* V6_M.  */
12336       T(V6S_M),         /* V6S_M.  */
12337       T(V7E_M),         /* V7E_M.  */
12338       T(V8),            /* V8.  */
12339       -1,               /* Unused.  */
12340       T(V8M_BASE),      /* V8-M BASELINE.  */
12341       T(V8M_MAIN),      /* V8-M MAINLINE.  */
12342       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
12343     };
12344   const int *comb[] =
12345     {
12346       v6t2,
12347       v6k,
12348       v7,
12349       v6_m,
12350       v6s_m,
12351       v7e_m,
12352       v8,
12353       NULL,
12354       v8m_baseline,
12355       v8m_mainline,
12356       /* Pseudo-architecture.  */
12357       v4t_plus_v6_m
12358     };
12359
12360   /* Check we've not got a higher architecture than we know about.  */
12361
12362   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
12363     {
12364       _bfd_error_handler (_("error: %B: Unknown CPU architecture"), ibfd);
12365       return -1;
12366     }
12367
12368   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
12369
12370   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
12371       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
12372     oldtag = T(V4T_PLUS_V6_M);
12373
12374   /* And override the new tag if we have a Tag_also_compatible_with on the
12375      input.  */
12376
12377   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
12378       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
12379     newtag = T(V4T_PLUS_V6_M);
12380
12381   tagl = (oldtag < newtag) ? oldtag : newtag;
12382   result = tagh = (oldtag > newtag) ? oldtag : newtag;
12383
12384   /* Architectures before V6KZ add features monotonically.  */
12385   if (tagh <= TAG_CPU_ARCH_V6KZ)
12386     return result;
12387
12388   result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
12389
12390   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
12391      as the canonical version.  */
12392   if (result == T(V4T_PLUS_V6_M))
12393     {
12394       result = T(V4T);
12395       *secondary_compat_out = T(V6_M);
12396     }
12397   else
12398     *secondary_compat_out = -1;
12399
12400   if (result == -1)
12401     {
12402       _bfd_error_handler (_("error: %B: Conflicting CPU architectures %d/%d"),
12403                           ibfd, oldtag, newtag);
12404       return -1;
12405     }
12406
12407   return result;
12408 #undef T
12409 }
12410
12411 /* Query attributes object to see if integer divide instructions may be
12412    present in an object.  */
12413 static bfd_boolean
12414 elf32_arm_attributes_accept_div (const obj_attribute *attr)
12415 {
12416   int arch = attr[Tag_CPU_arch].i;
12417   int profile = attr[Tag_CPU_arch_profile].i;
12418
12419   switch (attr[Tag_DIV_use].i)
12420     {
12421     case 0:
12422       /* Integer divide allowed if instruction contained in archetecture.  */
12423       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
12424         return TRUE;
12425       else if (arch >= TAG_CPU_ARCH_V7E_M)
12426         return TRUE;
12427       else
12428         return FALSE;
12429
12430     case 1:
12431       /* Integer divide explicitly prohibited.  */
12432       return FALSE;
12433
12434     default:
12435       /* Unrecognised case - treat as allowing divide everywhere.  */
12436     case 2:
12437       /* Integer divide allowed in ARM state.  */
12438       return TRUE;
12439     }
12440 }
12441
12442 /* Query attributes object to see if integer divide instructions are
12443    forbidden to be in the object.  This is not the inverse of
12444    elf32_arm_attributes_accept_div.  */
12445 static bfd_boolean
12446 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
12447 {
12448   return attr[Tag_DIV_use].i == 1;
12449 }
12450
12451 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
12452    are conflicting attributes.  */
12453
12454 static bfd_boolean
12455 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
12456 {
12457   obj_attribute *in_attr;
12458   obj_attribute *out_attr;
12459   /* Some tags have 0 = don't care, 1 = strong requirement,
12460      2 = weak requirement.  */
12461   static const int order_021[3] = {0, 2, 1};
12462   int i;
12463   bfd_boolean result = TRUE;
12464   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
12465
12466   /* Skip the linker stubs file.  This preserves previous behavior
12467      of accepting unknown attributes in the first input file - but
12468      is that a bug?  */
12469   if (ibfd->flags & BFD_LINKER_CREATED)
12470     return TRUE;
12471
12472   /* Skip any input that hasn't attribute section.
12473      This enables to link object files without attribute section with
12474      any others.  */
12475   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
12476     return TRUE;
12477
12478   if (!elf_known_obj_attributes_proc (obfd)[0].i)
12479     {
12480       /* This is the first object.  Copy the attributes.  */
12481       _bfd_elf_copy_obj_attributes (ibfd, obfd);
12482
12483       out_attr = elf_known_obj_attributes_proc (obfd);
12484
12485       /* Use the Tag_null value to indicate the attributes have been
12486          initialized.  */
12487       out_attr[0].i = 1;
12488
12489       /* We do not output objects with Tag_MPextension_use_legacy - we move
12490          the attribute's value to Tag_MPextension_use.  */
12491       if (out_attr[Tag_MPextension_use_legacy].i != 0)
12492         {
12493           if (out_attr[Tag_MPextension_use].i != 0
12494               && out_attr[Tag_MPextension_use_legacy].i
12495                 != out_attr[Tag_MPextension_use].i)
12496             {
12497               _bfd_error_handler
12498                 (_("Error: %B has both the current and legacy "
12499                    "Tag_MPextension_use attributes"), ibfd);
12500               result = FALSE;
12501             }
12502
12503           out_attr[Tag_MPextension_use] =
12504             out_attr[Tag_MPextension_use_legacy];
12505           out_attr[Tag_MPextension_use_legacy].type = 0;
12506           out_attr[Tag_MPextension_use_legacy].i = 0;
12507         }
12508
12509       return result;
12510     }
12511
12512   in_attr = elf_known_obj_attributes_proc (ibfd);
12513   out_attr = elf_known_obj_attributes_proc (obfd);
12514   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
12515   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
12516     {
12517       /* Ignore mismatches if the object doesn't use floating point or is
12518          floating point ABI independent.  */
12519       if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
12520           || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
12521               && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
12522         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
12523       else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
12524                && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
12525         {
12526           _bfd_error_handler
12527             (_("error: %B uses VFP register arguments, %B does not"),
12528              in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
12529              in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
12530           result = FALSE;
12531         }
12532     }
12533
12534   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
12535     {
12536       /* Merge this attribute with existing attributes.  */
12537       switch (i)
12538         {
12539         case Tag_CPU_raw_name:
12540         case Tag_CPU_name:
12541           /* These are merged after Tag_CPU_arch.  */
12542           break;
12543
12544         case Tag_ABI_optimization_goals:
12545         case Tag_ABI_FP_optimization_goals:
12546           /* Use the first value seen.  */
12547           break;
12548
12549         case Tag_CPU_arch:
12550           {
12551             int secondary_compat = -1, secondary_compat_out = -1;
12552             unsigned int saved_out_attr = out_attr[i].i;
12553             int arch_attr;
12554             static const char *name_table[] =
12555               {
12556                 /* These aren't real CPU names, but we can't guess
12557                    that from the architecture version alone.  */
12558                 "Pre v4",
12559                 "ARM v4",
12560                 "ARM v4T",
12561                 "ARM v5T",
12562                 "ARM v5TE",
12563                 "ARM v5TEJ",
12564                 "ARM v6",
12565                 "ARM v6KZ",
12566                 "ARM v6T2",
12567                 "ARM v6K",
12568                 "ARM v7",
12569                 "ARM v6-M",
12570                 "ARM v6S-M",
12571                 "ARM v8",
12572                 "",
12573                 "ARM v8-M.baseline",
12574                 "ARM v8-M.mainline",
12575             };
12576
12577             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
12578             secondary_compat = get_secondary_compatible_arch (ibfd);
12579             secondary_compat_out = get_secondary_compatible_arch (obfd);
12580             arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
12581                                               &secondary_compat_out,
12582                                               in_attr[i].i,
12583                                               secondary_compat);
12584
12585             /* Return with error if failed to merge.  */
12586             if (arch_attr == -1)
12587               return FALSE;
12588
12589             out_attr[i].i = arch_attr;
12590
12591             set_secondary_compatible_arch (obfd, secondary_compat_out);
12592
12593             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
12594             if (out_attr[i].i == saved_out_attr)
12595               ; /* Leave the names alone.  */
12596             else if (out_attr[i].i == in_attr[i].i)
12597               {
12598                 /* The output architecture has been changed to match the
12599                    input architecture.  Use the input names.  */
12600                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
12601                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
12602                   : NULL;
12603                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
12604                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
12605                   : NULL;
12606               }
12607             else
12608               {
12609                 out_attr[Tag_CPU_name].s = NULL;
12610                 out_attr[Tag_CPU_raw_name].s = NULL;
12611               }
12612
12613             /* If we still don't have a value for Tag_CPU_name,
12614                make one up now.  Tag_CPU_raw_name remains blank.  */
12615             if (out_attr[Tag_CPU_name].s == NULL
12616                 && out_attr[i].i < ARRAY_SIZE (name_table))
12617               out_attr[Tag_CPU_name].s =
12618                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
12619           }
12620           break;
12621
12622         case Tag_ARM_ISA_use:
12623         case Tag_THUMB_ISA_use:
12624         case Tag_WMMX_arch:
12625         case Tag_Advanced_SIMD_arch:
12626           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
12627         case Tag_ABI_FP_rounding:
12628         case Tag_ABI_FP_exceptions:
12629         case Tag_ABI_FP_user_exceptions:
12630         case Tag_ABI_FP_number_model:
12631         case Tag_FP_HP_extension:
12632         case Tag_CPU_unaligned_access:
12633         case Tag_T2EE_use:
12634         case Tag_MPextension_use:
12635           /* Use the largest value specified.  */
12636           if (in_attr[i].i > out_attr[i].i)
12637             out_attr[i].i = in_attr[i].i;
12638           break;
12639
12640         case Tag_ABI_align_preserved:
12641         case Tag_ABI_PCS_RO_data:
12642           /* Use the smallest value specified.  */
12643           if (in_attr[i].i < out_attr[i].i)
12644             out_attr[i].i = in_attr[i].i;
12645           break;
12646
12647         case Tag_ABI_align_needed:
12648           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
12649               && (in_attr[Tag_ABI_align_preserved].i == 0
12650                   || out_attr[Tag_ABI_align_preserved].i == 0))
12651             {
12652               /* This error message should be enabled once all non-conformant
12653                  binaries in the toolchain have had the attributes set
12654                  properly.
12655               _bfd_error_handler
12656                 (_("error: %B: 8-byte data alignment conflicts with %B"),
12657                  obfd, ibfd);
12658               result = FALSE; */
12659             }
12660           /* Fall through.  */
12661         case Tag_ABI_FP_denormal:
12662         case Tag_ABI_PCS_GOT_use:
12663           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
12664              value if greater than 2 (for future-proofing).  */
12665           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
12666               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
12667                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
12668             out_attr[i].i = in_attr[i].i;
12669           break;
12670
12671         case Tag_Virtualization_use:
12672           /* The virtualization tag effectively stores two bits of
12673              information: the intended use of TrustZone (in bit 0), and the
12674              intended use of Virtualization (in bit 1).  */
12675           if (out_attr[i].i == 0)
12676             out_attr[i].i = in_attr[i].i;
12677           else if (in_attr[i].i != 0
12678                    && in_attr[i].i != out_attr[i].i)
12679             {
12680               if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
12681                 out_attr[i].i = 3;
12682               else
12683                 {
12684                   _bfd_error_handler
12685                     (_("error: %B: unable to merge virtualization attributes "
12686                        "with %B"),
12687                      obfd, ibfd);
12688                   result = FALSE;
12689                 }
12690             }
12691           break;
12692
12693         case Tag_CPU_arch_profile:
12694           if (out_attr[i].i != in_attr[i].i)
12695             {
12696               /* 0 will merge with anything.
12697                  'A' and 'S' merge to 'A'.
12698                  'R' and 'S' merge to 'R'.
12699                  'M' and 'A|R|S' is an error.  */
12700               if (out_attr[i].i == 0
12701                   || (out_attr[i].i == 'S'
12702                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
12703                 out_attr[i].i = in_attr[i].i;
12704               else if (in_attr[i].i == 0
12705                        || (in_attr[i].i == 'S'
12706                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
12707                 ; /* Do nothing.  */
12708               else
12709                 {
12710                   _bfd_error_handler
12711                     (_("error: %B: Conflicting architecture profiles %c/%c"),
12712                      ibfd,
12713                      in_attr[i].i ? in_attr[i].i : '0',
12714                      out_attr[i].i ? out_attr[i].i : '0');
12715                   result = FALSE;
12716                 }
12717             }
12718           break;
12719         case Tag_FP_arch:
12720             {
12721               /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
12722                  the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
12723                  when it's 0.  It might mean absence of FP hardware if
12724                  Tag_FP_arch is zero.  */
12725
12726 #define VFP_VERSION_COUNT 9
12727               static const struct
12728               {
12729                   int ver;
12730                   int regs;
12731               } vfp_versions[VFP_VERSION_COUNT] =
12732                 {
12733                   {0, 0},
12734                   {1, 16},
12735                   {2, 16},
12736                   {3, 32},
12737                   {3, 16},
12738                   {4, 32},
12739                   {4, 16},
12740                   {8, 32},
12741                   {8, 16}
12742                 };
12743               int ver;
12744               int regs;
12745               int newval;
12746
12747               /* If the output has no requirement about FP hardware,
12748                  follow the requirement of the input.  */
12749               if (out_attr[i].i == 0)
12750                 {
12751                   BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
12752                   out_attr[i].i = in_attr[i].i;
12753                   out_attr[Tag_ABI_HardFP_use].i
12754                     = in_attr[Tag_ABI_HardFP_use].i;
12755                   break;
12756                 }
12757               /* If the input has no requirement about FP hardware, do
12758                  nothing.  */
12759               else if (in_attr[i].i == 0)
12760                 {
12761                   BFD_ASSERT (in_attr[Tag_ABI_HardFP_use].i == 0);
12762                   break;
12763                 }
12764
12765               /* Both the input and the output have nonzero Tag_FP_arch.
12766                  So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
12767
12768               /* If both the input and the output have zero Tag_ABI_HardFP_use,
12769                  do nothing.  */
12770               if (in_attr[Tag_ABI_HardFP_use].i == 0
12771                   && out_attr[Tag_ABI_HardFP_use].i == 0)
12772                 ;
12773               /* If the input and the output have different Tag_ABI_HardFP_use,
12774                  the combination of them is 0 (implied by Tag_FP_arch).  */
12775               else if (in_attr[Tag_ABI_HardFP_use].i
12776                        != out_attr[Tag_ABI_HardFP_use].i)
12777                 out_attr[Tag_ABI_HardFP_use].i = 0;
12778
12779               /* Now we can handle Tag_FP_arch.  */
12780
12781               /* Values of VFP_VERSION_COUNT or more aren't defined, so just
12782                  pick the biggest.  */
12783               if (in_attr[i].i >= VFP_VERSION_COUNT
12784                   && in_attr[i].i > out_attr[i].i)
12785                 {
12786                   out_attr[i] = in_attr[i];
12787                   break;
12788                 }
12789               /* The output uses the superset of input features
12790                  (ISA version) and registers.  */
12791               ver = vfp_versions[in_attr[i].i].ver;
12792               if (ver < vfp_versions[out_attr[i].i].ver)
12793                 ver = vfp_versions[out_attr[i].i].ver;
12794               regs = vfp_versions[in_attr[i].i].regs;
12795               if (regs < vfp_versions[out_attr[i].i].regs)
12796                 regs = vfp_versions[out_attr[i].i].regs;
12797               /* This assumes all possible supersets are also a valid
12798                  options.  */
12799               for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
12800                 {
12801                   if (regs == vfp_versions[newval].regs
12802                       && ver == vfp_versions[newval].ver)
12803                     break;
12804                 }
12805               out_attr[i].i = newval;
12806             }
12807           break;
12808         case Tag_PCS_config:
12809           if (out_attr[i].i == 0)
12810             out_attr[i].i = in_attr[i].i;
12811           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
12812             {
12813               /* It's sometimes ok to mix different configs, so this is only
12814                  a warning.  */
12815               _bfd_error_handler
12816                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
12817             }
12818           break;
12819         case Tag_ABI_PCS_R9_use:
12820           if (in_attr[i].i != out_attr[i].i
12821               && out_attr[i].i != AEABI_R9_unused
12822               && in_attr[i].i != AEABI_R9_unused)
12823             {
12824               _bfd_error_handler
12825                 (_("error: %B: Conflicting use of R9"), ibfd);
12826               result = FALSE;
12827             }
12828           if (out_attr[i].i == AEABI_R9_unused)
12829             out_attr[i].i = in_attr[i].i;
12830           break;
12831         case Tag_ABI_PCS_RW_data:
12832           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
12833               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
12834               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
12835             {
12836               _bfd_error_handler
12837                 (_("error: %B: SB relative addressing conflicts with use of R9"),
12838                  ibfd);
12839               result = FALSE;
12840             }
12841           /* Use the smallest value specified.  */
12842           if (in_attr[i].i < out_attr[i].i)
12843             out_attr[i].i = in_attr[i].i;
12844           break;
12845         case Tag_ABI_PCS_wchar_t:
12846           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
12847               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
12848             {
12849               _bfd_error_handler
12850                 (_("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"),
12851                  ibfd, in_attr[i].i, out_attr[i].i);
12852             }
12853           else if (in_attr[i].i && !out_attr[i].i)
12854             out_attr[i].i = in_attr[i].i;
12855           break;
12856         case Tag_ABI_enum_size:
12857           if (in_attr[i].i != AEABI_enum_unused)
12858             {
12859               if (out_attr[i].i == AEABI_enum_unused
12860                   || out_attr[i].i == AEABI_enum_forced_wide)
12861                 {
12862                   /* The existing object is compatible with anything.
12863                      Use whatever requirements the new object has.  */
12864                   out_attr[i].i = in_attr[i].i;
12865                 }
12866               else if (in_attr[i].i != AEABI_enum_forced_wide
12867                        && out_attr[i].i != in_attr[i].i
12868                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
12869                 {
12870                   static const char *aeabi_enum_names[] =
12871                     { "", "variable-size", "32-bit", "" };
12872                   const char *in_name =
12873                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
12874                     ? aeabi_enum_names[in_attr[i].i]
12875                     : "<unknown>";
12876                   const char *out_name =
12877                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
12878                     ? aeabi_enum_names[out_attr[i].i]
12879                     : "<unknown>";
12880                   _bfd_error_handler
12881                     (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
12882                      ibfd, in_name, out_name);
12883                 }
12884             }
12885           break;
12886         case Tag_ABI_VFP_args:
12887           /* Aready done.  */
12888           break;
12889         case Tag_ABI_WMMX_args:
12890           if (in_attr[i].i != out_attr[i].i)
12891             {
12892               _bfd_error_handler
12893                 (_("error: %B uses iWMMXt register arguments, %B does not"),
12894                  ibfd, obfd);
12895               result = FALSE;
12896             }
12897           break;
12898         case Tag_compatibility:
12899           /* Merged in target-independent code.  */
12900           break;
12901         case Tag_ABI_HardFP_use:
12902           /* This is handled along with Tag_FP_arch.  */
12903           break;
12904         case Tag_ABI_FP_16bit_format:
12905           if (in_attr[i].i != 0 && out_attr[i].i != 0)
12906             {
12907               if (in_attr[i].i != out_attr[i].i)
12908                 {
12909                   _bfd_error_handler
12910                     (_("error: fp16 format mismatch between %B and %B"),
12911                      ibfd, obfd);
12912                   result = FALSE;
12913                 }
12914             }
12915           if (in_attr[i].i != 0)
12916             out_attr[i].i = in_attr[i].i;
12917           break;
12918
12919         case Tag_DIV_use:
12920           /* A value of zero on input means that the divide instruction may
12921              be used if available in the base architecture as specified via
12922              Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
12923              the user did not want divide instructions.  A value of 2
12924              explicitly means that divide instructions were allowed in ARM
12925              and Thumb state.  */
12926           if (in_attr[i].i == out_attr[i].i)
12927             /* Do nothing.  */ ;
12928           else if (elf32_arm_attributes_forbid_div (in_attr)
12929                    && !elf32_arm_attributes_accept_div (out_attr))
12930             out_attr[i].i = 1;
12931           else if (elf32_arm_attributes_forbid_div (out_attr)
12932                    && elf32_arm_attributes_accept_div (in_attr))
12933             out_attr[i].i = in_attr[i].i;
12934           else if (in_attr[i].i == 2)
12935             out_attr[i].i = in_attr[i].i;
12936           break;
12937
12938         case Tag_MPextension_use_legacy:
12939           /* We don't output objects with Tag_MPextension_use_legacy - we
12940              move the value to Tag_MPextension_use.  */
12941           if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
12942             {
12943               if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
12944                 {
12945                   _bfd_error_handler
12946                     (_("%B has has both the current and legacy "
12947                        "Tag_MPextension_use attributes"),
12948                      ibfd);
12949                   result = FALSE;
12950                 }
12951             }
12952
12953           if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
12954             out_attr[Tag_MPextension_use] = in_attr[i];
12955
12956           break;
12957
12958         case Tag_nodefaults:
12959           /* This tag is set if it exists, but the value is unused (and is
12960              typically zero).  We don't actually need to do anything here -
12961              the merge happens automatically when the type flags are merged
12962              below.  */
12963           break;
12964         case Tag_also_compatible_with:
12965           /* Already done in Tag_CPU_arch.  */
12966           break;
12967         case Tag_conformance:
12968           /* Keep the attribute if it matches.  Throw it away otherwise.
12969              No attribute means no claim to conform.  */
12970           if (!in_attr[i].s || !out_attr[i].s
12971               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
12972             out_attr[i].s = NULL;
12973           break;
12974
12975         default:
12976           result
12977             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
12978         }
12979
12980       /* If out_attr was copied from in_attr then it won't have a type yet.  */
12981       if (in_attr[i].type && !out_attr[i].type)
12982         out_attr[i].type = in_attr[i].type;
12983     }
12984
12985   /* Merge Tag_compatibility attributes and any common GNU ones.  */
12986   if (!_bfd_elf_merge_object_attributes (ibfd, obfd))
12987     return FALSE;
12988
12989   /* Check for any attributes not known on ARM.  */
12990   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
12991
12992   return result;
12993 }
12994
12995
12996 /* Return TRUE if the two EABI versions are incompatible.  */
12997
12998 static bfd_boolean
12999 elf32_arm_versions_compatible (unsigned iver, unsigned over)
13000 {
13001   /* v4 and v5 are the same spec before and after it was released,
13002      so allow mixing them.  */
13003   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
13004       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
13005     return TRUE;
13006
13007   return (iver == over);
13008 }
13009
13010 /* Merge backend specific data from an object file to the output
13011    object file when linking.  */
13012
13013 static bfd_boolean
13014 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd);
13015
13016 /* Display the flags field.  */
13017
13018 static bfd_boolean
13019 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
13020 {
13021   FILE * file = (FILE *) ptr;
13022   unsigned long flags;
13023
13024   BFD_ASSERT (abfd != NULL && ptr != NULL);
13025
13026   /* Print normal ELF private data.  */
13027   _bfd_elf_print_private_bfd_data (abfd, ptr);
13028
13029   flags = elf_elfheader (abfd)->e_flags;
13030   /* Ignore init flag - it may not be set, despite the flags field
13031      containing valid data.  */
13032
13033   /* xgettext:c-format */
13034   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
13035
13036   switch (EF_ARM_EABI_VERSION (flags))
13037     {
13038     case EF_ARM_EABI_UNKNOWN:
13039       /* The following flag bits are GNU extensions and not part of the
13040          official ARM ELF extended ABI.  Hence they are only decoded if
13041          the EABI version is not set.  */
13042       if (flags & EF_ARM_INTERWORK)
13043         fprintf (file, _(" [interworking enabled]"));
13044
13045       if (flags & EF_ARM_APCS_26)
13046         fprintf (file, " [APCS-26]");
13047       else
13048         fprintf (file, " [APCS-32]");
13049
13050       if (flags & EF_ARM_VFP_FLOAT)
13051         fprintf (file, _(" [VFP float format]"));
13052       else if (flags & EF_ARM_MAVERICK_FLOAT)
13053         fprintf (file, _(" [Maverick float format]"));
13054       else
13055         fprintf (file, _(" [FPA float format]"));
13056
13057       if (flags & EF_ARM_APCS_FLOAT)
13058         fprintf (file, _(" [floats passed in float registers]"));
13059
13060       if (flags & EF_ARM_PIC)
13061         fprintf (file, _(" [position independent]"));
13062
13063       if (flags & EF_ARM_NEW_ABI)
13064         fprintf (file, _(" [new ABI]"));
13065
13066       if (flags & EF_ARM_OLD_ABI)
13067         fprintf (file, _(" [old ABI]"));
13068
13069       if (flags & EF_ARM_SOFT_FLOAT)
13070         fprintf (file, _(" [software FP]"));
13071
13072       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
13073                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
13074                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
13075                  | EF_ARM_MAVERICK_FLOAT);
13076       break;
13077
13078     case EF_ARM_EABI_VER1:
13079       fprintf (file, _(" [Version1 EABI]"));
13080
13081       if (flags & EF_ARM_SYMSARESORTED)
13082         fprintf (file, _(" [sorted symbol table]"));
13083       else
13084         fprintf (file, _(" [unsorted symbol table]"));
13085
13086       flags &= ~ EF_ARM_SYMSARESORTED;
13087       break;
13088
13089     case EF_ARM_EABI_VER2:
13090       fprintf (file, _(" [Version2 EABI]"));
13091
13092       if (flags & EF_ARM_SYMSARESORTED)
13093         fprintf (file, _(" [sorted symbol table]"));
13094       else
13095         fprintf (file, _(" [unsorted symbol table]"));
13096
13097       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
13098         fprintf (file, _(" [dynamic symbols use segment index]"));
13099
13100       if (flags & EF_ARM_MAPSYMSFIRST)
13101         fprintf (file, _(" [mapping symbols precede others]"));
13102
13103       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
13104                  | EF_ARM_MAPSYMSFIRST);
13105       break;
13106
13107     case EF_ARM_EABI_VER3:
13108       fprintf (file, _(" [Version3 EABI]"));
13109       break;
13110
13111     case EF_ARM_EABI_VER4:
13112       fprintf (file, _(" [Version4 EABI]"));
13113       goto eabi;
13114
13115     case EF_ARM_EABI_VER5:
13116       fprintf (file, _(" [Version5 EABI]"));
13117
13118       if (flags & EF_ARM_ABI_FLOAT_SOFT)
13119         fprintf (file, _(" [soft-float ABI]"));
13120
13121       if (flags & EF_ARM_ABI_FLOAT_HARD)
13122         fprintf (file, _(" [hard-float ABI]"));
13123
13124       flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
13125
13126     eabi:
13127       if (flags & EF_ARM_BE8)
13128         fprintf (file, _(" [BE8]"));
13129
13130       if (flags & EF_ARM_LE8)
13131         fprintf (file, _(" [LE8]"));
13132
13133       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
13134       break;
13135
13136     default:
13137       fprintf (file, _(" <EABI version unrecognised>"));
13138       break;
13139     }
13140
13141   flags &= ~ EF_ARM_EABIMASK;
13142
13143   if (flags & EF_ARM_RELEXEC)
13144     fprintf (file, _(" [relocatable executable]"));
13145
13146   flags &= ~EF_ARM_RELEXEC;
13147
13148   if (flags)
13149     fprintf (file, _("<Unrecognised flag bits set>"));
13150
13151   fputc ('\n', file);
13152
13153   return TRUE;
13154 }
13155
13156 static int
13157 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
13158 {
13159   switch (ELF_ST_TYPE (elf_sym->st_info))
13160     {
13161     case STT_ARM_TFUNC:
13162       return ELF_ST_TYPE (elf_sym->st_info);
13163
13164     case STT_ARM_16BIT:
13165       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
13166          This allows us to distinguish between data used by Thumb instructions
13167          and non-data (which is probably code) inside Thumb regions of an
13168          executable.  */
13169       if (type != STT_OBJECT && type != STT_TLS)
13170         return ELF_ST_TYPE (elf_sym->st_info);
13171       break;
13172
13173     default:
13174       break;
13175     }
13176
13177   return type;
13178 }
13179
13180 static asection *
13181 elf32_arm_gc_mark_hook (asection *sec,
13182                         struct bfd_link_info *info,
13183                         Elf_Internal_Rela *rel,
13184                         struct elf_link_hash_entry *h,
13185                         Elf_Internal_Sym *sym)
13186 {
13187   if (h != NULL)
13188     switch (ELF32_R_TYPE (rel->r_info))
13189       {
13190       case R_ARM_GNU_VTINHERIT:
13191       case R_ARM_GNU_VTENTRY:
13192         return NULL;
13193       }
13194
13195   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
13196 }
13197
13198 /* Update the got entry reference counts for the section being removed.  */
13199
13200 static bfd_boolean
13201 elf32_arm_gc_sweep_hook (bfd *                     abfd,
13202                          struct bfd_link_info *    info,
13203                          asection *                sec,
13204                          const Elf_Internal_Rela * relocs)
13205 {
13206   Elf_Internal_Shdr *symtab_hdr;
13207   struct elf_link_hash_entry **sym_hashes;
13208   bfd_signed_vma *local_got_refcounts;
13209   const Elf_Internal_Rela *rel, *relend;
13210   struct elf32_arm_link_hash_table * globals;
13211
13212   if (bfd_link_relocatable (info))
13213     return TRUE;
13214
13215   globals = elf32_arm_hash_table (info);
13216   if (globals == NULL)
13217     return FALSE;
13218
13219   elf_section_data (sec)->local_dynrel = NULL;
13220
13221   symtab_hdr = & elf_symtab_hdr (abfd);
13222   sym_hashes = elf_sym_hashes (abfd);
13223   local_got_refcounts = elf_local_got_refcounts (abfd);
13224
13225   check_use_blx (globals);
13226
13227   relend = relocs + sec->reloc_count;
13228   for (rel = relocs; rel < relend; rel++)
13229     {
13230       unsigned long r_symndx;
13231       struct elf_link_hash_entry *h = NULL;
13232       struct elf32_arm_link_hash_entry *eh;
13233       int r_type;
13234       bfd_boolean call_reloc_p;
13235       bfd_boolean may_become_dynamic_p;
13236       bfd_boolean may_need_local_target_p;
13237       union gotplt_union *root_plt;
13238       struct arm_plt_info *arm_plt;
13239
13240       r_symndx = ELF32_R_SYM (rel->r_info);
13241       if (r_symndx >= symtab_hdr->sh_info)
13242         {
13243           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13244           while (h->root.type == bfd_link_hash_indirect
13245                  || h->root.type == bfd_link_hash_warning)
13246             h = (struct elf_link_hash_entry *) h->root.u.i.link;
13247         }
13248       eh = (struct elf32_arm_link_hash_entry *) h;
13249
13250       call_reloc_p = FALSE;
13251       may_become_dynamic_p = FALSE;
13252       may_need_local_target_p = FALSE;
13253
13254       r_type = ELF32_R_TYPE (rel->r_info);
13255       r_type = arm_real_reloc_type (globals, r_type);
13256       switch (r_type)
13257         {
13258         case R_ARM_GOT32:
13259         case R_ARM_GOT_PREL:
13260         case R_ARM_TLS_GD32:
13261         case R_ARM_TLS_IE32:
13262           if (h != NULL)
13263             {
13264               if (h->got.refcount > 0)
13265                 h->got.refcount -= 1;
13266             }
13267           else if (local_got_refcounts != NULL)
13268             {
13269               if (local_got_refcounts[r_symndx] > 0)
13270                 local_got_refcounts[r_symndx] -= 1;
13271             }
13272           break;
13273
13274         case R_ARM_TLS_LDM32:
13275           globals->tls_ldm_got.refcount -= 1;
13276           break;
13277
13278         case R_ARM_PC24:
13279         case R_ARM_PLT32:
13280         case R_ARM_CALL:
13281         case R_ARM_JUMP24:
13282         case R_ARM_PREL31:
13283         case R_ARM_THM_CALL:
13284         case R_ARM_THM_JUMP24:
13285         case R_ARM_THM_JUMP19:
13286           call_reloc_p = TRUE;
13287           may_need_local_target_p = TRUE;
13288           break;
13289
13290         case R_ARM_ABS12:
13291           if (!globals->vxworks_p)
13292             {
13293               may_need_local_target_p = TRUE;
13294               break;
13295             }
13296           /* Fall through.  */
13297         case R_ARM_ABS32:
13298         case R_ARM_ABS32_NOI:
13299         case R_ARM_REL32:
13300         case R_ARM_REL32_NOI:
13301         case R_ARM_MOVW_ABS_NC:
13302         case R_ARM_MOVT_ABS:
13303         case R_ARM_MOVW_PREL_NC:
13304         case R_ARM_MOVT_PREL:
13305         case R_ARM_THM_MOVW_ABS_NC:
13306         case R_ARM_THM_MOVT_ABS:
13307         case R_ARM_THM_MOVW_PREL_NC:
13308         case R_ARM_THM_MOVT_PREL:
13309           /* Should the interworking branches be here also?  */
13310           if ((bfd_link_pic (info) || globals->root.is_relocatable_executable)
13311               && (sec->flags & SEC_ALLOC) != 0)
13312             {
13313               if (h == NULL
13314                   && elf32_arm_howto_from_type (r_type)->pc_relative)
13315                 {
13316                   call_reloc_p = TRUE;
13317                   may_need_local_target_p = TRUE;
13318                 }
13319               else
13320                 may_become_dynamic_p = TRUE;
13321             }
13322           else
13323             may_need_local_target_p = TRUE;
13324           break;
13325
13326         default:
13327           break;
13328         }
13329
13330       if (may_need_local_target_p
13331           && elf32_arm_get_plt_info (abfd, eh, r_symndx, &root_plt, &arm_plt))
13332         {
13333           /* If PLT refcount book-keeping is wrong and too low, we'll
13334              see a zero value (going to -1) for the root PLT reference
13335              count.  */
13336           if (root_plt->refcount >= 0)
13337             {
13338               BFD_ASSERT (root_plt->refcount != 0);
13339               root_plt->refcount -= 1;
13340             }
13341           else
13342             /* A value of -1 means the symbol has become local, forced
13343                or seeing a hidden definition.  Any other negative value
13344                is an error.  */
13345             BFD_ASSERT (root_plt->refcount == -1);
13346
13347           if (!call_reloc_p)
13348             arm_plt->noncall_refcount--;
13349
13350           if (r_type == R_ARM_THM_CALL)
13351             arm_plt->maybe_thumb_refcount--;
13352
13353           if (r_type == R_ARM_THM_JUMP24
13354               || r_type == R_ARM_THM_JUMP19)
13355             arm_plt->thumb_refcount--;
13356         }
13357
13358       if (may_become_dynamic_p)
13359         {
13360           struct elf_dyn_relocs **pp;
13361           struct elf_dyn_relocs *p;
13362
13363           if (h != NULL)
13364             pp = &(eh->dyn_relocs);
13365           else
13366             {
13367               Elf_Internal_Sym *isym;
13368
13369               isym = bfd_sym_from_r_symndx (&globals->sym_cache,
13370                                             abfd, r_symndx);
13371               if (isym == NULL)
13372                 return FALSE;
13373               pp = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
13374               if (pp == NULL)
13375                 return FALSE;
13376             }
13377           for (; (p = *pp) != NULL; pp = &p->next)
13378             if (p->sec == sec)
13379               {
13380                 /* Everything must go for SEC.  */
13381                 *pp = p->next;
13382                 break;
13383               }
13384         }
13385     }
13386
13387   return TRUE;
13388 }
13389
13390 /* Look through the relocs for a section during the first phase.  */
13391
13392 static bfd_boolean
13393 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
13394                         asection *sec, const Elf_Internal_Rela *relocs)
13395 {
13396   Elf_Internal_Shdr *symtab_hdr;
13397   struct elf_link_hash_entry **sym_hashes;
13398   const Elf_Internal_Rela *rel;
13399   const Elf_Internal_Rela *rel_end;
13400   bfd *dynobj;
13401   asection *sreloc;
13402   struct elf32_arm_link_hash_table *htab;
13403   bfd_boolean call_reloc_p;
13404   bfd_boolean may_become_dynamic_p;
13405   bfd_boolean may_need_local_target_p;
13406   unsigned long nsyms;
13407
13408   if (bfd_link_relocatable (info))
13409     return TRUE;
13410
13411   BFD_ASSERT (is_arm_elf (abfd));
13412
13413   htab = elf32_arm_hash_table (info);
13414   if (htab == NULL)
13415     return FALSE;
13416
13417   sreloc = NULL;
13418
13419   /* Create dynamic sections for relocatable executables so that we can
13420      copy relocations.  */
13421   if (htab->root.is_relocatable_executable
13422       && ! htab->root.dynamic_sections_created)
13423     {
13424       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
13425         return FALSE;
13426     }
13427
13428   if (htab->root.dynobj == NULL)
13429     htab->root.dynobj = abfd;
13430   if (!create_ifunc_sections (info))
13431     return FALSE;
13432
13433   dynobj = htab->root.dynobj;
13434
13435   symtab_hdr = & elf_symtab_hdr (abfd);
13436   sym_hashes = elf_sym_hashes (abfd);
13437   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
13438
13439   rel_end = relocs + sec->reloc_count;
13440   for (rel = relocs; rel < rel_end; rel++)
13441     {
13442       Elf_Internal_Sym *isym;
13443       struct elf_link_hash_entry *h;
13444       struct elf32_arm_link_hash_entry *eh;
13445       unsigned long r_symndx;
13446       int r_type;
13447
13448       r_symndx = ELF32_R_SYM (rel->r_info);
13449       r_type = ELF32_R_TYPE (rel->r_info);
13450       r_type = arm_real_reloc_type (htab, r_type);
13451
13452       if (r_symndx >= nsyms
13453           /* PR 9934: It is possible to have relocations that do not
13454              refer to symbols, thus it is also possible to have an
13455              object file containing relocations but no symbol table.  */
13456           && (r_symndx > STN_UNDEF || nsyms > 0))
13457         {
13458           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
13459                                    r_symndx);
13460           return FALSE;
13461         }
13462
13463       h = NULL;
13464       isym = NULL;
13465       if (nsyms > 0)
13466         {
13467           if (r_symndx < symtab_hdr->sh_info)
13468             {
13469               /* A local symbol.  */
13470               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
13471                                             abfd, r_symndx);
13472               if (isym == NULL)
13473                 return FALSE;
13474             }
13475           else
13476             {
13477               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13478               while (h->root.type == bfd_link_hash_indirect
13479                      || h->root.type == bfd_link_hash_warning)
13480                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13481
13482               /* PR15323, ref flags aren't set for references in the
13483                  same object.  */
13484               h->root.non_ir_ref = 1;
13485             }
13486         }
13487
13488       eh = (struct elf32_arm_link_hash_entry *) h;
13489
13490       call_reloc_p = FALSE;
13491       may_become_dynamic_p = FALSE;
13492       may_need_local_target_p = FALSE;
13493
13494       /* Could be done earlier, if h were already available.  */
13495       r_type = elf32_arm_tls_transition (info, r_type, h);
13496       switch (r_type)
13497         {
13498           case R_ARM_GOT32:
13499           case R_ARM_GOT_PREL:
13500           case R_ARM_TLS_GD32:
13501           case R_ARM_TLS_IE32:
13502           case R_ARM_TLS_GOTDESC:
13503           case R_ARM_TLS_DESCSEQ:
13504           case R_ARM_THM_TLS_DESCSEQ:
13505           case R_ARM_TLS_CALL:
13506           case R_ARM_THM_TLS_CALL:
13507             /* This symbol requires a global offset table entry.  */
13508             {
13509               int tls_type, old_tls_type;
13510
13511               switch (r_type)
13512                 {
13513                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
13514
13515                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
13516
13517                 case R_ARM_TLS_GOTDESC:
13518                 case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
13519                 case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
13520                   tls_type = GOT_TLS_GDESC; break;
13521
13522                 default: tls_type = GOT_NORMAL; break;
13523                 }
13524
13525               if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
13526                 info->flags |= DF_STATIC_TLS;
13527
13528               if (h != NULL)
13529                 {
13530                   h->got.refcount++;
13531                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
13532                 }
13533               else
13534                 {
13535                   /* This is a global offset table entry for a local symbol.  */
13536                   if (!elf32_arm_allocate_local_sym_info (abfd))
13537                     return FALSE;
13538                   elf_local_got_refcounts (abfd)[r_symndx] += 1;
13539                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
13540                 }
13541
13542               /* If a variable is accessed with both tls methods, two
13543                  slots may be created.  */
13544               if (GOT_TLS_GD_ANY_P (old_tls_type)
13545                   && GOT_TLS_GD_ANY_P (tls_type))
13546                 tls_type |= old_tls_type;
13547
13548               /* We will already have issued an error message if there
13549                  is a TLS/non-TLS mismatch, based on the symbol
13550                  type.  So just combine any TLS types needed.  */
13551               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
13552                   && tls_type != GOT_NORMAL)
13553                 tls_type |= old_tls_type;
13554
13555               /* If the symbol is accessed in both IE and GDESC
13556                  method, we're able to relax. Turn off the GDESC flag,
13557                  without messing up with any other kind of tls types
13558                  that may be involved.  */
13559               if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
13560                 tls_type &= ~GOT_TLS_GDESC;
13561
13562               if (old_tls_type != tls_type)
13563                 {
13564                   if (h != NULL)
13565                     elf32_arm_hash_entry (h)->tls_type = tls_type;
13566                   else
13567                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
13568                 }
13569             }
13570             /* Fall through.  */
13571
13572           case R_ARM_TLS_LDM32:
13573             if (r_type == R_ARM_TLS_LDM32)
13574                 htab->tls_ldm_got.refcount++;
13575             /* Fall through.  */
13576
13577           case R_ARM_GOTOFF32:
13578           case R_ARM_GOTPC:
13579             if (htab->root.sgot == NULL
13580                 && !create_got_section (htab->root.dynobj, info))
13581               return FALSE;
13582             break;
13583
13584           case R_ARM_PC24:
13585           case R_ARM_PLT32:
13586           case R_ARM_CALL:
13587           case R_ARM_JUMP24:
13588           case R_ARM_PREL31:
13589           case R_ARM_THM_CALL:
13590           case R_ARM_THM_JUMP24:
13591           case R_ARM_THM_JUMP19:
13592             call_reloc_p = TRUE;
13593             may_need_local_target_p = TRUE;
13594             break;
13595
13596           case R_ARM_ABS12:
13597             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
13598                ldr __GOTT_INDEX__ offsets.  */
13599             if (!htab->vxworks_p)
13600               {
13601                 may_need_local_target_p = TRUE;
13602                 break;
13603               }
13604             /* Fall through.  */
13605
13606           case R_ARM_MOVW_ABS_NC:
13607           case R_ARM_MOVT_ABS:
13608           case R_ARM_THM_MOVW_ABS_NC:
13609           case R_ARM_THM_MOVT_ABS:
13610             if (bfd_link_pic (info))
13611               {
13612                 (*_bfd_error_handler)
13613                   (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
13614                    abfd, elf32_arm_howto_table_1[r_type].name,
13615                    (h) ? h->root.root.string : "a local symbol");
13616                 bfd_set_error (bfd_error_bad_value);
13617                 return FALSE;
13618               }
13619
13620             /* Fall through.  */
13621           case R_ARM_ABS32:
13622           case R_ARM_ABS32_NOI:
13623             if (h != NULL && bfd_link_executable (info))
13624               {
13625                 h->pointer_equality_needed = 1;
13626               }
13627             /* Fall through.  */
13628           case R_ARM_REL32:
13629           case R_ARM_REL32_NOI:
13630           case R_ARM_MOVW_PREL_NC:
13631           case R_ARM_MOVT_PREL:
13632           case R_ARM_THM_MOVW_PREL_NC:
13633           case R_ARM_THM_MOVT_PREL:
13634
13635             /* Should the interworking branches be listed here?  */
13636             if ((bfd_link_pic (info) || htab->root.is_relocatable_executable)
13637                 && (sec->flags & SEC_ALLOC) != 0)
13638               {
13639                 if (h == NULL
13640                     && elf32_arm_howto_from_type (r_type)->pc_relative)
13641                   {
13642                     /* In shared libraries and relocatable executables,
13643                        we treat local relative references as calls;
13644                        see the related SYMBOL_CALLS_LOCAL code in
13645                        allocate_dynrelocs.  */
13646                     call_reloc_p = TRUE;
13647                     may_need_local_target_p = TRUE;
13648                   }
13649                 else
13650                   /* We are creating a shared library or relocatable
13651                      executable, and this is a reloc against a global symbol,
13652                      or a non-PC-relative reloc against a local symbol.
13653                      We may need to copy the reloc into the output.  */
13654                   may_become_dynamic_p = TRUE;
13655               }
13656             else
13657               may_need_local_target_p = TRUE;
13658             break;
13659
13660         /* This relocation describes the C++ object vtable hierarchy.
13661            Reconstruct it for later use during GC.  */
13662         case R_ARM_GNU_VTINHERIT:
13663           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
13664             return FALSE;
13665           break;
13666
13667         /* This relocation describes which C++ vtable entries are actually
13668            used.  Record for later use during GC.  */
13669         case R_ARM_GNU_VTENTRY:
13670           BFD_ASSERT (h != NULL);
13671           if (h != NULL
13672               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
13673             return FALSE;
13674           break;
13675         }
13676
13677       if (h != NULL)
13678         {
13679           if (call_reloc_p)
13680             /* We may need a .plt entry if the function this reloc
13681                refers to is in a different object, regardless of the
13682                symbol's type.  We can't tell for sure yet, because
13683                something later might force the symbol local.  */
13684             h->needs_plt = 1;
13685           else if (may_need_local_target_p)
13686             /* If this reloc is in a read-only section, we might
13687                need a copy reloc.  We can't check reliably at this
13688                stage whether the section is read-only, as input
13689                sections have not yet been mapped to output sections.
13690                Tentatively set the flag for now, and correct in
13691                adjust_dynamic_symbol.  */
13692             h->non_got_ref = 1;
13693         }
13694
13695       if (may_need_local_target_p
13696           && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
13697         {
13698           union gotplt_union *root_plt;
13699           struct arm_plt_info *arm_plt;
13700           struct arm_local_iplt_info *local_iplt;
13701
13702           if (h != NULL)
13703             {
13704               root_plt = &h->plt;
13705               arm_plt = &eh->plt;
13706             }
13707           else
13708             {
13709               local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
13710               if (local_iplt == NULL)
13711                 return FALSE;
13712               root_plt = &local_iplt->root;
13713               arm_plt = &local_iplt->arm;
13714             }
13715
13716           /* If the symbol is a function that doesn't bind locally,
13717              this relocation will need a PLT entry.  */
13718           if (root_plt->refcount != -1)
13719             root_plt->refcount += 1;
13720
13721           if (!call_reloc_p)
13722             arm_plt->noncall_refcount++;
13723
13724           /* It's too early to use htab->use_blx here, so we have to
13725              record possible blx references separately from
13726              relocs that definitely need a thumb stub.  */
13727
13728           if (r_type == R_ARM_THM_CALL)
13729             arm_plt->maybe_thumb_refcount += 1;
13730
13731           if (r_type == R_ARM_THM_JUMP24
13732               || r_type == R_ARM_THM_JUMP19)
13733             arm_plt->thumb_refcount += 1;
13734         }
13735
13736       if (may_become_dynamic_p)
13737         {
13738           struct elf_dyn_relocs *p, **head;
13739
13740           /* Create a reloc section in dynobj.  */
13741           if (sreloc == NULL)
13742             {
13743               sreloc = _bfd_elf_make_dynamic_reloc_section
13744                 (sec, dynobj, 2, abfd, ! htab->use_rel);
13745
13746               if (sreloc == NULL)
13747                 return FALSE;
13748
13749               /* BPABI objects never have dynamic relocations mapped.  */
13750               if (htab->symbian_p)
13751                 {
13752                   flagword flags;
13753
13754                   flags = bfd_get_section_flags (dynobj, sreloc);
13755                   flags &= ~(SEC_LOAD | SEC_ALLOC);
13756                   bfd_set_section_flags (dynobj, sreloc, flags);
13757                 }
13758             }
13759
13760           /* If this is a global symbol, count the number of
13761              relocations we need for this symbol.  */
13762           if (h != NULL)
13763             head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
13764           else
13765             {
13766               head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
13767               if (head == NULL)
13768                 return FALSE;
13769             }
13770
13771           p = *head;
13772           if (p == NULL || p->sec != sec)
13773             {
13774               bfd_size_type amt = sizeof *p;
13775
13776               p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
13777               if (p == NULL)
13778                 return FALSE;
13779               p->next = *head;
13780               *head = p;
13781               p->sec = sec;
13782               p->count = 0;
13783               p->pc_count = 0;
13784             }
13785
13786           if (elf32_arm_howto_from_type (r_type)->pc_relative)
13787             p->pc_count += 1;
13788           p->count += 1;
13789         }
13790     }
13791
13792   return TRUE;
13793 }
13794
13795 /* Unwinding tables are not referenced directly.  This pass marks them as
13796    required if the corresponding code section is marked.  */
13797
13798 static bfd_boolean
13799 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
13800                                   elf_gc_mark_hook_fn gc_mark_hook)
13801 {
13802   bfd *sub;
13803   Elf_Internal_Shdr **elf_shdrp;
13804   bfd_boolean again;
13805
13806   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
13807
13808   /* Marking EH data may cause additional code sections to be marked,
13809      requiring multiple passes.  */
13810   again = TRUE;
13811   while (again)
13812     {
13813       again = FALSE;
13814       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13815         {
13816           asection *o;
13817
13818           if (! is_arm_elf (sub))
13819             continue;
13820
13821           elf_shdrp = elf_elfsections (sub);
13822           for (o = sub->sections; o != NULL; o = o->next)
13823             {
13824               Elf_Internal_Shdr *hdr;
13825
13826               hdr = &elf_section_data (o)->this_hdr;
13827               if (hdr->sh_type == SHT_ARM_EXIDX
13828                   && hdr->sh_link
13829                   && hdr->sh_link < elf_numsections (sub)
13830                   && !o->gc_mark
13831                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
13832                 {
13833                   again = TRUE;
13834                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13835                     return FALSE;
13836                 }
13837             }
13838         }
13839     }
13840
13841   return TRUE;
13842 }
13843
13844 /* Treat mapping symbols as special target symbols.  */
13845
13846 static bfd_boolean
13847 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
13848 {
13849   return bfd_is_arm_special_symbol_name (sym->name,
13850                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
13851 }
13852
13853 /* This is a copy of elf_find_function() from elf.c except that
13854    ARM mapping symbols are ignored when looking for function names
13855    and STT_ARM_TFUNC is considered to a function type.  */
13856
13857 static bfd_boolean
13858 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
13859                        asymbol **    symbols,
13860                        asection *    section,
13861                        bfd_vma       offset,
13862                        const char ** filename_ptr,
13863                        const char ** functionname_ptr)
13864 {
13865   const char * filename = NULL;
13866   asymbol * func = NULL;
13867   bfd_vma low_func = 0;
13868   asymbol ** p;
13869
13870   for (p = symbols; *p != NULL; p++)
13871     {
13872       elf_symbol_type *q;
13873
13874       q = (elf_symbol_type *) *p;
13875
13876       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
13877         {
13878         default:
13879           break;
13880         case STT_FILE:
13881           filename = bfd_asymbol_name (&q->symbol);
13882           break;
13883         case STT_FUNC:
13884         case STT_ARM_TFUNC:
13885         case STT_NOTYPE:
13886           /* Skip mapping symbols.  */
13887           if ((q->symbol.flags & BSF_LOCAL)
13888               && bfd_is_arm_special_symbol_name (q->symbol.name,
13889                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
13890             continue;
13891           /* Fall through.  */
13892           if (bfd_get_section (&q->symbol) == section
13893               && q->symbol.value >= low_func
13894               && q->symbol.value <= offset)
13895             {
13896               func = (asymbol *) q;
13897               low_func = q->symbol.value;
13898             }
13899           break;
13900         }
13901     }
13902
13903   if (func == NULL)
13904     return FALSE;
13905
13906   if (filename_ptr)
13907     *filename_ptr = filename;
13908   if (functionname_ptr)
13909     *functionname_ptr = bfd_asymbol_name (func);
13910
13911   return TRUE;
13912 }
13913
13914
13915 /* Find the nearest line to a particular section and offset, for error
13916    reporting.   This code is a duplicate of the code in elf.c, except
13917    that it uses arm_elf_find_function.  */
13918
13919 static bfd_boolean
13920 elf32_arm_find_nearest_line (bfd *          abfd,
13921                              asymbol **     symbols,
13922                              asection *     section,
13923                              bfd_vma        offset,
13924                              const char **  filename_ptr,
13925                              const char **  functionname_ptr,
13926                              unsigned int * line_ptr,
13927                              unsigned int * discriminator_ptr)
13928 {
13929   bfd_boolean found = FALSE;
13930
13931   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13932                                      filename_ptr, functionname_ptr,
13933                                      line_ptr, discriminator_ptr,
13934                                      dwarf_debug_sections, 0,
13935                                      & elf_tdata (abfd)->dwarf2_find_line_info))
13936     {
13937       if (!*functionname_ptr)
13938         arm_elf_find_function (abfd, symbols, section, offset,
13939                                *filename_ptr ? NULL : filename_ptr,
13940                                functionname_ptr);
13941
13942       return TRUE;
13943     }
13944
13945   /* Skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain
13946      uses DWARF1.  */
13947
13948   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
13949                                              & found, filename_ptr,
13950                                              functionname_ptr, line_ptr,
13951                                              & elf_tdata (abfd)->line_info))
13952     return FALSE;
13953
13954   if (found && (*functionname_ptr || *line_ptr))
13955     return TRUE;
13956
13957   if (symbols == NULL)
13958     return FALSE;
13959
13960   if (! arm_elf_find_function (abfd, symbols, section, offset,
13961                                filename_ptr, functionname_ptr))
13962     return FALSE;
13963
13964   *line_ptr = 0;
13965   return TRUE;
13966 }
13967
13968 static bfd_boolean
13969 elf32_arm_find_inliner_info (bfd *          abfd,
13970                              const char **  filename_ptr,
13971                              const char **  functionname_ptr,
13972                              unsigned int * line_ptr)
13973 {
13974   bfd_boolean found;
13975   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13976                                          functionname_ptr, line_ptr,
13977                                          & elf_tdata (abfd)->dwarf2_find_line_info);
13978   return found;
13979 }
13980
13981 /* Adjust a symbol defined by a dynamic object and referenced by a
13982    regular object.  The current definition is in some section of the
13983    dynamic object, but we're not including those sections.  We have to
13984    change the definition to something the rest of the link can
13985    understand.  */
13986
13987 static bfd_boolean
13988 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
13989                                  struct elf_link_hash_entry * h)
13990 {
13991   bfd * dynobj;
13992   asection * s;
13993   struct elf32_arm_link_hash_entry * eh;
13994   struct elf32_arm_link_hash_table *globals;
13995
13996   globals = elf32_arm_hash_table (info);
13997   if (globals == NULL)
13998     return FALSE;
13999
14000   dynobj = elf_hash_table (info)->dynobj;
14001
14002   /* Make sure we know what is going on here.  */
14003   BFD_ASSERT (dynobj != NULL
14004               && (h->needs_plt
14005                   || h->type == STT_GNU_IFUNC
14006                   || h->u.weakdef != NULL
14007                   || (h->def_dynamic
14008                       && h->ref_regular
14009                       && !h->def_regular)));
14010
14011   eh = (struct elf32_arm_link_hash_entry *) h;
14012
14013   /* If this is a function, put it in the procedure linkage table.  We
14014      will fill in the contents of the procedure linkage table later,
14015      when we know the address of the .got section.  */
14016   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
14017     {
14018       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
14019          symbol binds locally.  */
14020       if (h->plt.refcount <= 0
14021           || (h->type != STT_GNU_IFUNC
14022               && (SYMBOL_CALLS_LOCAL (info, h)
14023                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
14024                       && h->root.type == bfd_link_hash_undefweak))))
14025         {
14026           /* This case can occur if we saw a PLT32 reloc in an input
14027              file, but the symbol was never referred to by a dynamic
14028              object, or if all references were garbage collected.  In
14029              such a case, we don't actually need to build a procedure
14030              linkage table, and we can just do a PC24 reloc instead.  */
14031           h->plt.offset = (bfd_vma) -1;
14032           eh->plt.thumb_refcount = 0;
14033           eh->plt.maybe_thumb_refcount = 0;
14034           eh->plt.noncall_refcount = 0;
14035           h->needs_plt = 0;
14036         }
14037
14038       return TRUE;
14039     }
14040   else
14041     {
14042       /* It's possible that we incorrectly decided a .plt reloc was
14043          needed for an R_ARM_PC24 or similar reloc to a non-function sym
14044          in check_relocs.  We can't decide accurately between function
14045          and non-function syms in check-relocs; Objects loaded later in
14046          the link may change h->type.  So fix it now.  */
14047       h->plt.offset = (bfd_vma) -1;
14048       eh->plt.thumb_refcount = 0;
14049       eh->plt.maybe_thumb_refcount = 0;
14050       eh->plt.noncall_refcount = 0;
14051     }
14052
14053   /* If this is a weak symbol, and there is a real definition, the
14054      processor independent code will have arranged for us to see the
14055      real definition first, and we can just use the same value.  */
14056   if (h->u.weakdef != NULL)
14057     {
14058       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
14059                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
14060       h->root.u.def.section = h->u.weakdef->root.u.def.section;
14061       h->root.u.def.value = h->u.weakdef->root.u.def.value;
14062       return TRUE;
14063     }
14064
14065   /* If there are no non-GOT references, we do not need a copy
14066      relocation.  */
14067   if (!h->non_got_ref)
14068     return TRUE;
14069
14070   /* This is a reference to a symbol defined by a dynamic object which
14071      is not a function.  */
14072
14073   /* If we are creating a shared library, we must presume that the
14074      only references to the symbol are via the global offset table.
14075      For such cases we need not do anything here; the relocations will
14076      be handled correctly by relocate_section.  Relocatable executables
14077      can reference data in shared objects directly, so we don't need to
14078      do anything here.  */
14079   if (bfd_link_pic (info) || globals->root.is_relocatable_executable)
14080     return TRUE;
14081
14082   /* We must allocate the symbol in our .dynbss section, which will
14083      become part of the .bss section of the executable.  There will be
14084      an entry for this symbol in the .dynsym section.  The dynamic
14085      object will contain position independent code, so all references
14086      from the dynamic object to this symbol will go through the global
14087      offset table.  The dynamic linker will use the .dynsym entry to
14088      determine the address it must put in the global offset table, so
14089      both the dynamic object and the regular object will refer to the
14090      same memory location for the variable.  */
14091   s = bfd_get_linker_section (dynobj, ".dynbss");
14092   BFD_ASSERT (s != NULL);
14093
14094   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
14095      copy the initial value out of the dynamic object and into the
14096      runtime process image.  We need to remember the offset into the
14097      .rel(a).bss section we are going to use.  */
14098   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
14099     {
14100       asection *srel;
14101
14102       srel = bfd_get_linker_section (dynobj, RELOC_SECTION (globals, ".bss"));
14103       elf32_arm_allocate_dynrelocs (info, srel, 1);
14104       h->needs_copy = 1;
14105     }
14106
14107   return _bfd_elf_adjust_dynamic_copy (info, h, s);
14108 }
14109
14110 /* Allocate space in .plt, .got and associated reloc sections for
14111    dynamic relocs.  */
14112
14113 static bfd_boolean
14114 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
14115 {
14116   struct bfd_link_info *info;
14117   struct elf32_arm_link_hash_table *htab;
14118   struct elf32_arm_link_hash_entry *eh;
14119   struct elf_dyn_relocs *p;
14120
14121   if (h->root.type == bfd_link_hash_indirect)
14122     return TRUE;
14123
14124   eh = (struct elf32_arm_link_hash_entry *) h;
14125
14126   info = (struct bfd_link_info *) inf;
14127   htab = elf32_arm_hash_table (info);
14128   if (htab == NULL)
14129     return FALSE;
14130
14131   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
14132       && h->plt.refcount > 0)
14133     {
14134       /* Make sure this symbol is output as a dynamic symbol.
14135          Undefined weak syms won't yet be marked as dynamic.  */
14136       if (h->dynindx == -1
14137           && !h->forced_local)
14138         {
14139           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14140             return FALSE;
14141         }
14142
14143       /* If the call in the PLT entry binds locally, the associated
14144          GOT entry should use an R_ARM_IRELATIVE relocation instead of
14145          the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
14146          than the .plt section.  */
14147       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
14148         {
14149           eh->is_iplt = 1;
14150           if (eh->plt.noncall_refcount == 0
14151               && SYMBOL_REFERENCES_LOCAL (info, h))
14152             /* All non-call references can be resolved directly.
14153                This means that they can (and in some cases, must)
14154                resolve directly to the run-time target, rather than
14155                to the PLT.  That in turns means that any .got entry
14156                would be equal to the .igot.plt entry, so there's
14157                no point having both.  */
14158             h->got.refcount = 0;
14159         }
14160
14161       if (bfd_link_pic (info)
14162           || eh->is_iplt
14163           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
14164         {
14165           elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
14166
14167           /* If this symbol is not defined in a regular file, and we are
14168              not generating a shared library, then set the symbol to this
14169              location in the .plt.  This is required to make function
14170              pointers compare as equal between the normal executable and
14171              the shared library.  */
14172           if (! bfd_link_pic (info)
14173               && !h->def_regular)
14174             {
14175               h->root.u.def.section = htab->root.splt;
14176               h->root.u.def.value = h->plt.offset;
14177
14178               /* Make sure the function is not marked as Thumb, in case
14179                  it is the target of an ABS32 relocation, which will
14180                  point to the PLT entry.  */
14181               h->target_internal = ST_BRANCH_TO_ARM;
14182             }
14183
14184           /* VxWorks executables have a second set of relocations for
14185              each PLT entry.  They go in a separate relocation section,
14186              which is processed by the kernel loader.  */
14187           if (htab->vxworks_p && !bfd_link_pic (info))
14188             {
14189               /* There is a relocation for the initial PLT entry:
14190                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
14191               if (h->plt.offset == htab->plt_header_size)
14192                 elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
14193
14194               /* There are two extra relocations for each subsequent
14195                  PLT entry: an R_ARM_32 relocation for the GOT entry,
14196                  and an R_ARM_32 relocation for the PLT entry.  */
14197               elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
14198             }
14199         }
14200       else
14201         {
14202           h->plt.offset = (bfd_vma) -1;
14203           h->needs_plt = 0;
14204         }
14205     }
14206   else
14207     {
14208       h->plt.offset = (bfd_vma) -1;
14209       h->needs_plt = 0;
14210     }
14211
14212   eh = (struct elf32_arm_link_hash_entry *) h;
14213   eh->tlsdesc_got = (bfd_vma) -1;
14214
14215   if (h->got.refcount > 0)
14216     {
14217       asection *s;
14218       bfd_boolean dyn;
14219       int tls_type = elf32_arm_hash_entry (h)->tls_type;
14220       int indx;
14221
14222       /* Make sure this symbol is output as a dynamic symbol.
14223          Undefined weak syms won't yet be marked as dynamic.  */
14224       if (h->dynindx == -1
14225           && !h->forced_local)
14226         {
14227           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14228             return FALSE;
14229         }
14230
14231       if (!htab->symbian_p)
14232         {
14233           s = htab->root.sgot;
14234           h->got.offset = s->size;
14235
14236           if (tls_type == GOT_UNKNOWN)
14237             abort ();
14238
14239           if (tls_type == GOT_NORMAL)
14240             /* Non-TLS symbols need one GOT slot.  */
14241             s->size += 4;
14242           else
14243             {
14244               if (tls_type & GOT_TLS_GDESC)
14245                 {
14246                   /* R_ARM_TLS_DESC needs 2 GOT slots.  */
14247                   eh->tlsdesc_got
14248                     = (htab->root.sgotplt->size
14249                        - elf32_arm_compute_jump_table_size (htab));
14250                   htab->root.sgotplt->size += 8;
14251                   h->got.offset = (bfd_vma) -2;
14252                   /* plt.got_offset needs to know there's a TLS_DESC
14253                      reloc in the middle of .got.plt.  */
14254                   htab->num_tls_desc++;
14255                 }
14256
14257               if (tls_type & GOT_TLS_GD)
14258                 {
14259                   /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  If
14260                      the symbol is both GD and GDESC, got.offset may
14261                      have been overwritten.  */
14262                   h->got.offset = s->size;
14263                   s->size += 8;
14264                 }
14265
14266               if (tls_type & GOT_TLS_IE)
14267                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
14268                 s->size += 4;
14269             }
14270
14271           dyn = htab->root.dynamic_sections_created;
14272
14273           indx = 0;
14274           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
14275                                                bfd_link_pic (info),
14276                                                h)
14277               && (!bfd_link_pic (info)
14278                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
14279             indx = h->dynindx;
14280
14281           if (tls_type != GOT_NORMAL
14282               && (bfd_link_pic (info) || indx != 0)
14283               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
14284                   || h->root.type != bfd_link_hash_undefweak))
14285             {
14286               if (tls_type & GOT_TLS_IE)
14287                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14288
14289               if (tls_type & GOT_TLS_GD)
14290                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14291
14292               if (tls_type & GOT_TLS_GDESC)
14293                 {
14294                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
14295                   /* GDESC needs a trampoline to jump to.  */
14296                   htab->tls_trampoline = -1;
14297                 }
14298
14299               /* Only GD needs it.  GDESC just emits one relocation per
14300                  2 entries.  */
14301               if ((tls_type & GOT_TLS_GD) && indx != 0)
14302                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14303             }
14304           else if (indx != -1 && !SYMBOL_REFERENCES_LOCAL (info, h))
14305             {
14306               if (htab->root.dynamic_sections_created)
14307                 /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
14308                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14309             }
14310           else if (h->type == STT_GNU_IFUNC
14311                    && eh->plt.noncall_refcount == 0)
14312             /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
14313                they all resolve dynamically instead.  Reserve room for the
14314                GOT entry's R_ARM_IRELATIVE relocation.  */
14315             elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
14316           else if (bfd_link_pic (info)
14317                    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
14318                        || h->root.type != bfd_link_hash_undefweak))
14319             /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
14320             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14321         }
14322     }
14323   else
14324     h->got.offset = (bfd_vma) -1;
14325
14326   /* Allocate stubs for exported Thumb functions on v4t.  */
14327   if (!htab->use_blx && h->dynindx != -1
14328       && h->def_regular
14329       && h->target_internal == ST_BRANCH_TO_THUMB
14330       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14331     {
14332       struct elf_link_hash_entry * th;
14333       struct bfd_link_hash_entry * bh;
14334       struct elf_link_hash_entry * myh;
14335       char name[1024];
14336       asection *s;
14337       bh = NULL;
14338       /* Create a new symbol to regist the real location of the function.  */
14339       s = h->root.u.def.section;
14340       sprintf (name, "__real_%s", h->root.root.string);
14341       _bfd_generic_link_add_one_symbol (info, s->owner,
14342                                         name, BSF_GLOBAL, s,
14343                                         h->root.u.def.value,
14344                                         NULL, TRUE, FALSE, &bh);
14345
14346       myh = (struct elf_link_hash_entry *) bh;
14347       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
14348       myh->forced_local = 1;
14349       myh->target_internal = ST_BRANCH_TO_THUMB;
14350       eh->export_glue = myh;
14351       th = record_arm_to_thumb_glue (info, h);
14352       /* Point the symbol at the stub.  */
14353       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
14354       h->target_internal = ST_BRANCH_TO_ARM;
14355       h->root.u.def.section = th->root.u.def.section;
14356       h->root.u.def.value = th->root.u.def.value & ~1;
14357     }
14358
14359   if (eh->dyn_relocs == NULL)
14360     return TRUE;
14361
14362   /* In the shared -Bsymbolic case, discard space allocated for
14363      dynamic pc-relative relocs against symbols which turn out to be
14364      defined in regular objects.  For the normal shared case, discard
14365      space for pc-relative relocs that have become local due to symbol
14366      visibility changes.  */
14367
14368   if (bfd_link_pic (info) || htab->root.is_relocatable_executable)
14369     {
14370       /* Relocs that use pc_count are PC-relative forms, which will appear
14371          on something like ".long foo - ." or "movw REG, foo - .".  We want
14372          calls to protected symbols to resolve directly to the function
14373          rather than going via the plt.  If people want function pointer
14374          comparisons to work as expected then they should avoid writing
14375          assembly like ".long foo - .".  */
14376       if (SYMBOL_CALLS_LOCAL (info, h))
14377         {
14378           struct elf_dyn_relocs **pp;
14379
14380           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
14381             {
14382               p->count -= p->pc_count;
14383               p->pc_count = 0;
14384               if (p->count == 0)
14385                 *pp = p->next;
14386               else
14387                 pp = &p->next;
14388             }
14389         }
14390
14391       if (htab->vxworks_p)
14392         {
14393           struct elf_dyn_relocs **pp;
14394
14395           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
14396             {
14397               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
14398                 *pp = p->next;
14399               else
14400                 pp = &p->next;
14401             }
14402         }
14403
14404       /* Also discard relocs on undefined weak syms with non-default
14405          visibility.  */
14406       if (eh->dyn_relocs != NULL
14407           && h->root.type == bfd_link_hash_undefweak)
14408         {
14409           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
14410             eh->dyn_relocs = NULL;
14411
14412           /* Make sure undefined weak symbols are output as a dynamic
14413              symbol in PIEs.  */
14414           else if (h->dynindx == -1
14415                    && !h->forced_local)
14416             {
14417               if (! bfd_elf_link_record_dynamic_symbol (info, h))
14418                 return FALSE;
14419             }
14420         }
14421
14422       else if (htab->root.is_relocatable_executable && h->dynindx == -1
14423                && h->root.type == bfd_link_hash_new)
14424         {
14425           /* Output absolute symbols so that we can create relocations
14426              against them.  For normal symbols we output a relocation
14427              against the section that contains them.  */
14428           if (! bfd_elf_link_record_dynamic_symbol (info, h))
14429             return FALSE;
14430         }
14431
14432     }
14433   else
14434     {
14435       /* For the non-shared case, discard space for relocs against
14436          symbols which turn out to need copy relocs or are not
14437          dynamic.  */
14438
14439       if (!h->non_got_ref
14440           && ((h->def_dynamic
14441                && !h->def_regular)
14442               || (htab->root.dynamic_sections_created
14443                   && (h->root.type == bfd_link_hash_undefweak
14444                       || h->root.type == bfd_link_hash_undefined))))
14445         {
14446           /* Make sure this symbol is output as a dynamic symbol.
14447              Undefined weak syms won't yet be marked as dynamic.  */
14448           if (h->dynindx == -1
14449               && !h->forced_local)
14450             {
14451               if (! bfd_elf_link_record_dynamic_symbol (info, h))
14452                 return FALSE;
14453             }
14454
14455           /* If that succeeded, we know we'll be keeping all the
14456              relocs.  */
14457           if (h->dynindx != -1)
14458             goto keep;
14459         }
14460
14461       eh->dyn_relocs = NULL;
14462
14463     keep: ;
14464     }
14465
14466   /* Finally, allocate space.  */
14467   for (p = eh->dyn_relocs; p != NULL; p = p->next)
14468     {
14469       asection *sreloc = elf_section_data (p->sec)->sreloc;
14470       if (h->type == STT_GNU_IFUNC
14471           && eh->plt.noncall_refcount == 0
14472           && SYMBOL_REFERENCES_LOCAL (info, h))
14473         elf32_arm_allocate_irelocs (info, sreloc, p->count);
14474       else
14475         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
14476     }
14477
14478   return TRUE;
14479 }
14480
14481 /* Find any dynamic relocs that apply to read-only sections.  */
14482
14483 static bfd_boolean
14484 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
14485 {
14486   struct elf32_arm_link_hash_entry * eh;
14487   struct elf_dyn_relocs * p;
14488
14489   eh = (struct elf32_arm_link_hash_entry *) h;
14490   for (p = eh->dyn_relocs; p != NULL; p = p->next)
14491     {
14492       asection *s = p->sec;
14493
14494       if (s != NULL && (s->flags & SEC_READONLY) != 0)
14495         {
14496           struct bfd_link_info *info = (struct bfd_link_info *) inf;
14497
14498           info->flags |= DF_TEXTREL;
14499
14500           /* Not an error, just cut short the traversal.  */
14501           return FALSE;
14502         }
14503     }
14504   return TRUE;
14505 }
14506
14507 void
14508 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
14509                                  int byteswap_code)
14510 {
14511   struct elf32_arm_link_hash_table *globals;
14512
14513   globals = elf32_arm_hash_table (info);
14514   if (globals == NULL)
14515     return;
14516
14517   globals->byteswap_code = byteswap_code;
14518 }
14519
14520 /* Set the sizes of the dynamic sections.  */
14521
14522 static bfd_boolean
14523 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
14524                                  struct bfd_link_info * info)
14525 {
14526   bfd * dynobj;
14527   asection * s;
14528   bfd_boolean plt;
14529   bfd_boolean relocs;
14530   bfd *ibfd;
14531   struct elf32_arm_link_hash_table *htab;
14532
14533   htab = elf32_arm_hash_table (info);
14534   if (htab == NULL)
14535     return FALSE;
14536
14537   dynobj = elf_hash_table (info)->dynobj;
14538   BFD_ASSERT (dynobj != NULL);
14539   check_use_blx (htab);
14540
14541   if (elf_hash_table (info)->dynamic_sections_created)
14542     {
14543       /* Set the contents of the .interp section to the interpreter.  */
14544       if (bfd_link_executable (info) && !info->nointerp)
14545         {
14546           s = bfd_get_linker_section (dynobj, ".interp");
14547           BFD_ASSERT (s != NULL);
14548           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
14549           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
14550         }
14551     }
14552
14553   /* Set up .got offsets for local syms, and space for local dynamic
14554      relocs.  */
14555   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14556     {
14557       bfd_signed_vma *local_got;
14558       bfd_signed_vma *end_local_got;
14559       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
14560       char *local_tls_type;
14561       bfd_vma *local_tlsdesc_gotent;
14562       bfd_size_type locsymcount;
14563       Elf_Internal_Shdr *symtab_hdr;
14564       asection *srel;
14565       bfd_boolean is_vxworks = htab->vxworks_p;
14566       unsigned int symndx;
14567
14568       if (! is_arm_elf (ibfd))
14569         continue;
14570
14571       for (s = ibfd->sections; s != NULL; s = s->next)
14572         {
14573           struct elf_dyn_relocs *p;
14574
14575           for (p = (struct elf_dyn_relocs *)
14576                    elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
14577             {
14578               if (!bfd_is_abs_section (p->sec)
14579                   && bfd_is_abs_section (p->sec->output_section))
14580                 {
14581                   /* Input section has been discarded, either because
14582                      it is a copy of a linkonce section or due to
14583                      linker script /DISCARD/, so we'll be discarding
14584                      the relocs too.  */
14585                 }
14586               else if (is_vxworks
14587                        && strcmp (p->sec->output_section->name,
14588                                   ".tls_vars") == 0)
14589                 {
14590                   /* Relocations in vxworks .tls_vars sections are
14591                      handled specially by the loader.  */
14592                 }
14593               else if (p->count != 0)
14594                 {
14595                   srel = elf_section_data (p->sec)->sreloc;
14596                   elf32_arm_allocate_dynrelocs (info, srel, p->count);
14597                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
14598                     info->flags |= DF_TEXTREL;
14599                 }
14600             }
14601         }
14602
14603       local_got = elf_local_got_refcounts (ibfd);
14604       if (!local_got)
14605         continue;
14606
14607       symtab_hdr = & elf_symtab_hdr (ibfd);
14608       locsymcount = symtab_hdr->sh_info;
14609       end_local_got = local_got + locsymcount;
14610       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
14611       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
14612       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
14613       symndx = 0;
14614       s = htab->root.sgot;
14615       srel = htab->root.srelgot;
14616       for (; local_got < end_local_got;
14617            ++local_got, ++local_iplt_ptr, ++local_tls_type,
14618            ++local_tlsdesc_gotent, ++symndx)
14619         {
14620           *local_tlsdesc_gotent = (bfd_vma) -1;
14621           local_iplt = *local_iplt_ptr;
14622           if (local_iplt != NULL)
14623             {
14624               struct elf_dyn_relocs *p;
14625
14626               if (local_iplt->root.refcount > 0)
14627                 {
14628                   elf32_arm_allocate_plt_entry (info, TRUE,
14629                                                 &local_iplt->root,
14630                                                 &local_iplt->arm);
14631                   if (local_iplt->arm.noncall_refcount == 0)
14632                     /* All references to the PLT are calls, so all
14633                        non-call references can resolve directly to the
14634                        run-time target.  This means that the .got entry
14635                        would be the same as the .igot.plt entry, so there's
14636                        no point creating both.  */
14637                     *local_got = 0;
14638                 }
14639               else
14640                 {
14641                   BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
14642                   local_iplt->root.offset = (bfd_vma) -1;
14643                 }
14644
14645               for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
14646                 {
14647                   asection *psrel;
14648
14649                   psrel = elf_section_data (p->sec)->sreloc;
14650                   if (local_iplt->arm.noncall_refcount == 0)
14651                     elf32_arm_allocate_irelocs (info, psrel, p->count);
14652                   else
14653                     elf32_arm_allocate_dynrelocs (info, psrel, p->count);
14654                 }
14655             }
14656           if (*local_got > 0)
14657             {
14658               Elf_Internal_Sym *isym;
14659
14660               *local_got = s->size;
14661               if (*local_tls_type & GOT_TLS_GD)
14662                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
14663                 s->size += 8;
14664               if (*local_tls_type & GOT_TLS_GDESC)
14665                 {
14666                   *local_tlsdesc_gotent = htab->root.sgotplt->size
14667                     - elf32_arm_compute_jump_table_size (htab);
14668                   htab->root.sgotplt->size += 8;
14669                   *local_got = (bfd_vma) -2;
14670                   /* plt.got_offset needs to know there's a TLS_DESC
14671                      reloc in the middle of .got.plt.  */
14672                   htab->num_tls_desc++;
14673                 }
14674               if (*local_tls_type & GOT_TLS_IE)
14675                 s->size += 4;
14676
14677               if (*local_tls_type & GOT_NORMAL)
14678                 {
14679                   /* If the symbol is both GD and GDESC, *local_got
14680                      may have been overwritten.  */
14681                   *local_got = s->size;
14682                   s->size += 4;
14683                 }
14684
14685               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
14686               if (isym == NULL)
14687                 return FALSE;
14688
14689               /* If all references to an STT_GNU_IFUNC PLT are calls,
14690                  then all non-call references, including this GOT entry,
14691                  resolve directly to the run-time target.  */
14692               if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
14693                   && (local_iplt == NULL
14694                       || local_iplt->arm.noncall_refcount == 0))
14695                 elf32_arm_allocate_irelocs (info, srel, 1);
14696               else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC)
14697                 {
14698                   if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC))
14699                       || *local_tls_type & GOT_TLS_GD)
14700                     elf32_arm_allocate_dynrelocs (info, srel, 1);
14701
14702                   if (bfd_link_pic (info) && *local_tls_type & GOT_TLS_GDESC)
14703                     {
14704                       elf32_arm_allocate_dynrelocs (info,
14705                                                     htab->root.srelplt, 1);
14706                       htab->tls_trampoline = -1;
14707                     }
14708                 }
14709             }
14710           else
14711             *local_got = (bfd_vma) -1;
14712         }
14713     }
14714
14715   if (htab->tls_ldm_got.refcount > 0)
14716     {
14717       /* Allocate two GOT entries and one dynamic relocation (if necessary)
14718          for R_ARM_TLS_LDM32 relocations.  */
14719       htab->tls_ldm_got.offset = htab->root.sgot->size;
14720       htab->root.sgot->size += 8;
14721       if (bfd_link_pic (info))
14722         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
14723     }
14724   else
14725     htab->tls_ldm_got.offset = -1;
14726
14727   /* Allocate global sym .plt and .got entries, and space for global
14728      sym dynamic relocs.  */
14729   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
14730
14731   /* Here we rummage through the found bfds to collect glue information.  */
14732   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14733     {
14734       if (! is_arm_elf (ibfd))
14735         continue;
14736
14737       /* Initialise mapping tables for code/data.  */
14738       bfd_elf32_arm_init_maps (ibfd);
14739
14740       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
14741           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
14742           || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
14743         /* xgettext:c-format */
14744         _bfd_error_handler (_("Errors encountered processing file %s"),
14745                             ibfd->filename);
14746     }
14747
14748   /* Allocate space for the glue sections now that we've sized them.  */
14749   bfd_elf32_arm_allocate_interworking_sections (info);
14750
14751   /* For every jump slot reserved in the sgotplt, reloc_count is
14752      incremented.  However, when we reserve space for TLS descriptors,
14753      it's not incremented, so in order to compute the space reserved
14754      for them, it suffices to multiply the reloc count by the jump
14755      slot size.  */
14756   if (htab->root.srelplt)
14757     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
14758
14759   if (htab->tls_trampoline)
14760     {
14761       if (htab->root.splt->size == 0)
14762         htab->root.splt->size += htab->plt_header_size;
14763
14764       htab->tls_trampoline = htab->root.splt->size;
14765       htab->root.splt->size += htab->plt_entry_size;
14766
14767       /* If we're not using lazy TLS relocations, don't generate the
14768          PLT and GOT entries they require.  */
14769       if (!(info->flags & DF_BIND_NOW))
14770         {
14771           htab->dt_tlsdesc_got = htab->root.sgot->size;
14772           htab->root.sgot->size += 4;
14773
14774           htab->dt_tlsdesc_plt = htab->root.splt->size;
14775           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
14776         }
14777     }
14778
14779   /* The check_relocs and adjust_dynamic_symbol entry points have
14780      determined the sizes of the various dynamic sections.  Allocate
14781      memory for them.  */
14782   plt = FALSE;
14783   relocs = FALSE;
14784   for (s = dynobj->sections; s != NULL; s = s->next)
14785     {
14786       const char * name;
14787
14788       if ((s->flags & SEC_LINKER_CREATED) == 0)
14789         continue;
14790
14791       /* It's OK to base decisions on the section name, because none
14792          of the dynobj section names depend upon the input files.  */
14793       name = bfd_get_section_name (dynobj, s);
14794
14795       if (s == htab->root.splt)
14796         {
14797           /* Remember whether there is a PLT.  */
14798           plt = s->size != 0;
14799         }
14800       else if (CONST_STRNEQ (name, ".rel"))
14801         {
14802           if (s->size != 0)
14803             {
14804               /* Remember whether there are any reloc sections other
14805                  than .rel(a).plt and .rela.plt.unloaded.  */
14806               if (s != htab->root.srelplt && s != htab->srelplt2)
14807                 relocs = TRUE;
14808
14809               /* We use the reloc_count field as a counter if we need
14810                  to copy relocs into the output file.  */
14811               s->reloc_count = 0;
14812             }
14813         }
14814       else if (s != htab->root.sgot
14815                && s != htab->root.sgotplt
14816                && s != htab->root.iplt
14817                && s != htab->root.igotplt
14818                && s != htab->sdynbss)
14819         {
14820           /* It's not one of our sections, so don't allocate space.  */
14821           continue;
14822         }
14823
14824       if (s->size == 0)
14825         {
14826           /* If we don't need this section, strip it from the
14827              output file.  This is mostly to handle .rel(a).bss and
14828              .rel(a).plt.  We must create both sections in
14829              create_dynamic_sections, because they must be created
14830              before the linker maps input sections to output
14831              sections.  The linker does that before
14832              adjust_dynamic_symbol is called, and it is that
14833              function which decides whether anything needs to go
14834              into these sections.  */
14835           s->flags |= SEC_EXCLUDE;
14836           continue;
14837         }
14838
14839       if ((s->flags & SEC_HAS_CONTENTS) == 0)
14840         continue;
14841
14842       /* Allocate memory for the section contents.  */
14843       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
14844       if (s->contents == NULL)
14845         return FALSE;
14846     }
14847
14848   if (elf_hash_table (info)->dynamic_sections_created)
14849     {
14850       /* Add some entries to the .dynamic section.  We fill in the
14851          values later, in elf32_arm_finish_dynamic_sections, but we
14852          must add the entries now so that we get the correct size for
14853          the .dynamic section.  The DT_DEBUG entry is filled in by the
14854          dynamic linker and used by the debugger.  */
14855 #define add_dynamic_entry(TAG, VAL) \
14856   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14857
14858      if (bfd_link_executable (info))
14859         {
14860           if (!add_dynamic_entry (DT_DEBUG, 0))
14861             return FALSE;
14862         }
14863
14864       if (plt)
14865         {
14866           if (   !add_dynamic_entry (DT_PLTGOT, 0)
14867               || !add_dynamic_entry (DT_PLTRELSZ, 0)
14868               || !add_dynamic_entry (DT_PLTREL,
14869                                      htab->use_rel ? DT_REL : DT_RELA)
14870               || !add_dynamic_entry (DT_JMPREL, 0))
14871             return FALSE;
14872
14873           if (htab->dt_tlsdesc_plt &&
14874                 (!add_dynamic_entry (DT_TLSDESC_PLT,0)
14875                  || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
14876             return FALSE;
14877         }
14878
14879       if (relocs)
14880         {
14881           if (htab->use_rel)
14882             {
14883               if (!add_dynamic_entry (DT_REL, 0)
14884                   || !add_dynamic_entry (DT_RELSZ, 0)
14885                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
14886                 return FALSE;
14887             }
14888           else
14889             {
14890               if (!add_dynamic_entry (DT_RELA, 0)
14891                   || !add_dynamic_entry (DT_RELASZ, 0)
14892                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
14893                 return FALSE;
14894             }
14895         }
14896
14897       /* If any dynamic relocs apply to a read-only section,
14898          then we need a DT_TEXTREL entry.  */
14899       if ((info->flags & DF_TEXTREL) == 0)
14900         elf_link_hash_traverse (& htab->root, elf32_arm_readonly_dynrelocs,
14901                                 info);
14902
14903       if ((info->flags & DF_TEXTREL) != 0)
14904         {
14905           if (!add_dynamic_entry (DT_TEXTREL, 0))
14906             return FALSE;
14907         }
14908       if (htab->vxworks_p
14909           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
14910         return FALSE;
14911     }
14912 #undef add_dynamic_entry
14913
14914   return TRUE;
14915 }
14916
14917 /* Size sections even though they're not dynamic.  We use it to setup
14918    _TLS_MODULE_BASE_, if needed.  */
14919
14920 static bfd_boolean
14921 elf32_arm_always_size_sections (bfd *output_bfd,
14922                                 struct bfd_link_info *info)
14923 {
14924   asection *tls_sec;
14925
14926   if (bfd_link_relocatable (info))
14927     return TRUE;
14928
14929   tls_sec = elf_hash_table (info)->tls_sec;
14930
14931   if (tls_sec)
14932     {
14933       struct elf_link_hash_entry *tlsbase;
14934
14935       tlsbase = elf_link_hash_lookup
14936         (elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
14937
14938       if (tlsbase)
14939         {
14940           struct bfd_link_hash_entry *bh = NULL;
14941           const struct elf_backend_data *bed
14942             = get_elf_backend_data (output_bfd);
14943
14944           if (!(_bfd_generic_link_add_one_symbol
14945                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
14946                  tls_sec, 0, NULL, FALSE,
14947                  bed->collect, &bh)))
14948             return FALSE;
14949
14950           tlsbase->type = STT_TLS;
14951           tlsbase = (struct elf_link_hash_entry *)bh;
14952           tlsbase->def_regular = 1;
14953           tlsbase->other = STV_HIDDEN;
14954           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
14955         }
14956     }
14957   return TRUE;
14958 }
14959
14960 /* Finish up dynamic symbol handling.  We set the contents of various
14961    dynamic sections here.  */
14962
14963 static bfd_boolean
14964 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
14965                                  struct bfd_link_info * info,
14966                                  struct elf_link_hash_entry * h,
14967                                  Elf_Internal_Sym * sym)
14968 {
14969   struct elf32_arm_link_hash_table *htab;
14970   struct elf32_arm_link_hash_entry *eh;
14971
14972   htab = elf32_arm_hash_table (info);
14973   if (htab == NULL)
14974     return FALSE;
14975
14976   eh = (struct elf32_arm_link_hash_entry *) h;
14977
14978   if (h->plt.offset != (bfd_vma) -1)
14979     {
14980       if (!eh->is_iplt)
14981         {
14982           BFD_ASSERT (h->dynindx != -1);
14983           if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
14984                                               h->dynindx, 0))
14985             return FALSE;
14986         }
14987
14988       if (!h->def_regular)
14989         {
14990           /* Mark the symbol as undefined, rather than as defined in
14991              the .plt section.  */
14992           sym->st_shndx = SHN_UNDEF;
14993           /* If the symbol is weak we need to clear the value.
14994              Otherwise, the PLT entry would provide a definition for
14995              the symbol even if the symbol wasn't defined anywhere,
14996              and so the symbol would never be NULL.  Leave the value if
14997              there were any relocations where pointer equality matters
14998              (this is a clue for the dynamic linker, to make function
14999              pointer comparisons work between an application and shared
15000              library).  */
15001           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
15002             sym->st_value = 0;
15003         }
15004       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
15005         {
15006           /* At least one non-call relocation references this .iplt entry,
15007              so the .iplt entry is the function's canonical address.  */
15008           sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
15009           sym->st_target_internal = ST_BRANCH_TO_ARM;
15010           sym->st_shndx = (_bfd_elf_section_from_bfd_section
15011                            (output_bfd, htab->root.iplt->output_section));
15012           sym->st_value = (h->plt.offset
15013                            + htab->root.iplt->output_section->vma
15014                            + htab->root.iplt->output_offset);
15015         }
15016     }
15017
15018   if (h->needs_copy)
15019     {
15020       asection * s;
15021       Elf_Internal_Rela rel;
15022
15023       /* This symbol needs a copy reloc.  Set it up.  */
15024       BFD_ASSERT (h->dynindx != -1
15025                   && (h->root.type == bfd_link_hash_defined
15026                       || h->root.type == bfd_link_hash_defweak));
15027
15028       s = htab->srelbss;
15029       BFD_ASSERT (s != NULL);
15030
15031       rel.r_addend = 0;
15032       rel.r_offset = (h->root.u.def.value
15033                       + h->root.u.def.section->output_section->vma
15034                       + h->root.u.def.section->output_offset);
15035       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
15036       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
15037     }
15038
15039   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
15040      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
15041      to the ".got" section.  */
15042   if (h == htab->root.hdynamic
15043       || (!htab->vxworks_p && h == htab->root.hgot))
15044     sym->st_shndx = SHN_ABS;
15045
15046   return TRUE;
15047 }
15048
15049 static void
15050 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
15051                     void *contents,
15052                     const unsigned long *template, unsigned count)
15053 {
15054   unsigned ix;
15055
15056   for (ix = 0; ix != count; ix++)
15057     {
15058       unsigned long insn = template[ix];
15059
15060       /* Emit mov pc,rx if bx is not permitted.  */
15061       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
15062         insn = (insn & 0xf000000f) | 0x01a0f000;
15063       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
15064     }
15065 }
15066
15067 /* Install the special first PLT entry for elf32-arm-nacl.  Unlike
15068    other variants, NaCl needs this entry in a static executable's
15069    .iplt too.  When we're handling that case, GOT_DISPLACEMENT is
15070    zero.  For .iplt really only the last bundle is useful, and .iplt
15071    could have a shorter first entry, with each individual PLT entry's
15072    relative branch calculated differently so it targets the last
15073    bundle instead of the instruction before it (labelled .Lplt_tail
15074    above).  But it's simpler to keep the size and layout of PLT0
15075    consistent with the dynamic case, at the cost of some dead code at
15076    the start of .iplt and the one dead store to the stack at the start
15077    of .Lplt_tail.  */
15078 static void
15079 arm_nacl_put_plt0 (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
15080                    asection *plt, bfd_vma got_displacement)
15081 {
15082   unsigned int i;
15083
15084   put_arm_insn (htab, output_bfd,
15085                 elf32_arm_nacl_plt0_entry[0]
15086                 | arm_movw_immediate (got_displacement),
15087                 plt->contents + 0);
15088   put_arm_insn (htab, output_bfd,
15089                 elf32_arm_nacl_plt0_entry[1]
15090                 | arm_movt_immediate (got_displacement),
15091                 plt->contents + 4);
15092
15093   for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
15094     put_arm_insn (htab, output_bfd,
15095                   elf32_arm_nacl_plt0_entry[i],
15096                   plt->contents + (i * 4));
15097 }
15098
15099 /* Finish up the dynamic sections.  */
15100
15101 static bfd_boolean
15102 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
15103 {
15104   bfd * dynobj;
15105   asection * sgot;
15106   asection * sdyn;
15107   struct elf32_arm_link_hash_table *htab;
15108
15109   htab = elf32_arm_hash_table (info);
15110   if (htab == NULL)
15111     return FALSE;
15112
15113   dynobj = elf_hash_table (info)->dynobj;
15114
15115   sgot = htab->root.sgotplt;
15116   /* A broken linker script might have discarded the dynamic sections.
15117      Catch this here so that we do not seg-fault later on.  */
15118   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
15119     return FALSE;
15120   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15121
15122   if (elf_hash_table (info)->dynamic_sections_created)
15123     {
15124       asection *splt;
15125       Elf32_External_Dyn *dyncon, *dynconend;
15126
15127       splt = htab->root.splt;
15128       BFD_ASSERT (splt != NULL && sdyn != NULL);
15129       BFD_ASSERT (htab->symbian_p || sgot != NULL);
15130
15131       dyncon = (Elf32_External_Dyn *) sdyn->contents;
15132       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
15133
15134       for (; dyncon < dynconend; dyncon++)
15135         {
15136           Elf_Internal_Dyn dyn;
15137           const char * name;
15138           asection * s;
15139
15140           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
15141
15142           switch (dyn.d_tag)
15143             {
15144               unsigned int type;
15145
15146             default:
15147               if (htab->vxworks_p
15148                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
15149                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15150               break;
15151
15152             case DT_HASH:
15153               name = ".hash";
15154               goto get_vma_if_bpabi;
15155             case DT_STRTAB:
15156               name = ".dynstr";
15157               goto get_vma_if_bpabi;
15158             case DT_SYMTAB:
15159               name = ".dynsym";
15160               goto get_vma_if_bpabi;
15161             case DT_VERSYM:
15162               name = ".gnu.version";
15163               goto get_vma_if_bpabi;
15164             case DT_VERDEF:
15165               name = ".gnu.version_d";
15166               goto get_vma_if_bpabi;
15167             case DT_VERNEED:
15168               name = ".gnu.version_r";
15169               goto get_vma_if_bpabi;
15170
15171             case DT_PLTGOT:
15172               name = ".got";
15173               goto get_vma;
15174             case DT_JMPREL:
15175               name = RELOC_SECTION (htab, ".plt");
15176             get_vma:
15177               s = bfd_get_section_by_name (output_bfd, name);
15178               if (s == NULL)
15179                 {
15180                   /* PR ld/14397: Issue an error message if a required section is missing.  */
15181                   (*_bfd_error_handler)
15182                     (_("error: required section '%s' not found in the linker script"), name);
15183                   bfd_set_error (bfd_error_invalid_operation);
15184                   return FALSE;
15185                 }
15186               if (!htab->symbian_p)
15187                 dyn.d_un.d_ptr = s->vma;
15188               else
15189                 /* In the BPABI, tags in the PT_DYNAMIC section point
15190                    at the file offset, not the memory address, for the
15191                    convenience of the post linker.  */
15192                 dyn.d_un.d_ptr = s->filepos;
15193               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15194               break;
15195
15196             get_vma_if_bpabi:
15197               if (htab->symbian_p)
15198                 goto get_vma;
15199               break;
15200
15201             case DT_PLTRELSZ:
15202               s = htab->root.srelplt;
15203               BFD_ASSERT (s != NULL);
15204               dyn.d_un.d_val = s->size;
15205               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15206               break;
15207
15208             case DT_RELSZ:
15209             case DT_RELASZ:
15210               if (!htab->symbian_p)
15211                 {
15212                   /* My reading of the SVR4 ABI indicates that the
15213                      procedure linkage table relocs (DT_JMPREL) should be
15214                      included in the overall relocs (DT_REL).  This is
15215                      what Solaris does.  However, UnixWare can not handle
15216                      that case.  Therefore, we override the DT_RELSZ entry
15217                      here to make it not include the JMPREL relocs.  Since
15218                      the linker script arranges for .rel(a).plt to follow all
15219                      other relocation sections, we don't have to worry
15220                      about changing the DT_REL entry.  */
15221                   s = htab->root.srelplt;
15222                   if (s != NULL)
15223                     dyn.d_un.d_val -= s->size;
15224                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15225                   break;
15226                 }
15227               /* Fall through.  */
15228
15229             case DT_REL:
15230             case DT_RELA:
15231               /* In the BPABI, the DT_REL tag must point at the file
15232                  offset, not the VMA, of the first relocation
15233                  section.  So, we use code similar to that in
15234                  elflink.c, but do not check for SHF_ALLOC on the
15235                  relcoation section, since relocations sections are
15236                  never allocated under the BPABI.  The comments above
15237                  about Unixware notwithstanding, we include all of the
15238                  relocations here.  */
15239               if (htab->symbian_p)
15240                 {
15241                   unsigned int i;
15242                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
15243                           ? SHT_REL : SHT_RELA);
15244                   dyn.d_un.d_val = 0;
15245                   for (i = 1; i < elf_numsections (output_bfd); i++)
15246                     {
15247                       Elf_Internal_Shdr *hdr
15248                         = elf_elfsections (output_bfd)[i];
15249                       if (hdr->sh_type == type)
15250                         {
15251                           if (dyn.d_tag == DT_RELSZ
15252                               || dyn.d_tag == DT_RELASZ)
15253                             dyn.d_un.d_val += hdr->sh_size;
15254                           else if ((ufile_ptr) hdr->sh_offset
15255                                    <= dyn.d_un.d_val - 1)
15256                             dyn.d_un.d_val = hdr->sh_offset;
15257                         }
15258                     }
15259                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15260                 }
15261               break;
15262
15263             case DT_TLSDESC_PLT:
15264               s = htab->root.splt;
15265               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
15266                                 + htab->dt_tlsdesc_plt);
15267               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15268               break;
15269
15270             case DT_TLSDESC_GOT:
15271               s = htab->root.sgot;
15272               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
15273                                 + htab->dt_tlsdesc_got);
15274               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15275               break;
15276
15277               /* Set the bottom bit of DT_INIT/FINI if the
15278                  corresponding function is Thumb.  */
15279             case DT_INIT:
15280               name = info->init_function;
15281               goto get_sym;
15282             case DT_FINI:
15283               name = info->fini_function;
15284             get_sym:
15285               /* If it wasn't set by elf_bfd_final_link
15286                  then there is nothing to adjust.  */
15287               if (dyn.d_un.d_val != 0)
15288                 {
15289                   struct elf_link_hash_entry * eh;
15290
15291                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
15292                                              FALSE, FALSE, TRUE);
15293                   if (eh != NULL && eh->target_internal == ST_BRANCH_TO_THUMB)
15294                     {
15295                       dyn.d_un.d_val |= 1;
15296                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
15297                     }
15298                 }
15299               break;
15300             }
15301         }
15302
15303       /* Fill in the first entry in the procedure linkage table.  */
15304       if (splt->size > 0 && htab->plt_header_size)
15305         {
15306           const bfd_vma *plt0_entry;
15307           bfd_vma got_address, plt_address, got_displacement;
15308
15309           /* Calculate the addresses of the GOT and PLT.  */
15310           got_address = sgot->output_section->vma + sgot->output_offset;
15311           plt_address = splt->output_section->vma + splt->output_offset;
15312
15313           if (htab->vxworks_p)
15314             {
15315               /* The VxWorks GOT is relocated by the dynamic linker.
15316                  Therefore, we must emit relocations rather than simply
15317                  computing the values now.  */
15318               Elf_Internal_Rela rel;
15319
15320               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
15321               put_arm_insn (htab, output_bfd, plt0_entry[0],
15322                             splt->contents + 0);
15323               put_arm_insn (htab, output_bfd, plt0_entry[1],
15324                             splt->contents + 4);
15325               put_arm_insn (htab, output_bfd, plt0_entry[2],
15326                             splt->contents + 8);
15327               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
15328
15329               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
15330               rel.r_offset = plt_address + 12;
15331               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
15332               rel.r_addend = 0;
15333               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
15334                                      htab->srelplt2->contents);
15335             }
15336           else if (htab->nacl_p)
15337             arm_nacl_put_plt0 (htab, output_bfd, splt,
15338                                got_address + 8 - (plt_address + 16));
15339           else if (using_thumb_only (htab))
15340             {
15341               got_displacement = got_address - (plt_address + 12);
15342
15343               plt0_entry = elf32_thumb2_plt0_entry;
15344               put_arm_insn (htab, output_bfd, plt0_entry[0],
15345                             splt->contents + 0);
15346               put_arm_insn (htab, output_bfd, plt0_entry[1],
15347                             splt->contents + 4);
15348               put_arm_insn (htab, output_bfd, plt0_entry[2],
15349                             splt->contents + 8);
15350
15351               bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
15352             }
15353           else
15354             {
15355               got_displacement = got_address - (plt_address + 16);
15356
15357               plt0_entry = elf32_arm_plt0_entry;
15358               put_arm_insn (htab, output_bfd, plt0_entry[0],
15359                             splt->contents + 0);
15360               put_arm_insn (htab, output_bfd, plt0_entry[1],
15361                             splt->contents + 4);
15362               put_arm_insn (htab, output_bfd, plt0_entry[2],
15363                             splt->contents + 8);
15364               put_arm_insn (htab, output_bfd, plt0_entry[3],
15365                             splt->contents + 12);
15366
15367 #ifdef FOUR_WORD_PLT
15368               /* The displacement value goes in the otherwise-unused
15369                  last word of the second entry.  */
15370               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
15371 #else
15372               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
15373 #endif
15374             }
15375         }
15376
15377       /* UnixWare sets the entsize of .plt to 4, although that doesn't
15378          really seem like the right value.  */
15379       if (splt->output_section->owner == output_bfd)
15380         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
15381
15382       if (htab->dt_tlsdesc_plt)
15383         {
15384           bfd_vma got_address
15385             = sgot->output_section->vma + sgot->output_offset;
15386           bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
15387                                     + htab->root.sgot->output_offset);
15388           bfd_vma plt_address
15389             = splt->output_section->vma + splt->output_offset;
15390
15391           arm_put_trampoline (htab, output_bfd,
15392                               splt->contents + htab->dt_tlsdesc_plt,
15393                               dl_tlsdesc_lazy_trampoline, 6);
15394
15395           bfd_put_32 (output_bfd,
15396                       gotplt_address + htab->dt_tlsdesc_got
15397                       - (plt_address + htab->dt_tlsdesc_plt)
15398                       - dl_tlsdesc_lazy_trampoline[6],
15399                       splt->contents + htab->dt_tlsdesc_plt + 24);
15400           bfd_put_32 (output_bfd,
15401                       got_address - (plt_address + htab->dt_tlsdesc_plt)
15402                       - dl_tlsdesc_lazy_trampoline[7],
15403                       splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
15404         }
15405
15406       if (htab->tls_trampoline)
15407         {
15408           arm_put_trampoline (htab, output_bfd,
15409                               splt->contents + htab->tls_trampoline,
15410                               tls_trampoline, 3);
15411 #ifdef FOUR_WORD_PLT
15412           bfd_put_32 (output_bfd, 0x00000000,
15413                       splt->contents + htab->tls_trampoline + 12);
15414 #endif
15415         }
15416
15417       if (htab->vxworks_p
15418           && !bfd_link_pic (info)
15419           && htab->root.splt->size > 0)
15420         {
15421           /* Correct the .rel(a).plt.unloaded relocations.  They will have
15422              incorrect symbol indexes.  */
15423           int num_plts;
15424           unsigned char *p;
15425
15426           num_plts = ((htab->root.splt->size - htab->plt_header_size)
15427                       / htab->plt_entry_size);
15428           p = htab->srelplt2->contents + RELOC_SIZE (htab);
15429
15430           for (; num_plts; num_plts--)
15431             {
15432               Elf_Internal_Rela rel;
15433
15434               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
15435               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
15436               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
15437               p += RELOC_SIZE (htab);
15438
15439               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
15440               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
15441               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
15442               p += RELOC_SIZE (htab);
15443             }
15444         }
15445     }
15446
15447   if (htab->nacl_p && htab->root.iplt != NULL && htab->root.iplt->size > 0)
15448     /* NaCl uses a special first entry in .iplt too.  */
15449     arm_nacl_put_plt0 (htab, output_bfd, htab->root.iplt, 0);
15450
15451   /* Fill in the first three entries in the global offset table.  */
15452   if (sgot)
15453     {
15454       if (sgot->size > 0)
15455         {
15456           if (sdyn == NULL)
15457             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
15458           else
15459             bfd_put_32 (output_bfd,
15460                         sdyn->output_section->vma + sdyn->output_offset,
15461                         sgot->contents);
15462           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
15463           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
15464         }
15465
15466       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
15467     }
15468
15469   return TRUE;
15470 }
15471
15472 static void
15473 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
15474 {
15475   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
15476   struct elf32_arm_link_hash_table *globals;
15477   struct elf_segment_map *m;
15478
15479   i_ehdrp = elf_elfheader (abfd);
15480
15481   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
15482     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
15483   else
15484     _bfd_elf_post_process_headers (abfd, link_info);
15485   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
15486
15487   if (link_info)
15488     {
15489       globals = elf32_arm_hash_table (link_info);
15490       if (globals != NULL && globals->byteswap_code)
15491         i_ehdrp->e_flags |= EF_ARM_BE8;
15492     }
15493
15494   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
15495       && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
15496     {
15497       int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
15498       if (abi == AEABI_VFP_args_vfp)
15499         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
15500       else
15501         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
15502     }
15503
15504   /* Scan segment to set p_flags attribute if it contains only sections with
15505      SHF_ARM_NOREAD flag.  */
15506   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
15507     {
15508       unsigned int j;
15509
15510       if (m->count == 0)
15511         continue;
15512       for (j = 0; j < m->count; j++)
15513         {
15514           if (!(elf_section_flags (m->sections[j]) & SHF_ARM_NOREAD))
15515             break;
15516         }
15517       if (j == m->count)
15518         {
15519           m->p_flags = PF_X;
15520           m->p_flags_valid = 1;
15521         }
15522     }
15523 }
15524
15525 static enum elf_reloc_type_class
15526 elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
15527                             const asection *rel_sec ATTRIBUTE_UNUSED,
15528                             const Elf_Internal_Rela *rela)
15529 {
15530   switch ((int) ELF32_R_TYPE (rela->r_info))
15531     {
15532     case R_ARM_RELATIVE:
15533       return reloc_class_relative;
15534     case R_ARM_JUMP_SLOT:
15535       return reloc_class_plt;
15536     case R_ARM_COPY:
15537       return reloc_class_copy;
15538     default:
15539       return reloc_class_normal;
15540     }
15541 }
15542
15543 static void
15544 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
15545 {
15546   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
15547 }
15548
15549 /* Return TRUE if this is an unwinding table entry.  */
15550
15551 static bfd_boolean
15552 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
15553 {
15554   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
15555           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
15556 }
15557
15558
15559 /* Set the type and flags for an ARM section.  We do this by
15560    the section name, which is a hack, but ought to work.  */
15561
15562 static bfd_boolean
15563 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
15564 {
15565   const char * name;
15566
15567   name = bfd_get_section_name (abfd, sec);
15568
15569   if (is_arm_elf_unwind_section_name (abfd, name))
15570     {
15571       hdr->sh_type = SHT_ARM_EXIDX;
15572       hdr->sh_flags |= SHF_LINK_ORDER;
15573     }
15574
15575   if (sec->flags & SEC_ELF_NOREAD)
15576     hdr->sh_flags |= SHF_ARM_NOREAD;
15577
15578   return TRUE;
15579 }
15580
15581 /* Handle an ARM specific section when reading an object file.  This is
15582    called when bfd_section_from_shdr finds a section with an unknown
15583    type.  */
15584
15585 static bfd_boolean
15586 elf32_arm_section_from_shdr (bfd *abfd,
15587                              Elf_Internal_Shdr * hdr,
15588                              const char *name,
15589                              int shindex)
15590 {
15591   /* There ought to be a place to keep ELF backend specific flags, but
15592      at the moment there isn't one.  We just keep track of the
15593      sections by their name, instead.  Fortunately, the ABI gives
15594      names for all the ARM specific sections, so we will probably get
15595      away with this.  */
15596   switch (hdr->sh_type)
15597     {
15598     case SHT_ARM_EXIDX:
15599     case SHT_ARM_PREEMPTMAP:
15600     case SHT_ARM_ATTRIBUTES:
15601       break;
15602
15603     default:
15604       return FALSE;
15605     }
15606
15607   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
15608     return FALSE;
15609
15610   return TRUE;
15611 }
15612
15613 static _arm_elf_section_data *
15614 get_arm_elf_section_data (asection * sec)
15615 {
15616   if (sec && sec->owner && is_arm_elf (sec->owner))
15617     return elf32_arm_section_data (sec);
15618   else
15619     return NULL;
15620 }
15621
15622 typedef struct
15623 {
15624   void *flaginfo;
15625   struct bfd_link_info *info;
15626   asection *sec;
15627   int sec_shndx;
15628   int (*func) (void *, const char *, Elf_Internal_Sym *,
15629                asection *, struct elf_link_hash_entry *);
15630 } output_arch_syminfo;
15631
15632 enum map_symbol_type
15633 {
15634   ARM_MAP_ARM,
15635   ARM_MAP_THUMB,
15636   ARM_MAP_DATA
15637 };
15638
15639
15640 /* Output a single mapping symbol.  */
15641
15642 static bfd_boolean
15643 elf32_arm_output_map_sym (output_arch_syminfo *osi,
15644                           enum map_symbol_type type,
15645                           bfd_vma offset)
15646 {
15647   static const char *names[3] = {"$a", "$t", "$d"};
15648   Elf_Internal_Sym sym;
15649
15650   sym.st_value = osi->sec->output_section->vma
15651                  + osi->sec->output_offset
15652                  + offset;
15653   sym.st_size = 0;
15654   sym.st_other = 0;
15655   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
15656   sym.st_shndx = osi->sec_shndx;
15657   sym.st_target_internal = 0;
15658   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
15659   return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
15660 }
15661
15662 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
15663    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
15664
15665 static bfd_boolean
15666 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
15667                             bfd_boolean is_iplt_entry_p,
15668                             union gotplt_union *root_plt,
15669                             struct arm_plt_info *arm_plt)
15670 {
15671   struct elf32_arm_link_hash_table *htab;
15672   bfd_vma addr, plt_header_size;
15673
15674   if (root_plt->offset == (bfd_vma) -1)
15675     return TRUE;
15676
15677   htab = elf32_arm_hash_table (osi->info);
15678   if (htab == NULL)
15679     return FALSE;
15680
15681   if (is_iplt_entry_p)
15682     {
15683       osi->sec = htab->root.iplt;
15684       plt_header_size = 0;
15685     }
15686   else
15687     {
15688       osi->sec = htab->root.splt;
15689       plt_header_size = htab->plt_header_size;
15690     }
15691   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
15692                     (osi->info->output_bfd, osi->sec->output_section));
15693
15694   addr = root_plt->offset & -2;
15695   if (htab->symbian_p)
15696     {
15697       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15698         return FALSE;
15699       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
15700         return FALSE;
15701     }
15702   else if (htab->vxworks_p)
15703     {
15704       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15705         return FALSE;
15706       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
15707         return FALSE;
15708       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
15709         return FALSE;
15710       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
15711         return FALSE;
15712     }
15713   else if (htab->nacl_p)
15714     {
15715       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15716         return FALSE;
15717     }
15718   else if (using_thumb_only (htab))
15719     {
15720       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
15721         return FALSE;
15722     }
15723   else
15724     {
15725       bfd_boolean thumb_stub_p;
15726
15727       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
15728       if (thumb_stub_p)
15729         {
15730           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
15731             return FALSE;
15732         }
15733 #ifdef FOUR_WORD_PLT
15734       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15735         return FALSE;
15736       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
15737         return FALSE;
15738 #else
15739       /* A three-word PLT with no Thumb thunk contains only Arm code,
15740          so only need to output a mapping symbol for the first PLT entry and
15741          entries with thumb thunks.  */
15742       if (thumb_stub_p || addr == plt_header_size)
15743         {
15744           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
15745             return FALSE;
15746         }
15747 #endif
15748     }
15749
15750   return TRUE;
15751 }
15752
15753 /* Output mapping symbols for PLT entries associated with H.  */
15754
15755 static bfd_boolean
15756 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
15757 {
15758   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
15759   struct elf32_arm_link_hash_entry *eh;
15760
15761   if (h->root.type == bfd_link_hash_indirect)
15762     return TRUE;
15763
15764   if (h->root.type == bfd_link_hash_warning)
15765     /* When warning symbols are created, they **replace** the "real"
15766        entry in the hash table, thus we never get to see the real
15767        symbol in a hash traversal.  So look at it now.  */
15768     h = (struct elf_link_hash_entry *) h->root.u.i.link;
15769
15770   eh = (struct elf32_arm_link_hash_entry *) h;
15771   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
15772                                      &h->plt, &eh->plt);
15773 }
15774
15775 /* Output a single local symbol for a generated stub.  */
15776
15777 static bfd_boolean
15778 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
15779                            bfd_vma offset, bfd_vma size)
15780 {
15781   Elf_Internal_Sym sym;
15782
15783   sym.st_value = osi->sec->output_section->vma
15784                  + osi->sec->output_offset
15785                  + offset;
15786   sym.st_size = size;
15787   sym.st_other = 0;
15788   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
15789   sym.st_shndx = osi->sec_shndx;
15790   sym.st_target_internal = 0;
15791   return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
15792 }
15793
15794 static bfd_boolean
15795 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
15796                   void * in_arg)
15797 {
15798   struct elf32_arm_stub_hash_entry *stub_entry;
15799   asection *stub_sec;
15800   bfd_vma addr;
15801   char *stub_name;
15802   output_arch_syminfo *osi;
15803   const insn_sequence *template_sequence;
15804   enum stub_insn_type prev_type;
15805   int size;
15806   int i;
15807   enum map_symbol_type sym_type;
15808
15809   /* Massage our args to the form they really have.  */
15810   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
15811   osi = (output_arch_syminfo *) in_arg;
15812
15813   stub_sec = stub_entry->stub_sec;
15814
15815   /* Ensure this stub is attached to the current section being
15816      processed.  */
15817   if (stub_sec != osi->sec)
15818     return TRUE;
15819
15820   addr = (bfd_vma) stub_entry->stub_offset;
15821   stub_name = stub_entry->output_name;
15822
15823   template_sequence = stub_entry->stub_template;
15824   switch (template_sequence[0].type)
15825     {
15826     case ARM_TYPE:
15827       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, stub_entry->stub_size))
15828         return FALSE;
15829       break;
15830     case THUMB16_TYPE:
15831     case THUMB32_TYPE:
15832       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
15833                                       stub_entry->stub_size))
15834         return FALSE;
15835       break;
15836     default:
15837       BFD_FAIL ();
15838       return 0;
15839     }
15840
15841   prev_type = DATA_TYPE;
15842   size = 0;
15843   for (i = 0; i < stub_entry->stub_template_size; i++)
15844     {
15845       switch (template_sequence[i].type)
15846         {
15847         case ARM_TYPE:
15848           sym_type = ARM_MAP_ARM;
15849           break;
15850
15851         case THUMB16_TYPE:
15852         case THUMB32_TYPE:
15853           sym_type = ARM_MAP_THUMB;
15854           break;
15855
15856         case DATA_TYPE:
15857           sym_type = ARM_MAP_DATA;
15858           break;
15859
15860         default:
15861           BFD_FAIL ();
15862           return FALSE;
15863         }
15864
15865       if (template_sequence[i].type != prev_type)
15866         {
15867           prev_type = template_sequence[i].type;
15868           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
15869             return FALSE;
15870         }
15871
15872       switch (template_sequence[i].type)
15873         {
15874         case ARM_TYPE:
15875         case THUMB32_TYPE:
15876           size += 4;
15877           break;
15878
15879         case THUMB16_TYPE:
15880           size += 2;
15881           break;
15882
15883         case DATA_TYPE:
15884           size += 4;
15885           break;
15886
15887         default:
15888           BFD_FAIL ();
15889           return FALSE;
15890         }
15891     }
15892
15893   return TRUE;
15894 }
15895
15896 /* Output mapping symbols for linker generated sections,
15897    and for those data-only sections that do not have a
15898    $d.  */
15899
15900 static bfd_boolean
15901 elf32_arm_output_arch_local_syms (bfd *output_bfd,
15902                                   struct bfd_link_info *info,
15903                                   void *flaginfo,
15904                                   int (*func) (void *, const char *,
15905                                                Elf_Internal_Sym *,
15906                                                asection *,
15907                                                struct elf_link_hash_entry *))
15908 {
15909   output_arch_syminfo osi;
15910   struct elf32_arm_link_hash_table *htab;
15911   bfd_vma offset;
15912   bfd_size_type size;
15913   bfd *input_bfd;
15914
15915   htab = elf32_arm_hash_table (info);
15916   if (htab == NULL)
15917     return FALSE;
15918
15919   check_use_blx (htab);
15920
15921   osi.flaginfo = flaginfo;
15922   osi.info = info;
15923   osi.func = func;
15924
15925   /* Add a $d mapping symbol to data-only sections that
15926      don't have any mapping symbol.  This may result in (harmless) redundant
15927      mapping symbols.  */
15928   for (input_bfd = info->input_bfds;
15929        input_bfd != NULL;
15930        input_bfd = input_bfd->link.next)
15931     {
15932       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
15933         for (osi.sec = input_bfd->sections;
15934              osi.sec != NULL;
15935              osi.sec = osi.sec->next)
15936           {
15937             if (osi.sec->output_section != NULL
15938                 && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
15939                     != 0)
15940                 && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
15941                    == SEC_HAS_CONTENTS
15942                 && get_arm_elf_section_data (osi.sec) != NULL
15943                 && get_arm_elf_section_data (osi.sec)->mapcount == 0
15944                 && osi.sec->size > 0
15945                 && (osi.sec->flags & SEC_EXCLUDE) == 0)
15946               {
15947                 osi.sec_shndx = _bfd_elf_section_from_bfd_section
15948                   (output_bfd, osi.sec->output_section);
15949                 if (osi.sec_shndx != (int)SHN_BAD)
15950                   elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
15951               }
15952           }
15953     }
15954
15955   /* ARM->Thumb glue.  */
15956   if (htab->arm_glue_size > 0)
15957     {
15958       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15959                                         ARM2THUMB_GLUE_SECTION_NAME);
15960
15961       osi.sec_shndx = _bfd_elf_section_from_bfd_section
15962           (output_bfd, osi.sec->output_section);
15963       if (bfd_link_pic (info) || htab->root.is_relocatable_executable
15964           || htab->pic_veneer)
15965         size = ARM2THUMB_PIC_GLUE_SIZE;
15966       else if (htab->use_blx)
15967         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
15968       else
15969         size = ARM2THUMB_STATIC_GLUE_SIZE;
15970
15971       for (offset = 0; offset < htab->arm_glue_size; offset += size)
15972         {
15973           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
15974           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
15975         }
15976     }
15977
15978   /* Thumb->ARM glue.  */
15979   if (htab->thumb_glue_size > 0)
15980     {
15981       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15982                                         THUMB2ARM_GLUE_SECTION_NAME);
15983
15984       osi.sec_shndx = _bfd_elf_section_from_bfd_section
15985           (output_bfd, osi.sec->output_section);
15986       size = THUMB2ARM_GLUE_SIZE;
15987
15988       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
15989         {
15990           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
15991           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
15992         }
15993     }
15994
15995   /* ARMv4 BX veneers.  */
15996   if (htab->bx_glue_size > 0)
15997     {
15998       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
15999                                         ARM_BX_GLUE_SECTION_NAME);
16000
16001       osi.sec_shndx = _bfd_elf_section_from_bfd_section
16002           (output_bfd, osi.sec->output_section);
16003
16004       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
16005     }
16006
16007   /* Long calls stubs.  */
16008   if (htab->stub_bfd && htab->stub_bfd->sections)
16009     {
16010       asection* stub_sec;
16011
16012       for (stub_sec = htab->stub_bfd->sections;
16013            stub_sec != NULL;
16014            stub_sec = stub_sec->next)
16015         {
16016           /* Ignore non-stub sections.  */
16017           if (!strstr (stub_sec->name, STUB_SUFFIX))
16018             continue;
16019
16020           osi.sec = stub_sec;
16021
16022           osi.sec_shndx = _bfd_elf_section_from_bfd_section
16023             (output_bfd, osi.sec->output_section);
16024
16025           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
16026         }
16027     }
16028
16029   /* Finally, output mapping symbols for the PLT.  */
16030   if (htab->root.splt && htab->root.splt->size > 0)
16031     {
16032       osi.sec = htab->root.splt;
16033       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
16034                        (output_bfd, osi.sec->output_section));
16035
16036       /* Output mapping symbols for the plt header.  SymbianOS does not have a
16037          plt header.  */
16038       if (htab->vxworks_p)
16039         {
16040           /* VxWorks shared libraries have no PLT header.  */
16041           if (!bfd_link_pic (info))
16042             {
16043               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16044                 return FALSE;
16045               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
16046                 return FALSE;
16047             }
16048         }
16049       else if (htab->nacl_p)
16050         {
16051           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16052             return FALSE;
16053         }
16054       else if (using_thumb_only (htab))
16055         {
16056           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
16057             return FALSE;
16058           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
16059             return FALSE;
16060           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
16061             return FALSE;
16062         }
16063       else if (!htab->symbian_p)
16064         {
16065           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16066             return FALSE;
16067 #ifndef FOUR_WORD_PLT
16068           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
16069             return FALSE;
16070 #endif
16071         }
16072     }
16073   if (htab->nacl_p && htab->root.iplt && htab->root.iplt->size > 0)
16074     {
16075       /* NaCl uses a special first entry in .iplt too.  */
16076       osi.sec = htab->root.iplt;
16077       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
16078                        (output_bfd, osi.sec->output_section));
16079       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
16080         return FALSE;
16081     }
16082   if ((htab->root.splt && htab->root.splt->size > 0)
16083       || (htab->root.iplt && htab->root.iplt->size > 0))
16084     {
16085       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
16086       for (input_bfd = info->input_bfds;
16087            input_bfd != NULL;
16088            input_bfd = input_bfd->link.next)
16089         {
16090           struct arm_local_iplt_info **local_iplt;
16091           unsigned int i, num_syms;
16092
16093           local_iplt = elf32_arm_local_iplt (input_bfd);
16094           if (local_iplt != NULL)
16095             {
16096               num_syms = elf_symtab_hdr (input_bfd).sh_info;
16097               for (i = 0; i < num_syms; i++)
16098                 if (local_iplt[i] != NULL
16099                     && !elf32_arm_output_plt_map_1 (&osi, TRUE,
16100                                                     &local_iplt[i]->root,
16101                                                     &local_iplt[i]->arm))
16102                   return FALSE;
16103             }
16104         }
16105     }
16106   if (htab->dt_tlsdesc_plt != 0)
16107     {
16108       /* Mapping symbols for the lazy tls trampoline.  */
16109       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
16110         return FALSE;
16111
16112       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
16113                                      htab->dt_tlsdesc_plt + 24))
16114         return FALSE;
16115     }
16116   if (htab->tls_trampoline != 0)
16117     {
16118       /* Mapping symbols for the tls trampoline.  */
16119       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
16120         return FALSE;
16121 #ifdef FOUR_WORD_PLT
16122       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
16123                                      htab->tls_trampoline + 12))
16124         return FALSE;
16125 #endif
16126     }
16127
16128   return TRUE;
16129 }
16130
16131 /* Allocate target specific section data.  */
16132
16133 static bfd_boolean
16134 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
16135 {
16136   if (!sec->used_by_bfd)
16137     {
16138       _arm_elf_section_data *sdata;
16139       bfd_size_type amt = sizeof (*sdata);
16140
16141       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
16142       if (sdata == NULL)
16143         return FALSE;
16144       sec->used_by_bfd = sdata;
16145     }
16146
16147   return _bfd_elf_new_section_hook (abfd, sec);
16148 }
16149
16150
16151 /* Used to order a list of mapping symbols by address.  */
16152
16153 static int
16154 elf32_arm_compare_mapping (const void * a, const void * b)
16155 {
16156   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
16157   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
16158
16159   if (amap->vma > bmap->vma)
16160     return 1;
16161   else if (amap->vma < bmap->vma)
16162     return -1;
16163   else if (amap->type > bmap->type)
16164     /* Ensure results do not depend on the host qsort for objects with
16165        multiple mapping symbols at the same address by sorting on type
16166        after vma.  */
16167     return 1;
16168   else if (amap->type < bmap->type)
16169     return -1;
16170   else
16171     return 0;
16172 }
16173
16174 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
16175
16176 static unsigned long
16177 offset_prel31 (unsigned long addr, bfd_vma offset)
16178 {
16179   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
16180 }
16181
16182 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
16183    relocations.  */
16184
16185 static void
16186 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
16187 {
16188   unsigned long first_word = bfd_get_32 (output_bfd, from);
16189   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
16190
16191   /* High bit of first word is supposed to be zero.  */
16192   if ((first_word & 0x80000000ul) == 0)
16193     first_word = offset_prel31 (first_word, offset);
16194
16195   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
16196      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
16197   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
16198     second_word = offset_prel31 (second_word, offset);
16199
16200   bfd_put_32 (output_bfd, first_word, to);
16201   bfd_put_32 (output_bfd, second_word, to + 4);
16202 }
16203
16204 /* Data for make_branch_to_a8_stub().  */
16205
16206 struct a8_branch_to_stub_data
16207 {
16208   asection *writing_section;
16209   bfd_byte *contents;
16210 };
16211
16212
16213 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
16214    places for a particular section.  */
16215
16216 static bfd_boolean
16217 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
16218                        void *in_arg)
16219 {
16220   struct elf32_arm_stub_hash_entry *stub_entry;
16221   struct a8_branch_to_stub_data *data;
16222   bfd_byte *contents;
16223   unsigned long branch_insn;
16224   bfd_vma veneered_insn_loc, veneer_entry_loc;
16225   bfd_signed_vma branch_offset;
16226   bfd *abfd;
16227   unsigned int target;
16228
16229   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
16230   data = (struct a8_branch_to_stub_data *) in_arg;
16231
16232   if (stub_entry->target_section != data->writing_section
16233       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
16234     return TRUE;
16235
16236   contents = data->contents;
16237
16238   veneered_insn_loc = stub_entry->target_section->output_section->vma
16239                       + stub_entry->target_section->output_offset
16240                       + stub_entry->target_value;
16241
16242   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
16243                      + stub_entry->stub_sec->output_offset
16244                      + stub_entry->stub_offset;
16245
16246   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
16247     veneered_insn_loc &= ~3u;
16248
16249   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
16250
16251   abfd = stub_entry->target_section->owner;
16252   target = stub_entry->target_value;
16253
16254   /* We attempt to avoid this condition by setting stubs_always_after_branch
16255      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
16256      This check is just to be on the safe side...  */
16257   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
16258     {
16259       (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub is "
16260                                "allocated in unsafe location"), abfd);
16261       return FALSE;
16262     }
16263
16264   switch (stub_entry->stub_type)
16265     {
16266     case arm_stub_a8_veneer_b:
16267     case arm_stub_a8_veneer_b_cond:
16268       branch_insn = 0xf0009000;
16269       goto jump24;
16270
16271     case arm_stub_a8_veneer_blx:
16272       branch_insn = 0xf000e800;
16273       goto jump24;
16274
16275     case arm_stub_a8_veneer_bl:
16276       {
16277         unsigned int i1, j1, i2, j2, s;
16278
16279         branch_insn = 0xf000d000;
16280
16281       jump24:
16282         if (branch_offset < -16777216 || branch_offset > 16777214)
16283           {
16284             /* There's not much we can do apart from complain if this
16285                happens.  */
16286             (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub out "
16287                                      "of range (input file too large)"), abfd);
16288             return FALSE;
16289           }
16290
16291         /* i1 = not(j1 eor s), so:
16292            not i1 = j1 eor s
16293            j1 = (not i1) eor s.  */
16294
16295         branch_insn |= (branch_offset >> 1) & 0x7ff;
16296         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
16297         i2 = (branch_offset >> 22) & 1;
16298         i1 = (branch_offset >> 23) & 1;
16299         s = (branch_offset >> 24) & 1;
16300         j1 = (!i1) ^ s;
16301         j2 = (!i2) ^ s;
16302         branch_insn |= j2 << 11;
16303         branch_insn |= j1 << 13;
16304         branch_insn |= s << 26;
16305       }
16306       break;
16307
16308     default:
16309       BFD_FAIL ();
16310       return FALSE;
16311     }
16312
16313   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[target]);
16314   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[target + 2]);
16315
16316   return TRUE;
16317 }
16318
16319 /* Beginning of stm32l4xx work-around.  */
16320
16321 /* Functions encoding instructions necessary for the emission of the
16322    fix-stm32l4xx-629360.
16323    Encoding is extracted from the
16324    ARM (C) Architecture Reference Manual
16325    ARMv7-A and ARMv7-R edition
16326    ARM DDI 0406C.b (ID072512).  */
16327
16328 static inline bfd_vma
16329 create_instruction_branch_absolute (int branch_offset)
16330 {
16331   /* A8.8.18 B (A8-334)
16332      B target_address (Encoding T4).  */
16333   /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
16334   /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
16335   /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
16336
16337   int s = ((branch_offset & 0x1000000) >> 24);
16338   int j1 = s ^ !((branch_offset & 0x800000) >> 23);
16339   int j2 = s ^ !((branch_offset & 0x400000) >> 22);
16340
16341   if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
16342     BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
16343
16344   bfd_vma patched_inst = 0xf0009000
16345     | s << 26 /* S.  */
16346     | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
16347     | j1 << 13 /* J1.  */
16348     | j2 << 11 /* J2.  */
16349     | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
16350
16351   return patched_inst;
16352 }
16353
16354 static inline bfd_vma
16355 create_instruction_ldmia (int base_reg, int wback, int reg_mask)
16356 {
16357   /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
16358      LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
16359   bfd_vma patched_inst = 0xe8900000
16360     | (/*W=*/wback << 21)
16361     | (base_reg << 16)
16362     | (reg_mask & 0x0000ffff);
16363
16364   return patched_inst;
16365 }
16366
16367 static inline bfd_vma
16368 create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
16369 {
16370   /* A8.8.60 LDMDB/LDMEA (A8-402)
16371      LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
16372   bfd_vma patched_inst = 0xe9100000
16373     | (/*W=*/wback << 21)
16374     | (base_reg << 16)
16375     | (reg_mask & 0x0000ffff);
16376
16377   return patched_inst;
16378 }
16379
16380 static inline bfd_vma
16381 create_instruction_mov (int target_reg, int source_reg)
16382 {
16383   /* A8.8.103 MOV (register) (A8-486)
16384      MOV Rd, Rm (Encoding T1).  */
16385   bfd_vma patched_inst = 0x4600
16386     | (target_reg & 0x7)
16387     | ((target_reg & 0x8) >> 3) << 7
16388     | (source_reg << 3);
16389
16390   return patched_inst;
16391 }
16392
16393 static inline bfd_vma
16394 create_instruction_sub (int target_reg, int source_reg, int value)
16395 {
16396   /* A8.8.221 SUB (immediate) (A8-708)
16397      SUB Rd, Rn, #value (Encoding T3).  */
16398   bfd_vma patched_inst = 0xf1a00000
16399     | (target_reg << 8)
16400     | (source_reg << 16)
16401     | (/*S=*/0 << 20)
16402     | ((value & 0x800) >> 11) << 26
16403     | ((value & 0x700) >>  8) << 12
16404     | (value & 0x0ff);
16405
16406   return patched_inst;
16407 }
16408
16409 static inline bfd_vma
16410 create_instruction_vldmia (int base_reg, int wback, int num_regs,
16411                            int first_reg)
16412 {
16413   /* A8.8.332 VLDM (A8-922)
16414      VLMD{MODE} Rn{!}, {list} (Encoding T2).  */
16415   bfd_vma patched_inst = 0xec900a00
16416     | (/*W=*/wback << 21)
16417     | (base_reg << 16)
16418     | (num_regs & 0x000000ff)
16419     | (((unsigned)first_reg>>1) & 0x0000000f) << 12
16420     | (first_reg & 0x00000001) << 22;
16421
16422   return patched_inst;
16423 }
16424
16425 static inline bfd_vma
16426 create_instruction_vldmdb (int base_reg, int num_regs, int first_reg)
16427 {
16428   /* A8.8.332 VLDM (A8-922)
16429      VLMD{MODE} Rn!, {} (Encoding T2).  */
16430   bfd_vma patched_inst = 0xed300a00
16431     | (base_reg << 16)
16432     | (num_regs & 0x000000ff)
16433     | (((unsigned)first_reg>>1) & 0x0000000f) << 12
16434     | (first_reg & 0x00000001) << 22;
16435
16436   return patched_inst;
16437 }
16438
16439 static inline bfd_vma
16440 create_instruction_udf_w (int value)
16441 {
16442   /* A8.8.247 UDF (A8-758)
16443      Undefined (Encoding T2).  */
16444   bfd_vma patched_inst = 0xf7f0a000
16445     | (value & 0x00000fff)
16446     | (value & 0x000f0000) << 16;
16447
16448   return patched_inst;
16449 }
16450
16451 static inline bfd_vma
16452 create_instruction_udf (int value)
16453 {
16454   /* A8.8.247 UDF (A8-758)
16455      Undefined (Encoding T1).  */
16456   bfd_vma patched_inst = 0xde00
16457     | (value & 0xff);
16458
16459   return patched_inst;
16460 }
16461
16462 /* Functions writing an instruction in memory, returning the next
16463    memory position to write to.  */
16464
16465 static inline bfd_byte *
16466 push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
16467                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
16468 {
16469   put_thumb2_insn (htab, output_bfd, insn, pt);
16470   return pt + 4;
16471 }
16472
16473 static inline bfd_byte *
16474 push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
16475                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
16476 {
16477   put_thumb_insn (htab, output_bfd, insn, pt);
16478   return pt + 2;
16479 }
16480
16481 /* Function filling up a region in memory with T1 and T2 UDFs taking
16482    care of alignment.  */
16483
16484 static bfd_byte *
16485 stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
16486                          bfd *                   output_bfd,
16487                          const bfd_byte * const  base_stub_contents,
16488                          bfd_byte * const        from_stub_contents,
16489                          const bfd_byte * const  end_stub_contents)
16490 {
16491   bfd_byte *current_stub_contents = from_stub_contents;
16492
16493   /* Fill the remaining of the stub with deterministic contents : UDF
16494      instructions.
16495      Check if realignment is needed on modulo 4 frontier using T1, to
16496      further use T2.  */
16497   if ((current_stub_contents < end_stub_contents)
16498       && !((current_stub_contents - base_stub_contents) % 2)
16499       && ((current_stub_contents - base_stub_contents) % 4))
16500     current_stub_contents =
16501       push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16502                           create_instruction_udf (0));
16503
16504   for (; current_stub_contents < end_stub_contents;)
16505     current_stub_contents =
16506       push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16507                           create_instruction_udf_w (0));
16508
16509   return current_stub_contents;
16510 }
16511
16512 /* Functions writing the stream of instructions equivalent to the
16513    derived sequence for ldmia, ldmdb, vldm respectively.  */
16514
16515 static void
16516 stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
16517                                        bfd * output_bfd,
16518                                        const insn32 initial_insn,
16519                                        const bfd_byte *const initial_insn_addr,
16520                                        bfd_byte *const base_stub_contents)
16521 {
16522   int wback = (initial_insn & 0x00200000) >> 21;
16523   int ri, rn = (initial_insn & 0x000F0000) >> 16;
16524   int insn_all_registers = initial_insn & 0x0000ffff;
16525   int insn_low_registers, insn_high_registers;
16526   int usable_register_mask;
16527   int nb_registers = popcount (insn_all_registers);
16528   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
16529   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
16530   bfd_byte *current_stub_contents = base_stub_contents;
16531
16532   BFD_ASSERT (is_thumb2_ldmia (initial_insn));
16533
16534   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16535      smaller than 8 registers load sequences that do not cause the
16536      hardware issue.  */
16537   if (nb_registers <= 8)
16538     {
16539       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
16540       current_stub_contents =
16541         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16542                             initial_insn);
16543
16544       /* B initial_insn_addr+4.  */
16545       if (!restore_pc)
16546         current_stub_contents =
16547           push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16548                               create_instruction_branch_absolute
16549                               (initial_insn_addr - current_stub_contents));
16550                                
16551
16552       /* Fill the remaining of the stub with deterministic contents.  */
16553       current_stub_contents =
16554         stm32l4xx_fill_stub_udf (htab, output_bfd,
16555                                  base_stub_contents, current_stub_contents,
16556                                  base_stub_contents +
16557                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16558
16559       return;
16560     }
16561
16562   /* - reg_list[13] == 0.  */
16563   BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
16564
16565   /* - reg_list[14] & reg_list[15] != 1.  */
16566   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
16567
16568   /* - if (wback==1) reg_list[rn] == 0.  */
16569   BFD_ASSERT (!wback || !restore_rn);
16570
16571   /* - nb_registers > 8.  */
16572   BFD_ASSERT (popcount (insn_all_registers) > 8);
16573
16574   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
16575
16576   /* In the following algorithm, we split this wide LDM using 2 LDM insns:
16577     - One with the 7 lowest registers (register mask 0x007F)
16578       This LDM will finally contain between 2 and 7 registers
16579     - One with the 7 highest registers (register mask 0xDF80)
16580       This ldm will finally contain between 2 and 7 registers.  */
16581   insn_low_registers = insn_all_registers & 0x007F;
16582   insn_high_registers = insn_all_registers & 0xDF80;
16583
16584   /* A spare register may be needed during this veneer to temporarily
16585      handle the base register.  This register will be restored with the
16586      last LDM operation.
16587      The usable register may be any general purpose register (that
16588      excludes PC, SP, LR : register mask is 0x1FFF).  */
16589   usable_register_mask = 0x1FFF;
16590
16591   /* Generate the stub function.  */
16592   if (wback)
16593     {
16594       /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
16595       current_stub_contents =
16596         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16597                             create_instruction_ldmia
16598                             (rn, /*wback=*/1, insn_low_registers));
16599
16600       /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
16601       current_stub_contents =
16602         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16603                             create_instruction_ldmia
16604                             (rn, /*wback=*/1, insn_high_registers));
16605       if (!restore_pc)
16606         {
16607           /* B initial_insn_addr+4.  */
16608           current_stub_contents =
16609             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16610                                 create_instruction_branch_absolute
16611                                 (initial_insn_addr - current_stub_contents));
16612        }
16613     }
16614   else /* if (!wback).  */
16615     {
16616       ri = rn;
16617
16618       /* If Rn is not part of the high-register-list, move it there.  */
16619       if (!(insn_high_registers & (1 << rn)))
16620         {
16621           /* Choose a Ri in the high-register-list that will be restored.  */
16622           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16623
16624           /* MOV Ri, Rn.  */
16625           current_stub_contents =
16626             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16627                                 create_instruction_mov (ri, rn));
16628         }
16629
16630       /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
16631       current_stub_contents =
16632         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16633                             create_instruction_ldmia
16634                             (ri, /*wback=*/1, insn_low_registers));
16635
16636       /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
16637       current_stub_contents =
16638         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16639                             create_instruction_ldmia
16640                             (ri, /*wback=*/0, insn_high_registers));
16641
16642       if (!restore_pc)
16643         {
16644           /* B initial_insn_addr+4.  */
16645           current_stub_contents =
16646             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16647                                 create_instruction_branch_absolute
16648                                 (initial_insn_addr - current_stub_contents));
16649         }
16650     }
16651
16652   /* Fill the remaining of the stub with deterministic contents.  */
16653   current_stub_contents =
16654     stm32l4xx_fill_stub_udf (htab, output_bfd,
16655                              base_stub_contents, current_stub_contents,
16656                              base_stub_contents +
16657                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16658 }
16659
16660 static void
16661 stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
16662                                        bfd * output_bfd,
16663                                        const insn32 initial_insn,
16664                                        const bfd_byte *const initial_insn_addr,
16665                                        bfd_byte *const base_stub_contents)
16666 {
16667   int wback = (initial_insn & 0x00200000) >> 21;
16668   int ri, rn = (initial_insn & 0x000f0000) >> 16;
16669   int insn_all_registers = initial_insn & 0x0000ffff;
16670   int insn_low_registers, insn_high_registers;
16671   int usable_register_mask;
16672   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
16673   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
16674   int nb_registers = popcount (insn_all_registers);
16675   bfd_byte *current_stub_contents = base_stub_contents;
16676
16677   BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
16678
16679   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16680      smaller than 8 registers load sequences that do not cause the
16681      hardware issue.  */
16682   if (nb_registers <= 8)
16683     {
16684       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
16685       current_stub_contents =
16686         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16687                             initial_insn);
16688
16689       /* B initial_insn_addr+4.  */
16690       current_stub_contents =
16691         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16692                             create_instruction_branch_absolute
16693                             (initial_insn_addr - current_stub_contents));
16694
16695       /* Fill the remaining of the stub with deterministic contents.  */
16696       current_stub_contents =
16697         stm32l4xx_fill_stub_udf (htab, output_bfd,
16698                                  base_stub_contents, current_stub_contents,
16699                                  base_stub_contents +
16700                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16701
16702       return;
16703     }
16704
16705   /* - reg_list[13] == 0.  */
16706   BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
16707
16708   /* - reg_list[14] & reg_list[15] != 1.  */
16709   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
16710
16711   /* - if (wback==1) reg_list[rn] == 0.  */
16712   BFD_ASSERT (!wback || !restore_rn);
16713
16714   /* - nb_registers > 8.  */
16715   BFD_ASSERT (popcount (insn_all_registers) > 8);
16716
16717   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
16718
16719   /* In the following algorithm, we split this wide LDM using 2 LDM insn:
16720     - One with the 7 lowest registers (register mask 0x007F)
16721       This LDM will finally contain between 2 and 7 registers
16722     - One with the 7 highest registers (register mask 0xDF80)
16723       This ldm will finally contain between 2 and 7 registers.  */
16724   insn_low_registers = insn_all_registers & 0x007F;
16725   insn_high_registers = insn_all_registers & 0xDF80;
16726
16727   /* A spare register may be needed during this veneer to temporarily
16728      handle the base register.  This register will be restored with
16729      the last LDM operation.
16730      The usable register may be any general purpose register (that excludes
16731      PC, SP, LR : register mask is 0x1FFF).  */
16732   usable_register_mask = 0x1FFF;
16733
16734   /* Generate the stub function.  */
16735   if (!wback && !restore_pc && !restore_rn)
16736     {
16737       /* Choose a Ri in the low-register-list that will be restored.  */
16738       ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
16739
16740       /* MOV Ri, Rn.  */
16741       current_stub_contents =
16742         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16743                             create_instruction_mov (ri, rn));
16744
16745       /* LDMDB Ri!, {R-high-register-list}.  */
16746       current_stub_contents =
16747         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16748                             create_instruction_ldmdb
16749                             (ri, /*wback=*/1, insn_high_registers));
16750
16751       /* LDMDB Ri, {R-low-register-list}.  */
16752       current_stub_contents =
16753         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16754                             create_instruction_ldmdb
16755                             (ri, /*wback=*/0, insn_low_registers));
16756
16757       /* B initial_insn_addr+4.  */
16758       current_stub_contents =
16759         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16760                             create_instruction_branch_absolute
16761                             (initial_insn_addr - current_stub_contents));
16762     }
16763   else if (wback && !restore_pc && !restore_rn)
16764     {
16765       /* LDMDB Rn!, {R-high-register-list}.  */
16766       current_stub_contents =
16767         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16768                             create_instruction_ldmdb
16769                             (rn, /*wback=*/1, insn_high_registers));
16770
16771       /* LDMDB Rn!, {R-low-register-list}.  */
16772       current_stub_contents =
16773         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16774                             create_instruction_ldmdb
16775                             (rn, /*wback=*/1, insn_low_registers));
16776
16777       /* B initial_insn_addr+4.  */
16778       current_stub_contents =
16779         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16780                             create_instruction_branch_absolute
16781                             (initial_insn_addr - current_stub_contents));
16782     }
16783   else if (!wback && restore_pc && !restore_rn)
16784     {
16785       /* Choose a Ri in the high-register-list that will be restored.  */
16786       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16787
16788       /* SUB Ri, Rn, #(4*nb_registers).  */
16789       current_stub_contents =
16790         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16791                             create_instruction_sub (ri, rn, (4 * nb_registers)));
16792
16793       /* LDMIA Ri!, {R-low-register-list}.  */
16794       current_stub_contents =
16795         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16796                             create_instruction_ldmia
16797                             (ri, /*wback=*/1, insn_low_registers));
16798
16799       /* LDMIA Ri, {R-high-register-list}.  */
16800       current_stub_contents =
16801         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16802                             create_instruction_ldmia
16803                             (ri, /*wback=*/0, insn_high_registers));
16804     }
16805   else if (wback && restore_pc && !restore_rn)
16806     {
16807       /* Choose a Ri in the high-register-list that will be restored.  */
16808       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16809
16810       /* SUB Rn, Rn, #(4*nb_registers)  */
16811       current_stub_contents =
16812         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16813                             create_instruction_sub (rn, rn, (4 * nb_registers)));
16814
16815       /* MOV Ri, Rn.  */
16816       current_stub_contents =
16817         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16818                             create_instruction_mov (ri, rn));
16819
16820       /* LDMIA Ri!, {R-low-register-list}.  */
16821       current_stub_contents =
16822         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16823                             create_instruction_ldmia
16824                             (ri, /*wback=*/1, insn_low_registers));
16825
16826       /* LDMIA Ri, {R-high-register-list}.  */
16827       current_stub_contents =
16828         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16829                             create_instruction_ldmia
16830                             (ri, /*wback=*/0, insn_high_registers));
16831     }
16832   else if (!wback && !restore_pc && restore_rn)
16833     {
16834       ri = rn;
16835       if (!(insn_low_registers & (1 << rn)))
16836         {
16837           /* Choose a Ri in the low-register-list that will be restored.  */
16838           ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
16839
16840           /* MOV Ri, Rn.  */
16841           current_stub_contents =
16842             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
16843                                 create_instruction_mov (ri, rn));
16844         }
16845
16846       /* LDMDB Ri!, {R-high-register-list}.  */
16847       current_stub_contents =
16848         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16849                             create_instruction_ldmdb
16850                             (ri, /*wback=*/1, insn_high_registers));
16851
16852       /* LDMDB Ri, {R-low-register-list}.  */
16853       current_stub_contents =
16854         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16855                             create_instruction_ldmdb
16856                             (ri, /*wback=*/0, insn_low_registers));
16857
16858       /* B initial_insn_addr+4.  */
16859       current_stub_contents =
16860         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16861                             create_instruction_branch_absolute
16862                             (initial_insn_addr - current_stub_contents));
16863     }
16864   else if (!wback && restore_pc && restore_rn)
16865     {
16866       ri = rn;
16867       if (!(insn_high_registers & (1 << rn)))
16868         {
16869           /* Choose a Ri in the high-register-list that will be restored.  */
16870           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
16871         }
16872
16873       /* SUB Ri, Rn, #(4*nb_registers).  */
16874       current_stub_contents =
16875         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16876                             create_instruction_sub (ri, rn, (4 * nb_registers)));
16877
16878       /* LDMIA Ri!, {R-low-register-list}.  */
16879       current_stub_contents =
16880         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16881                             create_instruction_ldmia
16882                             (ri, /*wback=*/1, insn_low_registers));
16883
16884       /* LDMIA Ri, {R-high-register-list}.  */
16885       current_stub_contents =
16886         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16887                             create_instruction_ldmia
16888                             (ri, /*wback=*/0, insn_high_registers));
16889     }
16890   else if (wback && restore_rn)
16891     {
16892       /* The assembler should not have accepted to encode this.  */
16893       BFD_ASSERT (0 && "Cannot patch an instruction that has an "
16894         "undefined behavior.\n");
16895     }
16896
16897   /* Fill the remaining of the stub with deterministic contents.  */
16898   current_stub_contents =
16899     stm32l4xx_fill_stub_udf (htab, output_bfd,
16900                              base_stub_contents, current_stub_contents,
16901                              base_stub_contents +
16902                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
16903
16904 }
16905
16906 static void
16907 stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
16908                                       bfd * output_bfd,
16909                                       const insn32 initial_insn,
16910                                       const bfd_byte *const initial_insn_addr,
16911                                       bfd_byte *const base_stub_contents)
16912 {
16913   int num_regs = ((unsigned int)initial_insn << 24) >> 24;
16914   bfd_byte *current_stub_contents = base_stub_contents;
16915
16916   BFD_ASSERT (is_thumb2_vldm (initial_insn));
16917
16918   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
16919      smaller than 8 registers load sequences that do not cause the
16920      hardware issue.  */
16921   if (num_regs <= 8)
16922     {
16923       /* Untouched instruction.  */
16924       current_stub_contents =
16925         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16926                             initial_insn);
16927
16928       /* B initial_insn_addr+4.  */
16929       current_stub_contents =
16930         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16931                             create_instruction_branch_absolute
16932                             (initial_insn_addr - current_stub_contents));
16933     }
16934   else
16935     {
16936       bfd_boolean is_ia_nobang = /* (IA without !).  */
16937         (((initial_insn << 7) >> 28) & 0xd) == 0x4;
16938       bfd_boolean is_ia_bang = /* (IA with !) - includes VPOP.  */
16939         (((initial_insn << 7) >> 28) & 0xd) == 0x5;
16940       bfd_boolean is_db_bang = /* (DB with !).  */
16941         (((initial_insn << 7) >> 28) & 0xd) == 0x9;
16942       int base_reg = ((unsigned int)initial_insn << 12) >> 28;
16943       /* d = UInt (Vd:D);.  */
16944       int first_reg = ((((unsigned int)initial_insn << 16) >> 28) << 1)
16945         | (((unsigned int)initial_insn << 9) >> 31);
16946
16947       /* Compute the number of 8-register chunks needed to split.  */
16948       int chunks = (num_regs%8) ? (num_regs/8 + 1) : (num_regs/8);
16949       int chunk;
16950
16951       /* The test coverage has been done assuming the following
16952          hypothesis that exactly one of the previous is_ predicates is
16953          true.  */
16954       BFD_ASSERT ((is_ia_nobang ^ is_ia_bang ^ is_db_bang) &&
16955                   !(is_ia_nobang & is_ia_bang & is_db_bang));
16956
16957       /* We treat the cutting of the register in one pass for all
16958          cases, then we emit the adjustments:
16959
16960          vldm rx, {...}
16961          -> vldm rx!, {8_words_or_less} for each needed 8_word
16962          -> sub rx, rx, #size (list)
16963
16964          vldm rx!, {...}
16965          -> vldm rx!, {8_words_or_less} for each needed 8_word
16966          This also handles vpop instruction (when rx is sp)
16967
16968          vldmd rx!, {...}
16969          -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
16970       for (chunk = 0; chunk<chunks; ++chunk)
16971         {
16972           if (is_ia_nobang || is_ia_bang)
16973             {
16974               current_stub_contents =
16975                 push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16976                                     create_instruction_vldmia
16977                                     (base_reg,
16978                                      /*wback= .  */1,
16979                                      chunks - (chunk + 1) ?
16980                                      8 : num_regs - chunk * 8,
16981                                      first_reg + chunk * 8));
16982             }
16983           else if (is_db_bang)
16984             {
16985               current_stub_contents =
16986                 push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
16987                                     create_instruction_vldmdb
16988                                     (base_reg,
16989                                      chunks - (chunk + 1) ?
16990                                      8 : num_regs - chunk * 8,
16991                                      first_reg + chunk * 8));
16992             }
16993         }
16994
16995       /* Only this case requires the base register compensation
16996          subtract.  */
16997       if (is_ia_nobang)
16998         {
16999           current_stub_contents =
17000             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
17001                                 create_instruction_sub
17002                                 (base_reg, base_reg, 4*num_regs));
17003         }
17004
17005       /* B initial_insn_addr+4.  */
17006       current_stub_contents =
17007         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
17008                             create_instruction_branch_absolute
17009                             (initial_insn_addr - current_stub_contents));
17010     }
17011
17012   /* Fill the remaining of the stub with deterministic contents.  */
17013   current_stub_contents =
17014     stm32l4xx_fill_stub_udf (htab, output_bfd,
17015                              base_stub_contents, current_stub_contents,
17016                              base_stub_contents +
17017                              STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
17018 }
17019
17020 static void
17021 stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
17022                                  bfd * output_bfd,
17023                                  const insn32 wrong_insn,
17024                                  const bfd_byte *const wrong_insn_addr,
17025                                  bfd_byte *const stub_contents)
17026 {
17027   if (is_thumb2_ldmia (wrong_insn))
17028     stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
17029                                            wrong_insn, wrong_insn_addr,
17030                                            stub_contents);
17031   else if (is_thumb2_ldmdb (wrong_insn))
17032     stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
17033                                            wrong_insn, wrong_insn_addr,
17034                                            stub_contents);
17035   else if (is_thumb2_vldm (wrong_insn))
17036     stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
17037                                           wrong_insn, wrong_insn_addr,
17038                                           stub_contents);
17039 }
17040
17041 /* End of stm32l4xx work-around.  */
17042
17043
17044 static void
17045 elf32_arm_add_relocation (bfd *output_bfd, struct bfd_link_info *info,
17046                           asection *output_sec, Elf_Internal_Rela *rel)
17047 {
17048   BFD_ASSERT (output_sec && rel);
17049   struct bfd_elf_section_reloc_data *output_reldata;
17050   struct elf32_arm_link_hash_table *htab;
17051   struct bfd_elf_section_data *oesd = elf_section_data (output_sec);
17052   Elf_Internal_Shdr *rel_hdr;
17053
17054
17055   if (oesd->rel.hdr)
17056     {
17057       rel_hdr = oesd->rel.hdr;
17058       output_reldata = &(oesd->rel);
17059     }
17060   else if (oesd->rela.hdr)
17061     {
17062       rel_hdr = oesd->rela.hdr;
17063       output_reldata = &(oesd->rela);
17064     }
17065   else
17066     {
17067       abort ();
17068     }
17069
17070   bfd_byte *erel = rel_hdr->contents;
17071   erel += output_reldata->count * rel_hdr->sh_entsize;
17072   htab = elf32_arm_hash_table (info);
17073   SWAP_RELOC_OUT (htab) (output_bfd, rel, erel);
17074   output_reldata->count++;
17075 }
17076
17077 /* Do code byteswapping.  Return FALSE afterwards so that the section is
17078    written out as normal.  */
17079
17080 static bfd_boolean
17081 elf32_arm_write_section (bfd *output_bfd,
17082                          struct bfd_link_info *link_info,
17083                          asection *sec,
17084                          bfd_byte *contents)
17085 {
17086   unsigned int mapcount, errcount;
17087   _arm_elf_section_data *arm_data;
17088   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
17089   elf32_arm_section_map *map;
17090   elf32_vfp11_erratum_list *errnode;
17091   elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
17092   bfd_vma ptr;
17093   bfd_vma end;
17094   bfd_vma offset = sec->output_section->vma + sec->output_offset;
17095   bfd_byte tmp;
17096   unsigned int i;
17097
17098   if (globals == NULL)
17099     return FALSE;
17100
17101   /* If this section has not been allocated an _arm_elf_section_data
17102      structure then we cannot record anything.  */
17103   arm_data = get_arm_elf_section_data (sec);
17104   if (arm_data == NULL)
17105     return FALSE;
17106
17107   mapcount = arm_data->mapcount;
17108   map = arm_data->map;
17109   errcount = arm_data->erratumcount;
17110
17111   if (errcount != 0)
17112     {
17113       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
17114
17115       for (errnode = arm_data->erratumlist; errnode != 0;
17116            errnode = errnode->next)
17117         {
17118           bfd_vma target = errnode->vma - offset;
17119
17120           switch (errnode->type)
17121             {
17122             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
17123               {
17124                 bfd_vma branch_to_veneer;
17125                 /* Original condition code of instruction, plus bit mask for
17126                    ARM B instruction.  */
17127                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
17128                                   | 0x0a000000;
17129
17130                 /* The instruction is before the label.  */
17131                 target -= 4;
17132
17133                 /* Above offset included in -4 below.  */
17134                 branch_to_veneer = errnode->u.b.veneer->vma
17135                                    - errnode->vma - 4;
17136
17137                 if ((signed) branch_to_veneer < -(1 << 25)
17138                     || (signed) branch_to_veneer >= (1 << 25))
17139                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
17140                                            "range"), output_bfd);
17141
17142                 insn |= (branch_to_veneer >> 2) & 0xffffff;
17143                 contents[endianflip ^ target] = insn & 0xff;
17144                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
17145                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
17146                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
17147               }
17148               break;
17149
17150             case VFP11_ERRATUM_ARM_VENEER:
17151               {
17152                 bfd_vma branch_from_veneer;
17153                 unsigned int insn;
17154
17155                 /* Take size of veneer into account.  */
17156                 branch_from_veneer = errnode->u.v.branch->vma
17157                                      - errnode->vma - 12;
17158
17159                 if ((signed) branch_from_veneer < -(1 << 25)
17160                     || (signed) branch_from_veneer >= (1 << 25))
17161                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
17162                                            "range"), output_bfd);
17163
17164                 /* Original instruction.  */
17165                 insn = errnode->u.v.branch->u.b.vfp_insn;
17166                 contents[endianflip ^ target] = insn & 0xff;
17167                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
17168                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
17169                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
17170
17171                 /* Branch back to insn after original insn.  */
17172                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
17173                 contents[endianflip ^ (target + 4)] = insn & 0xff;
17174                 contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
17175                 contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
17176                 contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
17177               }
17178               break;
17179
17180             default:
17181               abort ();
17182             }
17183         }
17184     }
17185
17186   if (arm_data->stm32l4xx_erratumcount != 0)
17187     {
17188       for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
17189            stm32l4xx_errnode != 0;
17190            stm32l4xx_errnode = stm32l4xx_errnode->next)
17191         {
17192           bfd_vma target = stm32l4xx_errnode->vma - offset;
17193
17194           switch (stm32l4xx_errnode->type)
17195             {
17196             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
17197               {
17198                 unsigned int insn;
17199                 bfd_vma branch_to_veneer =
17200                   stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
17201
17202                 if ((signed) branch_to_veneer < -(1 << 24)
17203                     || (signed) branch_to_veneer >= (1 << 24))
17204                   {
17205                     bfd_vma out_of_range =
17206                       ((signed) branch_to_veneer < -(1 << 24)) ?
17207                       - branch_to_veneer - (1 << 24) :
17208                       ((signed) branch_to_veneer >= (1 << 24)) ?
17209                       branch_to_veneer - (1 << 24) : 0;
17210
17211                     (*_bfd_error_handler)
17212                       (_("%B(%#x): error: Cannot create STM32L4XX veneer. "
17213                          "Jump out of range by %ld bytes. "
17214                          "Cannot encode branch instruction. "),
17215                        output_bfd,
17216                        (long) (stm32l4xx_errnode->vma - 4),
17217                        out_of_range);
17218                     continue;
17219                   }
17220
17221                 insn = create_instruction_branch_absolute
17222                   (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
17223
17224                 /* The instruction is before the label.  */
17225                 target -= 4;
17226
17227                 put_thumb2_insn (globals, output_bfd,
17228                                  (bfd_vma) insn, contents + target);
17229               }
17230               break;
17231
17232             case STM32L4XX_ERRATUM_VENEER:
17233               {
17234                 bfd_byte * veneer;
17235                 bfd_byte * veneer_r;
17236                 unsigned int insn;
17237
17238                 veneer = contents + target;
17239                 veneer_r = veneer
17240                   + stm32l4xx_errnode->u.b.veneer->vma
17241                   - stm32l4xx_errnode->vma - 4;
17242
17243                 if ((signed) (veneer_r - veneer -
17244                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
17245                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
17246                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
17247                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
17248                     || (signed) (veneer_r - veneer) >= (1 << 24))
17249                   {
17250                     (*_bfd_error_handler) (_("%B: error: Cannot create STM32L4XX "
17251                                              "veneer."), output_bfd);
17252                      continue;
17253                   }
17254
17255                 /* Original instruction.  */
17256                 insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
17257
17258                 stm32l4xx_create_replacing_stub
17259                   (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
17260               }
17261               break;
17262
17263             default:
17264               abort ();
17265             }
17266         }
17267     }
17268
17269   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
17270     {
17271       arm_unwind_table_edit *edit_node
17272         = arm_data->u.exidx.unwind_edit_list;
17273       /* Now, sec->size is the size of the section we will write.  The original
17274          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
17275          markers) was sec->rawsize.  (This isn't the case if we perform no
17276          edits, then rawsize will be zero and we should use size).  */
17277       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
17278       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
17279       unsigned int in_index, out_index;
17280       bfd_vma add_to_offsets = 0;
17281
17282       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
17283         {
17284           if (edit_node)
17285             {
17286               unsigned int edit_index = edit_node->index;
17287
17288               if (in_index < edit_index && in_index * 8 < input_size)
17289                 {
17290                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
17291                                     contents + in_index * 8, add_to_offsets);
17292                   out_index++;
17293                   in_index++;
17294                 }
17295               else if (in_index == edit_index
17296                        || (in_index * 8 >= input_size
17297                            && edit_index == UINT_MAX))
17298                 {
17299                   switch (edit_node->type)
17300                     {
17301                     case DELETE_EXIDX_ENTRY:
17302                       in_index++;
17303                       add_to_offsets += 8;
17304                       break;
17305
17306                     case INSERT_EXIDX_CANTUNWIND_AT_END:
17307                       {
17308                         asection *text_sec = edit_node->linked_section;
17309                         bfd_vma text_offset = text_sec->output_section->vma
17310                                               + text_sec->output_offset
17311                                               + text_sec->size;
17312                         bfd_vma exidx_offset = offset + out_index * 8;
17313                         unsigned long prel31_offset;
17314
17315                         /* Note: this is meant to be equivalent to an
17316                            R_ARM_PREL31 relocation.  These synthetic
17317                            EXIDX_CANTUNWIND markers are not relocated by the
17318                            usual BFD method.  */
17319                         prel31_offset = (text_offset - exidx_offset)
17320                                         & 0x7ffffffful;
17321                         if (bfd_link_relocatable (link_info))
17322                           {
17323                             /* Here relocation for new EXIDX_CANTUNWIND is
17324                                created, so there is no need to
17325                                adjust offset by hand.  */
17326                             prel31_offset = text_sec->output_offset
17327                                             + text_sec->size;
17328
17329                             /* New relocation entity.  */
17330                             asection *text_out = text_sec->output_section;
17331                             Elf_Internal_Rela rel;
17332                             rel.r_addend = 0;
17333                             rel.r_offset = exidx_offset;
17334                             rel.r_info = ELF32_R_INFO (text_out->target_index,
17335                                                        R_ARM_PREL31);
17336
17337                             elf32_arm_add_relocation (output_bfd, link_info,
17338                                                       sec->output_section,
17339                                                       &rel);
17340                           }
17341
17342                         /* First address we can't unwind.  */
17343                         bfd_put_32 (output_bfd, prel31_offset,
17344                                     &edited_contents[out_index * 8]);
17345
17346                         /* Code for EXIDX_CANTUNWIND.  */
17347                         bfd_put_32 (output_bfd, 0x1,
17348                                     &edited_contents[out_index * 8 + 4]);
17349
17350                         out_index++;
17351                         add_to_offsets -= 8;
17352                       }
17353                       break;
17354                     }
17355
17356                   edit_node = edit_node->next;
17357                 }
17358             }
17359           else
17360             {
17361               /* No more edits, copy remaining entries verbatim.  */
17362               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
17363                                 contents + in_index * 8, add_to_offsets);
17364               out_index++;
17365               in_index++;
17366             }
17367         }
17368
17369       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
17370         bfd_set_section_contents (output_bfd, sec->output_section,
17371                                   edited_contents,
17372                                   (file_ptr) sec->output_offset, sec->size);
17373
17374       return TRUE;
17375     }
17376
17377   /* Fix code to point to Cortex-A8 erratum stubs.  */
17378   if (globals->fix_cortex_a8)
17379     {
17380       struct a8_branch_to_stub_data data;
17381
17382       data.writing_section = sec;
17383       data.contents = contents;
17384
17385       bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
17386                          & data);
17387     }
17388
17389   if (mapcount == 0)
17390     return FALSE;
17391
17392   if (globals->byteswap_code)
17393     {
17394       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
17395
17396       ptr = map[0].vma;
17397       for (i = 0; i < mapcount; i++)
17398         {
17399           if (i == mapcount - 1)
17400             end = sec->size;
17401           else
17402             end = map[i + 1].vma;
17403
17404           switch (map[i].type)
17405             {
17406             case 'a':
17407               /* Byte swap code words.  */
17408               while (ptr + 3 < end)
17409                 {
17410                   tmp = contents[ptr];
17411                   contents[ptr] = contents[ptr + 3];
17412                   contents[ptr + 3] = tmp;
17413                   tmp = contents[ptr + 1];
17414                   contents[ptr + 1] = contents[ptr + 2];
17415                   contents[ptr + 2] = tmp;
17416                   ptr += 4;
17417                 }
17418               break;
17419
17420             case 't':
17421               /* Byte swap code halfwords.  */
17422               while (ptr + 1 < end)
17423                 {
17424                   tmp = contents[ptr];
17425                   contents[ptr] = contents[ptr + 1];
17426                   contents[ptr + 1] = tmp;
17427                   ptr += 2;
17428                 }
17429               break;
17430
17431             case 'd':
17432               /* Leave data alone.  */
17433               break;
17434             }
17435           ptr = end;
17436         }
17437     }
17438
17439   free (map);
17440   arm_data->mapcount = -1;
17441   arm_data->mapsize = 0;
17442   arm_data->map = NULL;
17443
17444   return FALSE;
17445 }
17446
17447 /* Mangle thumb function symbols as we read them in.  */
17448
17449 static bfd_boolean
17450 elf32_arm_swap_symbol_in (bfd * abfd,
17451                           const void *psrc,
17452                           const void *pshn,
17453                           Elf_Internal_Sym *dst)
17454 {
17455   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
17456     return FALSE;
17457
17458   /* New EABI objects mark thumb function symbols by setting the low bit of
17459      the address.  */
17460   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
17461       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
17462     {
17463       if (dst->st_value & 1)
17464         {
17465           dst->st_value &= ~(bfd_vma) 1;
17466           dst->st_target_internal = ST_BRANCH_TO_THUMB;
17467         }
17468       else
17469         dst->st_target_internal = ST_BRANCH_TO_ARM;
17470     }
17471   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
17472     {
17473       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
17474       dst->st_target_internal = ST_BRANCH_TO_THUMB;
17475     }
17476   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
17477     dst->st_target_internal = ST_BRANCH_LONG;
17478   else
17479     dst->st_target_internal = ST_BRANCH_UNKNOWN;
17480
17481   return TRUE;
17482 }
17483
17484
17485 /* Mangle thumb function symbols as we write them out.  */
17486
17487 static void
17488 elf32_arm_swap_symbol_out (bfd *abfd,
17489                            const Elf_Internal_Sym *src,
17490                            void *cdst,
17491                            void *shndx)
17492 {
17493   Elf_Internal_Sym newsym;
17494
17495   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
17496      of the address set, as per the new EABI.  We do this unconditionally
17497      because objcopy does not set the elf header flags until after
17498      it writes out the symbol table.  */
17499   if (src->st_target_internal == ST_BRANCH_TO_THUMB)
17500     {
17501       newsym = *src;
17502       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
17503         newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
17504       if (newsym.st_shndx != SHN_UNDEF)
17505         {
17506           /* Do this only for defined symbols. At link type, the static
17507              linker will simulate the work of dynamic linker of resolving
17508              symbols and will carry over the thumbness of found symbols to
17509              the output symbol table. It's not clear how it happens, but
17510              the thumbness of undefined symbols can well be different at
17511              runtime, and writing '1' for them will be confusing for users
17512              and possibly for dynamic linker itself.
17513           */
17514           newsym.st_value |= 1;
17515         }
17516
17517       src = &newsym;
17518     }
17519   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
17520 }
17521
17522 /* Add the PT_ARM_EXIDX program header.  */
17523
17524 static bfd_boolean
17525 elf32_arm_modify_segment_map (bfd *abfd,
17526                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
17527 {
17528   struct elf_segment_map *m;
17529   asection *sec;
17530
17531   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
17532   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
17533     {
17534       /* If there is already a PT_ARM_EXIDX header, then we do not
17535          want to add another one.  This situation arises when running
17536          "strip"; the input binary already has the header.  */
17537       m = elf_seg_map (abfd);
17538       while (m && m->p_type != PT_ARM_EXIDX)
17539         m = m->next;
17540       if (!m)
17541         {
17542           m = (struct elf_segment_map *)
17543               bfd_zalloc (abfd, sizeof (struct elf_segment_map));
17544           if (m == NULL)
17545             return FALSE;
17546           m->p_type = PT_ARM_EXIDX;
17547           m->count = 1;
17548           m->sections[0] = sec;
17549
17550           m->next = elf_seg_map (abfd);
17551           elf_seg_map (abfd) = m;
17552         }
17553     }
17554
17555   return TRUE;
17556 }
17557
17558 /* We may add a PT_ARM_EXIDX program header.  */
17559
17560 static int
17561 elf32_arm_additional_program_headers (bfd *abfd,
17562                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
17563 {
17564   asection *sec;
17565
17566   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
17567   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
17568     return 1;
17569   else
17570     return 0;
17571 }
17572
17573 /* Hook called by the linker routine which adds symbols from an object
17574    file.  */
17575
17576 static bfd_boolean
17577 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
17578                            Elf_Internal_Sym *sym, const char **namep,
17579                            flagword *flagsp, asection **secp, bfd_vma *valp)
17580 {
17581   if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
17582        || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
17583       && (abfd->flags & DYNAMIC) == 0
17584       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
17585     elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
17586
17587   if (elf32_arm_hash_table (info) == NULL)
17588     return FALSE;
17589
17590   if (elf32_arm_hash_table (info)->vxworks_p
17591       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
17592                                        flagsp, secp, valp))
17593     return FALSE;
17594
17595   return TRUE;
17596 }
17597
17598 /* We use this to override swap_symbol_in and swap_symbol_out.  */
17599 const struct elf_size_info elf32_arm_size_info =
17600 {
17601   sizeof (Elf32_External_Ehdr),
17602   sizeof (Elf32_External_Phdr),
17603   sizeof (Elf32_External_Shdr),
17604   sizeof (Elf32_External_Rel),
17605   sizeof (Elf32_External_Rela),
17606   sizeof (Elf32_External_Sym),
17607   sizeof (Elf32_External_Dyn),
17608   sizeof (Elf_External_Note),
17609   4,
17610   1,
17611   32, 2,
17612   ELFCLASS32, EV_CURRENT,
17613   bfd_elf32_write_out_phdrs,
17614   bfd_elf32_write_shdrs_and_ehdr,
17615   bfd_elf32_checksum_contents,
17616   bfd_elf32_write_relocs,
17617   elf32_arm_swap_symbol_in,
17618   elf32_arm_swap_symbol_out,
17619   bfd_elf32_slurp_reloc_table,
17620   bfd_elf32_slurp_symbol_table,
17621   bfd_elf32_swap_dyn_in,
17622   bfd_elf32_swap_dyn_out,
17623   bfd_elf32_swap_reloc_in,
17624   bfd_elf32_swap_reloc_out,
17625   bfd_elf32_swap_reloca_in,
17626   bfd_elf32_swap_reloca_out
17627 };
17628
17629 static bfd_vma
17630 read_code32 (const bfd *abfd, const bfd_byte *addr)
17631 {
17632   /* V7 BE8 code is always little endian.  */
17633   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
17634     return bfd_getl32 (addr);
17635
17636   return bfd_get_32 (abfd, addr);
17637 }
17638
17639 static bfd_vma
17640 read_code16 (const bfd *abfd, const bfd_byte *addr)
17641 {
17642   /* V7 BE8 code is always little endian.  */
17643   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
17644     return bfd_getl16 (addr);
17645
17646   return bfd_get_16 (abfd, addr);
17647 }
17648
17649 /* Return size of plt0 entry starting at ADDR
17650    or (bfd_vma) -1 if size can not be determined.  */
17651
17652 static bfd_vma
17653 elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr)
17654 {
17655   bfd_vma first_word;
17656   bfd_vma plt0_size;
17657
17658   first_word = read_code32 (abfd, addr);
17659
17660   if (first_word == elf32_arm_plt0_entry[0])
17661     plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
17662   else if (first_word == elf32_thumb2_plt0_entry[0])
17663     plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
17664   else
17665     /* We don't yet handle this PLT format.  */
17666     return (bfd_vma) -1;
17667
17668   return plt0_size;
17669 }
17670
17671 /* Return size of plt entry starting at offset OFFSET
17672    of plt section located at address START
17673    or (bfd_vma) -1 if size can not be determined.  */
17674
17675 static bfd_vma
17676 elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset)
17677 {
17678   bfd_vma first_insn;
17679   bfd_vma plt_size = 0;
17680   const bfd_byte *addr = start + offset;
17681
17682   /* PLT entry size if fixed on Thumb-only platforms.  */
17683   if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
17684       return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
17685
17686   /* Respect Thumb stub if necessary.  */
17687   if (read_code16 (abfd, addr) == elf32_arm_plt_thumb_stub[0])
17688     {
17689       plt_size += 2 * ARRAY_SIZE(elf32_arm_plt_thumb_stub);
17690     }
17691
17692   /* Strip immediate from first add.  */
17693   first_insn = read_code32 (abfd, addr + plt_size) & 0xffffff00;
17694
17695 #ifdef FOUR_WORD_PLT
17696   if (first_insn == elf32_arm_plt_entry[0])
17697     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
17698 #else
17699   if (first_insn == elf32_arm_plt_entry_long[0])
17700     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
17701   else if (first_insn == elf32_arm_plt_entry_short[0])
17702     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
17703 #endif
17704   else
17705     /* We don't yet handle this PLT format.  */
17706     return (bfd_vma) -1;
17707
17708   return plt_size;
17709 }
17710
17711 /* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
17712
17713 static long
17714 elf32_arm_get_synthetic_symtab (bfd *abfd,
17715                                long symcount ATTRIBUTE_UNUSED,
17716                                asymbol **syms ATTRIBUTE_UNUSED,
17717                                long dynsymcount,
17718                                asymbol **dynsyms,
17719                                asymbol **ret)
17720 {
17721   asection *relplt;
17722   asymbol *s;
17723   arelent *p;
17724   long count, i, n;
17725   size_t size;
17726   Elf_Internal_Shdr *hdr;
17727   char *names;
17728   asection *plt;
17729   bfd_vma offset;
17730   bfd_byte *data;
17731
17732   *ret = NULL;
17733
17734   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
17735     return 0;
17736
17737   if (dynsymcount <= 0)
17738     return 0;
17739
17740   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
17741   if (relplt == NULL)
17742     return 0;
17743
17744   hdr = &elf_section_data (relplt)->this_hdr;
17745   if (hdr->sh_link != elf_dynsymtab (abfd)
17746       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
17747     return 0;
17748
17749   plt = bfd_get_section_by_name (abfd, ".plt");
17750   if (plt == NULL)
17751     return 0;
17752
17753   if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, TRUE))
17754     return -1;
17755
17756   data = plt->contents;
17757   if (data == NULL)
17758     {
17759       if (!bfd_get_full_section_contents(abfd, (asection *) plt, &data) || data == NULL)
17760         return -1;
17761       bfd_cache_section_contents((asection *) plt, data);
17762     }
17763
17764   count = relplt->size / hdr->sh_entsize;
17765   size = count * sizeof (asymbol);
17766   p = relplt->relocation;
17767   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
17768     {
17769       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
17770       if (p->addend != 0)
17771         size += sizeof ("+0x") - 1 + 8;
17772     }
17773
17774   s = *ret = (asymbol *) bfd_malloc (size);
17775   if (s == NULL)
17776     return -1;
17777
17778   offset = elf32_arm_plt0_size (abfd, data);
17779   if (offset == (bfd_vma) -1)
17780     return -1;
17781
17782   names = (char *) (s + count);
17783   p = relplt->relocation;
17784   n = 0;
17785   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
17786     {
17787       size_t len;
17788
17789       bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset);
17790       if (plt_size == (bfd_vma) -1)
17791         break;
17792
17793       *s = **p->sym_ptr_ptr;
17794       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
17795          we are defining a symbol, ensure one of them is set.  */
17796       if ((s->flags & BSF_LOCAL) == 0)
17797         s->flags |= BSF_GLOBAL;
17798       s->flags |= BSF_SYNTHETIC;
17799       s->section = plt;
17800       s->value = offset;
17801       s->name = names;
17802       s->udata.p = NULL;
17803       len = strlen ((*p->sym_ptr_ptr)->name);
17804       memcpy (names, (*p->sym_ptr_ptr)->name, len);
17805       names += len;
17806       if (p->addend != 0)
17807         {
17808           char buf[30], *a;
17809
17810           memcpy (names, "+0x", sizeof ("+0x") - 1);
17811           names += sizeof ("+0x") - 1;
17812           bfd_sprintf_vma (abfd, buf, p->addend);
17813           for (a = buf; *a == '0'; ++a)
17814             ;
17815           len = strlen (a);
17816           memcpy (names, a, len);
17817           names += len;
17818         }
17819       memcpy (names, "@plt", sizeof ("@plt"));
17820       names += sizeof ("@plt");
17821       ++s, ++n;
17822       offset += plt_size;
17823     }
17824
17825   return n;
17826 }
17827
17828 static const struct bfd_elf_special_section
17829 elf32_arm_special_sections[] =
17830 {
17831 /* Catch sections with .text.noread prefix and apply allocate, execute and
17832    noread section attributes.  */
17833   { STRING_COMMA_LEN (".text.noread"),  -2, SHT_PROGBITS,
17834     SHF_ALLOC + SHF_EXECINSTR + SHF_ARM_NOREAD },
17835   { NULL,                             0, 0, 0,                  0 }
17836 };
17837
17838 static bfd_boolean
17839 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr * hdr)
17840 {
17841   if (hdr->sh_flags & SHF_ARM_NOREAD)
17842     *flags |= SEC_ELF_NOREAD;
17843   return TRUE;
17844 }
17845
17846 static flagword
17847 elf32_arm_lookup_section_flags (char *flag_name)
17848 {
17849   if (!strcmp (flag_name, "SHF_ARM_NOREAD"))
17850     return SHF_ARM_NOREAD;
17851
17852   return SEC_NO_FLAGS;
17853 }
17854
17855 static unsigned int
17856 elf32_arm_count_additional_relocs (asection *sec)
17857 {
17858   struct _arm_elf_section_data *arm_data;
17859   arm_data = get_arm_elf_section_data (sec);
17860   return arm_data->additional_reloc_count;
17861 }
17862
17863 #define ELF_ARCH                        bfd_arch_arm
17864 #define ELF_TARGET_ID                   ARM_ELF_DATA
17865 #define ELF_MACHINE_CODE                EM_ARM
17866 #ifdef __QNXTARGET__
17867 #define ELF_MAXPAGESIZE                 0x1000
17868 #else
17869 #define ELF_MAXPAGESIZE                 0x10000
17870 #endif
17871 #define ELF_MINPAGESIZE                 0x1000
17872 #define ELF_COMMONPAGESIZE              0x1000
17873
17874 #define bfd_elf32_mkobject                      elf32_arm_mkobject
17875
17876 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
17877 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
17878 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
17879 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
17880 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
17881 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
17882 #define bfd_elf32_bfd_reloc_name_lookup         elf32_arm_reloc_name_lookup
17883 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
17884 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
17885 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
17886 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
17887 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
17888 #define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
17889
17890 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
17891 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
17892 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
17893 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
17894 #define elf_backend_check_relocs                elf32_arm_check_relocs
17895 #define elf_backend_relocate_section            elf32_arm_relocate_section
17896 #define elf_backend_write_section               elf32_arm_write_section
17897 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
17898 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
17899 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
17900 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
17901 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
17902 #define elf_backend_always_size_sections        elf32_arm_always_size_sections
17903 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
17904 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
17905 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
17906 #define elf_backend_object_p                    elf32_arm_object_p
17907 #define elf_backend_fake_sections               elf32_arm_fake_sections
17908 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
17909 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
17910 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
17911 #define elf_backend_size_info                   elf32_arm_size_info
17912 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
17913 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
17914 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
17915 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
17916 #define elf_backend_add_symbol_hook             elf32_arm_add_symbol_hook
17917 #define elf_backend_count_additional_relocs     elf32_arm_count_additional_relocs
17918
17919 #define elf_backend_can_refcount       1
17920 #define elf_backend_can_gc_sections    1
17921 #define elf_backend_plt_readonly       1
17922 #define elf_backend_want_got_plt       1
17923 #define elf_backend_want_plt_sym       0
17924 #define elf_backend_may_use_rel_p      1
17925 #define elf_backend_may_use_rela_p     0
17926 #define elf_backend_default_use_rela_p 0
17927
17928 #define elf_backend_got_header_size     12
17929 #define elf_backend_extern_protected_data 1
17930
17931 #undef  elf_backend_obj_attrs_vendor
17932 #define elf_backend_obj_attrs_vendor            "aeabi"
17933 #undef  elf_backend_obj_attrs_section
17934 #define elf_backend_obj_attrs_section           ".ARM.attributes"
17935 #undef  elf_backend_obj_attrs_arg_type
17936 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
17937 #undef  elf_backend_obj_attrs_section_type
17938 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
17939 #define elf_backend_obj_attrs_order             elf32_arm_obj_attrs_order
17940 #define elf_backend_obj_attrs_handle_unknown    elf32_arm_obj_attrs_handle_unknown
17941
17942 #undef  elf_backend_special_sections
17943 #define elf_backend_special_sections            elf32_arm_special_sections
17944 #undef elf_backend_section_flags
17945 #define elf_backend_section_flags               elf32_arm_section_flags
17946 #undef elf_backend_lookup_section_flags_hook
17947 #define elf_backend_lookup_section_flags_hook   elf32_arm_lookup_section_flags
17948
17949 #include "elf32-target.h"
17950
17951 /* Native Client targets.  */
17952
17953 #undef  TARGET_LITTLE_SYM
17954 #define TARGET_LITTLE_SYM               arm_elf32_nacl_le_vec
17955 #undef  TARGET_LITTLE_NAME
17956 #define TARGET_LITTLE_NAME              "elf32-littlearm-nacl"
17957 #undef  TARGET_BIG_SYM
17958 #define TARGET_BIG_SYM                  arm_elf32_nacl_be_vec
17959 #undef  TARGET_BIG_NAME
17960 #define TARGET_BIG_NAME                 "elf32-bigarm-nacl"
17961
17962 /* Like elf32_arm_link_hash_table_create -- but overrides
17963    appropriately for NaCl.  */
17964
17965 static struct bfd_link_hash_table *
17966 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
17967 {
17968   struct bfd_link_hash_table *ret;
17969
17970   ret = elf32_arm_link_hash_table_create (abfd);
17971   if (ret)
17972     {
17973       struct elf32_arm_link_hash_table *htab
17974         = (struct elf32_arm_link_hash_table *) ret;
17975
17976       htab->nacl_p = 1;
17977
17978       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
17979       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
17980     }
17981   return ret;
17982 }
17983
17984 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
17985    really need to use elf32_arm_modify_segment_map.  But we do it
17986    anyway just to reduce gratuitous differences with the stock ARM backend.  */
17987
17988 static bfd_boolean
17989 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
17990 {
17991   return (elf32_arm_modify_segment_map (abfd, info)
17992           && nacl_modify_segment_map (abfd, info));
17993 }
17994
17995 static void
17996 elf32_arm_nacl_final_write_processing (bfd *abfd, bfd_boolean linker)
17997 {
17998   elf32_arm_final_write_processing (abfd, linker);
17999   nacl_final_write_processing (abfd, linker);
18000 }
18001
18002 static bfd_vma
18003 elf32_arm_nacl_plt_sym_val (bfd_vma i, const asection *plt,
18004                             const arelent *rel ATTRIBUTE_UNUSED)
18005 {
18006   return plt->vma
18007     + 4 * (ARRAY_SIZE (elf32_arm_nacl_plt0_entry) +
18008            i * ARRAY_SIZE (elf32_arm_nacl_plt_entry));
18009 }
18010
18011 #undef  elf32_bed
18012 #define elf32_bed                               elf32_arm_nacl_bed
18013 #undef  bfd_elf32_bfd_link_hash_table_create
18014 #define bfd_elf32_bfd_link_hash_table_create    \
18015   elf32_arm_nacl_link_hash_table_create
18016 #undef  elf_backend_plt_alignment
18017 #define elf_backend_plt_alignment               4
18018 #undef  elf_backend_modify_segment_map
18019 #define elf_backend_modify_segment_map          elf32_arm_nacl_modify_segment_map
18020 #undef  elf_backend_modify_program_headers
18021 #define elf_backend_modify_program_headers      nacl_modify_program_headers
18022 #undef  elf_backend_final_write_processing
18023 #define elf_backend_final_write_processing      elf32_arm_nacl_final_write_processing
18024 #undef bfd_elf32_get_synthetic_symtab
18025 #undef  elf_backend_plt_sym_val
18026 #define elf_backend_plt_sym_val                 elf32_arm_nacl_plt_sym_val
18027
18028 #undef  ELF_MINPAGESIZE
18029 #undef  ELF_COMMONPAGESIZE
18030
18031
18032 #include "elf32-target.h"
18033
18034 /* Reset to defaults.  */
18035 #undef  elf_backend_plt_alignment
18036 #undef  elf_backend_modify_segment_map
18037 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
18038 #undef  elf_backend_modify_program_headers
18039 #undef  elf_backend_final_write_processing
18040 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
18041 #undef  ELF_MINPAGESIZE
18042 #define ELF_MINPAGESIZE                 0x1000
18043 #undef  ELF_COMMONPAGESIZE
18044 #define ELF_COMMONPAGESIZE              0x1000
18045
18046
18047 /* VxWorks Targets.  */
18048
18049 #undef  TARGET_LITTLE_SYM
18050 #define TARGET_LITTLE_SYM               arm_elf32_vxworks_le_vec
18051 #undef  TARGET_LITTLE_NAME
18052 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
18053 #undef  TARGET_BIG_SYM
18054 #define TARGET_BIG_SYM                  arm_elf32_vxworks_be_vec
18055 #undef  TARGET_BIG_NAME
18056 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
18057
18058 /* Like elf32_arm_link_hash_table_create -- but overrides
18059    appropriately for VxWorks.  */
18060
18061 static struct bfd_link_hash_table *
18062 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
18063 {
18064   struct bfd_link_hash_table *ret;
18065
18066   ret = elf32_arm_link_hash_table_create (abfd);
18067   if (ret)
18068     {
18069       struct elf32_arm_link_hash_table *htab
18070         = (struct elf32_arm_link_hash_table *) ret;
18071       htab->use_rel = 0;
18072       htab->vxworks_p = 1;
18073     }
18074   return ret;
18075 }
18076
18077 static void
18078 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
18079 {
18080   elf32_arm_final_write_processing (abfd, linker);
18081   elf_vxworks_final_write_processing (abfd, linker);
18082 }
18083
18084 #undef  elf32_bed
18085 #define elf32_bed elf32_arm_vxworks_bed
18086
18087 #undef  bfd_elf32_bfd_link_hash_table_create
18088 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
18089 #undef  elf_backend_final_write_processing
18090 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
18091 #undef  elf_backend_emit_relocs
18092 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
18093
18094 #undef  elf_backend_may_use_rel_p
18095 #define elf_backend_may_use_rel_p       0
18096 #undef  elf_backend_may_use_rela_p
18097 #define elf_backend_may_use_rela_p      1
18098 #undef  elf_backend_default_use_rela_p
18099 #define elf_backend_default_use_rela_p  1
18100 #undef  elf_backend_want_plt_sym
18101 #define elf_backend_want_plt_sym        1
18102 #undef  ELF_MAXPAGESIZE
18103 #define ELF_MAXPAGESIZE                 0x1000
18104
18105 #include "elf32-target.h"
18106
18107
18108 /* Merge backend specific data from an object file to the output
18109    object file when linking.  */
18110
18111 static bfd_boolean
18112 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
18113 {
18114   flagword out_flags;
18115   flagword in_flags;
18116   bfd_boolean flags_compatible = TRUE;
18117   asection *sec;
18118
18119   /* Check if we have the same endianness.  */
18120   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
18121     return FALSE;
18122
18123   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
18124     return TRUE;
18125
18126   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
18127     return FALSE;
18128
18129   /* The input BFD must have had its flags initialised.  */
18130   /* The following seems bogus to me -- The flags are initialized in
18131      the assembler but I don't think an elf_flags_init field is
18132      written into the object.  */
18133   /* BFD_ASSERT (elf_flags_init (ibfd)); */
18134
18135   in_flags  = elf_elfheader (ibfd)->e_flags;
18136   out_flags = elf_elfheader (obfd)->e_flags;
18137
18138   /* In theory there is no reason why we couldn't handle this.  However
18139      in practice it isn't even close to working and there is no real
18140      reason to want it.  */
18141   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
18142       && !(ibfd->flags & DYNAMIC)
18143       && (in_flags & EF_ARM_BE8))
18144     {
18145       _bfd_error_handler (_("error: %B is already in final BE8 format"),
18146                           ibfd);
18147       return FALSE;
18148     }
18149
18150   if (!elf_flags_init (obfd))
18151     {
18152       /* If the input is the default architecture and had the default
18153          flags then do not bother setting the flags for the output
18154          architecture, instead allow future merges to do this.  If no
18155          future merges ever set these flags then they will retain their
18156          uninitialised values, which surprise surprise, correspond
18157          to the default values.  */
18158       if (bfd_get_arch_info (ibfd)->the_default
18159           && elf_elfheader (ibfd)->e_flags == 0)
18160         return TRUE;
18161
18162       elf_flags_init (obfd) = TRUE;
18163       elf_elfheader (obfd)->e_flags = in_flags;
18164
18165       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
18166           && bfd_get_arch_info (obfd)->the_default)
18167         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
18168
18169       return TRUE;
18170     }
18171
18172   /* Determine what should happen if the input ARM architecture
18173      does not match the output ARM architecture.  */
18174   if (! bfd_arm_merge_machines (ibfd, obfd))
18175     return FALSE;
18176
18177   /* Identical flags must be compatible.  */
18178   if (in_flags == out_flags)
18179     return TRUE;
18180
18181   /* Check to see if the input BFD actually contains any sections.  If
18182      not, its flags may not have been initialised either, but it
18183      cannot actually cause any incompatiblity.  Do not short-circuit
18184      dynamic objects; their section list may be emptied by
18185     elf_link_add_object_symbols.
18186
18187     Also check to see if there are no code sections in the input.
18188     In this case there is no need to check for code specific flags.
18189     XXX - do we need to worry about floating-point format compatability
18190     in data sections ?  */
18191   if (!(ibfd->flags & DYNAMIC))
18192     {
18193       bfd_boolean null_input_bfd = TRUE;
18194       bfd_boolean only_data_sections = TRUE;
18195
18196       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
18197         {
18198           /* Ignore synthetic glue sections.  */
18199           if (strcmp (sec->name, ".glue_7")
18200               && strcmp (sec->name, ".glue_7t"))
18201             {
18202               if ((bfd_get_section_flags (ibfd, sec)
18203                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
18204                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
18205                 only_data_sections = FALSE;
18206
18207               null_input_bfd = FALSE;
18208               break;
18209             }
18210         }
18211
18212       if (null_input_bfd || only_data_sections)
18213         return TRUE;
18214     }
18215
18216   /* Complain about various flag mismatches.  */
18217   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
18218                                       EF_ARM_EABI_VERSION (out_flags)))
18219     {
18220       _bfd_error_handler
18221         (_("error: Source object %B has EABI version %d, but target %B has EABI version %d"),
18222          ibfd, obfd,
18223          (in_flags & EF_ARM_EABIMASK) >> 24,
18224          (out_flags & EF_ARM_EABIMASK) >> 24);
18225       return FALSE;
18226     }
18227
18228   /* Not sure what needs to be checked for EABI versions >= 1.  */
18229   /* VxWorks libraries do not use these flags.  */
18230   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
18231       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
18232       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
18233     {
18234       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
18235         {
18236           _bfd_error_handler
18237             (_("error: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
18238              ibfd, obfd,
18239              in_flags & EF_ARM_APCS_26 ? 26 : 32,
18240              out_flags & EF_ARM_APCS_26 ? 26 : 32);
18241           flags_compatible = FALSE;
18242         }
18243
18244       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
18245         {
18246           if (in_flags & EF_ARM_APCS_FLOAT)
18247             _bfd_error_handler
18248               (_("error: %B passes floats in float registers, whereas %B passes them in integer registers"),
18249                ibfd, obfd);
18250           else
18251             _bfd_error_handler
18252               (_("error: %B passes floats in integer registers, whereas %B passes them in float registers"),
18253                ibfd, obfd);
18254
18255           flags_compatible = FALSE;
18256         }
18257
18258       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
18259         {
18260           if (in_flags & EF_ARM_VFP_FLOAT)
18261             _bfd_error_handler
18262               (_("error: %B uses VFP instructions, whereas %B does not"),
18263                ibfd, obfd);
18264           else
18265             _bfd_error_handler
18266               (_("error: %B uses FPA instructions, whereas %B does not"),
18267                ibfd, obfd);
18268
18269           flags_compatible = FALSE;
18270         }
18271
18272       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
18273         {
18274           if (in_flags & EF_ARM_MAVERICK_FLOAT)
18275             _bfd_error_handler
18276               (_("error: %B uses Maverick instructions, whereas %B does not"),
18277                ibfd, obfd);
18278           else
18279             _bfd_error_handler
18280               (_("error: %B does not use Maverick instructions, whereas %B does"),
18281                ibfd, obfd);
18282
18283           flags_compatible = FALSE;
18284         }
18285
18286 #ifdef EF_ARM_SOFT_FLOAT
18287       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
18288         {
18289           /* We can allow interworking between code that is VFP format
18290              layout, and uses either soft float or integer regs for
18291              passing floating point arguments and results.  We already
18292              know that the APCS_FLOAT flags match; similarly for VFP
18293              flags.  */
18294           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
18295               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
18296             {
18297               if (in_flags & EF_ARM_SOFT_FLOAT)
18298                 _bfd_error_handler
18299                   (_("error: %B uses software FP, whereas %B uses hardware FP"),
18300                    ibfd, obfd);
18301               else
18302                 _bfd_error_handler
18303                   (_("error: %B uses hardware FP, whereas %B uses software FP"),
18304                    ibfd, obfd);
18305
18306               flags_compatible = FALSE;
18307             }
18308         }
18309 #endif
18310
18311       /* Interworking mismatch is only a warning.  */
18312       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
18313         {
18314           if (in_flags & EF_ARM_INTERWORK)
18315             {
18316               _bfd_error_handler
18317                 (_("Warning: %B supports interworking, whereas %B does not"),
18318                  ibfd, obfd);
18319             }
18320           else
18321             {
18322               _bfd_error_handler
18323                 (_("Warning: %B does not support interworking, whereas %B does"),
18324                  ibfd, obfd);
18325             }
18326         }
18327     }
18328
18329   return flags_compatible;
18330 }
18331
18332
18333 /* Symbian OS Targets.  */
18334
18335 #undef  TARGET_LITTLE_SYM
18336 #define TARGET_LITTLE_SYM               arm_elf32_symbian_le_vec
18337 #undef  TARGET_LITTLE_NAME
18338 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
18339 #undef  TARGET_BIG_SYM
18340 #define TARGET_BIG_SYM                  arm_elf32_symbian_be_vec
18341 #undef  TARGET_BIG_NAME
18342 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
18343
18344 /* Like elf32_arm_link_hash_table_create -- but overrides
18345    appropriately for Symbian OS.  */
18346
18347 static struct bfd_link_hash_table *
18348 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
18349 {
18350   struct bfd_link_hash_table *ret;
18351
18352   ret = elf32_arm_link_hash_table_create (abfd);
18353   if (ret)
18354     {
18355       struct elf32_arm_link_hash_table *htab
18356         = (struct elf32_arm_link_hash_table *)ret;
18357       /* There is no PLT header for Symbian OS.  */
18358       htab->plt_header_size = 0;
18359       /* The PLT entries are each one instruction and one word.  */
18360       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
18361       htab->symbian_p = 1;
18362       /* Symbian uses armv5t or above, so use_blx is always true.  */
18363       htab->use_blx = 1;
18364       htab->root.is_relocatable_executable = 1;
18365     }
18366   return ret;
18367 }
18368
18369 static const struct bfd_elf_special_section
18370 elf32_arm_symbian_special_sections[] =
18371 {
18372   /* In a BPABI executable, the dynamic linking sections do not go in
18373      the loadable read-only segment.  The post-linker may wish to
18374      refer to these sections, but they are not part of the final
18375      program image.  */
18376   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
18377   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
18378   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
18379   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
18380   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
18381   /* These sections do not need to be writable as the SymbianOS
18382      postlinker will arrange things so that no dynamic relocation is
18383      required.  */
18384   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
18385   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
18386   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
18387   { NULL,                             0, 0, 0,                 0 }
18388 };
18389
18390 static void
18391 elf32_arm_symbian_begin_write_processing (bfd *abfd,
18392                                           struct bfd_link_info *link_info)
18393 {
18394   /* BPABI objects are never loaded directly by an OS kernel; they are
18395      processed by a postlinker first, into an OS-specific format.  If
18396      the D_PAGED bit is set on the file, BFD will align segments on
18397      page boundaries, so that an OS can directly map the file.  With
18398      BPABI objects, that just results in wasted space.  In addition,
18399      because we clear the D_PAGED bit, map_sections_to_segments will
18400      recognize that the program headers should not be mapped into any
18401      loadable segment.  */
18402   abfd->flags &= ~D_PAGED;
18403   elf32_arm_begin_write_processing (abfd, link_info);
18404 }
18405
18406 static bfd_boolean
18407 elf32_arm_symbian_modify_segment_map (bfd *abfd,
18408                                       struct bfd_link_info *info)
18409 {
18410   struct elf_segment_map *m;
18411   asection *dynsec;
18412
18413   /* BPABI shared libraries and executables should have a PT_DYNAMIC
18414      segment.  However, because the .dynamic section is not marked
18415      with SEC_LOAD, the generic ELF code will not create such a
18416      segment.  */
18417   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
18418   if (dynsec)
18419     {
18420       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
18421         if (m->p_type == PT_DYNAMIC)
18422           break;
18423
18424       if (m == NULL)
18425         {
18426           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
18427           m->next = elf_seg_map (abfd);
18428           elf_seg_map (abfd) = m;
18429         }
18430     }
18431
18432   /* Also call the generic arm routine.  */
18433   return elf32_arm_modify_segment_map (abfd, info);
18434 }
18435
18436 /* Return address for Ith PLT stub in section PLT, for relocation REL
18437    or (bfd_vma) -1 if it should not be included.  */
18438
18439 static bfd_vma
18440 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
18441                                const arelent *rel ATTRIBUTE_UNUSED)
18442 {
18443   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
18444 }
18445
18446
18447 #undef  elf32_bed
18448 #define elf32_bed elf32_arm_symbian_bed
18449
18450 /* The dynamic sections are not allocated on SymbianOS; the postlinker
18451    will process them and then discard them.  */
18452 #undef  ELF_DYNAMIC_SEC_FLAGS
18453 #define ELF_DYNAMIC_SEC_FLAGS \
18454   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
18455
18456 #undef elf_backend_emit_relocs
18457
18458 #undef  bfd_elf32_bfd_link_hash_table_create
18459 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
18460 #undef  elf_backend_special_sections
18461 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
18462 #undef  elf_backend_begin_write_processing
18463 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
18464 #undef  elf_backend_final_write_processing
18465 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
18466
18467 #undef  elf_backend_modify_segment_map
18468 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
18469
18470 /* There is no .got section for BPABI objects, and hence no header.  */
18471 #undef  elf_backend_got_header_size
18472 #define elf_backend_got_header_size 0
18473
18474 /* Similarly, there is no .got.plt section.  */
18475 #undef  elf_backend_want_got_plt
18476 #define elf_backend_want_got_plt 0
18477
18478 #undef  elf_backend_plt_sym_val
18479 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
18480
18481 #undef  elf_backend_may_use_rel_p
18482 #define elf_backend_may_use_rel_p       1
18483 #undef  elf_backend_may_use_rela_p
18484 #define elf_backend_may_use_rela_p      0
18485 #undef  elf_backend_default_use_rela_p
18486 #define elf_backend_default_use_rela_p  0
18487 #undef  elf_backend_want_plt_sym
18488 #define elf_backend_want_plt_sym        0
18489 #undef  ELF_MAXPAGESIZE
18490 #define ELF_MAXPAGESIZE                 0x8000
18491
18492 #include "elf32-target.h"