OSDN Git Service

* elf32-arm.c (elf32_arm_size_stubs): Don't die on undefined local
[pf3gnuchains/pf3gnuchains3x.git] / bfd / elf32-arm.c
1 /* 32-bit ELF support for ARM
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3    2008, 2009  Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include <limits.h>
24
25 #include "bfd.h"
26 #include "libiberty.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf-vxworks.h"
30 #include "elf/arm.h"
31
32 /* Return the relocation section associated with NAME.  HTAB is the
33    bfd's elf32_arm_link_hash_entry.  */
34 #define RELOC_SECTION(HTAB, NAME) \
35   ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
36
37 /* Return size of a relocation entry.  HTAB is the bfd's
38    elf32_arm_link_hash_entry.  */
39 #define RELOC_SIZE(HTAB) \
40   ((HTAB)->use_rel \
41    ? sizeof (Elf32_External_Rel) \
42    : sizeof (Elf32_External_Rela))
43
44 /* Return function to swap relocations in.  HTAB is the bfd's
45    elf32_arm_link_hash_entry.  */
46 #define SWAP_RELOC_IN(HTAB) \
47   ((HTAB)->use_rel \
48    ? bfd_elf32_swap_reloc_in \
49    : bfd_elf32_swap_reloca_in)
50
51 /* Return function to swap relocations out.  HTAB is the bfd's
52    elf32_arm_link_hash_entry.  */
53 #define SWAP_RELOC_OUT(HTAB) \
54   ((HTAB)->use_rel \
55    ? bfd_elf32_swap_reloc_out \
56    : bfd_elf32_swap_reloca_out)
57
58 #define elf_info_to_howto               0
59 #define elf_info_to_howto_rel           elf32_arm_info_to_howto
60
61 #define ARM_ELF_ABI_VERSION             0
62 #define ARM_ELF_OS_ABI_VERSION          ELFOSABI_ARM
63
64 static struct elf_backend_data elf32_arm_vxworks_bed;
65
66 static bfd_boolean elf32_arm_write_section (bfd *output_bfd,
67                                             struct bfd_link_info *link_info,
68                                             asection *sec,
69                                             bfd_byte *contents);
70
71 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
72    R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
73    in that slot.  */
74
75 static reloc_howto_type elf32_arm_howto_table_1[] =
76 {
77   /* No relocation.  */
78   HOWTO (R_ARM_NONE,            /* type */
79          0,                     /* rightshift */
80          0,                     /* size (0 = byte, 1 = short, 2 = long) */
81          0,                     /* bitsize */
82          FALSE,                 /* pc_relative */
83          0,                     /* bitpos */
84          complain_overflow_dont,/* complain_on_overflow */
85          bfd_elf_generic_reloc, /* special_function */
86          "R_ARM_NONE",          /* name */
87          FALSE,                 /* partial_inplace */
88          0,                     /* src_mask */
89          0,                     /* dst_mask */
90          FALSE),                /* pcrel_offset */
91
92   HOWTO (R_ARM_PC24,            /* type */
93          2,                     /* rightshift */
94          2,                     /* size (0 = byte, 1 = short, 2 = long) */
95          24,                    /* bitsize */
96          TRUE,                  /* pc_relative */
97          0,                     /* bitpos */
98          complain_overflow_signed,/* complain_on_overflow */
99          bfd_elf_generic_reloc, /* special_function */
100          "R_ARM_PC24",          /* name */
101          FALSE,                 /* partial_inplace */
102          0x00ffffff,            /* src_mask */
103          0x00ffffff,            /* dst_mask */
104          TRUE),                 /* pcrel_offset */
105
106   /* 32 bit absolute */
107   HOWTO (R_ARM_ABS32,           /* type */
108          0,                     /* rightshift */
109          2,                     /* size (0 = byte, 1 = short, 2 = long) */
110          32,                    /* bitsize */
111          FALSE,                 /* pc_relative */
112          0,                     /* bitpos */
113          complain_overflow_bitfield,/* complain_on_overflow */
114          bfd_elf_generic_reloc, /* special_function */
115          "R_ARM_ABS32",         /* name */
116          FALSE,                 /* partial_inplace */
117          0xffffffff,            /* src_mask */
118          0xffffffff,            /* dst_mask */
119          FALSE),                /* pcrel_offset */
120
121   /* standard 32bit pc-relative reloc */
122   HOWTO (R_ARM_REL32,           /* type */
123          0,                     /* rightshift */
124          2,                     /* size (0 = byte, 1 = short, 2 = long) */
125          32,                    /* bitsize */
126          TRUE,                  /* pc_relative */
127          0,                     /* bitpos */
128          complain_overflow_bitfield,/* complain_on_overflow */
129          bfd_elf_generic_reloc, /* special_function */
130          "R_ARM_REL32",         /* name */
131          FALSE,                 /* partial_inplace */
132          0xffffffff,            /* src_mask */
133          0xffffffff,            /* dst_mask */
134          TRUE),                 /* pcrel_offset */
135
136   /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
137   HOWTO (R_ARM_LDR_PC_G0,       /* type */
138          0,                     /* rightshift */
139          0,                     /* size (0 = byte, 1 = short, 2 = long) */
140          32,                    /* bitsize */
141          TRUE,                  /* pc_relative */
142          0,                     /* bitpos */
143          complain_overflow_dont,/* complain_on_overflow */
144          bfd_elf_generic_reloc, /* special_function */
145          "R_ARM_LDR_PC_G0",     /* name */
146          FALSE,                 /* partial_inplace */
147          0xffffffff,            /* src_mask */
148          0xffffffff,            /* dst_mask */
149          TRUE),                 /* pcrel_offset */
150
151    /* 16 bit absolute */
152   HOWTO (R_ARM_ABS16,           /* type */
153          0,                     /* rightshift */
154          1,                     /* size (0 = byte, 1 = short, 2 = long) */
155          16,                    /* bitsize */
156          FALSE,                 /* pc_relative */
157          0,                     /* bitpos */
158          complain_overflow_bitfield,/* complain_on_overflow */
159          bfd_elf_generic_reloc, /* special_function */
160          "R_ARM_ABS16",         /* name */
161          FALSE,                 /* partial_inplace */
162          0x0000ffff,            /* src_mask */
163          0x0000ffff,            /* dst_mask */
164          FALSE),                /* pcrel_offset */
165
166   /* 12 bit absolute */
167   HOWTO (R_ARM_ABS12,           /* type */
168          0,                     /* rightshift */
169          2,                     /* size (0 = byte, 1 = short, 2 = long) */
170          12,                    /* bitsize */
171          FALSE,                 /* pc_relative */
172          0,                     /* bitpos */
173          complain_overflow_bitfield,/* complain_on_overflow */
174          bfd_elf_generic_reloc, /* special_function */
175          "R_ARM_ABS12",         /* name */
176          FALSE,                 /* partial_inplace */
177          0x00000fff,            /* src_mask */
178          0x00000fff,            /* dst_mask */
179          FALSE),                /* pcrel_offset */
180
181   HOWTO (R_ARM_THM_ABS5,        /* type */
182          6,                     /* rightshift */
183          1,                     /* size (0 = byte, 1 = short, 2 = long) */
184          5,                     /* bitsize */
185          FALSE,                 /* pc_relative */
186          0,                     /* bitpos */
187          complain_overflow_bitfield,/* complain_on_overflow */
188          bfd_elf_generic_reloc, /* special_function */
189          "R_ARM_THM_ABS5",      /* name */
190          FALSE,                 /* partial_inplace */
191          0x000007e0,            /* src_mask */
192          0x000007e0,            /* dst_mask */
193          FALSE),                /* pcrel_offset */
194
195   /* 8 bit absolute */
196   HOWTO (R_ARM_ABS8,            /* type */
197          0,                     /* rightshift */
198          0,                     /* size (0 = byte, 1 = short, 2 = long) */
199          8,                     /* bitsize */
200          FALSE,                 /* pc_relative */
201          0,                     /* bitpos */
202          complain_overflow_bitfield,/* complain_on_overflow */
203          bfd_elf_generic_reloc, /* special_function */
204          "R_ARM_ABS8",          /* name */
205          FALSE,                 /* partial_inplace */
206          0x000000ff,            /* src_mask */
207          0x000000ff,            /* dst_mask */
208          FALSE),                /* pcrel_offset */
209
210   HOWTO (R_ARM_SBREL32,         /* type */
211          0,                     /* rightshift */
212          2,                     /* size (0 = byte, 1 = short, 2 = long) */
213          32,                    /* bitsize */
214          FALSE,                 /* pc_relative */
215          0,                     /* bitpos */
216          complain_overflow_dont,/* complain_on_overflow */
217          bfd_elf_generic_reloc, /* special_function */
218          "R_ARM_SBREL32",       /* name */
219          FALSE,                 /* partial_inplace */
220          0xffffffff,            /* src_mask */
221          0xffffffff,            /* dst_mask */
222          FALSE),                /* pcrel_offset */
223
224   HOWTO (R_ARM_THM_CALL,        /* type */
225          1,                     /* rightshift */
226          2,                     /* size (0 = byte, 1 = short, 2 = long) */
227          25,                    /* bitsize */
228          TRUE,                  /* pc_relative */
229          0,                     /* bitpos */
230          complain_overflow_signed,/* complain_on_overflow */
231          bfd_elf_generic_reloc, /* special_function */
232          "R_ARM_THM_CALL",      /* name */
233          FALSE,                 /* partial_inplace */
234          0x07ff07ff,            /* src_mask */
235          0x07ff07ff,            /* dst_mask */
236          TRUE),                 /* pcrel_offset */
237
238   HOWTO (R_ARM_THM_PC8,         /* type */
239          1,                     /* rightshift */
240          1,                     /* size (0 = byte, 1 = short, 2 = long) */
241          8,                     /* bitsize */
242          TRUE,                  /* pc_relative */
243          0,                     /* bitpos */
244          complain_overflow_signed,/* complain_on_overflow */
245          bfd_elf_generic_reloc, /* special_function */
246          "R_ARM_THM_PC8",       /* name */
247          FALSE,                 /* partial_inplace */
248          0x000000ff,            /* src_mask */
249          0x000000ff,            /* dst_mask */
250          TRUE),                 /* pcrel_offset */
251
252   HOWTO (R_ARM_BREL_ADJ,        /* type */
253          1,                     /* rightshift */
254          1,                     /* size (0 = byte, 1 = short, 2 = long) */
255          32,                    /* bitsize */
256          FALSE,                 /* pc_relative */
257          0,                     /* bitpos */
258          complain_overflow_signed,/* complain_on_overflow */
259          bfd_elf_generic_reloc, /* special_function */
260          "R_ARM_BREL_ADJ",      /* name */
261          FALSE,                 /* partial_inplace */
262          0xffffffff,            /* src_mask */
263          0xffffffff,            /* dst_mask */
264          FALSE),                /* pcrel_offset */
265
266   HOWTO (R_ARM_SWI24,           /* type */
267          0,                     /* rightshift */
268          0,                     /* size (0 = byte, 1 = short, 2 = long) */
269          0,                     /* bitsize */
270          FALSE,                 /* pc_relative */
271          0,                     /* bitpos */
272          complain_overflow_signed,/* complain_on_overflow */
273          bfd_elf_generic_reloc, /* special_function */
274          "R_ARM_SWI24",         /* name */
275          FALSE,                 /* partial_inplace */
276          0x00000000,            /* src_mask */
277          0x00000000,            /* dst_mask */
278          FALSE),                /* pcrel_offset */
279
280   HOWTO (R_ARM_THM_SWI8,        /* type */
281          0,                     /* rightshift */
282          0,                     /* size (0 = byte, 1 = short, 2 = long) */
283          0,                     /* bitsize */
284          FALSE,                 /* pc_relative */
285          0,                     /* bitpos */
286          complain_overflow_signed,/* complain_on_overflow */
287          bfd_elf_generic_reloc, /* special_function */
288          "R_ARM_SWI8",          /* name */
289          FALSE,                 /* partial_inplace */
290          0x00000000,            /* src_mask */
291          0x00000000,            /* dst_mask */
292          FALSE),                /* pcrel_offset */
293
294   /* BLX instruction for the ARM.  */
295   HOWTO (R_ARM_XPC25,           /* type */
296          2,                     /* rightshift */
297          2,                     /* size (0 = byte, 1 = short, 2 = long) */
298          25,                    /* bitsize */
299          TRUE,                  /* pc_relative */
300          0,                     /* bitpos */
301          complain_overflow_signed,/* complain_on_overflow */
302          bfd_elf_generic_reloc, /* special_function */
303          "R_ARM_XPC25",         /* name */
304          FALSE,                 /* partial_inplace */
305          0x00ffffff,            /* src_mask */
306          0x00ffffff,            /* dst_mask */
307          TRUE),                 /* pcrel_offset */
308
309   /* BLX instruction for the Thumb.  */
310   HOWTO (R_ARM_THM_XPC22,       /* type */
311          2,                     /* rightshift */
312          2,                     /* size (0 = byte, 1 = short, 2 = long) */
313          22,                    /* bitsize */
314          TRUE,                  /* pc_relative */
315          0,                     /* bitpos */
316          complain_overflow_signed,/* complain_on_overflow */
317          bfd_elf_generic_reloc, /* special_function */
318          "R_ARM_THM_XPC22",     /* name */
319          FALSE,                 /* partial_inplace */
320          0x07ff07ff,            /* src_mask */
321          0x07ff07ff,            /* dst_mask */
322          TRUE),                 /* pcrel_offset */
323
324   /* Dynamic TLS relocations.  */
325
326   HOWTO (R_ARM_TLS_DTPMOD32,    /* type */
327          0,                     /* rightshift */
328          2,                     /* size (0 = byte, 1 = short, 2 = long) */
329          32,                    /* bitsize */
330          FALSE,                 /* pc_relative */
331          0,                     /* bitpos */
332          complain_overflow_bitfield,/* complain_on_overflow */
333          bfd_elf_generic_reloc, /* special_function */
334          "R_ARM_TLS_DTPMOD32",  /* name */
335          TRUE,                  /* partial_inplace */
336          0xffffffff,            /* src_mask */
337          0xffffffff,            /* dst_mask */
338          FALSE),                /* pcrel_offset */
339
340   HOWTO (R_ARM_TLS_DTPOFF32,    /* type */
341          0,                     /* rightshift */
342          2,                     /* size (0 = byte, 1 = short, 2 = long) */
343          32,                    /* bitsize */
344          FALSE,                 /* pc_relative */
345          0,                     /* bitpos */
346          complain_overflow_bitfield,/* complain_on_overflow */
347          bfd_elf_generic_reloc, /* special_function */
348          "R_ARM_TLS_DTPOFF32",  /* name */
349          TRUE,                  /* partial_inplace */
350          0xffffffff,            /* src_mask */
351          0xffffffff,            /* dst_mask */
352          FALSE),                /* pcrel_offset */
353
354   HOWTO (R_ARM_TLS_TPOFF32,     /* type */
355          0,                     /* rightshift */
356          2,                     /* size (0 = byte, 1 = short, 2 = long) */
357          32,                    /* bitsize */
358          FALSE,                 /* pc_relative */
359          0,                     /* bitpos */
360          complain_overflow_bitfield,/* complain_on_overflow */
361          bfd_elf_generic_reloc, /* special_function */
362          "R_ARM_TLS_TPOFF32",   /* name */
363          TRUE,                  /* partial_inplace */
364          0xffffffff,            /* src_mask */
365          0xffffffff,            /* dst_mask */
366          FALSE),                /* pcrel_offset */
367
368   /* Relocs used in ARM Linux */
369
370   HOWTO (R_ARM_COPY,            /* type */
371          0,                     /* rightshift */
372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
373          32,                    /* bitsize */
374          FALSE,                 /* pc_relative */
375          0,                     /* bitpos */
376          complain_overflow_bitfield,/* complain_on_overflow */
377          bfd_elf_generic_reloc, /* special_function */
378          "R_ARM_COPY",          /* name */
379          TRUE,                  /* partial_inplace */
380          0xffffffff,            /* src_mask */
381          0xffffffff,            /* dst_mask */
382          FALSE),                /* pcrel_offset */
383
384   HOWTO (R_ARM_GLOB_DAT,        /* type */
385          0,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          32,                    /* bitsize */
388          FALSE,                 /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_bitfield,/* complain_on_overflow */
391          bfd_elf_generic_reloc, /* special_function */
392          "R_ARM_GLOB_DAT",      /* name */
393          TRUE,                  /* partial_inplace */
394          0xffffffff,            /* src_mask */
395          0xffffffff,            /* dst_mask */
396          FALSE),                /* pcrel_offset */
397
398   HOWTO (R_ARM_JUMP_SLOT,       /* type */
399          0,                     /* rightshift */
400          2,                     /* size (0 = byte, 1 = short, 2 = long) */
401          32,                    /* bitsize */
402          FALSE,                 /* pc_relative */
403          0,                     /* bitpos */
404          complain_overflow_bitfield,/* complain_on_overflow */
405          bfd_elf_generic_reloc, /* special_function */
406          "R_ARM_JUMP_SLOT",     /* name */
407          TRUE,                  /* partial_inplace */
408          0xffffffff,            /* src_mask */
409          0xffffffff,            /* dst_mask */
410          FALSE),                /* pcrel_offset */
411
412   HOWTO (R_ARM_RELATIVE,        /* type */
413          0,                     /* rightshift */
414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
415          32,                    /* bitsize */
416          FALSE,                 /* pc_relative */
417          0,                     /* bitpos */
418          complain_overflow_bitfield,/* complain_on_overflow */
419          bfd_elf_generic_reloc, /* special_function */
420          "R_ARM_RELATIVE",      /* name */
421          TRUE,                  /* partial_inplace */
422          0xffffffff,            /* src_mask */
423          0xffffffff,            /* dst_mask */
424          FALSE),                /* pcrel_offset */
425
426   HOWTO (R_ARM_GOTOFF32,        /* type */
427          0,                     /* rightshift */
428          2,                     /* size (0 = byte, 1 = short, 2 = long) */
429          32,                    /* bitsize */
430          FALSE,                 /* pc_relative */
431          0,                     /* bitpos */
432          complain_overflow_bitfield,/* complain_on_overflow */
433          bfd_elf_generic_reloc, /* special_function */
434          "R_ARM_GOTOFF32",      /* name */
435          TRUE,                  /* partial_inplace */
436          0xffffffff,            /* src_mask */
437          0xffffffff,            /* dst_mask */
438          FALSE),                /* pcrel_offset */
439
440   HOWTO (R_ARM_GOTPC,           /* type */
441          0,                     /* rightshift */
442          2,                     /* size (0 = byte, 1 = short, 2 = long) */
443          32,                    /* bitsize */
444          TRUE,                  /* pc_relative */
445          0,                     /* bitpos */
446          complain_overflow_bitfield,/* complain_on_overflow */
447          bfd_elf_generic_reloc, /* special_function */
448          "R_ARM_GOTPC",         /* name */
449          TRUE,                  /* partial_inplace */
450          0xffffffff,            /* src_mask */
451          0xffffffff,            /* dst_mask */
452          TRUE),                 /* pcrel_offset */
453
454   HOWTO (R_ARM_GOT32,           /* type */
455          0,                     /* rightshift */
456          2,                     /* size (0 = byte, 1 = short, 2 = long) */
457          32,                    /* bitsize */
458          FALSE,                 /* pc_relative */
459          0,                     /* bitpos */
460          complain_overflow_bitfield,/* complain_on_overflow */
461          bfd_elf_generic_reloc, /* special_function */
462          "R_ARM_GOT32",         /* name */
463          TRUE,                  /* partial_inplace */
464          0xffffffff,            /* src_mask */
465          0xffffffff,            /* dst_mask */
466          FALSE),                /* pcrel_offset */
467
468   HOWTO (R_ARM_PLT32,           /* type */
469          2,                     /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          24,                    /* bitsize */
472          TRUE,                  /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_bitfield,/* complain_on_overflow */
475          bfd_elf_generic_reloc, /* special_function */
476          "R_ARM_PLT32",         /* name */
477          FALSE,                 /* partial_inplace */
478          0x00ffffff,            /* src_mask */
479          0x00ffffff,            /* dst_mask */
480          TRUE),                 /* pcrel_offset */
481
482   HOWTO (R_ARM_CALL,            /* type */
483          2,                     /* rightshift */
484          2,                     /* size (0 = byte, 1 = short, 2 = long) */
485          24,                    /* bitsize */
486          TRUE,                  /* pc_relative */
487          0,                     /* bitpos */
488          complain_overflow_signed,/* complain_on_overflow */
489          bfd_elf_generic_reloc, /* special_function */
490          "R_ARM_CALL",          /* name */
491          FALSE,                 /* partial_inplace */
492          0x00ffffff,            /* src_mask */
493          0x00ffffff,            /* dst_mask */
494          TRUE),                 /* pcrel_offset */
495
496   HOWTO (R_ARM_JUMP24,          /* type */
497          2,                     /* rightshift */
498          2,                     /* size (0 = byte, 1 = short, 2 = long) */
499          24,                    /* bitsize */
500          TRUE,                  /* pc_relative */
501          0,                     /* bitpos */
502          complain_overflow_signed,/* complain_on_overflow */
503          bfd_elf_generic_reloc, /* special_function */
504          "R_ARM_JUMP24",        /* name */
505          FALSE,                 /* partial_inplace */
506          0x00ffffff,            /* src_mask */
507          0x00ffffff,            /* dst_mask */
508          TRUE),                 /* pcrel_offset */
509
510   HOWTO (R_ARM_THM_JUMP24,      /* type */
511          1,                     /* rightshift */
512          2,                     /* size (0 = byte, 1 = short, 2 = long) */
513          24,                    /* bitsize */
514          TRUE,                  /* pc_relative */
515          0,                     /* bitpos */
516          complain_overflow_signed,/* complain_on_overflow */
517          bfd_elf_generic_reloc, /* special_function */
518          "R_ARM_THM_JUMP24",    /* name */
519          FALSE,                 /* partial_inplace */
520          0x07ff2fff,            /* src_mask */
521          0x07ff2fff,            /* dst_mask */
522          TRUE),                 /* pcrel_offset */
523
524   HOWTO (R_ARM_BASE_ABS,        /* type */
525          0,                     /* rightshift */
526          2,                     /* size (0 = byte, 1 = short, 2 = long) */
527          32,                    /* bitsize */
528          FALSE,                 /* pc_relative */
529          0,                     /* bitpos */
530          complain_overflow_dont,/* complain_on_overflow */
531          bfd_elf_generic_reloc, /* special_function */
532          "R_ARM_BASE_ABS",      /* name */
533          FALSE,                 /* partial_inplace */
534          0xffffffff,            /* src_mask */
535          0xffffffff,            /* dst_mask */
536          FALSE),                /* pcrel_offset */
537
538   HOWTO (R_ARM_ALU_PCREL7_0,    /* type */
539          0,                     /* rightshift */
540          2,                     /* size (0 = byte, 1 = short, 2 = long) */
541          12,                    /* bitsize */
542          TRUE,                  /* pc_relative */
543          0,                     /* bitpos */
544          complain_overflow_dont,/* complain_on_overflow */
545          bfd_elf_generic_reloc, /* special_function */
546          "R_ARM_ALU_PCREL_7_0", /* name */
547          FALSE,                 /* partial_inplace */
548          0x00000fff,            /* src_mask */
549          0x00000fff,            /* dst_mask */
550          TRUE),                 /* pcrel_offset */
551
552   HOWTO (R_ARM_ALU_PCREL15_8,   /* type */
553          0,                     /* rightshift */
554          2,                     /* size (0 = byte, 1 = short, 2 = long) */
555          12,                    /* bitsize */
556          TRUE,                  /* pc_relative */
557          8,                     /* bitpos */
558          complain_overflow_dont,/* complain_on_overflow */
559          bfd_elf_generic_reloc, /* special_function */
560          "R_ARM_ALU_PCREL_15_8",/* name */
561          FALSE,                 /* partial_inplace */
562          0x00000fff,            /* src_mask */
563          0x00000fff,            /* dst_mask */
564          TRUE),                 /* pcrel_offset */
565
566   HOWTO (R_ARM_ALU_PCREL23_15,  /* type */
567          0,                     /* rightshift */
568          2,                     /* size (0 = byte, 1 = short, 2 = long) */
569          12,                    /* bitsize */
570          TRUE,                  /* pc_relative */
571          16,                    /* bitpos */
572          complain_overflow_dont,/* complain_on_overflow */
573          bfd_elf_generic_reloc, /* special_function */
574          "R_ARM_ALU_PCREL_23_15",/* name */
575          FALSE,                 /* partial_inplace */
576          0x00000fff,            /* src_mask */
577          0x00000fff,            /* dst_mask */
578          TRUE),                 /* pcrel_offset */
579
580   HOWTO (R_ARM_LDR_SBREL_11_0,  /* type */
581          0,                     /* rightshift */
582          2,                     /* size (0 = byte, 1 = short, 2 = long) */
583          12,                    /* bitsize */
584          FALSE,                 /* pc_relative */
585          0,                     /* bitpos */
586          complain_overflow_dont,/* complain_on_overflow */
587          bfd_elf_generic_reloc, /* special_function */
588          "R_ARM_LDR_SBREL_11_0",/* name */
589          FALSE,                 /* partial_inplace */
590          0x00000fff,            /* src_mask */
591          0x00000fff,            /* dst_mask */
592          FALSE),                /* pcrel_offset */
593
594   HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
595          0,                     /* rightshift */
596          2,                     /* size (0 = byte, 1 = short, 2 = long) */
597          8,                     /* bitsize */
598          FALSE,                 /* pc_relative */
599          12,                    /* bitpos */
600          complain_overflow_dont,/* complain_on_overflow */
601          bfd_elf_generic_reloc, /* special_function */
602          "R_ARM_ALU_SBREL_19_12",/* name */
603          FALSE,                 /* partial_inplace */
604          0x000ff000,            /* src_mask */
605          0x000ff000,            /* dst_mask */
606          FALSE),                /* pcrel_offset */
607
608   HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
609          0,                     /* rightshift */
610          2,                     /* size (0 = byte, 1 = short, 2 = long) */
611          8,                     /* bitsize */
612          FALSE,                 /* pc_relative */
613          20,                    /* bitpos */
614          complain_overflow_dont,/* complain_on_overflow */
615          bfd_elf_generic_reloc, /* special_function */
616          "R_ARM_ALU_SBREL_27_20",/* name */
617          FALSE,                 /* partial_inplace */
618          0x0ff00000,            /* src_mask */
619          0x0ff00000,            /* dst_mask */
620          FALSE),                /* pcrel_offset */
621
622   HOWTO (R_ARM_TARGET1,         /* type */
623          0,                     /* rightshift */
624          2,                     /* size (0 = byte, 1 = short, 2 = long) */
625          32,                    /* bitsize */
626          FALSE,                 /* pc_relative */
627          0,                     /* bitpos */
628          complain_overflow_dont,/* complain_on_overflow */
629          bfd_elf_generic_reloc, /* special_function */
630          "R_ARM_TARGET1",       /* name */
631          FALSE,                 /* partial_inplace */
632          0xffffffff,            /* src_mask */
633          0xffffffff,            /* dst_mask */
634          FALSE),                /* pcrel_offset */
635
636   HOWTO (R_ARM_ROSEGREL32,      /* type */
637          0,                     /* rightshift */
638          2,                     /* size (0 = byte, 1 = short, 2 = long) */
639          32,                    /* bitsize */
640          FALSE,                 /* pc_relative */
641          0,                     /* bitpos */
642          complain_overflow_dont,/* complain_on_overflow */
643          bfd_elf_generic_reloc, /* special_function */
644          "R_ARM_ROSEGREL32",    /* name */
645          FALSE,                 /* partial_inplace */
646          0xffffffff,            /* src_mask */
647          0xffffffff,            /* dst_mask */
648          FALSE),                /* pcrel_offset */
649
650   HOWTO (R_ARM_V4BX,            /* type */
651          0,                     /* rightshift */
652          2,                     /* size (0 = byte, 1 = short, 2 = long) */
653          32,                    /* bitsize */
654          FALSE,                 /* pc_relative */
655          0,                     /* bitpos */
656          complain_overflow_dont,/* complain_on_overflow */
657          bfd_elf_generic_reloc, /* special_function */
658          "R_ARM_V4BX",          /* name */
659          FALSE,                 /* partial_inplace */
660          0xffffffff,            /* src_mask */
661          0xffffffff,            /* dst_mask */
662          FALSE),                /* pcrel_offset */
663
664   HOWTO (R_ARM_TARGET2,         /* type */
665          0,                     /* rightshift */
666          2,                     /* size (0 = byte, 1 = short, 2 = long) */
667          32,                    /* bitsize */
668          FALSE,                 /* pc_relative */
669          0,                     /* bitpos */
670          complain_overflow_signed,/* complain_on_overflow */
671          bfd_elf_generic_reloc, /* special_function */
672          "R_ARM_TARGET2",       /* name */
673          FALSE,                 /* partial_inplace */
674          0xffffffff,            /* src_mask */
675          0xffffffff,            /* dst_mask */
676          TRUE),                 /* pcrel_offset */
677
678   HOWTO (R_ARM_PREL31,          /* type */
679          0,                     /* rightshift */
680          2,                     /* size (0 = byte, 1 = short, 2 = long) */
681          31,                    /* bitsize */
682          TRUE,                  /* pc_relative */
683          0,                     /* bitpos */
684          complain_overflow_signed,/* complain_on_overflow */
685          bfd_elf_generic_reloc, /* special_function */
686          "R_ARM_PREL31",        /* name */
687          FALSE,                 /* partial_inplace */
688          0x7fffffff,            /* src_mask */
689          0x7fffffff,            /* dst_mask */
690          TRUE),                 /* pcrel_offset */
691
692   HOWTO (R_ARM_MOVW_ABS_NC,     /* type */
693          0,                     /* rightshift */
694          2,                     /* size (0 = byte, 1 = short, 2 = long) */
695          16,                    /* bitsize */
696          FALSE,                 /* pc_relative */
697          0,                     /* bitpos */
698          complain_overflow_dont,/* complain_on_overflow */
699          bfd_elf_generic_reloc, /* special_function */
700          "R_ARM_MOVW_ABS_NC",   /* name */
701          FALSE,                 /* partial_inplace */
702          0x000f0fff,            /* src_mask */
703          0x000f0fff,            /* dst_mask */
704          FALSE),                /* pcrel_offset */
705
706   HOWTO (R_ARM_MOVT_ABS,        /* type */
707          0,                     /* rightshift */
708          2,                     /* size (0 = byte, 1 = short, 2 = long) */
709          16,                    /* bitsize */
710          FALSE,                 /* pc_relative */
711          0,                     /* bitpos */
712          complain_overflow_bitfield,/* complain_on_overflow */
713          bfd_elf_generic_reloc, /* special_function */
714          "R_ARM_MOVT_ABS",      /* name */
715          FALSE,                 /* partial_inplace */
716          0x000f0fff,            /* src_mask */
717          0x000f0fff,            /* dst_mask */
718          FALSE),                /* pcrel_offset */
719
720   HOWTO (R_ARM_MOVW_PREL_NC,    /* type */
721          0,                     /* rightshift */
722          2,                     /* size (0 = byte, 1 = short, 2 = long) */
723          16,                    /* bitsize */
724          TRUE,                  /* pc_relative */
725          0,                     /* bitpos */
726          complain_overflow_dont,/* complain_on_overflow */
727          bfd_elf_generic_reloc, /* special_function */
728          "R_ARM_MOVW_PREL_NC",  /* name */
729          FALSE,                 /* partial_inplace */
730          0x000f0fff,            /* src_mask */
731          0x000f0fff,            /* dst_mask */
732          TRUE),                 /* pcrel_offset */
733
734   HOWTO (R_ARM_MOVT_PREL,       /* type */
735          0,                     /* rightshift */
736          2,                     /* size (0 = byte, 1 = short, 2 = long) */
737          16,                    /* bitsize */
738          TRUE,                  /* pc_relative */
739          0,                     /* bitpos */
740          complain_overflow_bitfield,/* complain_on_overflow */
741          bfd_elf_generic_reloc, /* special_function */
742          "R_ARM_MOVT_PREL",     /* name */
743          FALSE,                 /* partial_inplace */
744          0x000f0fff,            /* src_mask */
745          0x000f0fff,            /* dst_mask */
746          TRUE),                 /* pcrel_offset */
747
748   HOWTO (R_ARM_THM_MOVW_ABS_NC, /* type */
749          0,                     /* rightshift */
750          2,                     /* size (0 = byte, 1 = short, 2 = long) */
751          16,                    /* bitsize */
752          FALSE,                 /* pc_relative */
753          0,                     /* bitpos */
754          complain_overflow_dont,/* complain_on_overflow */
755          bfd_elf_generic_reloc, /* special_function */
756          "R_ARM_THM_MOVW_ABS_NC",/* name */
757          FALSE,                 /* partial_inplace */
758          0x040f70ff,            /* src_mask */
759          0x040f70ff,            /* dst_mask */
760          FALSE),                /* pcrel_offset */
761
762   HOWTO (R_ARM_THM_MOVT_ABS,    /* type */
763          0,                     /* rightshift */
764          2,                     /* size (0 = byte, 1 = short, 2 = long) */
765          16,                    /* bitsize */
766          FALSE,                 /* pc_relative */
767          0,                     /* bitpos */
768          complain_overflow_bitfield,/* complain_on_overflow */
769          bfd_elf_generic_reloc, /* special_function */
770          "R_ARM_THM_MOVT_ABS",  /* name */
771          FALSE,                 /* partial_inplace */
772          0x040f70ff,            /* src_mask */
773          0x040f70ff,            /* dst_mask */
774          FALSE),                /* pcrel_offset */
775
776   HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
777          0,                     /* rightshift */
778          2,                     /* size (0 = byte, 1 = short, 2 = long) */
779          16,                    /* bitsize */
780          TRUE,                  /* pc_relative */
781          0,                     /* bitpos */
782          complain_overflow_dont,/* complain_on_overflow */
783          bfd_elf_generic_reloc, /* special_function */
784          "R_ARM_THM_MOVW_PREL_NC",/* name */
785          FALSE,                 /* partial_inplace */
786          0x040f70ff,            /* src_mask */
787          0x040f70ff,            /* dst_mask */
788          TRUE),                 /* pcrel_offset */
789
790   HOWTO (R_ARM_THM_MOVT_PREL,   /* type */
791          0,                     /* rightshift */
792          2,                     /* size (0 = byte, 1 = short, 2 = long) */
793          16,                    /* bitsize */
794          TRUE,                  /* pc_relative */
795          0,                     /* bitpos */
796          complain_overflow_bitfield,/* complain_on_overflow */
797          bfd_elf_generic_reloc, /* special_function */
798          "R_ARM_THM_MOVT_PREL", /* name */
799          FALSE,                 /* partial_inplace */
800          0x040f70ff,            /* src_mask */
801          0x040f70ff,            /* dst_mask */
802          TRUE),                 /* pcrel_offset */
803
804   HOWTO (R_ARM_THM_JUMP19,      /* type */
805          1,                     /* rightshift */
806          2,                     /* size (0 = byte, 1 = short, 2 = long) */
807          19,                    /* bitsize */
808          TRUE,                  /* pc_relative */
809          0,                     /* bitpos */
810          complain_overflow_signed,/* complain_on_overflow */
811          bfd_elf_generic_reloc, /* special_function */
812          "R_ARM_THM_JUMP19",    /* name */
813          FALSE,                 /* partial_inplace */
814          0x043f2fff,            /* src_mask */
815          0x043f2fff,            /* dst_mask */
816          TRUE),                 /* pcrel_offset */
817
818   HOWTO (R_ARM_THM_JUMP6,       /* type */
819          1,                     /* rightshift */
820          1,                     /* size (0 = byte, 1 = short, 2 = long) */
821          6,                     /* bitsize */
822          TRUE,                  /* pc_relative */
823          0,                     /* bitpos */
824          complain_overflow_unsigned,/* complain_on_overflow */
825          bfd_elf_generic_reloc, /* special_function */
826          "R_ARM_THM_JUMP6",     /* name */
827          FALSE,                 /* partial_inplace */
828          0x02f8,                /* src_mask */
829          0x02f8,                /* dst_mask */
830          TRUE),                 /* pcrel_offset */
831
832   /* These are declared as 13-bit signed relocations because we can
833      address -4095 .. 4095(base) by altering ADDW to SUBW or vice
834      versa.  */
835   HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
836          0,                     /* rightshift */
837          2,                     /* size (0 = byte, 1 = short, 2 = long) */
838          13,                    /* bitsize */
839          TRUE,                  /* pc_relative */
840          0,                     /* bitpos */
841          complain_overflow_dont,/* complain_on_overflow */
842          bfd_elf_generic_reloc, /* special_function */
843          "R_ARM_THM_ALU_PREL_11_0",/* name */
844          FALSE,                 /* partial_inplace */
845          0xffffffff,            /* src_mask */
846          0xffffffff,            /* dst_mask */
847          TRUE),                 /* pcrel_offset */
848
849   HOWTO (R_ARM_THM_PC12,        /* type */
850          0,                     /* rightshift */
851          2,                     /* size (0 = byte, 1 = short, 2 = long) */
852          13,                    /* bitsize */
853          TRUE,                  /* pc_relative */
854          0,                     /* bitpos */
855          complain_overflow_dont,/* complain_on_overflow */
856          bfd_elf_generic_reloc, /* special_function */
857          "R_ARM_THM_PC12",      /* name */
858          FALSE,                 /* partial_inplace */
859          0xffffffff,            /* src_mask */
860          0xffffffff,            /* dst_mask */
861          TRUE),                 /* pcrel_offset */
862
863   HOWTO (R_ARM_ABS32_NOI,       /* type */
864          0,                     /* rightshift */
865          2,                     /* size (0 = byte, 1 = short, 2 = long) */
866          32,                    /* bitsize */
867          FALSE,                 /* pc_relative */
868          0,                     /* bitpos */
869          complain_overflow_dont,/* complain_on_overflow */
870          bfd_elf_generic_reloc, /* special_function */
871          "R_ARM_ABS32_NOI",     /* name */
872          FALSE,                 /* partial_inplace */
873          0xffffffff,            /* src_mask */
874          0xffffffff,            /* dst_mask */
875          FALSE),                /* pcrel_offset */
876
877   HOWTO (R_ARM_REL32_NOI,       /* type */
878          0,                     /* rightshift */
879          2,                     /* size (0 = byte, 1 = short, 2 = long) */
880          32,                    /* bitsize */
881          TRUE,                  /* pc_relative */
882          0,                     /* bitpos */
883          complain_overflow_dont,/* complain_on_overflow */
884          bfd_elf_generic_reloc, /* special_function */
885          "R_ARM_REL32_NOI",     /* name */
886          FALSE,                 /* partial_inplace */
887          0xffffffff,            /* src_mask */
888          0xffffffff,            /* dst_mask */
889          FALSE),                /* pcrel_offset */
890
891   /* Group relocations.  */
892
893   HOWTO (R_ARM_ALU_PC_G0_NC,    /* type */
894          0,                     /* rightshift */
895          2,                     /* size (0 = byte, 1 = short, 2 = long) */
896          32,                    /* bitsize */
897          TRUE,                  /* pc_relative */
898          0,                     /* bitpos */
899          complain_overflow_dont,/* complain_on_overflow */
900          bfd_elf_generic_reloc, /* special_function */
901          "R_ARM_ALU_PC_G0_NC",  /* name */
902          FALSE,                 /* partial_inplace */
903          0xffffffff,            /* src_mask */
904          0xffffffff,            /* dst_mask */
905          TRUE),                 /* pcrel_offset */
906
907   HOWTO (R_ARM_ALU_PC_G0,       /* type */
908          0,                     /* rightshift */
909          2,                     /* size (0 = byte, 1 = short, 2 = long) */
910          32,                    /* bitsize */
911          TRUE,                  /* pc_relative */
912          0,                     /* bitpos */
913          complain_overflow_dont,/* complain_on_overflow */
914          bfd_elf_generic_reloc, /* special_function */
915          "R_ARM_ALU_PC_G0",     /* name */
916          FALSE,                 /* partial_inplace */
917          0xffffffff,            /* src_mask */
918          0xffffffff,            /* dst_mask */
919          TRUE),                 /* pcrel_offset */
920
921   HOWTO (R_ARM_ALU_PC_G1_NC,    /* type */
922          0,                     /* rightshift */
923          2,                     /* size (0 = byte, 1 = short, 2 = long) */
924          32,                    /* bitsize */
925          TRUE,                  /* pc_relative */
926          0,                     /* bitpos */
927          complain_overflow_dont,/* complain_on_overflow */
928          bfd_elf_generic_reloc, /* special_function */
929          "R_ARM_ALU_PC_G1_NC",  /* name */
930          FALSE,                 /* partial_inplace */
931          0xffffffff,            /* src_mask */
932          0xffffffff,            /* dst_mask */
933          TRUE),                 /* pcrel_offset */
934
935   HOWTO (R_ARM_ALU_PC_G1,       /* type */
936          0,                     /* rightshift */
937          2,                     /* size (0 = byte, 1 = short, 2 = long) */
938          32,                    /* bitsize */
939          TRUE,                  /* pc_relative */
940          0,                     /* bitpos */
941          complain_overflow_dont,/* complain_on_overflow */
942          bfd_elf_generic_reloc, /* special_function */
943          "R_ARM_ALU_PC_G1",     /* name */
944          FALSE,                 /* partial_inplace */
945          0xffffffff,            /* src_mask */
946          0xffffffff,            /* dst_mask */
947          TRUE),                 /* pcrel_offset */
948
949   HOWTO (R_ARM_ALU_PC_G2,       /* type */
950          0,                     /* rightshift */
951          2,                     /* size (0 = byte, 1 = short, 2 = long) */
952          32,                    /* bitsize */
953          TRUE,                  /* pc_relative */
954          0,                     /* bitpos */
955          complain_overflow_dont,/* complain_on_overflow */
956          bfd_elf_generic_reloc, /* special_function */
957          "R_ARM_ALU_PC_G2",     /* name */
958          FALSE,                 /* partial_inplace */
959          0xffffffff,            /* src_mask */
960          0xffffffff,            /* dst_mask */
961          TRUE),                 /* pcrel_offset */
962
963   HOWTO (R_ARM_LDR_PC_G1,       /* type */
964          0,                     /* rightshift */
965          2,                     /* size (0 = byte, 1 = short, 2 = long) */
966          32,                    /* bitsize */
967          TRUE,                  /* pc_relative */
968          0,                     /* bitpos */
969          complain_overflow_dont,/* complain_on_overflow */
970          bfd_elf_generic_reloc, /* special_function */
971          "R_ARM_LDR_PC_G1",     /* name */
972          FALSE,                 /* partial_inplace */
973          0xffffffff,            /* src_mask */
974          0xffffffff,            /* dst_mask */
975          TRUE),                 /* pcrel_offset */
976
977   HOWTO (R_ARM_LDR_PC_G2,       /* type */
978          0,                     /* rightshift */
979          2,                     /* size (0 = byte, 1 = short, 2 = long) */
980          32,                    /* bitsize */
981          TRUE,                  /* pc_relative */
982          0,                     /* bitpos */
983          complain_overflow_dont,/* complain_on_overflow */
984          bfd_elf_generic_reloc, /* special_function */
985          "R_ARM_LDR_PC_G2",     /* name */
986          FALSE,                 /* partial_inplace */
987          0xffffffff,            /* src_mask */
988          0xffffffff,            /* dst_mask */
989          TRUE),                 /* pcrel_offset */
990
991   HOWTO (R_ARM_LDRS_PC_G0,      /* type */
992          0,                     /* rightshift */
993          2,                     /* size (0 = byte, 1 = short, 2 = long) */
994          32,                    /* bitsize */
995          TRUE,                  /* pc_relative */
996          0,                     /* bitpos */
997          complain_overflow_dont,/* complain_on_overflow */
998          bfd_elf_generic_reloc, /* special_function */
999          "R_ARM_LDRS_PC_G0",    /* name */
1000          FALSE,                 /* partial_inplace */
1001          0xffffffff,            /* src_mask */
1002          0xffffffff,            /* dst_mask */
1003          TRUE),                 /* pcrel_offset */
1004
1005   HOWTO (R_ARM_LDRS_PC_G1,      /* type */
1006          0,                     /* rightshift */
1007          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1008          32,                    /* bitsize */
1009          TRUE,                  /* pc_relative */
1010          0,                     /* bitpos */
1011          complain_overflow_dont,/* complain_on_overflow */
1012          bfd_elf_generic_reloc, /* special_function */
1013          "R_ARM_LDRS_PC_G1",    /* name */
1014          FALSE,                 /* partial_inplace */
1015          0xffffffff,            /* src_mask */
1016          0xffffffff,            /* dst_mask */
1017          TRUE),                 /* pcrel_offset */
1018
1019   HOWTO (R_ARM_LDRS_PC_G2,      /* type */
1020          0,                     /* rightshift */
1021          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1022          32,                    /* bitsize */
1023          TRUE,                  /* pc_relative */
1024          0,                     /* bitpos */
1025          complain_overflow_dont,/* complain_on_overflow */
1026          bfd_elf_generic_reloc, /* special_function */
1027          "R_ARM_LDRS_PC_G2",    /* name */
1028          FALSE,                 /* partial_inplace */
1029          0xffffffff,            /* src_mask */
1030          0xffffffff,            /* dst_mask */
1031          TRUE),                 /* pcrel_offset */
1032
1033   HOWTO (R_ARM_LDC_PC_G0,       /* type */
1034          0,                     /* rightshift */
1035          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1036          32,                    /* bitsize */
1037          TRUE,                  /* pc_relative */
1038          0,                     /* bitpos */
1039          complain_overflow_dont,/* complain_on_overflow */
1040          bfd_elf_generic_reloc, /* special_function */
1041          "R_ARM_LDC_PC_G0",     /* name */
1042          FALSE,                 /* partial_inplace */
1043          0xffffffff,            /* src_mask */
1044          0xffffffff,            /* dst_mask */
1045          TRUE),                 /* pcrel_offset */
1046
1047   HOWTO (R_ARM_LDC_PC_G1,       /* type */
1048          0,                     /* rightshift */
1049          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1050          32,                    /* bitsize */
1051          TRUE,                  /* pc_relative */
1052          0,                     /* bitpos */
1053          complain_overflow_dont,/* complain_on_overflow */
1054          bfd_elf_generic_reloc, /* special_function */
1055          "R_ARM_LDC_PC_G1",     /* name */
1056          FALSE,                 /* partial_inplace */
1057          0xffffffff,            /* src_mask */
1058          0xffffffff,            /* dst_mask */
1059          TRUE),                 /* pcrel_offset */
1060
1061   HOWTO (R_ARM_LDC_PC_G2,       /* type */
1062          0,                     /* rightshift */
1063          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1064          32,                    /* bitsize */
1065          TRUE,                  /* pc_relative */
1066          0,                     /* bitpos */
1067          complain_overflow_dont,/* complain_on_overflow */
1068          bfd_elf_generic_reloc, /* special_function */
1069          "R_ARM_LDC_PC_G2",     /* name */
1070          FALSE,                 /* partial_inplace */
1071          0xffffffff,            /* src_mask */
1072          0xffffffff,            /* dst_mask */
1073          TRUE),                 /* pcrel_offset */
1074
1075   HOWTO (R_ARM_ALU_SB_G0_NC,    /* type */
1076          0,                     /* rightshift */
1077          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1078          32,                    /* bitsize */
1079          TRUE,                  /* pc_relative */
1080          0,                     /* bitpos */
1081          complain_overflow_dont,/* complain_on_overflow */
1082          bfd_elf_generic_reloc, /* special_function */
1083          "R_ARM_ALU_SB_G0_NC",  /* name */
1084          FALSE,                 /* partial_inplace */
1085          0xffffffff,            /* src_mask */
1086          0xffffffff,            /* dst_mask */
1087          TRUE),                 /* pcrel_offset */
1088
1089   HOWTO (R_ARM_ALU_SB_G0,       /* type */
1090          0,                     /* rightshift */
1091          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1092          32,                    /* bitsize */
1093          TRUE,                  /* pc_relative */
1094          0,                     /* bitpos */
1095          complain_overflow_dont,/* complain_on_overflow */
1096          bfd_elf_generic_reloc, /* special_function */
1097          "R_ARM_ALU_SB_G0",     /* name */
1098          FALSE,                 /* partial_inplace */
1099          0xffffffff,            /* src_mask */
1100          0xffffffff,            /* dst_mask */
1101          TRUE),                 /* pcrel_offset */
1102
1103   HOWTO (R_ARM_ALU_SB_G1_NC,    /* type */
1104          0,                     /* rightshift */
1105          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1106          32,                    /* bitsize */
1107          TRUE,                  /* pc_relative */
1108          0,                     /* bitpos */
1109          complain_overflow_dont,/* complain_on_overflow */
1110          bfd_elf_generic_reloc, /* special_function */
1111          "R_ARM_ALU_SB_G1_NC",  /* name */
1112          FALSE,                 /* partial_inplace */
1113          0xffffffff,            /* src_mask */
1114          0xffffffff,            /* dst_mask */
1115          TRUE),                 /* pcrel_offset */
1116
1117   HOWTO (R_ARM_ALU_SB_G1,       /* type */
1118          0,                     /* rightshift */
1119          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1120          32,                    /* bitsize */
1121          TRUE,                  /* pc_relative */
1122          0,                     /* bitpos */
1123          complain_overflow_dont,/* complain_on_overflow */
1124          bfd_elf_generic_reloc, /* special_function */
1125          "R_ARM_ALU_SB_G1",     /* name */
1126          FALSE,                 /* partial_inplace */
1127          0xffffffff,            /* src_mask */
1128          0xffffffff,            /* dst_mask */
1129          TRUE),                 /* pcrel_offset */
1130
1131   HOWTO (R_ARM_ALU_SB_G2,       /* type */
1132          0,                     /* rightshift */
1133          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1134          32,                    /* bitsize */
1135          TRUE,                  /* pc_relative */
1136          0,                     /* bitpos */
1137          complain_overflow_dont,/* complain_on_overflow */
1138          bfd_elf_generic_reloc, /* special_function */
1139          "R_ARM_ALU_SB_G2",     /* name */
1140          FALSE,                 /* partial_inplace */
1141          0xffffffff,            /* src_mask */
1142          0xffffffff,            /* dst_mask */
1143          TRUE),                 /* pcrel_offset */
1144
1145   HOWTO (R_ARM_LDR_SB_G0,       /* type */
1146          0,                     /* rightshift */
1147          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1148          32,                    /* bitsize */
1149          TRUE,                  /* pc_relative */
1150          0,                     /* bitpos */
1151          complain_overflow_dont,/* complain_on_overflow */
1152          bfd_elf_generic_reloc, /* special_function */
1153          "R_ARM_LDR_SB_G0",     /* name */
1154          FALSE,                 /* partial_inplace */
1155          0xffffffff,            /* src_mask */
1156          0xffffffff,            /* dst_mask */
1157          TRUE),                 /* pcrel_offset */
1158
1159   HOWTO (R_ARM_LDR_SB_G1,       /* type */
1160          0,                     /* rightshift */
1161          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1162          32,                    /* bitsize */
1163          TRUE,                  /* pc_relative */
1164          0,                     /* bitpos */
1165          complain_overflow_dont,/* complain_on_overflow */
1166          bfd_elf_generic_reloc, /* special_function */
1167          "R_ARM_LDR_SB_G1",     /* name */
1168          FALSE,                 /* partial_inplace */
1169          0xffffffff,            /* src_mask */
1170          0xffffffff,            /* dst_mask */
1171          TRUE),                 /* pcrel_offset */
1172
1173   HOWTO (R_ARM_LDR_SB_G2,       /* type */
1174          0,                     /* rightshift */
1175          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1176          32,                    /* bitsize */
1177          TRUE,                  /* pc_relative */
1178          0,                     /* bitpos */
1179          complain_overflow_dont,/* complain_on_overflow */
1180          bfd_elf_generic_reloc, /* special_function */
1181          "R_ARM_LDR_SB_G2",     /* name */
1182          FALSE,                 /* partial_inplace */
1183          0xffffffff,            /* src_mask */
1184          0xffffffff,            /* dst_mask */
1185          TRUE),                 /* pcrel_offset */
1186
1187   HOWTO (R_ARM_LDRS_SB_G0,      /* type */
1188          0,                     /* rightshift */
1189          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1190          32,                    /* bitsize */
1191          TRUE,                  /* pc_relative */
1192          0,                     /* bitpos */
1193          complain_overflow_dont,/* complain_on_overflow */
1194          bfd_elf_generic_reloc, /* special_function */
1195          "R_ARM_LDRS_SB_G0",    /* name */
1196          FALSE,                 /* partial_inplace */
1197          0xffffffff,            /* src_mask */
1198          0xffffffff,            /* dst_mask */
1199          TRUE),                 /* pcrel_offset */
1200
1201   HOWTO (R_ARM_LDRS_SB_G1,      /* type */
1202          0,                     /* rightshift */
1203          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1204          32,                    /* bitsize */
1205          TRUE,                  /* pc_relative */
1206          0,                     /* bitpos */
1207          complain_overflow_dont,/* complain_on_overflow */
1208          bfd_elf_generic_reloc, /* special_function */
1209          "R_ARM_LDRS_SB_G1",    /* name */
1210          FALSE,                 /* partial_inplace */
1211          0xffffffff,            /* src_mask */
1212          0xffffffff,            /* dst_mask */
1213          TRUE),                 /* pcrel_offset */
1214
1215   HOWTO (R_ARM_LDRS_SB_G2,      /* type */
1216          0,                     /* rightshift */
1217          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1218          32,                    /* bitsize */
1219          TRUE,                  /* pc_relative */
1220          0,                     /* bitpos */
1221          complain_overflow_dont,/* complain_on_overflow */
1222          bfd_elf_generic_reloc, /* special_function */
1223          "R_ARM_LDRS_SB_G2",    /* name */
1224          FALSE,                 /* partial_inplace */
1225          0xffffffff,            /* src_mask */
1226          0xffffffff,            /* dst_mask */
1227          TRUE),                 /* pcrel_offset */
1228
1229   HOWTO (R_ARM_LDC_SB_G0,       /* type */
1230          0,                     /* rightshift */
1231          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1232          32,                    /* bitsize */
1233          TRUE,                  /* pc_relative */
1234          0,                     /* bitpos */
1235          complain_overflow_dont,/* complain_on_overflow */
1236          bfd_elf_generic_reloc, /* special_function */
1237          "R_ARM_LDC_SB_G0",     /* name */
1238          FALSE,                 /* partial_inplace */
1239          0xffffffff,            /* src_mask */
1240          0xffffffff,            /* dst_mask */
1241          TRUE),                 /* pcrel_offset */
1242
1243   HOWTO (R_ARM_LDC_SB_G1,       /* type */
1244          0,                     /* rightshift */
1245          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1246          32,                    /* bitsize */
1247          TRUE,                  /* pc_relative */
1248          0,                     /* bitpos */
1249          complain_overflow_dont,/* complain_on_overflow */
1250          bfd_elf_generic_reloc, /* special_function */
1251          "R_ARM_LDC_SB_G1",     /* name */
1252          FALSE,                 /* partial_inplace */
1253          0xffffffff,            /* src_mask */
1254          0xffffffff,            /* dst_mask */
1255          TRUE),                 /* pcrel_offset */
1256
1257   HOWTO (R_ARM_LDC_SB_G2,       /* type */
1258          0,                     /* rightshift */
1259          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1260          32,                    /* bitsize */
1261          TRUE,                  /* pc_relative */
1262          0,                     /* bitpos */
1263          complain_overflow_dont,/* complain_on_overflow */
1264          bfd_elf_generic_reloc, /* special_function */
1265          "R_ARM_LDC_SB_G2",     /* name */
1266          FALSE,                 /* partial_inplace */
1267          0xffffffff,            /* src_mask */
1268          0xffffffff,            /* dst_mask */
1269          TRUE),                 /* pcrel_offset */
1270
1271   /* End of group relocations.  */
1272
1273   HOWTO (R_ARM_MOVW_BREL_NC,    /* type */
1274          0,                     /* rightshift */
1275          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1276          16,                    /* bitsize */
1277          FALSE,                 /* pc_relative */
1278          0,                     /* bitpos */
1279          complain_overflow_dont,/* complain_on_overflow */
1280          bfd_elf_generic_reloc, /* special_function */
1281          "R_ARM_MOVW_BREL_NC",  /* name */
1282          FALSE,                 /* partial_inplace */
1283          0x0000ffff,            /* src_mask */
1284          0x0000ffff,            /* dst_mask */
1285          FALSE),                /* pcrel_offset */
1286
1287   HOWTO (R_ARM_MOVT_BREL,       /* type */
1288          0,                     /* rightshift */
1289          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1290          16,                    /* bitsize */
1291          FALSE,                 /* pc_relative */
1292          0,                     /* bitpos */
1293          complain_overflow_bitfield,/* complain_on_overflow */
1294          bfd_elf_generic_reloc, /* special_function */
1295          "R_ARM_MOVT_BREL",     /* name */
1296          FALSE,                 /* partial_inplace */
1297          0x0000ffff,            /* src_mask */
1298          0x0000ffff,            /* dst_mask */
1299          FALSE),                /* pcrel_offset */
1300
1301   HOWTO (R_ARM_MOVW_BREL,       /* type */
1302          0,                     /* rightshift */
1303          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1304          16,                    /* bitsize */
1305          FALSE,                 /* pc_relative */
1306          0,                     /* bitpos */
1307          complain_overflow_dont,/* complain_on_overflow */
1308          bfd_elf_generic_reloc, /* special_function */
1309          "R_ARM_MOVW_BREL",     /* name */
1310          FALSE,                 /* partial_inplace */
1311          0x0000ffff,            /* src_mask */
1312          0x0000ffff,            /* dst_mask */
1313          FALSE),                /* pcrel_offset */
1314
1315   HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
1316          0,                     /* rightshift */
1317          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1318          16,                    /* bitsize */
1319          FALSE,                 /* pc_relative */
1320          0,                     /* bitpos */
1321          complain_overflow_dont,/* complain_on_overflow */
1322          bfd_elf_generic_reloc, /* special_function */
1323          "R_ARM_THM_MOVW_BREL_NC",/* name */
1324          FALSE,                 /* partial_inplace */
1325          0x040f70ff,            /* src_mask */
1326          0x040f70ff,            /* dst_mask */
1327          FALSE),                /* pcrel_offset */
1328
1329   HOWTO (R_ARM_THM_MOVT_BREL,   /* type */
1330          0,                     /* rightshift */
1331          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1332          16,                    /* bitsize */
1333          FALSE,                 /* pc_relative */
1334          0,                     /* bitpos */
1335          complain_overflow_bitfield,/* complain_on_overflow */
1336          bfd_elf_generic_reloc, /* special_function */
1337          "R_ARM_THM_MOVT_BREL", /* name */
1338          FALSE,                 /* partial_inplace */
1339          0x040f70ff,            /* src_mask */
1340          0x040f70ff,            /* dst_mask */
1341          FALSE),                /* pcrel_offset */
1342
1343   HOWTO (R_ARM_THM_MOVW_BREL,   /* type */
1344          0,                     /* rightshift */
1345          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1346          16,                    /* bitsize */
1347          FALSE,                 /* pc_relative */
1348          0,                     /* bitpos */
1349          complain_overflow_dont,/* complain_on_overflow */
1350          bfd_elf_generic_reloc, /* special_function */
1351          "R_ARM_THM_MOVW_BREL", /* name */
1352          FALSE,                 /* partial_inplace */
1353          0x040f70ff,            /* src_mask */
1354          0x040f70ff,            /* dst_mask */
1355          FALSE),                /* pcrel_offset */
1356
1357   EMPTY_HOWTO (90),   /* Unallocated.  */
1358   EMPTY_HOWTO (91),
1359   EMPTY_HOWTO (92),
1360   EMPTY_HOWTO (93),
1361
1362   HOWTO (R_ARM_PLT32_ABS,       /* type */
1363          0,                     /* rightshift */
1364          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1365          32,                    /* bitsize */
1366          FALSE,                 /* pc_relative */
1367          0,                     /* bitpos */
1368          complain_overflow_dont,/* complain_on_overflow */
1369          bfd_elf_generic_reloc, /* special_function */
1370          "R_ARM_PLT32_ABS",     /* name */
1371          FALSE,                 /* partial_inplace */
1372          0xffffffff,            /* src_mask */
1373          0xffffffff,            /* dst_mask */
1374          FALSE),                /* pcrel_offset */
1375
1376   HOWTO (R_ARM_GOT_ABS,         /* type */
1377          0,                     /* rightshift */
1378          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1379          32,                    /* bitsize */
1380          FALSE,                 /* pc_relative */
1381          0,                     /* bitpos */
1382          complain_overflow_dont,/* complain_on_overflow */
1383          bfd_elf_generic_reloc, /* special_function */
1384          "R_ARM_GOT_ABS",       /* name */
1385          FALSE,                 /* partial_inplace */
1386          0xffffffff,            /* src_mask */
1387          0xffffffff,            /* dst_mask */
1388          FALSE),                        /* pcrel_offset */
1389
1390   HOWTO (R_ARM_GOT_PREL,        /* type */
1391          0,                     /* rightshift */
1392          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1393          32,                    /* bitsize */
1394          TRUE,                  /* pc_relative */
1395          0,                     /* bitpos */
1396          complain_overflow_dont,        /* complain_on_overflow */
1397          bfd_elf_generic_reloc, /* special_function */
1398          "R_ARM_GOT_PREL",      /* name */
1399          FALSE,                 /* partial_inplace */
1400          0xffffffff,            /* src_mask */
1401          0xffffffff,            /* dst_mask */
1402          TRUE),                 /* pcrel_offset */
1403
1404   HOWTO (R_ARM_GOT_BREL12,      /* type */
1405          0,                     /* rightshift */
1406          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1407          12,                    /* bitsize */
1408          FALSE,                 /* pc_relative */
1409          0,                     /* bitpos */
1410          complain_overflow_bitfield,/* complain_on_overflow */
1411          bfd_elf_generic_reloc, /* special_function */
1412          "R_ARM_GOT_BREL12",    /* name */
1413          FALSE,                 /* partial_inplace */
1414          0x00000fff,            /* src_mask */
1415          0x00000fff,            /* dst_mask */
1416          FALSE),                /* pcrel_offset */
1417
1418   HOWTO (R_ARM_GOTOFF12,        /* type */
1419          0,                     /* rightshift */
1420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1421          12,                    /* bitsize */
1422          FALSE,                 /* pc_relative */
1423          0,                     /* bitpos */
1424          complain_overflow_bitfield,/* complain_on_overflow */
1425          bfd_elf_generic_reloc, /* special_function */
1426          "R_ARM_GOTOFF12",      /* name */
1427          FALSE,                 /* partial_inplace */
1428          0x00000fff,            /* src_mask */
1429          0x00000fff,            /* dst_mask */
1430          FALSE),                /* pcrel_offset */
1431
1432   EMPTY_HOWTO (R_ARM_GOTRELAX),  /* reserved for future GOT-load optimizations */
1433
1434   /* GNU extension to record C++ vtable member usage */
1435   HOWTO (R_ARM_GNU_VTENTRY,     /* type */
1436          0,                     /* rightshift */
1437          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1438          0,                     /* bitsize */
1439          FALSE,                 /* pc_relative */
1440          0,                     /* bitpos */
1441          complain_overflow_dont, /* complain_on_overflow */
1442          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
1443          "R_ARM_GNU_VTENTRY",   /* name */
1444          FALSE,                 /* partial_inplace */
1445          0,                     /* src_mask */
1446          0,                     /* dst_mask */
1447          FALSE),                /* pcrel_offset */
1448
1449   /* GNU extension to record C++ vtable hierarchy */
1450   HOWTO (R_ARM_GNU_VTINHERIT, /* type */
1451          0,                     /* rightshift */
1452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1453          0,                     /* bitsize */
1454          FALSE,                 /* pc_relative */
1455          0,                     /* bitpos */
1456          complain_overflow_dont, /* complain_on_overflow */
1457          NULL,                  /* special_function */
1458          "R_ARM_GNU_VTINHERIT", /* name */
1459          FALSE,                 /* partial_inplace */
1460          0,                     /* src_mask */
1461          0,                     /* dst_mask */
1462          FALSE),                /* pcrel_offset */
1463
1464   HOWTO (R_ARM_THM_JUMP11,      /* type */
1465          1,                     /* rightshift */
1466          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1467          11,                    /* bitsize */
1468          TRUE,                  /* pc_relative */
1469          0,                     /* bitpos */
1470          complain_overflow_signed,      /* complain_on_overflow */
1471          bfd_elf_generic_reloc, /* special_function */
1472          "R_ARM_THM_JUMP11",    /* name */
1473          FALSE,                 /* partial_inplace */
1474          0x000007ff,            /* src_mask */
1475          0x000007ff,            /* dst_mask */
1476          TRUE),                 /* pcrel_offset */
1477
1478   HOWTO (R_ARM_THM_JUMP8,       /* type */
1479          1,                     /* rightshift */
1480          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1481          8,                     /* bitsize */
1482          TRUE,                  /* pc_relative */
1483          0,                     /* bitpos */
1484          complain_overflow_signed,      /* complain_on_overflow */
1485          bfd_elf_generic_reloc, /* special_function */
1486          "R_ARM_THM_JUMP8",     /* name */
1487          FALSE,                 /* partial_inplace */
1488          0x000000ff,            /* src_mask */
1489          0x000000ff,            /* dst_mask */
1490          TRUE),                 /* pcrel_offset */
1491
1492   /* TLS relocations */
1493   HOWTO (R_ARM_TLS_GD32,        /* type */
1494          0,                     /* rightshift */
1495          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1496          32,                    /* bitsize */
1497          FALSE,                 /* pc_relative */
1498          0,                     /* bitpos */
1499          complain_overflow_bitfield,/* complain_on_overflow */
1500          NULL,                  /* special_function */
1501          "R_ARM_TLS_GD32",      /* name */
1502          TRUE,                  /* partial_inplace */
1503          0xffffffff,            /* src_mask */
1504          0xffffffff,            /* dst_mask */
1505          FALSE),                /* pcrel_offset */
1506
1507   HOWTO (R_ARM_TLS_LDM32,       /* type */
1508          0,                     /* rightshift */
1509          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1510          32,                    /* bitsize */
1511          FALSE,                 /* pc_relative */
1512          0,                     /* bitpos */
1513          complain_overflow_bitfield,/* complain_on_overflow */
1514          bfd_elf_generic_reloc, /* special_function */
1515          "R_ARM_TLS_LDM32",     /* name */
1516          TRUE,                  /* partial_inplace */
1517          0xffffffff,            /* src_mask */
1518          0xffffffff,            /* dst_mask */
1519          FALSE),                /* pcrel_offset */
1520
1521   HOWTO (R_ARM_TLS_LDO32,       /* type */
1522          0,                     /* rightshift */
1523          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          32,                    /* bitsize */
1525          FALSE,                 /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_bitfield,/* complain_on_overflow */
1528          bfd_elf_generic_reloc, /* special_function */
1529          "R_ARM_TLS_LDO32",     /* name */
1530          TRUE,                  /* partial_inplace */
1531          0xffffffff,            /* src_mask */
1532          0xffffffff,            /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   HOWTO (R_ARM_TLS_IE32,        /* type */
1536          0,                     /* rightshift */
1537          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1538          32,                    /* bitsize */
1539          FALSE,                  /* pc_relative */
1540          0,                     /* bitpos */
1541          complain_overflow_bitfield,/* complain_on_overflow */
1542          NULL,                  /* special_function */
1543          "R_ARM_TLS_IE32",      /* name */
1544          TRUE,                  /* partial_inplace */
1545          0xffffffff,            /* src_mask */
1546          0xffffffff,            /* dst_mask */
1547          FALSE),                /* pcrel_offset */
1548
1549   HOWTO (R_ARM_TLS_LE32,        /* type */
1550          0,                     /* rightshift */
1551          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1552          32,                    /* bitsize */
1553          FALSE,                 /* pc_relative */
1554          0,                     /* bitpos */
1555          complain_overflow_bitfield,/* complain_on_overflow */
1556          bfd_elf_generic_reloc, /* special_function */
1557          "R_ARM_TLS_LE32",      /* name */
1558          TRUE,                  /* partial_inplace */
1559          0xffffffff,            /* src_mask */
1560          0xffffffff,            /* dst_mask */
1561          FALSE),                /* pcrel_offset */
1562
1563   HOWTO (R_ARM_TLS_LDO12,       /* type */
1564          0,                     /* rightshift */
1565          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1566          12,                    /* bitsize */
1567          FALSE,                 /* pc_relative */
1568          0,                     /* bitpos */
1569          complain_overflow_bitfield,/* complain_on_overflow */
1570          bfd_elf_generic_reloc, /* special_function */
1571          "R_ARM_TLS_LDO12",     /* name */
1572          FALSE,                 /* partial_inplace */
1573          0x00000fff,            /* src_mask */
1574          0x00000fff,            /* dst_mask */
1575          FALSE),                /* pcrel_offset */
1576
1577   HOWTO (R_ARM_TLS_LE12,        /* type */
1578          0,                     /* rightshift */
1579          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1580          12,                    /* bitsize */
1581          FALSE,                 /* pc_relative */
1582          0,                     /* bitpos */
1583          complain_overflow_bitfield,/* complain_on_overflow */
1584          bfd_elf_generic_reloc, /* special_function */
1585          "R_ARM_TLS_LE12",      /* name */
1586          FALSE,                 /* partial_inplace */
1587          0x00000fff,            /* src_mask */
1588          0x00000fff,            /* dst_mask */
1589          FALSE),                /* pcrel_offset */
1590
1591   HOWTO (R_ARM_TLS_IE12GP,      /* type */
1592          0,                     /* rightshift */
1593          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1594          12,                    /* bitsize */
1595          FALSE,                 /* pc_relative */
1596          0,                     /* bitpos */
1597          complain_overflow_bitfield,/* complain_on_overflow */
1598          bfd_elf_generic_reloc, /* special_function */
1599          "R_ARM_TLS_IE12GP",    /* name */
1600          FALSE,                 /* partial_inplace */
1601          0x00000fff,            /* src_mask */
1602          0x00000fff,            /* dst_mask */
1603          FALSE),                /* pcrel_offset */
1604 };
1605
1606 /* 112-127 private relocations
1607    128 R_ARM_ME_TOO, obsolete
1608    129-255 unallocated in AAELF.
1609
1610    249-255 extended, currently unused, relocations:  */
1611
1612 static reloc_howto_type elf32_arm_howto_table_2[4] =
1613 {
1614   HOWTO (R_ARM_RREL32,          /* type */
1615          0,                     /* rightshift */
1616          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1617          0,                     /* bitsize */
1618          FALSE,                 /* pc_relative */
1619          0,                     /* bitpos */
1620          complain_overflow_dont,/* complain_on_overflow */
1621          bfd_elf_generic_reloc, /* special_function */
1622          "R_ARM_RREL32",        /* name */
1623          FALSE,                 /* partial_inplace */
1624          0,                     /* src_mask */
1625          0,                     /* dst_mask */
1626          FALSE),                /* pcrel_offset */
1627
1628   HOWTO (R_ARM_RABS32,          /* type */
1629          0,                     /* rightshift */
1630          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1631          0,                     /* bitsize */
1632          FALSE,                 /* pc_relative */
1633          0,                     /* bitpos */
1634          complain_overflow_dont,/* complain_on_overflow */
1635          bfd_elf_generic_reloc, /* special_function */
1636          "R_ARM_RABS32",        /* name */
1637          FALSE,                 /* partial_inplace */
1638          0,                     /* src_mask */
1639          0,                     /* dst_mask */
1640          FALSE),                /* pcrel_offset */
1641
1642   HOWTO (R_ARM_RPC24,           /* type */
1643          0,                     /* rightshift */
1644          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1645          0,                     /* bitsize */
1646          FALSE,                 /* pc_relative */
1647          0,                     /* bitpos */
1648          complain_overflow_dont,/* complain_on_overflow */
1649          bfd_elf_generic_reloc, /* special_function */
1650          "R_ARM_RPC24",         /* name */
1651          FALSE,                 /* partial_inplace */
1652          0,                     /* src_mask */
1653          0,                     /* dst_mask */
1654          FALSE),                /* pcrel_offset */
1655
1656   HOWTO (R_ARM_RBASE,           /* type */
1657          0,                     /* rightshift */
1658          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1659          0,                     /* bitsize */
1660          FALSE,                 /* pc_relative */
1661          0,                     /* bitpos */
1662          complain_overflow_dont,/* complain_on_overflow */
1663          bfd_elf_generic_reloc, /* special_function */
1664          "R_ARM_RBASE",         /* name */
1665          FALSE,                 /* partial_inplace */
1666          0,                     /* src_mask */
1667          0,                     /* dst_mask */
1668          FALSE)                 /* pcrel_offset */
1669 };
1670
1671 static reloc_howto_type *
1672 elf32_arm_howto_from_type (unsigned int r_type)
1673 {
1674   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1675     return &elf32_arm_howto_table_1[r_type];
1676
1677   if (r_type >= R_ARM_RREL32
1678       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_2))
1679     return &elf32_arm_howto_table_2[r_type - R_ARM_RREL32];
1680
1681   return NULL;
1682 }
1683
1684 static void
1685 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
1686                          Elf_Internal_Rela * elf_reloc)
1687 {
1688   unsigned int r_type;
1689
1690   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1691   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
1692 }
1693
1694 struct elf32_arm_reloc_map
1695   {
1696     bfd_reloc_code_real_type  bfd_reloc_val;
1697     unsigned char             elf_reloc_val;
1698   };
1699
1700 /* All entries in this list must also be present in elf32_arm_howto_table.  */
1701 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1702   {
1703     {BFD_RELOC_NONE,                 R_ARM_NONE},
1704     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1705     {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
1706     {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
1707     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1708     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1709     {BFD_RELOC_32,                   R_ARM_ABS32},
1710     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1711     {BFD_RELOC_8,                    R_ARM_ABS8},
1712     {BFD_RELOC_16,                   R_ARM_ABS16},
1713     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1714     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1715     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1716     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1717     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1718     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1719     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1720     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1721     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1722     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1723     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1724     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1725     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1726     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1727     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1728     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
1729     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
1730     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
1731     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
1732     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
1733     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1734     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
1735     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
1736     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
1737     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
1738     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
1739     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
1740     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
1741     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
1742     {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
1743     {BFD_RELOC_VTABLE_ENTRY,         R_ARM_GNU_VTENTRY},
1744     {BFD_RELOC_ARM_MOVW,             R_ARM_MOVW_ABS_NC},
1745     {BFD_RELOC_ARM_MOVT,             R_ARM_MOVT_ABS},
1746     {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
1747     {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
1748     {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
1749     {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
1750     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
1751     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
1752     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
1753     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
1754     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
1755     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
1756     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
1757     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
1758     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
1759     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
1760     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
1761     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
1762     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
1763     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
1764     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
1765     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
1766     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
1767     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
1768     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
1769     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
1770     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
1771     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
1772     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
1773     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
1774     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
1775     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
1776     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
1777     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
1778     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
1779     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
1780     {BFD_RELOC_ARM_V4BX,             R_ARM_V4BX}
1781   };
1782
1783 static reloc_howto_type *
1784 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1785                              bfd_reloc_code_real_type code)
1786 {
1787   unsigned int i;
1788
1789   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
1790     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1791       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
1792
1793   return NULL;
1794 }
1795
1796 static reloc_howto_type *
1797 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1798                              const char *r_name)
1799 {
1800   unsigned int i;
1801
1802   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
1803     if (elf32_arm_howto_table_1[i].name != NULL
1804         && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
1805       return &elf32_arm_howto_table_1[i];
1806
1807   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
1808     if (elf32_arm_howto_table_2[i].name != NULL
1809         && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
1810       return &elf32_arm_howto_table_2[i];
1811
1812   return NULL;
1813 }
1814
1815 /* Support for core dump NOTE sections.  */
1816
1817 static bfd_boolean
1818 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1819 {
1820   int offset;
1821   size_t size;
1822
1823   switch (note->descsz)
1824     {
1825       default:
1826         return FALSE;
1827
1828       case 148:         /* Linux/ARM 32-bit.  */
1829         /* pr_cursig */
1830         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1831
1832         /* pr_pid */
1833         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1834
1835         /* pr_reg */
1836         offset = 72;
1837         size = 72;
1838
1839         break;
1840     }
1841
1842   /* Make a ".reg/999" section.  */
1843   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1844                                           size, note->descpos + offset);
1845 }
1846
1847 static bfd_boolean
1848 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1849 {
1850   switch (note->descsz)
1851     {
1852       default:
1853         return FALSE;
1854
1855       case 124:         /* Linux/ARM elf_prpsinfo.  */
1856         elf_tdata (abfd)->core_program
1857          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1858         elf_tdata (abfd)->core_command
1859          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1860     }
1861
1862   /* Note that for some reason, a spurious space is tacked
1863      onto the end of the args in some (at least one anyway)
1864      implementations, so strip it off if it exists.  */
1865   {
1866     char *command = elf_tdata (abfd)->core_command;
1867     int n = strlen (command);
1868
1869     if (0 < n && command[n - 1] == ' ')
1870       command[n - 1] = '\0';
1871   }
1872
1873   return TRUE;
1874 }
1875
1876 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
1877 #define TARGET_LITTLE_NAME              "elf32-littlearm"
1878 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
1879 #define TARGET_BIG_NAME                 "elf32-bigarm"
1880
1881 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
1882 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
1883
1884 typedef unsigned long int insn32;
1885 typedef unsigned short int insn16;
1886
1887 /* In lieu of proper flags, assume all EABIv4 or later objects are
1888    interworkable.  */
1889 #define INTERWORK_FLAG(abfd)  \
1890   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
1891   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
1892   || ((abfd)->flags & BFD_LINKER_CREATED))
1893
1894 /* The linker script knows the section names for placement.
1895    The entry_names are used to do simple name mangling on the stubs.
1896    Given a function name, and its type, the stub can be found. The
1897    name can be changed. The only requirement is the %s be present.  */
1898 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1899 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
1900
1901 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1902 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
1903
1904 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
1905 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
1906
1907 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
1908 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
1909
1910 #define STUB_ENTRY_NAME   "__%s_veneer"
1911
1912 /* The name of the dynamic interpreter.  This is put in the .interp
1913    section.  */
1914 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1915
1916 #ifdef FOUR_WORD_PLT
1917
1918 /* The first entry in a procedure linkage table looks like
1919    this.  It is set up so that any shared library function that is
1920    called before the relocation has been set up calls the dynamic
1921    linker first.  */
1922 static const bfd_vma elf32_arm_plt0_entry [] =
1923   {
1924     0xe52de004,         /* str   lr, [sp, #-4]! */
1925     0xe59fe010,         /* ldr   lr, [pc, #16]  */
1926     0xe08fe00e,         /* add   lr, pc, lr     */
1927     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1928   };
1929
1930 /* Subsequent entries in a procedure linkage table look like
1931    this.  */
1932 static const bfd_vma elf32_arm_plt_entry [] =
1933   {
1934     0xe28fc600,         /* add   ip, pc, #NN    */
1935     0xe28cca00,         /* add   ip, ip, #NN    */
1936     0xe5bcf000,         /* ldr   pc, [ip, #NN]! */
1937     0x00000000,         /* unused               */
1938   };
1939
1940 #else
1941
1942 /* The first entry in a procedure linkage table looks like
1943    this.  It is set up so that any shared library function that is
1944    called before the relocation has been set up calls the dynamic
1945    linker first.  */
1946 static const bfd_vma elf32_arm_plt0_entry [] =
1947   {
1948     0xe52de004,         /* str   lr, [sp, #-4]! */
1949     0xe59fe004,         /* ldr   lr, [pc, #4]   */
1950     0xe08fe00e,         /* add   lr, pc, lr     */
1951     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1952     0x00000000,         /* &GOT[0] - .          */
1953   };
1954
1955 /* Subsequent entries in a procedure linkage table look like
1956    this.  */
1957 static const bfd_vma elf32_arm_plt_entry [] =
1958   {
1959     0xe28fc600,         /* add   ip, pc, #0xNN00000 */
1960     0xe28cca00,         /* add   ip, ip, #0xNN000   */
1961     0xe5bcf000,         /* ldr   pc, [ip, #0xNNN]!  */
1962   };
1963
1964 #endif
1965
1966 /* The format of the first entry in the procedure linkage table
1967    for a VxWorks executable.  */
1968 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
1969   {
1970     0xe52dc008,         /* str    ip,[sp,#-8]!                  */
1971     0xe59fc000,         /* ldr    ip,[pc]                       */
1972     0xe59cf008,         /* ldr    pc,[ip,#8]                    */
1973     0x00000000,         /* .long  _GLOBAL_OFFSET_TABLE_         */
1974   };
1975
1976 /* The format of subsequent entries in a VxWorks executable.  */
1977 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
1978   {
1979     0xe59fc000,         /* ldr    ip,[pc]                       */
1980     0xe59cf000,         /* ldr    pc,[ip]                       */
1981     0x00000000,         /* .long  @got                          */
1982     0xe59fc000,         /* ldr    ip,[pc]                       */
1983     0xea000000,         /* b      _PLT                          */
1984     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1985   };
1986
1987 /* The format of entries in a VxWorks shared library.  */
1988 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
1989   {
1990     0xe59fc000,         /* ldr    ip,[pc]                       */
1991     0xe79cf009,         /* ldr    pc,[ip,r9]                    */
1992     0x00000000,         /* .long  @got                          */
1993     0xe59fc000,         /* ldr    ip,[pc]                       */
1994     0xe599f008,         /* ldr    pc,[r9,#8]                    */
1995     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1996   };
1997
1998 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
1999 #define PLT_THUMB_STUB_SIZE 4
2000 static const bfd_vma elf32_arm_plt_thumb_stub [] =
2001   {
2002     0x4778,             /* bx pc */
2003     0x46c0              /* nop   */
2004   };
2005
2006 /* The entries in a PLT when using a DLL-based target with multiple
2007    address spaces.  */
2008 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2009   {
2010     0xe51ff004,         /* ldr   pc, [pc, #-4] */
2011     0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2012   };
2013
2014 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2015 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2016 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2017 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2018 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2019 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2020
2021 enum stub_insn_type
2022   {
2023     THUMB16_TYPE = 1,
2024     THUMB32_TYPE,
2025     ARM_TYPE,
2026     DATA_TYPE
2027   };
2028
2029 #define THUMB16_INSN(X)         {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2030 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2031    is inserted in arm_build_one_stub().  */
2032 #define THUMB16_BCOND_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2033 #define THUMB32_INSN(X)         {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2034 #define THUMB32_B_INSN(X, Z)    {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2035 #define ARM_INSN(X)             {(X), ARM_TYPE, R_ARM_NONE, 0}
2036 #define ARM_REL_INSN(X, Z)      {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2037 #define DATA_WORD(X,Y,Z)        {(X), DATA_TYPE, (Y), (Z)}
2038
2039 typedef struct
2040 {
2041   bfd_vma data;
2042   enum stub_insn_type type;
2043   unsigned int r_type;
2044   int reloc_addend;
2045 }  insn_sequence;
2046
2047 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2048    to reach the stub if necessary.  */
2049 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2050   {
2051     ARM_INSN(0xe51ff004),            /* ldr   pc, [pc, #-4] */
2052     DATA_WORD(0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2053   };
2054
2055 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2056    available.  */
2057 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2058   {
2059     ARM_INSN(0xe59fc000),            /* ldr   ip, [pc, #0] */
2060     ARM_INSN(0xe12fff1c),            /* bx    ip */
2061     DATA_WORD(0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2062   };
2063
2064 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2065 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2066   {
2067     THUMB16_INSN(0xb401),             /* push {r0} */
2068     THUMB16_INSN(0x4802),             /* ldr  r0, [pc, #8] */
2069     THUMB16_INSN(0x4684),             /* mov  ip, r0 */
2070     THUMB16_INSN(0xbc01),             /* pop  {r0} */
2071     THUMB16_INSN(0x4760),             /* bx   ip */
2072     THUMB16_INSN(0xbf00),             /* nop */
2073     DATA_WORD(0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2074   };
2075
2076 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
2077    allowed.  */
2078 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2079   {
2080     THUMB16_INSN(0x4778),             /* bx   pc */
2081     THUMB16_INSN(0x46c0),             /* nop */
2082     ARM_INSN(0xe59fc000),             /* ldr  ip, [pc, #0] */
2083     ARM_INSN(0xe12fff1c),             /* bx   ip */
2084     DATA_WORD(0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2085   };
2086
2087 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2088    available.  */
2089 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2090   {
2091     THUMB16_INSN(0x4778),             /* bx   pc */
2092     THUMB16_INSN(0x46c0),             /* nop   */
2093     ARM_INSN(0xe51ff004),             /* ldr   pc, [pc, #-4] */
2094     DATA_WORD(0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2095   };
2096
2097 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2098    one, when the destination is close enough.  */
2099 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2100   {
2101     THUMB16_INSN(0x4778),             /* bx   pc */
2102     THUMB16_INSN(0x46c0),             /* nop   */
2103     ARM_REL_INSN(0xea000000, -8),     /* b    (X-8) */
2104   };
2105
2106 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2107    blx to reach the stub if necessary.  */
2108 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2109   {
2110     ARM_INSN(0xe59fc000),             /* ldr   r12, [pc] */
2111     ARM_INSN(0xe08ff00c),             /* add   pc, pc, ip */
2112     DATA_WORD(0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2113   };
2114
2115 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2116    blx to reach the stub if necessary.  We can not add into pc;
2117    it is not guaranteed to mode switch (different in ARMv6 and
2118    ARMv7).  */
2119 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2120   {
2121     ARM_INSN(0xe59fc004),             /* ldr   r12, [pc, #4] */
2122     ARM_INSN(0xe08fc00c),             /* add   ip, pc, ip */
2123     ARM_INSN(0xe12fff1c),             /* bx    ip */
2124     DATA_WORD(0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2125   };
2126
2127 /* V4T ARM -> ARM long branch stub, PIC.  */
2128 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2129   {
2130     ARM_INSN(0xe59fc004),             /* ldr   ip, [pc, #4] */
2131     ARM_INSN(0xe08fc00c),             /* add   ip, pc, ip */
2132     ARM_INSN(0xe12fff1c),             /* bx    ip */
2133     DATA_WORD(0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2134   };
2135
2136 /* V4T Thumb -> ARM long branch stub, PIC.  */
2137 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2138   {
2139     THUMB16_INSN(0x4778),             /* bx   pc */
2140     THUMB16_INSN(0x46c0),             /* nop  */
2141     ARM_INSN(0xe59fc000),             /* ldr  ip, [pc, #0] */
2142     ARM_INSN(0xe08cf00f),             /* add  pc, ip, pc */
2143     DATA_WORD(0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2144   };
2145
2146 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2147    architectures.  */
2148 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2149   {
2150     THUMB16_INSN(0xb401),             /* push {r0} */
2151     THUMB16_INSN(0x4802),             /* ldr  r0, [pc, #8] */
2152     THUMB16_INSN(0x46fc),             /* mov  ip, pc */
2153     THUMB16_INSN(0x4484),             /* add  ip, r0 */
2154     THUMB16_INSN(0xbc01),             /* pop  {r0} */
2155     THUMB16_INSN(0x4760),             /* bx   ip */
2156     DATA_WORD(0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2157   };
2158
2159 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2160    allowed.  */
2161 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2162   {
2163     THUMB16_INSN(0x4778),             /* bx   pc */
2164     THUMB16_INSN(0x46c0),             /* nop */
2165     ARM_INSN(0xe59fc004),             /* ldr  ip, [pc, #4] */
2166     ARM_INSN(0xe08fc00c),             /* add   ip, pc, ip */
2167     ARM_INSN(0xe12fff1c),             /* bx   ip */
2168     DATA_WORD(0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2169   };
2170
2171 /* Cortex-A8 erratum-workaround stubs.  */
2172
2173 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2174    can't use a conditional branch to reach this stub).  */
2175
2176 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2177   {
2178     THUMB16_BCOND_INSN(0xd001),         /* b<cond>.n true.  */
2179     THUMB32_B_INSN(0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2180     THUMB32_B_INSN(0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2181   };
2182
2183 /* Stub used for b.w and bl.w instructions.  */
2184
2185 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2186   {
2187     THUMB32_B_INSN(0xf000b800, -4)      /* b.w original_branch_dest.  */
2188   };
2189
2190 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2191   {
2192     THUMB32_B_INSN(0xf000b800, -4)      /* b.w original_branch_dest.  */
2193   };
2194
2195 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2196    instruction (which switches to ARM mode) to point to this stub.  Jump to the
2197    real destination using an ARM-mode branch.  */
2198
2199 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2200   {
2201     ARM_REL_INSN(0xea000000, -8)        /* b original_branch_dest.  */
2202   };
2203
2204 /* Section name for stubs is the associated section name plus this
2205    string.  */
2206 #define STUB_SUFFIX ".stub"
2207
2208 /* One entry per long/short branch stub defined above.  */
2209 #define DEF_STUBS \
2210   DEF_STUB(long_branch_any_any) \
2211   DEF_STUB(long_branch_v4t_arm_thumb) \
2212   DEF_STUB(long_branch_thumb_only) \
2213   DEF_STUB(long_branch_v4t_thumb_thumb) \
2214   DEF_STUB(long_branch_v4t_thumb_arm) \
2215   DEF_STUB(short_branch_v4t_thumb_arm) \
2216   DEF_STUB(long_branch_any_arm_pic) \
2217   DEF_STUB(long_branch_any_thumb_pic) \
2218   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
2219   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
2220   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
2221   DEF_STUB(long_branch_thumb_only_pic) \
2222   DEF_STUB(a8_veneer_b_cond) \
2223   DEF_STUB(a8_veneer_b) \
2224   DEF_STUB(a8_veneer_bl) \
2225   DEF_STUB(a8_veneer_blx)
2226
2227 #define DEF_STUB(x) arm_stub_##x,
2228 enum elf32_arm_stub_type {
2229   arm_stub_none,
2230   DEF_STUBS
2231   /* Note the first a8_veneer type */
2232   arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond
2233 };
2234 #undef DEF_STUB
2235
2236 typedef struct
2237 {
2238   const insn_sequence* template;
2239   int template_size;
2240 } stub_def;
2241
2242 #define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
2243 static const stub_def stub_definitions[] = {
2244   {NULL, 0},
2245   DEF_STUBS
2246 };
2247
2248 struct elf32_arm_stub_hash_entry
2249 {
2250   /* Base hash table entry structure.  */
2251   struct bfd_hash_entry root;
2252
2253   /* The stub section.  */
2254   asection *stub_sec;
2255
2256   /* Offset within stub_sec of the beginning of this stub.  */
2257   bfd_vma stub_offset;
2258
2259   /* Given the symbol's value and its section we can determine its final
2260      value when building the stubs (so the stub knows where to jump).  */
2261   bfd_vma target_value;
2262   asection *target_section;
2263
2264   /* Offset to apply to relocation referencing target_value.  */
2265   bfd_vma target_addend;
2266
2267   /* The instruction which caused this stub to be generated (only valid for
2268      Cortex-A8 erratum workaround stubs at present).  */
2269   unsigned long orig_insn;
2270
2271   /* The stub type.  */
2272   enum elf32_arm_stub_type stub_type;
2273   /* Its encoding size in bytes.  */
2274   int stub_size;
2275   /* Its template.  */
2276   const insn_sequence *stub_template;
2277   /* The size of the template (number of entries).  */
2278   int stub_template_size;
2279
2280   /* The symbol table entry, if any, that this was derived from.  */
2281   struct elf32_arm_link_hash_entry *h;
2282
2283   /* Destination symbol type (STT_ARM_TFUNC, ...) */
2284   unsigned char st_type;
2285
2286   /* Where this stub is being called from, or, in the case of combined
2287      stub sections, the first input section in the group.  */
2288   asection *id_sec;
2289
2290   /* The name for the local symbol at the start of this stub.  The
2291      stub name in the hash table has to be unique; this does not, so
2292      it can be friendlier.  */
2293   char *output_name;
2294 };
2295
2296 /* Used to build a map of a section.  This is required for mixed-endian
2297    code/data.  */
2298
2299 typedef struct elf32_elf_section_map
2300 {
2301   bfd_vma vma;
2302   char type;
2303 }
2304 elf32_arm_section_map;
2305
2306 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2307
2308 typedef enum
2309 {
2310   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2311   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2312   VFP11_ERRATUM_ARM_VENEER,
2313   VFP11_ERRATUM_THUMB_VENEER
2314 }
2315 elf32_vfp11_erratum_type;
2316
2317 typedef struct elf32_vfp11_erratum_list
2318 {
2319   struct elf32_vfp11_erratum_list *next;
2320   bfd_vma vma;
2321   union
2322   {
2323     struct
2324     {
2325       struct elf32_vfp11_erratum_list *veneer;
2326       unsigned int vfp_insn;
2327     } b;
2328     struct
2329     {
2330       struct elf32_vfp11_erratum_list *branch;
2331       unsigned int id;
2332     } v;
2333   } u;
2334   elf32_vfp11_erratum_type type;
2335 }
2336 elf32_vfp11_erratum_list;
2337
2338 typedef enum
2339 {
2340   DELETE_EXIDX_ENTRY,
2341   INSERT_EXIDX_CANTUNWIND_AT_END
2342 }
2343 arm_unwind_edit_type;
2344
2345 /* A (sorted) list of edits to apply to an unwind table.  */
2346 typedef struct arm_unwind_table_edit
2347 {
2348   arm_unwind_edit_type type;
2349   /* Note: we sometimes want to insert an unwind entry corresponding to a
2350      section different from the one we're currently writing out, so record the
2351      (text) section this edit relates to here.  */
2352   asection *linked_section;
2353   unsigned int index;
2354   struct arm_unwind_table_edit *next;
2355 }
2356 arm_unwind_table_edit;
2357
2358 typedef struct _arm_elf_section_data
2359 {
2360   /* Information about mapping symbols.  */
2361   struct bfd_elf_section_data elf;
2362   unsigned int mapcount;
2363   unsigned int mapsize;
2364   elf32_arm_section_map *map;
2365   /* Information about CPU errata.  */
2366   unsigned int erratumcount;
2367   elf32_vfp11_erratum_list *erratumlist;
2368   /* Information about unwind tables.  */
2369   union
2370   {
2371     /* Unwind info attached to a text section.  */
2372     struct
2373     {
2374       asection *arm_exidx_sec;
2375     } text;
2376
2377     /* Unwind info attached to an .ARM.exidx section.  */
2378     struct
2379     {
2380       arm_unwind_table_edit *unwind_edit_list;
2381       arm_unwind_table_edit *unwind_edit_tail;
2382     } exidx;
2383   } u;
2384 }
2385 _arm_elf_section_data;
2386
2387 #define elf32_arm_section_data(sec) \
2388   ((_arm_elf_section_data *) elf_section_data (sec))
2389
2390 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
2391    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
2392    so may be created multiple times: we use an array of these entries whilst
2393    relaxing which we can refresh easily, then create stubs for each potentially
2394    erratum-triggering instruction once we've settled on a solution.  */
2395
2396 struct a8_erratum_fix {
2397   bfd *input_bfd;
2398   asection *section;
2399   bfd_vma offset;
2400   bfd_vma addend;
2401   unsigned long orig_insn;
2402   char *stub_name;
2403   enum elf32_arm_stub_type stub_type;
2404 };
2405
2406 /* A table of relocs applied to branches which might trigger Cortex-A8
2407    erratum.  */
2408
2409 struct a8_erratum_reloc {
2410   bfd_vma from;
2411   bfd_vma destination;
2412   unsigned int r_type;
2413   unsigned char st_type;
2414   const char *sym_name;
2415   bfd_boolean non_a8_stub;
2416 };
2417
2418 /* The size of the thread control block.  */
2419 #define TCB_SIZE        8
2420
2421 struct elf_arm_obj_tdata
2422 {
2423   struct elf_obj_tdata root;
2424
2425   /* tls_type for each local got entry.  */
2426   char *local_got_tls_type;
2427
2428   /* Zero to warn when linking objects with incompatible enum sizes.  */
2429   int no_enum_size_warning;
2430
2431   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
2432   int no_wchar_size_warning;
2433 };
2434
2435 #define elf_arm_tdata(bfd) \
2436   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
2437
2438 #define elf32_arm_local_got_tls_type(bfd) \
2439   (elf_arm_tdata (bfd)->local_got_tls_type)
2440
2441 #define is_arm_elf(bfd) \
2442   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2443    && elf_tdata (bfd) != NULL \
2444    && elf_object_id (bfd) == ARM_ELF_TDATA)
2445
2446 static bfd_boolean
2447 elf32_arm_mkobject (bfd *abfd)
2448 {
2449   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
2450                                   ARM_ELF_TDATA);
2451 }
2452
2453 /* The ARM linker needs to keep track of the number of relocs that it
2454    decides to copy in check_relocs for each symbol.  This is so that
2455    it can discard PC relative relocs if it doesn't need them when
2456    linking with -Bsymbolic.  We store the information in a field
2457    extending the regular ELF linker hash table.  */
2458
2459 /* This structure keeps track of the number of relocs we have copied
2460    for a given symbol.  */
2461 struct elf32_arm_relocs_copied
2462   {
2463     /* Next section.  */
2464     struct elf32_arm_relocs_copied * next;
2465     /* A section in dynobj.  */
2466     asection * section;
2467     /* Number of relocs copied in this section.  */
2468     bfd_size_type count;
2469     /* Number of PC-relative relocs copied in this section.  */
2470     bfd_size_type pc_count;
2471   };
2472
2473 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2474
2475 /* Arm ELF linker hash entry.  */
2476 struct elf32_arm_link_hash_entry
2477   {
2478     struct elf_link_hash_entry root;
2479
2480     /* Number of PC relative relocs copied for this symbol.  */
2481     struct elf32_arm_relocs_copied * relocs_copied;
2482
2483     /* We reference count Thumb references to a PLT entry separately,
2484        so that we can emit the Thumb trampoline only if needed.  */
2485     bfd_signed_vma plt_thumb_refcount;
2486
2487     /* Some references from Thumb code may be eliminated by BL->BLX
2488        conversion, so record them separately.  */
2489     bfd_signed_vma plt_maybe_thumb_refcount;
2490
2491     /* Since PLT entries have variable size if the Thumb prologue is
2492        used, we need to record the index into .got.plt instead of
2493        recomputing it from the PLT offset.  */
2494     bfd_signed_vma plt_got_offset;
2495
2496 #define GOT_UNKNOWN     0
2497 #define GOT_NORMAL      1
2498 #define GOT_TLS_GD      2
2499 #define GOT_TLS_IE      4
2500     unsigned char tls_type;
2501
2502     /* The symbol marking the real symbol location for exported thumb
2503        symbols with Arm stubs.  */
2504     struct elf_link_hash_entry *export_glue;
2505
2506    /* A pointer to the most recently used stub hash entry against this
2507      symbol.  */
2508     struct elf32_arm_stub_hash_entry *stub_cache;
2509   };
2510
2511 /* Traverse an arm ELF linker hash table.  */
2512 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2513   (elf_link_hash_traverse                                               \
2514    (&(table)->root,                                                     \
2515     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2516     (info)))
2517
2518 /* Get the ARM elf linker hash table from a link_info structure.  */
2519 #define elf32_arm_hash_table(info) \
2520   ((struct elf32_arm_link_hash_table *) ((info)->hash))
2521
2522 #define arm_stub_hash_lookup(table, string, create, copy) \
2523   ((struct elf32_arm_stub_hash_entry *) \
2524    bfd_hash_lookup ((table), (string), (create), (copy)))
2525
2526 /* ARM ELF linker hash table.  */
2527 struct elf32_arm_link_hash_table
2528 {
2529   /* The main hash table.  */
2530   struct elf_link_hash_table root;
2531
2532   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
2533   bfd_size_type thumb_glue_size;
2534
2535   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
2536   bfd_size_type arm_glue_size;
2537
2538   /* The size in bytes of section containing the ARMv4 BX veneers.  */
2539   bfd_size_type bx_glue_size;
2540
2541   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
2542      veneer has been populated.  */
2543   bfd_vma bx_glue_offset[15];
2544
2545   /* The size in bytes of the section containing glue for VFP11 erratum
2546      veneers.  */
2547   bfd_size_type vfp11_erratum_glue_size;
2548
2549   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
2550      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
2551      elf32_arm_write_section().  */
2552   struct a8_erratum_fix *a8_erratum_fixes;
2553   unsigned int num_a8_erratum_fixes;
2554
2555   /* An arbitrary input BFD chosen to hold the glue sections.  */
2556   bfd * bfd_of_glue_owner;
2557
2558   /* Nonzero to output a BE8 image.  */
2559   int byteswap_code;
2560
2561   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
2562      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
2563   int target1_is_rel;
2564
2565   /* The relocation to use for R_ARM_TARGET2 relocations.  */
2566   int target2_reloc;
2567
2568   /* 0 = Ignore R_ARM_V4BX.
2569      1 = Convert BX to MOV PC.
2570      2 = Generate v4 interworing stubs.  */
2571   int fix_v4bx;
2572
2573   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
2574   int fix_cortex_a8;
2575
2576   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
2577   int use_blx;
2578
2579   /* What sort of code sequences we should look for which may trigger the
2580      VFP11 denorm erratum.  */
2581   bfd_arm_vfp11_fix vfp11_fix;
2582
2583   /* Global counter for the number of fixes we have emitted.  */
2584   int num_vfp11_fixes;
2585
2586   /* Nonzero to force PIC branch veneers.  */
2587   int pic_veneer;
2588
2589   /* The number of bytes in the initial entry in the PLT.  */
2590   bfd_size_type plt_header_size;
2591
2592   /* The number of bytes in the subsequent PLT etries.  */
2593   bfd_size_type plt_entry_size;
2594
2595   /* True if the target system is VxWorks.  */
2596   int vxworks_p;
2597
2598   /* True if the target system is Symbian OS.  */
2599   int symbian_p;
2600
2601   /* True if the target uses REL relocations.  */
2602   int use_rel;
2603
2604   /* Short-cuts to get to dynamic linker sections.  */
2605   asection *sgot;
2606   asection *sgotplt;
2607   asection *srelgot;
2608   asection *splt;
2609   asection *srelplt;
2610   asection *sdynbss;
2611   asection *srelbss;
2612
2613   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2614   asection *srelplt2;
2615
2616   /* Data for R_ARM_TLS_LDM32 relocations.  */
2617   union
2618   {
2619     bfd_signed_vma refcount;
2620     bfd_vma offset;
2621   } tls_ldm_got;
2622
2623   /* Small local sym cache.  */
2624   struct sym_cache sym_cache;
2625
2626   /* For convenience in allocate_dynrelocs.  */
2627   bfd * obfd;
2628
2629   /* The stub hash table.  */
2630   struct bfd_hash_table stub_hash_table;
2631
2632   /* Linker stub bfd.  */
2633   bfd *stub_bfd;
2634
2635   /* Linker call-backs.  */
2636   asection * (*add_stub_section) (const char *, asection *);
2637   void (*layout_sections_again) (void);
2638
2639   /* Array to keep track of which stub sections have been created, and
2640      information on stub grouping.  */
2641   struct map_stub
2642   {
2643     /* This is the section to which stubs in the group will be
2644        attached.  */
2645     asection *link_sec;
2646     /* The stub section.  */
2647     asection *stub_sec;
2648   } *stub_group;
2649
2650   /* Assorted information used by elf32_arm_size_stubs.  */
2651   unsigned int bfd_count;
2652   int top_index;
2653   asection **input_list;
2654 };
2655
2656 /* Create an entry in an ARM ELF linker hash table.  */
2657
2658 static struct bfd_hash_entry *
2659 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
2660                              struct bfd_hash_table * table,
2661                              const char * string)
2662 {
2663   struct elf32_arm_link_hash_entry * ret =
2664     (struct elf32_arm_link_hash_entry *) entry;
2665
2666   /* Allocate the structure if it has not already been allocated by a
2667      subclass.  */
2668   if (ret == NULL)
2669     ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
2670   if (ret == NULL)
2671     return (struct bfd_hash_entry *) ret;
2672
2673   /* Call the allocation method of the superclass.  */
2674   ret = ((struct elf32_arm_link_hash_entry *)
2675          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2676                                      table, string));
2677   if (ret != NULL)
2678     {
2679       ret->relocs_copied = NULL;
2680       ret->tls_type = GOT_UNKNOWN;
2681       ret->plt_thumb_refcount = 0;
2682       ret->plt_maybe_thumb_refcount = 0;
2683       ret->plt_got_offset = -1;
2684       ret->export_glue = NULL;
2685
2686       ret->stub_cache = NULL;
2687     }
2688
2689   return (struct bfd_hash_entry *) ret;
2690 }
2691
2692 /* Initialize an entry in the stub hash table.  */
2693
2694 static struct bfd_hash_entry *
2695 stub_hash_newfunc (struct bfd_hash_entry *entry,
2696                    struct bfd_hash_table *table,
2697                    const char *string)
2698 {
2699   /* Allocate the structure if it has not already been allocated by a
2700      subclass.  */
2701   if (entry == NULL)
2702     {
2703       entry = bfd_hash_allocate (table,
2704                                  sizeof (struct elf32_arm_stub_hash_entry));
2705       if (entry == NULL)
2706         return entry;
2707     }
2708
2709   /* Call the allocation method of the superclass.  */
2710   entry = bfd_hash_newfunc (entry, table, string);
2711   if (entry != NULL)
2712     {
2713       struct elf32_arm_stub_hash_entry *eh;
2714
2715       /* Initialize the local fields.  */
2716       eh = (struct elf32_arm_stub_hash_entry *) entry;
2717       eh->stub_sec = NULL;
2718       eh->stub_offset = 0;
2719       eh->target_value = 0;
2720       eh->target_section = NULL;
2721       eh->target_addend = 0;
2722       eh->orig_insn = 0;
2723       eh->stub_type = arm_stub_none;
2724       eh->stub_size = 0;
2725       eh->stub_template = NULL;
2726       eh->stub_template_size = 0;
2727       eh->h = NULL;
2728       eh->id_sec = NULL;
2729       eh->output_name = NULL;
2730     }
2731
2732   return entry;
2733 }
2734
2735 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
2736    shortcuts to them in our hash table.  */
2737
2738 static bfd_boolean
2739 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2740 {
2741   struct elf32_arm_link_hash_table *htab;
2742
2743   htab = elf32_arm_hash_table (info);
2744   /* BPABI objects never have a GOT, or associated sections.  */
2745   if (htab->symbian_p)
2746     return TRUE;
2747
2748   if (! _bfd_elf_create_got_section (dynobj, info))
2749     return FALSE;
2750
2751   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2752   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2753   if (!htab->sgot || !htab->sgotplt)
2754     abort ();
2755
2756   htab->srelgot = bfd_get_section_by_name (dynobj,
2757                                            RELOC_SECTION (htab, ".got"));
2758   if (htab->srelgot == NULL)
2759     return FALSE;
2760   return TRUE;
2761 }
2762
2763 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
2764    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
2765    hash table.  */
2766
2767 static bfd_boolean
2768 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2769 {
2770   struct elf32_arm_link_hash_table *htab;
2771
2772   htab = elf32_arm_hash_table (info);
2773   if (!htab->sgot && !create_got_section (dynobj, info))
2774     return FALSE;
2775
2776   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2777     return FALSE;
2778
2779   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2780   htab->srelplt = bfd_get_section_by_name (dynobj,
2781                                            RELOC_SECTION (htab, ".plt"));
2782   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2783   if (!info->shared)
2784     htab->srelbss = bfd_get_section_by_name (dynobj,
2785                                              RELOC_SECTION (htab, ".bss"));
2786
2787   if (htab->vxworks_p)
2788     {
2789       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
2790         return FALSE;
2791
2792       if (info->shared)
2793         {
2794           htab->plt_header_size = 0;
2795           htab->plt_entry_size
2796             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
2797         }
2798       else
2799         {
2800           htab->plt_header_size
2801             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
2802           htab->plt_entry_size
2803             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
2804         }
2805     }
2806
2807   if (!htab->splt
2808       || !htab->srelplt
2809       || !htab->sdynbss
2810       || (!info->shared && !htab->srelbss))
2811     abort ();
2812
2813   return TRUE;
2814 }
2815
2816 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2817
2818 static void
2819 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
2820                                 struct elf_link_hash_entry *dir,
2821                                 struct elf_link_hash_entry *ind)
2822 {
2823   struct elf32_arm_link_hash_entry *edir, *eind;
2824
2825   edir = (struct elf32_arm_link_hash_entry *) dir;
2826   eind = (struct elf32_arm_link_hash_entry *) ind;
2827
2828   if (eind->relocs_copied != NULL)
2829     {
2830       if (edir->relocs_copied != NULL)
2831         {
2832           struct elf32_arm_relocs_copied **pp;
2833           struct elf32_arm_relocs_copied *p;
2834
2835           /* Add reloc counts against the indirect sym to the direct sym
2836              list.  Merge any entries against the same section.  */
2837           for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
2838             {
2839               struct elf32_arm_relocs_copied *q;
2840
2841               for (q = edir->relocs_copied; q != NULL; q = q->next)
2842                 if (q->section == p->section)
2843                   {
2844                     q->pc_count += p->pc_count;
2845                     q->count += p->count;
2846                     *pp = p->next;
2847                     break;
2848                   }
2849               if (q == NULL)
2850                 pp = &p->next;
2851             }
2852           *pp = edir->relocs_copied;
2853         }
2854
2855       edir->relocs_copied = eind->relocs_copied;
2856       eind->relocs_copied = NULL;
2857     }
2858
2859   if (ind->root.type == bfd_link_hash_indirect)
2860     {
2861       /* Copy over PLT info.  */
2862       edir->plt_thumb_refcount += eind->plt_thumb_refcount;
2863       eind->plt_thumb_refcount = 0;
2864       edir->plt_maybe_thumb_refcount += eind->plt_maybe_thumb_refcount;
2865       eind->plt_maybe_thumb_refcount = 0;
2866
2867       if (dir->got.refcount <= 0)
2868         {
2869           edir->tls_type = eind->tls_type;
2870           eind->tls_type = GOT_UNKNOWN;
2871         }
2872     }
2873
2874   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2875 }
2876
2877 /* Create an ARM elf linker hash table.  */
2878
2879 static struct bfd_link_hash_table *
2880 elf32_arm_link_hash_table_create (bfd *abfd)
2881 {
2882   struct elf32_arm_link_hash_table *ret;
2883   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
2884
2885   ret = bfd_malloc (amt);
2886   if (ret == NULL)
2887     return NULL;
2888
2889   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
2890                                       elf32_arm_link_hash_newfunc,
2891                                       sizeof (struct elf32_arm_link_hash_entry)))
2892     {
2893       free (ret);
2894       return NULL;
2895     }
2896
2897   ret->sgot = NULL;
2898   ret->sgotplt = NULL;
2899   ret->srelgot = NULL;
2900   ret->splt = NULL;
2901   ret->srelplt = NULL;
2902   ret->sdynbss = NULL;
2903   ret->srelbss = NULL;
2904   ret->srelplt2 = NULL;
2905   ret->thumb_glue_size = 0;
2906   ret->arm_glue_size = 0;
2907   ret->bx_glue_size = 0;
2908   memset (ret->bx_glue_offset, 0, sizeof (ret->bx_glue_offset));
2909   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
2910   ret->vfp11_erratum_glue_size = 0;
2911   ret->num_vfp11_fixes = 0;
2912   ret->fix_cortex_a8 = 0;
2913   ret->bfd_of_glue_owner = NULL;
2914   ret->byteswap_code = 0;
2915   ret->target1_is_rel = 0;
2916   ret->target2_reloc = R_ARM_NONE;
2917 #ifdef FOUR_WORD_PLT
2918   ret->plt_header_size = 16;
2919   ret->plt_entry_size = 16;
2920 #else
2921   ret->plt_header_size = 20;
2922   ret->plt_entry_size = 12;
2923 #endif
2924   ret->fix_v4bx = 0;
2925   ret->use_blx = 0;
2926   ret->vxworks_p = 0;
2927   ret->symbian_p = 0;
2928   ret->use_rel = 1;
2929   ret->sym_cache.abfd = NULL;
2930   ret->obfd = abfd;
2931   ret->tls_ldm_got.refcount = 0;
2932   ret->stub_bfd = NULL;
2933   ret->add_stub_section = NULL;
2934   ret->layout_sections_again = NULL;
2935   ret->stub_group = NULL;
2936   ret->bfd_count = 0;
2937   ret->top_index = 0;
2938   ret->input_list = NULL;
2939
2940   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2941                             sizeof (struct elf32_arm_stub_hash_entry)))
2942     {
2943       free (ret);
2944       return NULL;
2945     }
2946
2947   return &ret->root.root;
2948 }
2949
2950 /* Free the derived linker hash table.  */
2951
2952 static void
2953 elf32_arm_hash_table_free (struct bfd_link_hash_table *hash)
2954 {
2955   struct elf32_arm_link_hash_table *ret
2956     = (struct elf32_arm_link_hash_table *) hash;
2957
2958   bfd_hash_table_free (&ret->stub_hash_table);
2959   _bfd_generic_link_hash_table_free (hash);
2960 }
2961
2962 /* Determine if we're dealing with a Thumb only architecture.  */
2963
2964 static bfd_boolean
2965 using_thumb_only (struct elf32_arm_link_hash_table *globals)
2966 {
2967   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2968                                        Tag_CPU_arch);
2969   int profile;
2970
2971   if (arch != TAG_CPU_ARCH_V7)
2972     return FALSE;
2973
2974   profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2975                                       Tag_CPU_arch_profile);
2976
2977   return profile == 'M';
2978 }
2979
2980 /* Determine if we're dealing with a Thumb-2 object.  */
2981
2982 static bfd_boolean
2983 using_thumb2 (struct elf32_arm_link_hash_table *globals)
2984 {
2985   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2986                                        Tag_CPU_arch);
2987   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
2988 }
2989
2990 static bfd_boolean
2991 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
2992 {
2993   switch (stub_type)
2994     {
2995     case arm_stub_long_branch_thumb_only:
2996     case arm_stub_long_branch_v4t_thumb_arm:
2997     case arm_stub_short_branch_v4t_thumb_arm:
2998     case arm_stub_long_branch_v4t_thumb_arm_pic:
2999     case arm_stub_long_branch_thumb_only_pic:
3000       return TRUE;
3001     case arm_stub_none:
3002       BFD_FAIL ();
3003       return FALSE;
3004       break;
3005     default:
3006       return FALSE;
3007     }
3008 }
3009
3010 /* Determine the type of stub needed, if any, for a call.  */
3011
3012 static enum elf32_arm_stub_type
3013 arm_type_of_stub (struct bfd_link_info *info,
3014                   asection *input_sec,
3015                   const Elf_Internal_Rela *rel,
3016                   unsigned char st_type,
3017                   struct elf32_arm_link_hash_entry *hash,
3018                   bfd_vma destination,
3019                   asection *sym_sec,
3020                   bfd *input_bfd,
3021                   const char *name)
3022 {
3023   bfd_vma location;
3024   bfd_signed_vma branch_offset;
3025   unsigned int r_type;
3026   struct elf32_arm_link_hash_table * globals;
3027   int thumb2;
3028   int thumb_only;
3029   enum elf32_arm_stub_type stub_type = arm_stub_none;
3030   int use_plt = 0;
3031
3032   /* We don't know the actual type of destination in case it is of
3033      type STT_SECTION: give up.  */
3034   if (st_type == STT_SECTION)
3035     return stub_type;
3036
3037   globals = elf32_arm_hash_table (info);
3038
3039   thumb_only = using_thumb_only (globals);
3040
3041   thumb2 = using_thumb2 (globals);
3042
3043   /* Determine where the call point is.  */
3044   location = (input_sec->output_offset
3045               + input_sec->output_section->vma
3046               + rel->r_offset);
3047
3048   branch_offset = (bfd_signed_vma)(destination - location);
3049
3050   r_type = ELF32_R_TYPE (rel->r_info);
3051
3052   /* Keep a simpler condition, for the sake of clarity.  */
3053   if (globals->splt != NULL && hash != NULL && hash->root.plt.offset != (bfd_vma) -1)
3054     {
3055       use_plt = 1;
3056       /* Note when dealing with PLT entries: the main PLT stub is in
3057          ARM mode, so if the branch is in Thumb mode, another
3058          Thumb->ARM stub will be inserted later just before the ARM
3059          PLT stub. We don't take this extra distance into account
3060          here, because if a long branch stub is needed, we'll add a
3061          Thumb->Arm one and branch directly to the ARM PLT entry
3062          because it avoids spreading offset corrections in several
3063          places.  */
3064     }
3065
3066   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
3067     {
3068       /* Handle cases where:
3069          - this call goes too far (different Thumb/Thumb2 max
3070            distance)
3071          - it's a Thumb->Arm call and blx is not available, or it's a
3072            Thumb->Arm branch (not bl). A stub is needed in this case,
3073            but only if this call is not through a PLT entry. Indeed,
3074            PLT stubs handle mode switching already.
3075       */
3076       if ((!thumb2
3077             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3078                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3079           || (thumb2
3080               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3081                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3082           || ((st_type != STT_ARM_TFUNC)
3083               && (((r_type == R_ARM_THM_CALL) && !globals->use_blx)
3084                   || (r_type == R_ARM_THM_JUMP24))
3085               && !use_plt))
3086         {
3087           if (st_type == STT_ARM_TFUNC)
3088             {
3089               /* Thumb to thumb.  */
3090               if (!thumb_only)
3091                 {
3092                   stub_type = (info->shared | globals->pic_veneer)
3093                     /* PIC stubs.  */
3094                     ? ((globals->use_blx
3095                         && (r_type ==R_ARM_THM_CALL))
3096                        /* V5T and above. Stub starts with ARM code, so
3097                           we must be able to switch mode before
3098                           reaching it, which is only possible for 'bl'
3099                           (ie R_ARM_THM_CALL relocation).  */
3100                        ? arm_stub_long_branch_any_thumb_pic
3101                        /* On V4T, use Thumb code only.  */
3102                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
3103
3104                     /* non-PIC stubs.  */
3105                     : ((globals->use_blx
3106                         && (r_type ==R_ARM_THM_CALL))
3107                        /* V5T and above.  */
3108                        ? arm_stub_long_branch_any_any
3109                        /* V4T.  */
3110                        : arm_stub_long_branch_v4t_thumb_thumb);
3111                 }
3112               else
3113                 {
3114                   stub_type = (info->shared | globals->pic_veneer)
3115                     /* PIC stub.  */
3116                     ? arm_stub_long_branch_thumb_only_pic
3117                     /* non-PIC stub.  */
3118                     : arm_stub_long_branch_thumb_only;
3119                 }
3120             }
3121           else
3122             {
3123               /* Thumb to arm.  */
3124               if (sym_sec != NULL
3125                   && sym_sec->owner != NULL
3126                   && !INTERWORK_FLAG (sym_sec->owner))
3127                 {
3128                   (*_bfd_error_handler)
3129                     (_("%B(%s): warning: interworking not enabled.\n"
3130                        "  first occurrence: %B: Thumb call to ARM"),
3131                      sym_sec->owner, input_bfd, name);
3132                 }
3133
3134               stub_type = (info->shared | globals->pic_veneer)
3135                 /* PIC stubs.  */
3136                 ? ((globals->use_blx
3137                     && (r_type ==R_ARM_THM_CALL))
3138                    /* V5T and above.  */
3139                    ? arm_stub_long_branch_any_arm_pic
3140                    /* V4T PIC stub.  */
3141                    : arm_stub_long_branch_v4t_thumb_arm_pic)
3142
3143                 /* non-PIC stubs.  */
3144                 : ((globals->use_blx
3145                     && (r_type ==R_ARM_THM_CALL))
3146                    /* V5T and above.  */
3147                    ? arm_stub_long_branch_any_any
3148                    /* V4T.  */
3149                    : arm_stub_long_branch_v4t_thumb_arm);
3150
3151               /* Handle v4t short branches.  */
3152               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3153                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3154                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3155                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3156             }
3157         }
3158     }
3159   else if (r_type == R_ARM_CALL || r_type == R_ARM_JUMP24 || r_type == R_ARM_PLT32)
3160     {
3161       if (st_type == STT_ARM_TFUNC)
3162         {
3163           /* Arm to thumb.  */
3164
3165           if (sym_sec != NULL
3166               && sym_sec->owner != NULL
3167               && !INTERWORK_FLAG (sym_sec->owner))
3168             {
3169               (*_bfd_error_handler)
3170                 (_("%B(%s): warning: interworking not enabled.\n"
3171                    "  first occurrence: %B: ARM call to Thumb"),
3172                  sym_sec->owner, input_bfd, name);
3173             }
3174
3175           /* We have an extra 2-bytes reach because of
3176              the mode change (bit 24 (H) of BLX encoding).  */
3177           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3178               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3179               || ((r_type == R_ARM_CALL) && !globals->use_blx)
3180               || (r_type == R_ARM_JUMP24)
3181               || (r_type == R_ARM_PLT32))
3182             {
3183               stub_type = (info->shared | globals->pic_veneer)
3184                 /* PIC stubs.  */
3185                 ? ((globals->use_blx)
3186                    /* V5T and above.  */
3187                    ? arm_stub_long_branch_any_thumb_pic
3188                    /* V4T stub.  */
3189                    : arm_stub_long_branch_v4t_arm_thumb_pic)
3190
3191                 /* non-PIC stubs.  */
3192                 : ((globals->use_blx)
3193                    /* V5T and above.  */
3194                    ? arm_stub_long_branch_any_any
3195                    /* V4T.  */
3196                    : arm_stub_long_branch_v4t_arm_thumb);
3197             }
3198         }
3199       else
3200         {
3201           /* Arm to arm.  */
3202           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3203               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3204             {
3205               stub_type = (info->shared | globals->pic_veneer)
3206                 /* PIC stubs.  */
3207                 ? arm_stub_long_branch_any_arm_pic
3208                 /* non-PIC stubs.  */
3209                 : arm_stub_long_branch_any_any;
3210             }
3211         }
3212     }
3213
3214   return stub_type;
3215 }
3216
3217 /* Build a name for an entry in the stub hash table.  */
3218
3219 static char *
3220 elf32_arm_stub_name (const asection *input_section,
3221                      const asection *sym_sec,
3222                      const struct elf32_arm_link_hash_entry *hash,
3223                      const Elf_Internal_Rela *rel)
3224 {
3225   char *stub_name;
3226   bfd_size_type len;
3227
3228   if (hash)
3229     {
3230       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1;
3231       stub_name = bfd_malloc (len);
3232       if (stub_name != NULL)
3233         sprintf (stub_name, "%08x_%s+%x",
3234                  input_section->id & 0xffffffff,
3235                  hash->root.root.root.string,
3236                  (int) rel->r_addend & 0xffffffff);
3237     }
3238   else
3239     {
3240       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3241       stub_name = bfd_malloc (len);
3242       if (stub_name != NULL)
3243         sprintf (stub_name, "%08x_%x:%x+%x",
3244                  input_section->id & 0xffffffff,
3245                  sym_sec->id & 0xffffffff,
3246                  (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
3247                  (int) rel->r_addend & 0xffffffff);
3248     }
3249
3250   return stub_name;
3251 }
3252
3253 /* Look up an entry in the stub hash.  Stub entries are cached because
3254    creating the stub name takes a bit of time.  */
3255
3256 static struct elf32_arm_stub_hash_entry *
3257 elf32_arm_get_stub_entry (const asection *input_section,
3258                           const asection *sym_sec,
3259                           struct elf_link_hash_entry *hash,
3260                           const Elf_Internal_Rela *rel,
3261                           struct elf32_arm_link_hash_table *htab)
3262 {
3263   struct elf32_arm_stub_hash_entry *stub_entry;
3264   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
3265   const asection *id_sec;
3266
3267   if ((input_section->flags & SEC_CODE) == 0)
3268     return NULL;
3269
3270   /* If this input section is part of a group of sections sharing one
3271      stub section, then use the id of the first section in the group.
3272      Stub names need to include a section id, as there may well be
3273      more than one stub used to reach say, printf, and we need to
3274      distinguish between them.  */
3275   id_sec = htab->stub_group[input_section->id].link_sec;
3276
3277   if (h != NULL && h->stub_cache != NULL
3278       && h->stub_cache->h == h
3279       && h->stub_cache->id_sec == id_sec)
3280     {
3281       stub_entry = h->stub_cache;
3282     }
3283   else
3284     {
3285       char *stub_name;
3286
3287       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel);
3288       if (stub_name == NULL)
3289         return NULL;
3290
3291       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
3292                                         stub_name, FALSE, FALSE);
3293       if (h != NULL)
3294         h->stub_cache = stub_entry;
3295
3296       free (stub_name);
3297     }
3298
3299   return stub_entry;
3300 }
3301
3302 /* Find or create a stub section.  Returns a pointer to the stub section, and
3303    the section to which the stub section will be attached (in *LINK_SEC_P). 
3304    LINK_SEC_P may be NULL.  */
3305
3306 static asection *
3307 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
3308                                    struct elf32_arm_link_hash_table *htab)
3309 {
3310   asection *link_sec;
3311   asection *stub_sec;
3312
3313   link_sec = htab->stub_group[section->id].link_sec;
3314   stub_sec = htab->stub_group[section->id].stub_sec;
3315   if (stub_sec == NULL)
3316     {
3317       stub_sec = htab->stub_group[link_sec->id].stub_sec;
3318       if (stub_sec == NULL)
3319         {
3320           size_t namelen;
3321           bfd_size_type len;
3322           char *s_name;
3323
3324           namelen = strlen (link_sec->name);
3325           len = namelen + sizeof (STUB_SUFFIX);
3326           s_name = bfd_alloc (htab->stub_bfd, len);
3327           if (s_name == NULL)
3328             return NULL;
3329
3330           memcpy (s_name, link_sec->name, namelen);
3331           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3332           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3333           if (stub_sec == NULL)
3334             return NULL;
3335           htab->stub_group[link_sec->id].stub_sec = stub_sec;
3336         }
3337       htab->stub_group[section->id].stub_sec = stub_sec;
3338     }
3339   
3340   if (link_sec_p)
3341     *link_sec_p = link_sec;
3342   
3343   return stub_sec;
3344 }
3345
3346 /* Add a new stub entry to the stub hash.  Not all fields of the new
3347    stub entry are initialised.  */
3348
3349 static struct elf32_arm_stub_hash_entry *
3350 elf32_arm_add_stub (const char *stub_name,
3351                     asection *section,
3352                     struct elf32_arm_link_hash_table *htab)
3353 {
3354   asection *link_sec;
3355   asection *stub_sec;
3356   struct elf32_arm_stub_hash_entry *stub_entry;
3357
3358   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab);
3359   if (stub_sec == NULL)
3360     return NULL;
3361
3362   /* Enter this entry into the linker stub hash table.  */
3363   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3364                                      TRUE, FALSE);
3365   if (stub_entry == NULL)
3366     {
3367       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3368                              section->owner,
3369                              stub_name);
3370       return NULL;
3371     }
3372
3373   stub_entry->stub_sec = stub_sec;
3374   stub_entry->stub_offset = 0;
3375   stub_entry->id_sec = link_sec;
3376
3377   return stub_entry;
3378 }
3379
3380 /* Store an Arm insn into an output section not processed by
3381    elf32_arm_write_section.  */
3382
3383 static void
3384 put_arm_insn (struct elf32_arm_link_hash_table * htab,
3385               bfd * output_bfd, bfd_vma val, void * ptr)
3386 {
3387   if (htab->byteswap_code != bfd_little_endian (output_bfd))
3388     bfd_putl32 (val, ptr);
3389   else
3390     bfd_putb32 (val, ptr);
3391 }
3392
3393 /* Store a 16-bit Thumb insn into an output section not processed by
3394    elf32_arm_write_section.  */
3395
3396 static void
3397 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
3398                 bfd * output_bfd, bfd_vma val, void * ptr)
3399 {
3400   if (htab->byteswap_code != bfd_little_endian (output_bfd))
3401     bfd_putl16 (val, ptr);
3402   else
3403     bfd_putb16 (val, ptr);
3404 }
3405
3406 static bfd_reloc_status_type elf32_arm_final_link_relocate
3407   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
3408    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
3409    const char *, int, struct elf_link_hash_entry *, bfd_boolean *, char **);
3410
3411 static bfd_boolean
3412 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
3413                     void * in_arg)
3414 {
3415 #define MAXRELOCS 2
3416   struct elf32_arm_stub_hash_entry *stub_entry;
3417   struct bfd_link_info *info;
3418   struct elf32_arm_link_hash_table *htab;
3419   asection *stub_sec;
3420   bfd *stub_bfd;
3421   bfd_vma stub_addr;
3422   bfd_byte *loc;
3423   bfd_vma sym_value;
3424   int template_size;
3425   int size;
3426   const insn_sequence *template;
3427   int i;
3428   struct elf32_arm_link_hash_table * globals;
3429   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3430   int stub_reloc_offset[MAXRELOCS] = {0, 0};
3431   int nrelocs = 0;
3432
3433   /* Massage our args to the form they really have.  */
3434   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
3435   info = (struct bfd_link_info *) in_arg;
3436
3437   globals = elf32_arm_hash_table (info);
3438
3439   htab = elf32_arm_hash_table (info);
3440   stub_sec = stub_entry->stub_sec;
3441
3442   if ((htab->fix_cortex_a8 < 0)
3443       != (stub_entry->stub_type >= arm_stub_a8_veneer_lwm))
3444     /* We have to do the a8 fixes last, as they are less aligned than
3445        the other veneers.  */
3446     return TRUE;
3447   
3448   /* Make a note of the offset within the stubs for this entry.  */
3449   stub_entry->stub_offset = stub_sec->size;
3450   loc = stub_sec->contents + stub_entry->stub_offset;
3451
3452   stub_bfd = stub_sec->owner;
3453
3454   /* This is the address of the start of the stub.  */
3455   stub_addr = stub_sec->output_section->vma + stub_sec->output_offset
3456     + stub_entry->stub_offset;
3457
3458   /* This is the address of the stub destination.  */
3459   sym_value = (stub_entry->target_value
3460                + stub_entry->target_section->output_offset
3461                + stub_entry->target_section->output_section->vma);
3462
3463   template = stub_entry->stub_template;
3464   template_size = stub_entry->stub_template_size;
3465
3466   size = 0;
3467   for (i = 0; i < template_size; i++)
3468     {
3469       switch (template[i].type)
3470         {
3471         case THUMB16_TYPE:
3472           {
3473             bfd_vma data = template[i].data;
3474             if (template[i].reloc_addend != 0)
3475               {
3476                 /* We've borrowed the reloc_addend field to mean we should
3477                    insert a condition code into this (Thumb-1 branch)
3478                    instruction.  See THUMB16_BCOND_INSN.  */
3479                 BFD_ASSERT ((data & 0xff00) == 0xd000);
3480                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
3481               }
3482             put_thumb_insn (globals, stub_bfd, data, loc + size);
3483             size += 2;
3484           }
3485           break;
3486
3487         case THUMB32_TYPE:
3488           put_thumb_insn (globals, stub_bfd, (template[i].data >> 16) & 0xffff,
3489                           loc + size);
3490           put_thumb_insn (globals, stub_bfd, template[i].data & 0xffff,
3491                           loc + size + 2);
3492           if (template[i].r_type != R_ARM_NONE)
3493             {
3494               stub_reloc_idx[nrelocs] = i;
3495               stub_reloc_offset[nrelocs++] = size;
3496             }
3497           size += 4;
3498           break;
3499
3500         case ARM_TYPE:
3501           put_arm_insn (globals, stub_bfd, template[i].data, loc + size);
3502           /* Handle cases where the target is encoded within the
3503              instruction.  */
3504           if (template[i].r_type == R_ARM_JUMP24)
3505             {
3506               stub_reloc_idx[nrelocs] = i;
3507               stub_reloc_offset[nrelocs++] = size;
3508             }
3509           size += 4;
3510           break;
3511
3512         case DATA_TYPE:
3513           bfd_put_32 (stub_bfd, template[i].data, loc + size);
3514           stub_reloc_idx[nrelocs] = i;
3515           stub_reloc_offset[nrelocs++] = size;
3516           size += 4;
3517           break;
3518
3519         default:
3520           BFD_FAIL ();
3521           return FALSE;
3522         }
3523     }
3524
3525   stub_sec->size += size;
3526
3527   /* Stub size has already been computed in arm_size_one_stub. Check
3528      consistency.  */
3529   BFD_ASSERT (size == stub_entry->stub_size);
3530
3531   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
3532   if (stub_entry->st_type == STT_ARM_TFUNC)
3533     sym_value |= 1;
3534
3535   /* Assume there is at least one and at most MAXRELOCS entries to relocate
3536      in each stub.  */
3537   BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3538
3539   for (i = 0; i < nrelocs; i++)
3540     if (template[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP24
3541         || template[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP19
3542         || template[stub_reloc_idx[i]].r_type == R_ARM_THM_CALL
3543         || template[stub_reloc_idx[i]].r_type == R_ARM_THM_XPC22)
3544       {
3545         Elf_Internal_Rela rel;
3546         bfd_boolean unresolved_reloc;
3547         char *error_message;
3548         int sym_flags
3549           = (template[stub_reloc_idx[i]].r_type != R_ARM_THM_XPC22)
3550             ? STT_ARM_TFUNC : 0;
3551         bfd_vma points_to = sym_value + stub_entry->target_addend;
3552
3553         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3554         rel.r_info = ELF32_R_INFO (0, template[stub_reloc_idx[i]].r_type);
3555         rel.r_addend = template[stub_reloc_idx[i]].reloc_addend;
3556
3557         if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
3558           /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
3559              template should refer back to the instruction after the original
3560              branch.  */
3561           points_to = sym_value;
3562
3563         /* There may be unintended consequences if this is not true.  */
3564         BFD_ASSERT (stub_entry->h == NULL);
3565
3566         /* Note: _bfd_final_link_relocate doesn't handle these relocations
3567            properly.  We should probably use this function unconditionally,
3568            rather than only for certain relocations listed in the enclosing
3569            conditional, for the sake of consistency.  */
3570         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
3571             (template[stub_reloc_idx[i]].r_type),
3572           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
3573           points_to, info, stub_entry->target_section, "", sym_flags,
3574           (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
3575           &error_message);
3576       }
3577     else
3578       {
3579         _bfd_final_link_relocate (elf32_arm_howto_from_type
3580             (template[stub_reloc_idx[i]].r_type), stub_bfd, stub_sec,
3581           stub_sec->contents, stub_entry->stub_offset + stub_reloc_offset[i],
3582           sym_value + stub_entry->target_addend,
3583           template[stub_reloc_idx[i]].reloc_addend);
3584       }
3585
3586   return TRUE;
3587 #undef MAXRELOCS
3588 }
3589
3590 /* Calculate the template, template size and instruction size for a stub.
3591    Return value is the instruction size.  */
3592
3593 static unsigned int
3594 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
3595                              const insn_sequence **stub_template,
3596                              int *stub_template_size)
3597 {
3598   const insn_sequence *template = NULL;
3599   int template_size = 0, i;
3600   unsigned int size;
3601
3602   template = stub_definitions[stub_type].template;
3603   template_size = stub_definitions[stub_type].template_size;
3604
3605   size = 0;
3606   for (i = 0; i < template_size; i++)
3607     {
3608       switch (template[i].type)
3609         {
3610         case THUMB16_TYPE:
3611           size += 2;
3612           break;
3613
3614         case ARM_TYPE:
3615         case THUMB32_TYPE:
3616         case DATA_TYPE:
3617           size += 4;
3618           break;
3619
3620         default:
3621           BFD_FAIL ();
3622           return FALSE;
3623         }
3624     }
3625
3626   if (stub_template)
3627     *stub_template = template;
3628
3629   if (stub_template_size)
3630     *stub_template_size = template_size;
3631
3632   return size;
3633 }
3634
3635 /* As above, but don't actually build the stub.  Just bump offset so
3636    we know stub section sizes.  */
3637
3638 static bfd_boolean
3639 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
3640                    void * in_arg)
3641 {
3642   struct elf32_arm_stub_hash_entry *stub_entry;
3643   struct elf32_arm_link_hash_table *htab;
3644   const insn_sequence *template;
3645   int template_size, size;
3646
3647   /* Massage our args to the form they really have.  */
3648   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
3649   htab = (struct elf32_arm_link_hash_table *) in_arg;
3650
3651   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
3652              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
3653
3654   size = find_stub_size_and_template (stub_entry->stub_type, &template,
3655                                       &template_size);
3656
3657   stub_entry->stub_size = size;
3658   stub_entry->stub_template = template;
3659   stub_entry->stub_template_size = template_size;
3660
3661   size = (size + 7) & ~7;
3662   stub_entry->stub_sec->size += size;
3663
3664   return TRUE;
3665 }
3666
3667 /* External entry points for sizing and building linker stubs.  */
3668
3669 /* Set up various things so that we can make a list of input sections
3670    for each output section included in the link.  Returns -1 on error,
3671    0 when no stubs will be needed, and 1 on success.  */
3672
3673 int
3674 elf32_arm_setup_section_lists (bfd *output_bfd,
3675                                struct bfd_link_info *info)
3676 {
3677   bfd *input_bfd;
3678   unsigned int bfd_count;
3679   int top_id, top_index;
3680   asection *section;
3681   asection **input_list, **list;
3682   bfd_size_type amt;
3683   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3684
3685   if (! is_elf_hash_table (htab))
3686     return 0;
3687
3688   /* Count the number of input BFDs and find the top input section id.  */
3689   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3690        input_bfd != NULL;
3691        input_bfd = input_bfd->link_next)
3692     {
3693       bfd_count += 1;
3694       for (section = input_bfd->sections;
3695            section != NULL;
3696            section = section->next)
3697         {
3698           if (top_id < section->id)
3699             top_id = section->id;
3700         }
3701     }
3702   htab->bfd_count = bfd_count;
3703
3704   amt = sizeof (struct map_stub) * (top_id + 1);
3705   htab->stub_group = bfd_zmalloc (amt);
3706   if (htab->stub_group == NULL)
3707     return -1;
3708
3709   /* We can't use output_bfd->section_count here to find the top output
3710      section index as some sections may have been removed, and
3711      _bfd_strip_section_from_output doesn't renumber the indices.  */
3712   for (section = output_bfd->sections, top_index = 0;
3713        section != NULL;
3714        section = section->next)
3715     {
3716       if (top_index < section->index)
3717         top_index = section->index;
3718     }
3719
3720   htab->top_index = top_index;
3721   amt = sizeof (asection *) * (top_index + 1);
3722   input_list = bfd_malloc (amt);
3723   htab->input_list = input_list;
3724   if (input_list == NULL)
3725     return -1;
3726
3727   /* For sections we aren't interested in, mark their entries with a
3728      value we can check later.  */
3729   list = input_list + top_index;
3730   do
3731     *list = bfd_abs_section_ptr;
3732   while (list-- != input_list);
3733
3734   for (section = output_bfd->sections;
3735        section != NULL;
3736        section = section->next)
3737     {
3738       if ((section->flags & SEC_CODE) != 0)
3739         input_list[section->index] = NULL;
3740     }
3741
3742   return 1;
3743 }
3744
3745 /* The linker repeatedly calls this function for each input section,
3746    in the order that input sections are linked into output sections.
3747    Build lists of input sections to determine groupings between which
3748    we may insert linker stubs.  */
3749
3750 void
3751 elf32_arm_next_input_section (struct bfd_link_info *info,
3752                               asection *isec)
3753 {
3754   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3755
3756   if (isec->output_section->index <= htab->top_index)
3757     {
3758       asection **list = htab->input_list + isec->output_section->index;
3759
3760       if (*list != bfd_abs_section_ptr)
3761         {
3762           /* Steal the link_sec pointer for our list.  */
3763 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3764           /* This happens to make the list in reverse order,
3765              which we reverse later.  */
3766           PREV_SEC (isec) = *list;
3767           *list = isec;
3768         }
3769     }
3770 }
3771
3772 /* See whether we can group stub sections together.  Grouping stub
3773    sections may result in fewer stubs.  More importantly, we need to
3774    put all .init* and .fini* stubs at the end of the .init or
3775    .fini output sections respectively, because glibc splits the
3776    _init and _fini functions into multiple parts.  Putting a stub in
3777    the middle of a function is not a good idea.  */
3778
3779 static void
3780 group_sections (struct elf32_arm_link_hash_table *htab,
3781                 bfd_size_type stub_group_size,
3782                 bfd_boolean stubs_always_after_branch)
3783 {
3784   asection **list = htab->input_list;
3785
3786   do
3787     {
3788       asection *tail = *list;
3789       asection *head;
3790
3791       if (tail == bfd_abs_section_ptr)
3792         continue;
3793
3794       /* Reverse the list: we must avoid placing stubs at the
3795          beginning of the section because the beginning of the text
3796          section may be required for an interrupt vector in bare metal
3797          code.  */
3798 #define NEXT_SEC PREV_SEC
3799       head = NULL;
3800       while (tail != NULL)
3801         {
3802           /* Pop from tail.  */
3803           asection *item = tail;
3804           tail = PREV_SEC (item);
3805
3806           /* Push on head.  */
3807           NEXT_SEC (item) = head;
3808           head = item;
3809         }
3810
3811       while (head != NULL)
3812         {
3813           asection *curr;
3814           asection *next;
3815           bfd_vma stub_group_start = head->output_offset;
3816           bfd_vma end_of_next;
3817
3818           curr = head;
3819           while (NEXT_SEC (curr) != NULL)
3820             {
3821               next = NEXT_SEC (curr);
3822               end_of_next = next->output_offset + next->size;
3823               if (end_of_next - stub_group_start >= stub_group_size)
3824                 /* End of NEXT is too far from start, so stop.  */
3825                 break;
3826               /* Add NEXT to the group.  */
3827               curr = next;
3828             }
3829
3830           /* OK, the size from the start to the start of CURR is less
3831              than stub_group_size and thus can be handled by one stub
3832              section.  (Or the head section is itself larger than
3833              stub_group_size, in which case we may be toast.)
3834              We should really be keeping track of the total size of
3835              stubs added here, as stubs contribute to the final output
3836              section size.  */
3837           do
3838             {
3839               next = NEXT_SEC (head);
3840               /* Set up this stub group.  */
3841               htab->stub_group[head->id].link_sec = curr;
3842             }
3843           while (head != curr && (head = next) != NULL);
3844
3845           /* But wait, there's more!  Input sections up to stub_group_size
3846              bytes after the stub section can be handled by it too.  */
3847           if (!stubs_always_after_branch)
3848             {
3849               stub_group_start = curr->output_offset + curr->size;
3850
3851               while (next != NULL)
3852                 {
3853                   end_of_next = next->output_offset + next->size;
3854                   if (end_of_next - stub_group_start >= stub_group_size)
3855                     /* End of NEXT is too far from stubs, so stop.  */
3856                     break;
3857                   /* Add NEXT to the stub group.  */
3858                   head = next;
3859                   next = NEXT_SEC (head);
3860                   htab->stub_group[head->id].link_sec = curr;
3861                 }
3862             }
3863           head = next;
3864         }
3865     }
3866   while (list++ != htab->input_list + htab->top_index);
3867
3868   free (htab->input_list);
3869 #undef PREV_SEC
3870 #undef NEXT_SEC
3871 }
3872
3873 /* Comparison function for sorting/searching relocations relating to Cortex-A8
3874    erratum fix.  */
3875
3876 static int
3877 a8_reloc_compare (const void *a, const void *b)
3878 {
3879   const struct a8_erratum_reloc *ra = a, *rb = b;
3880
3881   if (ra->from < rb->from)
3882     return -1;
3883   else if (ra->from > rb->from)
3884     return 1;
3885   else
3886     return 0;
3887 }
3888
3889 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
3890                                                     const char *, char **);
3891
3892 /* Helper function to scan code for sequences which might trigger the Cortex-A8
3893    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
3894    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
3895    otherwise.  */
3896
3897 static bfd_boolean
3898 cortex_a8_erratum_scan (bfd *input_bfd,
3899                         struct bfd_link_info *info,
3900                         struct a8_erratum_fix **a8_fixes_p,
3901                         unsigned int *num_a8_fixes_p,
3902                         unsigned int *a8_fix_table_size_p,
3903                         struct a8_erratum_reloc *a8_relocs,
3904                         unsigned int num_a8_relocs,
3905                         unsigned prev_num_a8_fixes,
3906                         bfd_boolean *stub_changed_p)
3907 {
3908   asection *section;
3909   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3910   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
3911   unsigned int num_a8_fixes = *num_a8_fixes_p;
3912   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
3913
3914   for (section = input_bfd->sections;
3915        section != NULL;
3916        section = section->next)
3917     {
3918       bfd_byte *contents = NULL;
3919       struct _arm_elf_section_data *sec_data;
3920       unsigned int span;
3921       bfd_vma base_vma;
3922
3923       if (elf_section_type (section) != SHT_PROGBITS
3924           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3925           || (section->flags & SEC_EXCLUDE) != 0
3926           || (section->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3927           || (section->output_section == bfd_abs_section_ptr))
3928         continue;
3929
3930       base_vma = section->output_section->vma + section->output_offset;
3931
3932       if (elf_section_data (section)->this_hdr.contents != NULL)
3933         contents = elf_section_data (section)->this_hdr.contents;
3934       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3935         return TRUE;
3936
3937       sec_data = elf32_arm_section_data (section);
3938
3939       for (span = 0; span < sec_data->mapcount; span++)
3940         {
3941           unsigned int span_start = sec_data->map[span].vma;
3942           unsigned int span_end = (span == sec_data->mapcount - 1)
3943             ? section->size : sec_data->map[span + 1].vma;
3944           unsigned int i;
3945           char span_type = sec_data->map[span].type;
3946           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
3947
3948           if (span_type != 't')
3949             continue;
3950
3951           /* Span is entirely within a single 4KB region: skip scanning.  */
3952           if (((base_vma + span_start) & ~0xfff)
3953               == ((base_vma + span_end) & ~0xfff))
3954             continue;
3955
3956           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
3957
3958                * The opcode is BLX.W, BL.W, B.W, Bcc.W
3959                * The branch target is in the same 4KB region as the
3960                  first half of the branch.
3961                * The instruction before the branch is a 32-bit
3962                  length non-branch instruction.  */
3963           for (i = span_start; i < span_end;)
3964             {
3965               unsigned int insn = bfd_getl16 (&contents[i]);
3966               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
3967               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
3968
3969               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
3970                 insn_32bit = TRUE;
3971
3972               if (insn_32bit)
3973                 {
3974                   /* Load the rest of the insn (in manual-friendly order).  */
3975                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
3976
3977                   /* Encoding T4: B<c>.W.  */
3978                   is_b = (insn & 0xf800d000) == 0xf0009000;
3979                   /* Encoding T1: BL<c>.W.  */
3980                   is_bl = (insn & 0xf800d000) == 0xf000d000;
3981                   /* Encoding T2: BLX<c>.W.  */
3982                   is_blx = (insn & 0xf800d000) == 0xf000c000;
3983                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
3984                   is_bcc = (insn & 0xf800d000) == 0xf0008000
3985                            && (insn & 0x07f00000) != 0x03800000;
3986                 }
3987
3988               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
3989                            
3990               if (((base_vma + i) & 0xfff) == 0xffe
3991                   && insn_32bit
3992                   && is_32bit_branch
3993                   && last_was_32bit
3994                   && ! last_was_branch)
3995                 {
3996                   bfd_signed_vma offset;
3997                   bfd_boolean force_target_arm = FALSE;
3998                   bfd_boolean force_target_thumb = FALSE;
3999                   bfd_vma target;
4000                   enum elf32_arm_stub_type stub_type = arm_stub_none;
4001                   struct a8_erratum_reloc key, *found;
4002
4003                   key.from = base_vma + i;
4004                   found = bsearch (&key, a8_relocs, num_a8_relocs,
4005                                    sizeof (struct a8_erratum_reloc),
4006                                    &a8_reloc_compare);
4007
4008                   if (found)
4009                     {
4010                       char *error_message = NULL;
4011                       struct elf_link_hash_entry *entry;
4012
4013                       /* We don't care about the error returned from this
4014                          function, only if there is glue or not.  */
4015                       entry = find_thumb_glue (info, found->sym_name,
4016                                                &error_message);
4017
4018                       if (entry)
4019                         found->non_a8_stub = TRUE;
4020
4021                       if (found->r_type == R_ARM_THM_CALL
4022                           && found->st_type != STT_ARM_TFUNC)
4023                         force_target_arm = TRUE;
4024                       else if (found->r_type == R_ARM_THM_CALL
4025                                && found->st_type == STT_ARM_TFUNC)
4026                         force_target_thumb = TRUE;
4027                     }
4028
4029                   /* Check if we have an offending branch instruction.  */
4030
4031                   if (found && found->non_a8_stub)
4032                     /* We've already made a stub for this instruction, e.g.
4033                        it's a long branch or a Thumb->ARM stub.  Assume that
4034                        stub will suffice to work around the A8 erratum (see
4035                        setting of always_after_branch above).  */
4036                     ;
4037                   else if (is_bcc)
4038                     {
4039                       offset = (insn & 0x7ff) << 1;
4040                       offset |= (insn & 0x3f0000) >> 4;
4041                       offset |= (insn & 0x2000) ? 0x40000 : 0;
4042                       offset |= (insn & 0x800) ? 0x80000 : 0;
4043                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
4044                       if (offset & 0x100000)
4045                         offset |= ~ ((bfd_signed_vma) 0xfffff);
4046                       stub_type = arm_stub_a8_veneer_b_cond;
4047                     }
4048                   else if (is_b || is_bl || is_blx)
4049                     {
4050                       int s = (insn & 0x4000000) != 0;
4051                       int j1 = (insn & 0x2000) != 0;
4052                       int j2 = (insn & 0x800) != 0;
4053                       int i1 = !(j1 ^ s);
4054                       int i2 = !(j2 ^ s);
4055
4056                       offset = (insn & 0x7ff) << 1;
4057                       offset |= (insn & 0x3ff0000) >> 4;
4058                       offset |= i2 << 22;
4059                       offset |= i1 << 23;
4060                       offset |= s << 24;
4061                       if (offset & 0x1000000)
4062                         offset |= ~ ((bfd_signed_vma) 0xffffff);
4063
4064                       if (is_blx)
4065                         offset &= ~ ((bfd_signed_vma) 3);
4066
4067                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
4068                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
4069                     }
4070
4071                   if (stub_type != arm_stub_none)
4072                     {
4073                       bfd_vma pc_for_insn = base_vma + i + 4;
4074
4075                       /* The original instruction is a BL, but the target is
4076                          an ARM instruction.  If we were not making a stub,
4077                          the BL would have been converted to a BLX.  Use the
4078                          BLX stub instead in that case.  */
4079                       if (htab->use_blx && force_target_arm
4080                           && stub_type == arm_stub_a8_veneer_bl)
4081                         {
4082                           stub_type = arm_stub_a8_veneer_blx;
4083                           is_blx = TRUE;
4084                           is_bl = FALSE;
4085                         }
4086                       /* Conversely, if the original instruction was
4087                          BLX but the target is Thumb mode, use the BL
4088                          stub.  */
4089                       else if (force_target_thumb
4090                                && stub_type == arm_stub_a8_veneer_blx)
4091                         {
4092                           stub_type = arm_stub_a8_veneer_bl;
4093                           is_blx = FALSE;
4094                           is_bl = TRUE;
4095                         }
4096
4097                       if (is_blx)
4098                         pc_for_insn &= ~ ((bfd_vma) 3);
4099
4100                       /* If we found a relocation, use the proper destination,
4101                          not the offset in the (unrelocated) instruction.
4102                          Note this is always done if we switched the stub type
4103                          above.  */
4104                       if (found)
4105                         offset =
4106                           (bfd_signed_vma) (found->destination - pc_for_insn);
4107
4108                       target = pc_for_insn + offset;
4109
4110                       /* The BLX stub is ARM-mode code.  Adjust the offset to
4111                          take the different PC value (+8 instead of +4) into
4112                          account.  */
4113                       if (stub_type == arm_stub_a8_veneer_blx)
4114                         offset += 4;
4115
4116                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
4117                         {
4118                           char *stub_name = NULL;
4119
4120                           if (num_a8_fixes == a8_fix_table_size)
4121                             {
4122                               a8_fix_table_size *= 2;
4123                               a8_fixes = bfd_realloc (a8_fixes,
4124                                 sizeof (struct a8_erratum_fix)
4125                                 * a8_fix_table_size);
4126                             }
4127
4128                           if (num_a8_fixes < prev_num_a8_fixes)
4129                             {
4130                               /* If we're doing a subsequent scan,
4131                                  check if we've found the same fix as
4132                                  before, and try and reuse the stub
4133                                  name.  */
4134                               stub_name = a8_fixes[num_a8_fixes].stub_name;
4135                               if ((a8_fixes[num_a8_fixes].section != section)
4136                                   || (a8_fixes[num_a8_fixes].offset != i))
4137                                 {
4138                                   free (stub_name);
4139                                   stub_name = NULL;
4140                                   *stub_changed_p = TRUE;
4141                                 }
4142                             }
4143
4144                           if (!stub_name)
4145                             {
4146                               stub_name = bfd_malloc (8 + 1 + 8 + 1);
4147                               if (stub_name != NULL)
4148                                 sprintf (stub_name, "%x:%x", section->id, i);
4149                             }
4150
4151                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
4152                           a8_fixes[num_a8_fixes].section = section;
4153                           a8_fixes[num_a8_fixes].offset = i;
4154                           a8_fixes[num_a8_fixes].addend = offset;
4155                           a8_fixes[num_a8_fixes].orig_insn = insn;
4156                           a8_fixes[num_a8_fixes].stub_name = stub_name;
4157                           a8_fixes[num_a8_fixes].stub_type = stub_type;
4158
4159                           num_a8_fixes++;
4160                         }
4161                     }
4162                 }
4163
4164               i += insn_32bit ? 4 : 2;
4165               last_was_32bit = insn_32bit;
4166               last_was_branch = is_32bit_branch;
4167             }
4168         }
4169
4170       if (elf_section_data (section)->this_hdr.contents == NULL)
4171         free (contents);
4172     }
4173   
4174   *a8_fixes_p = a8_fixes;
4175   *num_a8_fixes_p = num_a8_fixes;
4176   *a8_fix_table_size_p = a8_fix_table_size;
4177   
4178   return FALSE;
4179 }
4180
4181 /* Determine and set the size of the stub section for a final link.
4182
4183    The basic idea here is to examine all the relocations looking for
4184    PC-relative calls to a target that is unreachable with a "bl"
4185    instruction.  */
4186
4187 bfd_boolean
4188 elf32_arm_size_stubs (bfd *output_bfd,
4189                       bfd *stub_bfd,
4190                       struct bfd_link_info *info,
4191                       bfd_signed_vma group_size,
4192                       asection * (*add_stub_section) (const char *, asection *),
4193                       void (*layout_sections_again) (void))
4194 {
4195   bfd_size_type stub_group_size;
4196   bfd_boolean stubs_always_after_branch;
4197   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4198   struct a8_erratum_fix *a8_fixes = NULL;
4199   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
4200   struct a8_erratum_reloc *a8_relocs = NULL;
4201   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
4202
4203   if (htab->fix_cortex_a8)
4204     {
4205       a8_fixes = bfd_zmalloc (sizeof (struct a8_erratum_fix)
4206                               * a8_fix_table_size);
4207       a8_relocs = bfd_zmalloc (sizeof (struct a8_erratum_reloc)
4208                                * a8_reloc_table_size);
4209     }
4210
4211   /* Propagate mach to stub bfd, because it may not have been
4212      finalized when we created stub_bfd.  */
4213   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
4214                      bfd_get_mach (output_bfd));
4215
4216   /* Stash our params away.  */
4217   htab->stub_bfd = stub_bfd;
4218   htab->add_stub_section = add_stub_section;
4219   htab->layout_sections_again = layout_sections_again;
4220   stubs_always_after_branch = group_size < 0;
4221
4222   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
4223      as the first half of a 32-bit branch straddling two 4K pages.  This is a
4224      crude way of enforcing that.  */
4225   if (htab->fix_cortex_a8)
4226     stubs_always_after_branch = 1;
4227
4228   if (group_size < 0)
4229     stub_group_size = -group_size;
4230   else
4231     stub_group_size = group_size;
4232
4233   if (stub_group_size == 1)
4234     {
4235       /* Default values.  */
4236       /* Thumb branch range is +-4MB has to be used as the default
4237          maximum size (a given section can contain both ARM and Thumb
4238          code, so the worst case has to be taken into account).
4239
4240          This value is 24K less than that, which allows for 2025
4241          12-byte stubs.  If we exceed that, then we will fail to link.
4242          The user will have to relink with an explicit group size
4243          option.  */
4244       stub_group_size = 4170000;
4245     }
4246
4247   group_sections (htab, stub_group_size, stubs_always_after_branch);
4248
4249   /* If we're applying the cortex A8 fix, we need to determine the
4250      program header size now, because we cannot change it later --
4251      that could alter section placements.  Notice the A8 erratum fix
4252      ends up requiring the section addresses to remain unchanged
4253      modulo the page size.  That's something we cannot represent
4254      inside BFD, and we don't want to force the section alignment to
4255      be the page size.  */
4256   if (htab->fix_cortex_a8)
4257     (*htab->layout_sections_again) ();
4258
4259   while (1)
4260     {
4261       bfd *input_bfd;
4262       unsigned int bfd_indx;
4263       asection *stub_sec;
4264       bfd_boolean stub_changed = FALSE;
4265       unsigned prev_num_a8_fixes = num_a8_fixes;
4266
4267       num_a8_fixes = 0;
4268       for (input_bfd = info->input_bfds, bfd_indx = 0;
4269            input_bfd != NULL;
4270            input_bfd = input_bfd->link_next, bfd_indx++)
4271         {
4272           Elf_Internal_Shdr *symtab_hdr;
4273           asection *section;
4274           Elf_Internal_Sym *local_syms = NULL;
4275
4276           num_a8_relocs = 0;
4277
4278           /* We'll need the symbol table in a second.  */
4279           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4280           if (symtab_hdr->sh_info == 0)
4281             continue;
4282
4283           /* Walk over each section attached to the input bfd.  */
4284           for (section = input_bfd->sections;
4285                section != NULL;
4286                section = section->next)
4287             {
4288               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4289
4290               /* If there aren't any relocs, then there's nothing more
4291                  to do.  */
4292               if ((section->flags & SEC_RELOC) == 0
4293                   || section->reloc_count == 0
4294                   || (section->flags & SEC_CODE) == 0)
4295                 continue;
4296
4297               /* If this section is a link-once section that will be
4298                  discarded, then don't create any stubs.  */
4299               if (section->output_section == NULL
4300                   || section->output_section->owner != output_bfd)
4301                 continue;
4302
4303               /* Get the relocs.  */
4304               internal_relocs
4305                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
4306                                              NULL, info->keep_memory);
4307               if (internal_relocs == NULL)
4308                 goto error_ret_free_local;
4309
4310               /* Now examine each relocation.  */
4311               irela = internal_relocs;
4312               irelaend = irela + section->reloc_count;
4313               for (; irela < irelaend; irela++)
4314                 {
4315                   unsigned int r_type, r_indx;
4316                   enum elf32_arm_stub_type stub_type;
4317                   struct elf32_arm_stub_hash_entry *stub_entry;
4318                   asection *sym_sec;
4319                   bfd_vma sym_value;
4320                   bfd_vma destination;
4321                   struct elf32_arm_link_hash_entry *hash;
4322                   const char *sym_name;
4323                   char *stub_name;
4324                   const asection *id_sec;
4325                   unsigned char st_type;
4326                   bfd_boolean created_stub = FALSE;
4327
4328                   r_type = ELF32_R_TYPE (irela->r_info);
4329                   r_indx = ELF32_R_SYM (irela->r_info);
4330
4331                   if (r_type >= (unsigned int) R_ARM_max)
4332                     {
4333                       bfd_set_error (bfd_error_bad_value);
4334                     error_ret_free_internal:
4335                       if (elf_section_data (section)->relocs == NULL)
4336                         free (internal_relocs);
4337                       goto error_ret_free_local;
4338                     }
4339
4340                   /* Only look for stubs on branch instructions.  */
4341                   if ((r_type != (unsigned int) R_ARM_CALL)
4342                       && (r_type != (unsigned int) R_ARM_THM_CALL)
4343                       && (r_type != (unsigned int) R_ARM_JUMP24)
4344                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
4345                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
4346                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
4347                       && (r_type != (unsigned int) R_ARM_PLT32))
4348                     continue;
4349
4350                   /* Now determine the call target, its name, value,
4351                      section.  */
4352                   sym_sec = NULL;
4353                   sym_value = 0;
4354                   destination = 0;
4355                   hash = NULL;
4356                   sym_name = NULL;
4357                   if (r_indx < symtab_hdr->sh_info)
4358                     {
4359                       /* It's a local symbol.  */
4360                       Elf_Internal_Sym *sym;
4361                       Elf_Internal_Shdr *hdr;
4362
4363                       if (local_syms == NULL)
4364                         {
4365                           local_syms
4366                             = (Elf_Internal_Sym *) symtab_hdr->contents;
4367                           if (local_syms == NULL)
4368                             local_syms
4369                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4370                                                       symtab_hdr->sh_info, 0,
4371                                                       NULL, NULL, NULL);
4372                           if (local_syms == NULL)
4373                             goto error_ret_free_internal;
4374                         }
4375
4376                       sym = local_syms + r_indx;
4377                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4378                       sym_sec = hdr->bfd_section;
4379                       if (!sym_sec)
4380                         /* This is an undefined symbol.  It can never
4381                            be resolved. */
4382                         continue;
4383                   
4384                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4385                         sym_value = sym->st_value;
4386                       destination = (sym_value + irela->r_addend
4387                                      + sym_sec->output_offset
4388                                      + sym_sec->output_section->vma);
4389                       st_type = ELF_ST_TYPE (sym->st_info);
4390                       sym_name
4391                         = bfd_elf_string_from_elf_section (input_bfd,
4392                                                            symtab_hdr->sh_link,
4393                                                            sym->st_name);
4394                     }
4395                   else
4396                     {
4397                       /* It's an external symbol.  */
4398                       int e_indx;
4399
4400                       e_indx = r_indx - symtab_hdr->sh_info;
4401                       hash = ((struct elf32_arm_link_hash_entry *)
4402                               elf_sym_hashes (input_bfd)[e_indx]);
4403
4404                       while (hash->root.root.type == bfd_link_hash_indirect
4405                              || hash->root.root.type == bfd_link_hash_warning)
4406                         hash = ((struct elf32_arm_link_hash_entry *)
4407                                 hash->root.root.u.i.link);
4408
4409                       if (hash->root.root.type == bfd_link_hash_defined
4410                           || hash->root.root.type == bfd_link_hash_defweak)
4411                         {
4412                           sym_sec = hash->root.root.u.def.section;
4413                           sym_value = hash->root.root.u.def.value;
4414
4415                           struct elf32_arm_link_hash_table *globals =
4416                                                   elf32_arm_hash_table (info);
4417
4418                           /* For a destination in a shared library,
4419                              use the PLT stub as target address to
4420                              decide whether a branch stub is
4421                              needed.  */
4422                           if (globals->splt != NULL && hash != NULL
4423                               && hash->root.plt.offset != (bfd_vma) -1)
4424                             {
4425                               sym_sec = globals->splt;
4426                               sym_value = hash->root.plt.offset;
4427                               if (sym_sec->output_section != NULL)
4428                                 destination = (sym_value
4429                                                + sym_sec->output_offset
4430                                                + sym_sec->output_section->vma);
4431                             }
4432                           else if (sym_sec->output_section != NULL)
4433                             destination = (sym_value + irela->r_addend
4434                                            + sym_sec->output_offset
4435                                            + sym_sec->output_section->vma);
4436                         }
4437                       else if ((hash->root.root.type == bfd_link_hash_undefined)
4438                                || (hash->root.root.type == bfd_link_hash_undefweak))
4439                         {
4440                           /* For a shared library, use the PLT stub as
4441                              target address to decide whether a long
4442                              branch stub is needed.
4443                              For absolute code, they cannot be handled.  */
4444                           struct elf32_arm_link_hash_table *globals =
4445                             elf32_arm_hash_table (info);
4446
4447                           if (globals->splt != NULL && hash != NULL
4448                               && hash->root.plt.offset != (bfd_vma) -1)
4449                             {
4450                               sym_sec = globals->splt;
4451                               sym_value = hash->root.plt.offset;
4452                               if (sym_sec->output_section != NULL)
4453                                 destination = (sym_value
4454                                                + sym_sec->output_offset
4455                                                + sym_sec->output_section->vma);
4456                             }
4457                           else
4458                             continue;
4459                         }
4460                       else
4461                         {
4462                           bfd_set_error (bfd_error_bad_value);
4463                           goto error_ret_free_internal;
4464                         }
4465                       st_type = ELF_ST_TYPE (hash->root.type);
4466                       sym_name = hash->root.root.root.string;
4467                     }
4468
4469                   do
4470                     {
4471                       /* Determine what (if any) linker stub is needed.  */
4472                       stub_type = arm_type_of_stub (info, section, irela,
4473                                                     st_type, hash,
4474                                                     destination, sym_sec,
4475                                                     input_bfd, sym_name);
4476                       if (stub_type == arm_stub_none)
4477                         break;
4478
4479                       /* Support for grouping stub sections.  */
4480                       id_sec = htab->stub_group[section->id].link_sec;
4481
4482                       /* Get the name of this stub.  */
4483                       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash,
4484                                                        irela);
4485                       if (!stub_name)
4486                         goto error_ret_free_internal;
4487
4488                       /* We've either created a stub for this reloc already,
4489                          or we are about to.  */
4490                       created_stub = TRUE;
4491
4492                       stub_entry = arm_stub_hash_lookup
4493                                      (&htab->stub_hash_table, stub_name,
4494                                       FALSE, FALSE);
4495                       if (stub_entry != NULL)
4496                         {
4497                           /* The proper stub has already been created.  */
4498                           free (stub_name);
4499                           stub_entry->target_value = sym_value;
4500                           break;
4501                         }
4502
4503                       stub_entry = elf32_arm_add_stub (stub_name, section,
4504                                                        htab);
4505                       if (stub_entry == NULL)
4506                         {
4507                           free (stub_name);
4508                           goto error_ret_free_internal;
4509                         }
4510
4511                       stub_entry->target_value = sym_value;
4512                       stub_entry->target_section = sym_sec;
4513                       stub_entry->stub_type = stub_type;
4514                       stub_entry->h = hash;
4515                       stub_entry->st_type = st_type;
4516
4517                       if (sym_name == NULL)
4518                         sym_name = "unnamed";
4519                       stub_entry->output_name
4520                         = bfd_alloc (htab->stub_bfd,
4521                                      sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
4522                                      + strlen (sym_name));
4523                       if (stub_entry->output_name == NULL)
4524                         {
4525                           free (stub_name);
4526                           goto error_ret_free_internal;
4527                         }
4528
4529                       /* For historical reasons, use the existing names for
4530                          ARM-to-Thumb and Thumb-to-ARM stubs.  */
4531                       if ( ((r_type == (unsigned int) R_ARM_THM_CALL)
4532                             || (r_type == (unsigned int) R_ARM_THM_JUMP24))
4533                            && st_type != STT_ARM_TFUNC)
4534                         sprintf (stub_entry->output_name,
4535                                  THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
4536                       else if ( ((r_type == (unsigned int) R_ARM_CALL)
4537                                  || (r_type == (unsigned int) R_ARM_JUMP24))
4538                                && st_type == STT_ARM_TFUNC)
4539                         sprintf (stub_entry->output_name,
4540                                  ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
4541                       else
4542                         sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
4543                                  sym_name);
4544
4545                       stub_changed = TRUE;
4546                     }
4547                   while (0);
4548
4549                   /* Look for relocations which might trigger Cortex-A8
4550                      erratum.  */
4551                   if (htab->fix_cortex_a8
4552                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
4553                           || r_type == (unsigned int) R_ARM_THM_JUMP19
4554                           || r_type == (unsigned int) R_ARM_THM_CALL
4555                           || r_type == (unsigned int) R_ARM_THM_XPC22))
4556                     {
4557                       bfd_vma from = section->output_section->vma
4558                                      + section->output_offset
4559                                      + irela->r_offset;
4560
4561                       if ((from & 0xfff) == 0xffe)
4562                         {
4563                           /* Found a candidate.  Note we haven't checked the
4564                              destination is within 4K here: if we do so (and
4565                              don't create an entry in a8_relocs) we can't tell
4566                              that a branch should have been relocated when
4567                              scanning later.  */
4568                           if (num_a8_relocs == a8_reloc_table_size)
4569                             {
4570                               a8_reloc_table_size *= 2;
4571                               a8_relocs = bfd_realloc (a8_relocs,
4572                                 sizeof (struct a8_erratum_reloc)
4573                                 * a8_reloc_table_size);
4574                             }
4575
4576                           a8_relocs[num_a8_relocs].from = from;
4577                           a8_relocs[num_a8_relocs].destination = destination;
4578                           a8_relocs[num_a8_relocs].r_type = r_type;
4579                           a8_relocs[num_a8_relocs].st_type = st_type;
4580                           a8_relocs[num_a8_relocs].sym_name = sym_name;
4581                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
4582
4583                           num_a8_relocs++;
4584                         }
4585                     }
4586                 }
4587
4588               /* We're done with the internal relocs, free them.  */
4589               if (elf_section_data (section)->relocs == NULL)
4590                 free (internal_relocs);
4591             }
4592
4593           if (htab->fix_cortex_a8)
4594             {
4595               /* Sort relocs which might apply to Cortex-A8 erratum.  */
4596               qsort (a8_relocs, num_a8_relocs,
4597                      sizeof (struct a8_erratum_reloc),
4598                      &a8_reloc_compare);
4599
4600               /* Scan for branches which might trigger Cortex-A8 erratum.  */
4601               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
4602                                           &num_a8_fixes, &a8_fix_table_size,
4603                                           a8_relocs, num_a8_relocs,
4604                                           prev_num_a8_fixes, &stub_changed)
4605                   != 0)
4606                 goto error_ret_free_local;
4607             }
4608         }
4609
4610       if (prev_num_a8_fixes != num_a8_fixes)
4611         stub_changed = TRUE;
4612
4613       if (!stub_changed)
4614         break;
4615
4616       /* OK, we've added some stubs.  Find out the new size of the
4617          stub sections.  */
4618       for (stub_sec = htab->stub_bfd->sections;
4619            stub_sec != NULL;
4620            stub_sec = stub_sec->next)
4621         {
4622           /* Ignore non-stub sections.  */
4623           if (!strstr (stub_sec->name, STUB_SUFFIX))
4624             continue;
4625
4626           stub_sec->size = 0;
4627         }
4628
4629       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
4630
4631       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
4632       if (htab->fix_cortex_a8)
4633         for (i = 0; i < num_a8_fixes; i++)
4634           {
4635             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
4636                          a8_fixes[i].section, htab);
4637
4638             if (stub_sec == NULL)
4639               goto error_ret_free_local;
4640
4641             stub_sec->size
4642               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
4643                                               NULL);
4644           }
4645
4646
4647       /* Ask the linker to do its stuff.  */
4648       (*htab->layout_sections_again) ();
4649     }
4650
4651   /* Add stubs for Cortex-A8 erratum fixes now.  */
4652   if (htab->fix_cortex_a8)
4653     {
4654       for (i = 0; i < num_a8_fixes; i++)
4655         {
4656           struct elf32_arm_stub_hash_entry *stub_entry;
4657           char *stub_name = a8_fixes[i].stub_name;
4658           asection *section = a8_fixes[i].section;
4659           unsigned int section_id = a8_fixes[i].section->id;
4660           asection *link_sec = htab->stub_group[section_id].link_sec;
4661           asection *stub_sec = htab->stub_group[section_id].stub_sec;
4662           const insn_sequence *template;
4663           int template_size, size = 0;
4664
4665           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4666                                              TRUE, FALSE);
4667           if (stub_entry == NULL)
4668             {
4669               (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
4670                                      section->owner,
4671                                      stub_name);
4672               return FALSE;
4673             }
4674
4675           stub_entry->stub_sec = stub_sec;
4676           stub_entry->stub_offset = 0;
4677           stub_entry->id_sec = link_sec;
4678           stub_entry->stub_type = a8_fixes[i].stub_type;
4679           stub_entry->target_section = a8_fixes[i].section;
4680           stub_entry->target_value = a8_fixes[i].offset;
4681           stub_entry->target_addend = a8_fixes[i].addend;
4682           stub_entry->orig_insn = a8_fixes[i].orig_insn;
4683           stub_entry->st_type = STT_ARM_TFUNC;
4684
4685           size = find_stub_size_and_template (a8_fixes[i].stub_type, &template,
4686                                               &template_size);
4687
4688           stub_entry->stub_size = size;
4689           stub_entry->stub_template = template;
4690           stub_entry->stub_template_size = template_size;
4691         }
4692
4693       /* Stash the Cortex-A8 erratum fix array for use later in
4694          elf32_arm_write_section().  */
4695       htab->a8_erratum_fixes = a8_fixes;
4696       htab->num_a8_erratum_fixes = num_a8_fixes;
4697     }
4698   else
4699     {
4700       htab->a8_erratum_fixes = NULL;
4701       htab->num_a8_erratum_fixes = 0;
4702     }
4703   return TRUE;
4704
4705  error_ret_free_local:
4706   return FALSE;
4707 }
4708
4709 /* Build all the stubs associated with the current output file.  The
4710    stubs are kept in a hash table attached to the main linker hash
4711    table.  We also set up the .plt entries for statically linked PIC
4712    functions here.  This function is called via arm_elf_finish in the
4713    linker.  */
4714
4715 bfd_boolean
4716 elf32_arm_build_stubs (struct bfd_link_info *info)
4717 {
4718   asection *stub_sec;
4719   struct bfd_hash_table *table;
4720   struct elf32_arm_link_hash_table *htab;
4721
4722   htab = elf32_arm_hash_table (info);
4723
4724   for (stub_sec = htab->stub_bfd->sections;
4725        stub_sec != NULL;
4726        stub_sec = stub_sec->next)
4727     {
4728       bfd_size_type size;
4729
4730       /* Ignore non-stub sections.  */
4731       if (!strstr (stub_sec->name, STUB_SUFFIX))
4732         continue;
4733
4734       /* Allocate memory to hold the linker stubs.  */
4735       size = stub_sec->size;
4736       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4737       if (stub_sec->contents == NULL && size != 0)
4738         return FALSE;
4739       stub_sec->size = 0;
4740     }
4741
4742   /* Build the stubs as directed by the stub hash table.  */
4743   table = &htab->stub_hash_table;
4744   bfd_hash_traverse (table, arm_build_one_stub, info);
4745   if (htab->fix_cortex_a8)
4746     {
4747       /* Place the cortex a8 stubs last.  */
4748       htab->fix_cortex_a8 = -1;
4749       bfd_hash_traverse (table, arm_build_one_stub, info);
4750     }
4751
4752   return TRUE;
4753 }
4754
4755 /* Locate the Thumb encoded calling stub for NAME.  */
4756
4757 static struct elf_link_hash_entry *
4758 find_thumb_glue (struct bfd_link_info *link_info,
4759                  const char *name,
4760                  char **error_message)
4761 {
4762   char *tmp_name;
4763   struct elf_link_hash_entry *hash;
4764   struct elf32_arm_link_hash_table *hash_table;
4765
4766   /* We need a pointer to the armelf specific hash table.  */
4767   hash_table = elf32_arm_hash_table (link_info);
4768
4769   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
4770                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
4771
4772   BFD_ASSERT (tmp_name);
4773
4774   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
4775
4776   hash = elf_link_hash_lookup
4777     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
4778
4779   if (hash == NULL
4780       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
4781                    tmp_name, name) == -1)
4782     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
4783
4784   free (tmp_name);
4785
4786   return hash;
4787 }
4788
4789 /* Locate the ARM encoded calling stub for NAME.  */
4790
4791 static struct elf_link_hash_entry *
4792 find_arm_glue (struct bfd_link_info *link_info,
4793                const char *name,
4794                char **error_message)
4795 {
4796   char *tmp_name;
4797   struct elf_link_hash_entry *myh;
4798   struct elf32_arm_link_hash_table *hash_table;
4799
4800   /* We need a pointer to the elfarm specific hash table.  */
4801   hash_table = elf32_arm_hash_table (link_info);
4802
4803   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
4804                          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
4805
4806   BFD_ASSERT (tmp_name);
4807
4808   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
4809
4810   myh = elf_link_hash_lookup
4811     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
4812
4813   if (myh == NULL
4814       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
4815                    tmp_name, name) == -1)
4816     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
4817
4818   free (tmp_name);
4819
4820   return myh;
4821 }
4822
4823 /* ARM->Thumb glue (static images):
4824
4825    .arm
4826    __func_from_arm:
4827    ldr r12, __func_addr
4828    bx  r12
4829    __func_addr:
4830    .word func    @ behave as if you saw a ARM_32 reloc.
4831
4832    (v5t static images)
4833    .arm
4834    __func_from_arm:
4835    ldr pc, __func_addr
4836    __func_addr:
4837    .word func    @ behave as if you saw a ARM_32 reloc.
4838
4839    (relocatable images)
4840    .arm
4841    __func_from_arm:
4842    ldr r12, __func_offset
4843    add r12, r12, pc
4844    bx  r12
4845    __func_offset:
4846    .word func - .   */
4847
4848 #define ARM2THUMB_STATIC_GLUE_SIZE 12
4849 static const insn32 a2t1_ldr_insn = 0xe59fc000;
4850 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
4851 static const insn32 a2t3_func_addr_insn = 0x00000001;
4852
4853 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
4854 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
4855 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
4856
4857 #define ARM2THUMB_PIC_GLUE_SIZE 16
4858 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
4859 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
4860 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
4861
4862 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
4863
4864      .thumb                             .thumb
4865      .align 2                           .align 2
4866  __func_from_thumb:                 __func_from_thumb:
4867      bx pc                              push {r6, lr}
4868      nop                                ldr  r6, __func_addr
4869      .arm                               mov  lr, pc
4870      b func                             bx   r6
4871                                         .arm
4872                                     ;; back_to_thumb       
4873                                         ldmia r13! {r6, lr}
4874                                         bx    lr           
4875                                     __func_addr:
4876                                         .word        func  */
4877
4878 #define THUMB2ARM_GLUE_SIZE 8
4879 static const insn16 t2a1_bx_pc_insn = 0x4778;
4880 static const insn16 t2a2_noop_insn = 0x46c0;
4881 static const insn32 t2a3_b_insn = 0xea000000;
4882
4883 #define VFP11_ERRATUM_VENEER_SIZE 8
4884
4885 #define ARM_BX_VENEER_SIZE 12
4886 static const insn32 armbx1_tst_insn = 0xe3100001;
4887 static const insn32 armbx2_moveq_insn = 0x01a0f000;
4888 static const insn32 armbx3_bx_insn = 0xe12fff10;
4889
4890 #ifndef ELFARM_NABI_C_INCLUDED
4891 static void
4892 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
4893 {
4894   asection * s;
4895   bfd_byte * contents;
4896
4897   if (size == 0)
4898     {
4899       /* Do not include empty glue sections in the output.  */
4900       if (abfd != NULL)
4901         {
4902           s = bfd_get_section_by_name (abfd, name);
4903           if (s != NULL)
4904             s->flags |= SEC_EXCLUDE;
4905         }
4906       return;
4907     }
4908
4909   BFD_ASSERT (abfd != NULL);
4910
4911   s = bfd_get_section_by_name (abfd, name);
4912   BFD_ASSERT (s != NULL);
4913
4914   contents = bfd_alloc (abfd, size);
4915
4916   BFD_ASSERT (s->size == size);
4917   s->contents = contents;
4918 }
4919
4920 bfd_boolean
4921 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
4922 {
4923   struct elf32_arm_link_hash_table * globals;
4924
4925   globals = elf32_arm_hash_table (info);
4926   BFD_ASSERT (globals != NULL);
4927
4928   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
4929                                    globals->arm_glue_size,
4930                                    ARM2THUMB_GLUE_SECTION_NAME);
4931
4932   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
4933                                    globals->thumb_glue_size,
4934                                    THUMB2ARM_GLUE_SECTION_NAME);
4935
4936   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
4937                                    globals->vfp11_erratum_glue_size,
4938                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
4939
4940   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
4941                                    globals->bx_glue_size,
4942                                    ARM_BX_GLUE_SECTION_NAME);
4943
4944   return TRUE;
4945 }
4946
4947 /* Allocate space and symbols for calling a Thumb function from Arm mode.
4948    returns the symbol identifying the stub.  */
4949
4950 static struct elf_link_hash_entry *
4951 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
4952                           struct elf_link_hash_entry * h)
4953 {
4954   const char * name = h->root.root.string;
4955   asection * s;
4956   char * tmp_name;
4957   struct elf_link_hash_entry * myh;
4958   struct bfd_link_hash_entry * bh;
4959   struct elf32_arm_link_hash_table * globals;
4960   bfd_vma val;
4961   bfd_size_type size;
4962
4963   globals = elf32_arm_hash_table (link_info);
4964
4965   BFD_ASSERT (globals != NULL);
4966   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
4967
4968   s = bfd_get_section_by_name
4969     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
4970
4971   BFD_ASSERT (s != NULL);
4972
4973   tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
4974
4975   BFD_ASSERT (tmp_name);
4976
4977   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
4978
4979   myh = elf_link_hash_lookup
4980     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
4981
4982   if (myh != NULL)
4983     {
4984       /* We've already seen this guy.  */
4985       free (tmp_name);
4986       return myh;
4987     }
4988
4989   /* The only trick here is using hash_table->arm_glue_size as the value.
4990      Even though the section isn't allocated yet, this is where we will be
4991      putting it.  The +1 on the value marks that the stub has not been
4992      output yet - not that it is a Thumb function.  */
4993   bh = NULL;
4994   val = globals->arm_glue_size + 1;
4995   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
4996                                     tmp_name, BSF_GLOBAL, s, val,
4997                                     NULL, TRUE, FALSE, &bh);
4998
4999   myh = (struct elf_link_hash_entry *) bh;
5000   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5001   myh->forced_local = 1;
5002
5003   free (tmp_name);
5004
5005   if (link_info->shared || globals->root.is_relocatable_executable
5006       || globals->pic_veneer)
5007     size = ARM2THUMB_PIC_GLUE_SIZE;
5008   else if (globals->use_blx)
5009     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
5010   else
5011     size = ARM2THUMB_STATIC_GLUE_SIZE;
5012
5013   s->size += size;
5014   globals->arm_glue_size += size;
5015
5016   return myh;
5017 }
5018
5019 /* Allocate space for ARMv4 BX veneers.  */
5020
5021 static void
5022 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
5023 {
5024   asection * s;
5025   struct elf32_arm_link_hash_table *globals;
5026   char *tmp_name;
5027   struct elf_link_hash_entry *myh;
5028   struct bfd_link_hash_entry *bh;
5029   bfd_vma val;
5030
5031   /* BX PC does not need a veneer.  */
5032   if (reg == 15)
5033     return;
5034
5035   globals = elf32_arm_hash_table (link_info);
5036
5037   BFD_ASSERT (globals != NULL);
5038   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5039
5040   /* Check if this veneer has already been allocated.  */
5041   if (globals->bx_glue_offset[reg])
5042     return;
5043
5044   s = bfd_get_section_by_name
5045     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
5046
5047   BFD_ASSERT (s != NULL);
5048
5049   /* Add symbol for veneer.  */
5050   tmp_name = bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
5051
5052   BFD_ASSERT (tmp_name);
5053
5054   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
5055
5056   myh = elf_link_hash_lookup
5057     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
5058
5059   BFD_ASSERT (myh == NULL);
5060
5061   bh = NULL;
5062   val = globals->bx_glue_size;
5063   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
5064                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
5065                                     NULL, TRUE, FALSE, &bh);
5066
5067   myh = (struct elf_link_hash_entry *) bh;
5068   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5069   myh->forced_local = 1;
5070
5071   s->size += ARM_BX_VENEER_SIZE;
5072   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
5073   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
5074 }
5075
5076
5077 /* Add an entry to the code/data map for section SEC.  */
5078
5079 static void
5080 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
5081 {
5082   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
5083   unsigned int newidx;
5084
5085   if (sec_data->map == NULL)
5086     {
5087       sec_data->map = bfd_malloc (sizeof (elf32_arm_section_map));
5088       sec_data->mapcount = 0;
5089       sec_data->mapsize = 1;
5090     }
5091
5092   newidx = sec_data->mapcount++;
5093
5094   if (sec_data->mapcount > sec_data->mapsize)
5095     {
5096       sec_data->mapsize *= 2;
5097       sec_data->map = bfd_realloc_or_free (sec_data->map, sec_data->mapsize
5098                                            * sizeof (elf32_arm_section_map));
5099     }
5100
5101   if (sec_data->map)
5102     {
5103       sec_data->map[newidx].vma = vma;
5104       sec_data->map[newidx].type = type;
5105     }
5106 }
5107
5108
5109 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
5110    veneers are handled for now.  */
5111
5112 static bfd_vma
5113 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
5114                              elf32_vfp11_erratum_list *branch,
5115                              bfd *branch_bfd,
5116                              asection *branch_sec,
5117                              unsigned int offset)
5118 {
5119   asection *s;
5120   struct elf32_arm_link_hash_table *hash_table;
5121   char *tmp_name;
5122   struct elf_link_hash_entry *myh;
5123   struct bfd_link_hash_entry *bh;
5124   bfd_vma val;
5125   struct _arm_elf_section_data *sec_data;
5126   int errcount;
5127   elf32_vfp11_erratum_list *newerr;
5128
5129   hash_table = elf32_arm_hash_table (link_info);
5130
5131   BFD_ASSERT (hash_table != NULL);
5132   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
5133
5134   s = bfd_get_section_by_name
5135     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
5136
5137   sec_data = elf32_arm_section_data (s);
5138
5139   BFD_ASSERT (s != NULL);
5140
5141   tmp_name = bfd_malloc ((bfd_size_type) strlen
5142                          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
5143
5144   BFD_ASSERT (tmp_name);
5145
5146   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
5147            hash_table->num_vfp11_fixes);
5148
5149   myh = elf_link_hash_lookup
5150     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
5151
5152   BFD_ASSERT (myh == NULL);
5153
5154   bh = NULL;
5155   val = hash_table->vfp11_erratum_glue_size;
5156   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
5157                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
5158                                     NULL, TRUE, FALSE, &bh);
5159
5160   myh = (struct elf_link_hash_entry *) bh;
5161   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5162   myh->forced_local = 1;
5163
5164   /* Link veneer back to calling location.  */
5165   errcount = ++(sec_data->erratumcount);
5166   newerr = bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
5167
5168   newerr->type = VFP11_ERRATUM_ARM_VENEER;
5169   newerr->vma = -1;
5170   newerr->u.v.branch = branch;
5171   newerr->u.v.id = hash_table->num_vfp11_fixes;
5172   branch->u.b.veneer = newerr;
5173
5174   newerr->next = sec_data->erratumlist;
5175   sec_data->erratumlist = newerr;
5176
5177   /* A symbol for the return from the veneer.  */
5178   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
5179            hash_table->num_vfp11_fixes);
5180
5181   myh = elf_link_hash_lookup
5182     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
5183
5184   if (myh != NULL)
5185     abort ();
5186
5187   bh = NULL;
5188   val = offset + 4;
5189   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
5190                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
5191
5192   myh = (struct elf_link_hash_entry *) bh;
5193   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5194   myh->forced_local = 1;
5195
5196   free (tmp_name);
5197
5198   /* Generate a mapping symbol for the veneer section, and explicitly add an
5199      entry for that symbol to the code/data map for the section.  */
5200   if (hash_table->vfp11_erratum_glue_size == 0)
5201     {
5202       bh = NULL;
5203       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
5204          ever requires this erratum fix.  */
5205       _bfd_generic_link_add_one_symbol (link_info,
5206                                         hash_table->bfd_of_glue_owner, "$a",
5207                                         BSF_LOCAL, s, 0, NULL,
5208                                         TRUE, FALSE, &bh);
5209
5210       myh = (struct elf_link_hash_entry *) bh;
5211       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5212       myh->forced_local = 1;
5213
5214       /* The elf32_arm_init_maps function only cares about symbols from input
5215          BFDs.  We must make a note of this generated mapping symbol
5216          ourselves so that code byteswapping works properly in
5217          elf32_arm_write_section.  */
5218       elf32_arm_section_map_add (s, 'a', 0);
5219     }
5220
5221   s->size += VFP11_ERRATUM_VENEER_SIZE;
5222   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
5223   hash_table->num_vfp11_fixes++;
5224
5225   /* The offset of the veneer.  */
5226   return val;
5227 }
5228
5229 #define ARM_GLUE_SECTION_FLAGS \
5230   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
5231    | SEC_READONLY | SEC_LINKER_CREATED)
5232
5233 /* Create a fake section for use by the ARM backend of the linker.  */
5234
5235 static bfd_boolean
5236 arm_make_glue_section (bfd * abfd, const char * name)
5237 {
5238   asection * sec;
5239
5240   sec = bfd_get_section_by_name (abfd, name);
5241   if (sec != NULL)
5242     /* Already made.  */
5243     return TRUE;
5244
5245   sec = bfd_make_section_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
5246
5247   if (sec == NULL
5248       || !bfd_set_section_alignment (abfd, sec, 2))
5249     return FALSE;
5250
5251   /* Set the gc mark to prevent the section from being removed by garbage
5252      collection, despite the fact that no relocs refer to this section.  */
5253   sec->gc_mark = 1;
5254
5255   return TRUE;
5256 }
5257
5258 /* Add the glue sections to ABFD.  This function is called from the
5259    linker scripts in ld/emultempl/{armelf}.em.  */
5260
5261 bfd_boolean
5262 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
5263                                         struct bfd_link_info *info)
5264 {
5265   /* If we are only performing a partial
5266      link do not bother adding the glue.  */
5267   if (info->relocatable)
5268     return TRUE;
5269
5270   return arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
5271     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
5272     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
5273     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
5274 }
5275
5276 /* Select a BFD to be used to hold the sections used by the glue code.
5277    This function is called from the linker scripts in ld/emultempl/
5278    {armelf/pe}.em.  */
5279
5280 bfd_boolean
5281 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
5282 {
5283   struct elf32_arm_link_hash_table *globals;
5284
5285   /* If we are only performing a partial link
5286      do not bother getting a bfd to hold the glue.  */
5287   if (info->relocatable)
5288     return TRUE;
5289
5290   /* Make sure we don't attach the glue sections to a dynamic object.  */
5291   BFD_ASSERT (!(abfd->flags & DYNAMIC));
5292
5293   globals = elf32_arm_hash_table (info);
5294
5295   BFD_ASSERT (globals != NULL);
5296
5297   if (globals->bfd_of_glue_owner != NULL)
5298     return TRUE;
5299
5300   /* Save the bfd for later use.  */
5301   globals->bfd_of_glue_owner = abfd;
5302
5303   return TRUE;
5304 }
5305
5306 static void
5307 check_use_blx (struct elf32_arm_link_hash_table *globals)
5308 {
5309   if (bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
5310                                 Tag_CPU_arch) > 2)
5311     globals->use_blx = 1;
5312 }
5313
5314 bfd_boolean
5315 bfd_elf32_arm_process_before_allocation (bfd *abfd,
5316                                          struct bfd_link_info *link_info)
5317 {
5318   Elf_Internal_Shdr *symtab_hdr;
5319   Elf_Internal_Rela *internal_relocs = NULL;
5320   Elf_Internal_Rela *irel, *irelend;
5321   bfd_byte *contents = NULL;
5322
5323   asection *sec;
5324   struct elf32_arm_link_hash_table *globals;
5325
5326   /* If we are only performing a partial link do not bother
5327      to construct any glue.  */
5328   if (link_info->relocatable)
5329     return TRUE;
5330
5331   /* Here we have a bfd that is to be included on the link.  We have a
5332      hook to do reloc rummaging, before section sizes are nailed down.  */
5333   globals = elf32_arm_hash_table (link_info);
5334
5335   BFD_ASSERT (globals != NULL);
5336
5337   check_use_blx (globals);
5338
5339   if (globals->byteswap_code && !bfd_big_endian (abfd))
5340     {
5341       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
5342                           abfd);
5343       return FALSE;
5344     }
5345
5346   /* PR 5398: If we have not decided to include any loadable sections in
5347      the output then we will not have a glue owner bfd.  This is OK, it
5348      just means that there is nothing else for us to do here.  */
5349   if (globals->bfd_of_glue_owner == NULL)
5350     return TRUE;
5351
5352   /* Rummage around all the relocs and map the glue vectors.  */
5353   sec = abfd->sections;
5354
5355   if (sec == NULL)
5356     return TRUE;
5357
5358   for (; sec != NULL; sec = sec->next)
5359     {
5360       if (sec->reloc_count == 0)
5361         continue;
5362
5363       if ((sec->flags & SEC_EXCLUDE) != 0)
5364         continue;
5365
5366       symtab_hdr = & elf_symtab_hdr (abfd);
5367
5368       /* Load the relocs.  */
5369       internal_relocs
5370         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
5371
5372       if (internal_relocs == NULL)
5373         goto error_return;
5374
5375       irelend = internal_relocs + sec->reloc_count;
5376       for (irel = internal_relocs; irel < irelend; irel++)
5377         {
5378           long r_type;
5379           unsigned long r_index;
5380
5381           struct elf_link_hash_entry *h;
5382
5383           r_type = ELF32_R_TYPE (irel->r_info);
5384           r_index = ELF32_R_SYM (irel->r_info);
5385
5386           /* These are the only relocation types we care about.  */
5387           if (   r_type != R_ARM_PC24
5388               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
5389             continue;
5390
5391           /* Get the section contents if we haven't done so already.  */
5392           if (contents == NULL)
5393             {
5394               /* Get cached copy if it exists.  */
5395               if (elf_section_data (sec)->this_hdr.contents != NULL)
5396                 contents = elf_section_data (sec)->this_hdr.contents;
5397               else
5398                 {
5399                   /* Go get them off disk.  */
5400                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
5401                     goto error_return;
5402                 }
5403             }
5404
5405           if (r_type == R_ARM_V4BX)
5406             {
5407               int reg;
5408
5409               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
5410               record_arm_bx_glue (link_info, reg);
5411               continue;
5412             }
5413
5414           /* If the relocation is not against a symbol it cannot concern us.  */
5415           h = NULL;
5416
5417           /* We don't care about local symbols.  */
5418           if (r_index < symtab_hdr->sh_info)
5419             continue;
5420
5421           /* This is an external symbol.  */
5422           r_index -= symtab_hdr->sh_info;
5423           h = (struct elf_link_hash_entry *)
5424             elf_sym_hashes (abfd)[r_index];
5425
5426           /* If the relocation is against a static symbol it must be within
5427              the current section and so cannot be a cross ARM/Thumb relocation.  */
5428           if (h == NULL)
5429             continue;
5430
5431           /* If the call will go through a PLT entry then we do not need
5432              glue.  */
5433           if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
5434             continue;
5435
5436           switch (r_type)
5437             {
5438             case R_ARM_PC24:
5439               /* This one is a call from arm code.  We need to look up
5440                  the target of the call.  If it is a thumb target, we
5441                  insert glue.  */
5442               if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
5443                 record_arm_to_thumb_glue (link_info, h);
5444               break;
5445
5446             default:
5447               abort ();
5448             }
5449         }
5450
5451       if (contents != NULL
5452           && elf_section_data (sec)->this_hdr.contents != contents)
5453         free (contents);
5454       contents = NULL;
5455
5456       if (internal_relocs != NULL
5457           && elf_section_data (sec)->relocs != internal_relocs)
5458         free (internal_relocs);
5459       internal_relocs = NULL;
5460     }
5461
5462   return TRUE;
5463
5464 error_return:
5465   if (contents != NULL
5466       && elf_section_data (sec)->this_hdr.contents != contents)
5467     free (contents);
5468   if (internal_relocs != NULL
5469       && elf_section_data (sec)->relocs != internal_relocs)
5470     free (internal_relocs);
5471
5472   return FALSE;
5473 }
5474 #endif
5475
5476
5477 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
5478
5479 void
5480 bfd_elf32_arm_init_maps (bfd *abfd)
5481 {
5482   Elf_Internal_Sym *isymbuf;
5483   Elf_Internal_Shdr *hdr;
5484   unsigned int i, localsyms;
5485
5486   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
5487   if (! is_arm_elf (abfd))
5488     return;
5489
5490   if ((abfd->flags & DYNAMIC) != 0)
5491     return;
5492
5493   hdr = & elf_symtab_hdr (abfd);
5494   localsyms = hdr->sh_info;
5495
5496   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
5497      should contain the number of local symbols, which should come before any
5498      global symbols.  Mapping symbols are always local.  */
5499   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
5500                                   NULL);
5501
5502   /* No internal symbols read?  Skip this BFD.  */
5503   if (isymbuf == NULL)
5504     return;
5505
5506   for (i = 0; i < localsyms; i++)
5507     {
5508       Elf_Internal_Sym *isym = &isymbuf[i];
5509       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5510       const char *name;
5511
5512       if (sec != NULL
5513           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
5514         {
5515           name = bfd_elf_string_from_elf_section (abfd,
5516             hdr->sh_link, isym->st_name);
5517
5518           if (bfd_is_arm_special_symbol_name (name,
5519                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
5520             elf32_arm_section_map_add (sec, name[1], isym->st_value);
5521         }
5522     }
5523 }
5524
5525
5526 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
5527    say what they wanted.  */
5528
5529 void
5530 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
5531 {
5532   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
5533   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
5534
5535   if (globals->fix_cortex_a8 == -1)
5536     {
5537       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
5538       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
5539           && (out_attr[Tag_CPU_arch_profile].i == 'A'
5540               || out_attr[Tag_CPU_arch_profile].i == 0))
5541         globals->fix_cortex_a8 = 1;
5542       else
5543         globals->fix_cortex_a8 = 0;
5544     }
5545 }
5546
5547
5548 void
5549 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
5550 {
5551   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
5552   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
5553
5554   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
5555   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
5556     {
5557       switch (globals->vfp11_fix)
5558         {
5559         case BFD_ARM_VFP11_FIX_DEFAULT:
5560         case BFD_ARM_VFP11_FIX_NONE:
5561           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
5562           break;
5563
5564         default:
5565           /* Give a warning, but do as the user requests anyway.  */
5566           (*_bfd_error_handler) (_("%B: warning: selected VFP11 erratum "
5567             "workaround is not necessary for target architecture"), obfd);
5568         }
5569     }
5570   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
5571     /* For earlier architectures, we might need the workaround, but do not
5572        enable it by default.  If users is running with broken hardware, they
5573        must enable the erratum fix explicitly.  */
5574     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
5575 }
5576
5577
5578 enum bfd_arm_vfp11_pipe
5579 {
5580   VFP11_FMAC,
5581   VFP11_LS,
5582   VFP11_DS,
5583   VFP11_BAD
5584 };
5585
5586 /* Return a VFP register number.  This is encoded as RX:X for single-precision
5587    registers, or X:RX for double-precision registers, where RX is the group of
5588    four bits in the instruction encoding and X is the single extension bit.
5589    RX and X fields are specified using their lowest (starting) bit.  The return
5590    value is:
5591
5592      0...31: single-precision registers s0...s31
5593      32...63: double-precision registers d0...d31.
5594
5595    Although X should be zero for VFP11 (encoding d0...d15 only), we might
5596    encounter VFP3 instructions, so we allow the full range for DP registers.  */
5597
5598 static unsigned int
5599 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
5600                      unsigned int x)
5601 {
5602   if (is_double)
5603     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
5604   else
5605     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
5606 }
5607
5608 /* Set bits in *WMASK according to a register number REG as encoded by
5609    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
5610
5611 static void
5612 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
5613 {
5614   if (reg < 32)
5615     *wmask |= 1 << reg;
5616   else if (reg < 48)
5617     *wmask |= 3 << ((reg - 32) * 2);
5618 }
5619
5620 /* Return TRUE if WMASK overwrites anything in REGS.  */
5621
5622 static bfd_boolean
5623 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
5624 {
5625   int i;
5626
5627   for (i = 0; i < numregs; i++)
5628     {
5629       unsigned int reg = regs[i];
5630
5631       if (reg < 32 && (wmask & (1 << reg)) != 0)
5632         return TRUE;
5633
5634       reg -= 32;
5635
5636       if (reg >= 16)
5637         continue;
5638
5639       if ((wmask & (3 << (reg * 2))) != 0)
5640         return TRUE;
5641     }
5642
5643   return FALSE;
5644 }
5645
5646 /* In this function, we're interested in two things: finding input registers
5647    for VFP data-processing instructions, and finding the set of registers which
5648    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
5649    hold the written set, so FLDM etc. are easy to deal with (we're only
5650    interested in 32 SP registers or 16 dp registers, due to the VFP version
5651    implemented by the chip in question).  DP registers are marked by setting
5652    both SP registers in the write mask).  */
5653
5654 static enum bfd_arm_vfp11_pipe
5655 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
5656                            int *numregs)
5657 {
5658   enum bfd_arm_vfp11_pipe pipe = VFP11_BAD;
5659   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
5660
5661   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
5662     {
5663       unsigned int pqrs;
5664       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
5665       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
5666
5667       pqrs = ((insn & 0x00800000) >> 20)
5668            | ((insn & 0x00300000) >> 19)
5669            | ((insn & 0x00000040) >> 6);
5670
5671       switch (pqrs)
5672         {
5673         case 0: /* fmac[sd].  */
5674         case 1: /* fnmac[sd].  */
5675         case 2: /* fmsc[sd].  */
5676         case 3: /* fnmsc[sd].  */
5677           pipe = VFP11_FMAC;
5678           bfd_arm_vfp11_write_mask (destmask, fd);
5679           regs[0] = fd;
5680           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
5681           regs[2] = fm;
5682           *numregs = 3;
5683           break;
5684
5685         case 4: /* fmul[sd].  */
5686         case 5: /* fnmul[sd].  */
5687         case 6: /* fadd[sd].  */
5688         case 7: /* fsub[sd].  */
5689           pipe = VFP11_FMAC;
5690           goto vfp_binop;
5691
5692         case 8: /* fdiv[sd].  */
5693           pipe = VFP11_DS;
5694           vfp_binop:
5695           bfd_arm_vfp11_write_mask (destmask, fd);
5696           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
5697           regs[1] = fm;
5698           *numregs = 2;
5699           break;
5700
5701         case 15: /* extended opcode.  */
5702           {
5703             unsigned int extn = ((insn >> 15) & 0x1e)
5704                               | ((insn >> 7) & 1);
5705
5706             switch (extn)
5707               {
5708               case 0: /* fcpy[sd].  */
5709               case 1: /* fabs[sd].  */
5710               case 2: /* fneg[sd].  */
5711               case 8: /* fcmp[sd].  */
5712               case 9: /* fcmpe[sd].  */
5713               case 10: /* fcmpz[sd].  */
5714               case 11: /* fcmpez[sd].  */
5715               case 16: /* fuito[sd].  */
5716               case 17: /* fsito[sd].  */
5717               case 24: /* ftoui[sd].  */
5718               case 25: /* ftouiz[sd].  */
5719               case 26: /* ftosi[sd].  */
5720               case 27: /* ftosiz[sd].  */
5721                 /* These instructions will not bounce due to underflow.  */
5722                 *numregs = 0;
5723                 pipe = VFP11_FMAC;
5724                 break;
5725
5726               case 3: /* fsqrt[sd].  */
5727                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
5728                    registers to cause the erratum in previous instructions.  */
5729                 bfd_arm_vfp11_write_mask (destmask, fd);
5730                 pipe = VFP11_DS;
5731                 break;
5732
5733               case 15: /* fcvt{ds,sd}.  */
5734                 {
5735                   int rnum = 0;
5736
5737                   bfd_arm_vfp11_write_mask (destmask, fd);
5738
5739                   /* Only FCVTSD can underflow.  */
5740                   if ((insn & 0x100) != 0)
5741                     regs[rnum++] = fm;
5742
5743                   *numregs = rnum;
5744
5745                   pipe = VFP11_FMAC;
5746                 }
5747                 break;
5748
5749               default:
5750                 return VFP11_BAD;
5751               }
5752           }
5753           break;
5754
5755         default:
5756           return VFP11_BAD;
5757         }
5758     }
5759   /* Two-register transfer.  */
5760   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
5761     {
5762       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
5763
5764       if ((insn & 0x100000) == 0)
5765         {
5766           if (is_double)
5767             bfd_arm_vfp11_write_mask (destmask, fm);
5768           else
5769             {
5770               bfd_arm_vfp11_write_mask (destmask, fm);
5771               bfd_arm_vfp11_write_mask (destmask, fm + 1);
5772             }
5773         }
5774
5775       pipe = VFP11_LS;
5776     }
5777   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
5778     {
5779       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
5780       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
5781
5782       switch (puw)
5783         {
5784         case 0: /* Two-reg transfer.  We should catch these above.  */
5785           abort ();
5786
5787         case 2: /* fldm[sdx].  */
5788         case 3:
5789         case 5:
5790           {
5791             unsigned int i, offset = insn & 0xff;
5792
5793             if (is_double)
5794               offset >>= 1;
5795
5796             for (i = fd; i < fd + offset; i++)
5797               bfd_arm_vfp11_write_mask (destmask, i);
5798           }
5799           break;
5800
5801         case 4: /* fld[sd].  */
5802         case 6:
5803           bfd_arm_vfp11_write_mask (destmask, fd);
5804           break;
5805
5806         default:
5807           return VFP11_BAD;
5808         }
5809
5810       pipe = VFP11_LS;
5811     }
5812   /* Single-register transfer. Note L==0.  */
5813   else if ((insn & 0x0f100e10) == 0x0e000a10)
5814     {
5815       unsigned int opcode = (insn >> 21) & 7;
5816       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
5817
5818       switch (opcode)
5819         {
5820         case 0: /* fmsr/fmdlr.  */
5821         case 1: /* fmdhr.  */
5822           /* Mark fmdhr and fmdlr as writing to the whole of the DP
5823              destination register.  I don't know if this is exactly right,
5824              but it is the conservative choice.  */
5825           bfd_arm_vfp11_write_mask (destmask, fn);
5826           break;
5827
5828         case 7: /* fmxr.  */
5829           break;
5830         }
5831
5832       pipe = VFP11_LS;
5833     }
5834
5835   return pipe;
5836 }
5837
5838
5839 static int elf32_arm_compare_mapping (const void * a, const void * b);
5840
5841
5842 /* Look for potentially-troublesome code sequences which might trigger the
5843    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
5844    (available from ARM) for details of the erratum.  A short version is
5845    described in ld.texinfo.  */
5846
5847 bfd_boolean
5848 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
5849 {
5850   asection *sec;
5851   bfd_byte *contents = NULL;
5852   int state = 0;
5853   int regs[3], numregs = 0;
5854   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
5855   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
5856
5857   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
5858      The states transition as follows:
5859
5860        0 -> 1 (vector) or 0 -> 2 (scalar)
5861            A VFP FMAC-pipeline instruction has been seen. Fill
5862            regs[0]..regs[numregs-1] with its input operands. Remember this
5863            instruction in 'first_fmac'.
5864
5865        1 -> 2
5866            Any instruction, except for a VFP instruction which overwrites
5867            regs[*].
5868
5869        1 -> 3 [ -> 0 ]  or
5870        2 -> 3 [ -> 0 ]
5871            A VFP instruction has been seen which overwrites any of regs[*].
5872            We must make a veneer!  Reset state to 0 before examining next
5873            instruction.
5874
5875        2 -> 0
5876            If we fail to match anything in state 2, reset to state 0 and reset
5877            the instruction pointer to the instruction after 'first_fmac'.
5878
5879      If the VFP11 vector mode is in use, there must be at least two unrelated
5880      instructions between anti-dependent VFP11 instructions to properly avoid
5881      triggering the erratum, hence the use of the extra state 1.  */
5882
5883   /* If we are only performing a partial link do not bother
5884      to construct any glue.  */
5885   if (link_info->relocatable)
5886     return TRUE;
5887
5888   /* Skip if this bfd does not correspond to an ELF image.  */
5889   if (! is_arm_elf (abfd))
5890     return TRUE;
5891
5892   /* We should have chosen a fix type by the time we get here.  */
5893   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
5894
5895   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
5896     return TRUE;
5897
5898   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
5899   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5900     return TRUE;
5901
5902   for (sec = abfd->sections; sec != NULL; sec = sec->next)
5903     {
5904       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
5905       struct _arm_elf_section_data *sec_data;
5906
5907       /* If we don't have executable progbits, we're not interested in this
5908          section.  Also skip if section is to be excluded.  */
5909       if (elf_section_type (sec) != SHT_PROGBITS
5910           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
5911           || (sec->flags & SEC_EXCLUDE) != 0
5912           || sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
5913           || sec->output_section == bfd_abs_section_ptr
5914           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
5915         continue;
5916
5917       sec_data = elf32_arm_section_data (sec);
5918
5919       if (sec_data->mapcount == 0)
5920         continue;
5921
5922       if (elf_section_data (sec)->this_hdr.contents != NULL)
5923         contents = elf_section_data (sec)->this_hdr.contents;
5924       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
5925         goto error_return;
5926
5927       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
5928              elf32_arm_compare_mapping);
5929
5930       for (span = 0; span < sec_data->mapcount; span++)
5931         {
5932           unsigned int span_start = sec_data->map[span].vma;
5933           unsigned int span_end = (span == sec_data->mapcount - 1)
5934                                   ? sec->size : sec_data->map[span + 1].vma;
5935           char span_type = sec_data->map[span].type;
5936
5937           /* FIXME: Only ARM mode is supported at present.  We may need to
5938              support Thumb-2 mode also at some point.  */
5939           if (span_type != 'a')
5940             continue;
5941
5942           for (i = span_start; i < span_end;)
5943             {
5944               unsigned int next_i = i + 4;
5945               unsigned int insn = bfd_big_endian (abfd)
5946                 ? (contents[i] << 24)
5947                   | (contents[i + 1] << 16)
5948                   | (contents[i + 2] << 8)
5949                   | contents[i + 3]
5950                 : (contents[i + 3] << 24)
5951                   | (contents[i + 2] << 16)
5952                   | (contents[i + 1] << 8)
5953                   | contents[i];
5954               unsigned int writemask = 0;
5955               enum bfd_arm_vfp11_pipe pipe;
5956
5957               switch (state)
5958                 {
5959                 case 0:
5960                   pipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
5961                                                     &numregs);
5962                   /* I'm assuming the VFP11 erratum can trigger with denorm
5963                      operands on either the FMAC or the DS pipeline. This might
5964                      lead to slightly overenthusiastic veneer insertion.  */
5965                   if (pipe == VFP11_FMAC || pipe == VFP11_DS)
5966                     {
5967                       state = use_vector ? 1 : 2;
5968                       first_fmac = i;
5969                       veneer_of_insn = insn;
5970                     }
5971                   break;
5972
5973                 case 1:
5974                   {
5975                     int other_regs[3], other_numregs;
5976                     pipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
5977                                                       other_regs,
5978                                                       &other_numregs);
5979                     if (pipe != VFP11_BAD
5980                         && bfd_arm_vfp11_antidependency (writemask, regs,
5981                                                          numregs))
5982                       state = 3;
5983                     else
5984                       state = 2;
5985                   }
5986                   break;
5987
5988                 case 2:
5989                   {
5990                     int other_regs[3], other_numregs;
5991                     pipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
5992                                                       other_regs,
5993                                                       &other_numregs);
5994                     if (pipe != VFP11_BAD
5995                         && bfd_arm_vfp11_antidependency (writemask, regs,
5996                                                          numregs))
5997                       state = 3;
5998                     else
5999                       {
6000                         state = 0;
6001                         next_i = first_fmac + 4;
6002                       }
6003                   }
6004                   break;
6005
6006                 case 3:
6007                   abort ();  /* Should be unreachable.  */
6008                 }
6009
6010               if (state == 3)
6011                 {
6012                   elf32_vfp11_erratum_list *newerr
6013                     = bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
6014                   int errcount;
6015
6016                   errcount = ++(elf32_arm_section_data (sec)->erratumcount);
6017
6018                   newerr->u.b.vfp_insn = veneer_of_insn;
6019
6020                   switch (span_type)
6021                     {
6022                     case 'a':
6023                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
6024                       break;
6025
6026                     default:
6027                       abort ();
6028                     }
6029
6030                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
6031                                                first_fmac);
6032
6033                   newerr->vma = -1;
6034
6035                   newerr->next = sec_data->erratumlist;
6036                   sec_data->erratumlist = newerr;
6037
6038                   state = 0;
6039                 }
6040
6041               i = next_i;
6042             }
6043         }
6044
6045       if (contents != NULL
6046           && elf_section_data (sec)->this_hdr.contents != contents)
6047         free (contents);
6048       contents = NULL;
6049     }
6050
6051   return TRUE;
6052
6053 error_return:
6054   if (contents != NULL
6055       && elf_section_data (sec)->this_hdr.contents != contents)
6056     free (contents);
6057
6058   return FALSE;
6059 }
6060
6061 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
6062    after sections have been laid out, using specially-named symbols.  */
6063
6064 void
6065 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
6066                                           struct bfd_link_info *link_info)
6067 {
6068   asection *sec;
6069   struct elf32_arm_link_hash_table *globals;
6070   char *tmp_name;
6071
6072   if (link_info->relocatable)
6073     return;
6074
6075   /* Skip if this bfd does not correspond to an ELF image.  */
6076   if (! is_arm_elf (abfd))
6077     return;
6078
6079   globals = elf32_arm_hash_table (link_info);
6080
6081   tmp_name = bfd_malloc ((bfd_size_type) strlen
6082                            (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
6083
6084   for (sec = abfd->sections; sec != NULL; sec = sec->next)
6085     {
6086       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
6087       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
6088
6089       for (; errnode != NULL; errnode = errnode->next)
6090         {
6091           struct elf_link_hash_entry *myh;
6092           bfd_vma vma;
6093
6094           switch (errnode->type)
6095             {
6096             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
6097             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
6098               /* Find veneer symbol.  */
6099               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
6100                        errnode->u.b.veneer->u.v.id);
6101
6102               myh = elf_link_hash_lookup
6103                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
6104
6105               if (myh == NULL)
6106                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
6107                                          "`%s'"), abfd, tmp_name);
6108
6109               vma = myh->root.u.def.section->output_section->vma
6110                     + myh->root.u.def.section->output_offset
6111                     + myh->root.u.def.value;
6112
6113               errnode->u.b.veneer->vma = vma;
6114               break;
6115
6116             case VFP11_ERRATUM_ARM_VENEER:
6117             case VFP11_ERRATUM_THUMB_VENEER:
6118               /* Find return location.  */
6119               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
6120                        errnode->u.v.id);
6121
6122               myh = elf_link_hash_lookup
6123                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
6124
6125               if (myh == NULL)
6126                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
6127                                          "`%s'"), abfd, tmp_name);
6128
6129               vma = myh->root.u.def.section->output_section->vma
6130                     + myh->root.u.def.section->output_offset
6131                     + myh->root.u.def.value;
6132
6133               errnode->u.v.branch->vma = vma;
6134               break;
6135
6136             default:
6137               abort ();
6138             }
6139         }
6140     }
6141
6142   free (tmp_name);
6143 }
6144
6145
6146 /* Set target relocation values needed during linking.  */
6147
6148 void
6149 bfd_elf32_arm_set_target_relocs (struct bfd *output_bfd,
6150                                  struct bfd_link_info *link_info,
6151                                  int target1_is_rel,
6152                                  char * target2_type,
6153                                  int fix_v4bx,
6154                                  int use_blx,
6155                                  bfd_arm_vfp11_fix vfp11_fix,
6156                                  int no_enum_warn, int no_wchar_warn,
6157                                  int pic_veneer, int fix_cortex_a8)
6158 {
6159   struct elf32_arm_link_hash_table *globals;
6160
6161   globals = elf32_arm_hash_table (link_info);
6162
6163   globals->target1_is_rel = target1_is_rel;
6164   if (strcmp (target2_type, "rel") == 0)
6165     globals->target2_reloc = R_ARM_REL32;
6166   else if (strcmp (target2_type, "abs") == 0)
6167     globals->target2_reloc = R_ARM_ABS32;
6168   else if (strcmp (target2_type, "got-rel") == 0)
6169     globals->target2_reloc = R_ARM_GOT_PREL;
6170   else
6171     {
6172       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
6173                           target2_type);
6174     }
6175   globals->fix_v4bx = fix_v4bx;
6176   globals->use_blx |= use_blx;
6177   globals->vfp11_fix = vfp11_fix;
6178   globals->pic_veneer = pic_veneer;
6179   globals->fix_cortex_a8 = fix_cortex_a8;
6180
6181   BFD_ASSERT (is_arm_elf (output_bfd));
6182   elf_arm_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
6183   elf_arm_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
6184 }
6185
6186 /* Replace the target offset of a Thumb bl or b.w instruction.  */
6187
6188 static void
6189 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
6190 {
6191   bfd_vma upper;
6192   bfd_vma lower;
6193   int reloc_sign;
6194
6195   BFD_ASSERT ((offset & 1) == 0);
6196
6197   upper = bfd_get_16 (abfd, insn);
6198   lower = bfd_get_16 (abfd, insn + 2);
6199   reloc_sign = (offset < 0) ? 1 : 0;
6200   upper = (upper & ~(bfd_vma) 0x7ff)
6201           | ((offset >> 12) & 0x3ff)
6202           | (reloc_sign << 10);
6203   lower = (lower & ~(bfd_vma) 0x2fff)
6204           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
6205           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
6206           | ((offset >> 1) & 0x7ff);
6207   bfd_put_16 (abfd, upper, insn);
6208   bfd_put_16 (abfd, lower, insn + 2);
6209 }
6210
6211 /* Thumb code calling an ARM function.  */
6212
6213 static int
6214 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
6215                          const char *           name,
6216                          bfd *                  input_bfd,
6217                          bfd *                  output_bfd,
6218                          asection *             input_section,
6219                          bfd_byte *             hit_data,
6220                          asection *             sym_sec,
6221                          bfd_vma                offset,
6222                          bfd_signed_vma         addend,
6223                          bfd_vma                val,
6224                          char **error_message)
6225 {
6226   asection * s = 0;
6227   bfd_vma my_offset;
6228   long int ret_offset;
6229   struct elf_link_hash_entry * myh;
6230   struct elf32_arm_link_hash_table * globals;
6231
6232   myh = find_thumb_glue (info, name, error_message);
6233   if (myh == NULL)
6234     return FALSE;
6235
6236   globals = elf32_arm_hash_table (info);
6237
6238   BFD_ASSERT (globals != NULL);
6239   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6240
6241   my_offset = myh->root.u.def.value;
6242
6243   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
6244                                THUMB2ARM_GLUE_SECTION_NAME);
6245
6246   BFD_ASSERT (s != NULL);
6247   BFD_ASSERT (s->contents != NULL);
6248   BFD_ASSERT (s->output_section != NULL);
6249
6250   if ((my_offset & 0x01) == 0x01)
6251     {
6252       if (sym_sec != NULL
6253           && sym_sec->owner != NULL
6254           && !INTERWORK_FLAG (sym_sec->owner))
6255         {
6256           (*_bfd_error_handler)
6257             (_("%B(%s): warning: interworking not enabled.\n"
6258                "  first occurrence: %B: thumb call to arm"),
6259              sym_sec->owner, input_bfd, name);
6260
6261           return FALSE;
6262         }
6263
6264       --my_offset;
6265       myh->root.u.def.value = my_offset;
6266
6267       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
6268                       s->contents + my_offset);
6269
6270       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
6271                       s->contents + my_offset + 2);
6272
6273       ret_offset =
6274         /* Address of destination of the stub.  */
6275         ((bfd_signed_vma) val)
6276         - ((bfd_signed_vma)
6277            /* Offset from the start of the current section
6278               to the start of the stubs.  */
6279            (s->output_offset
6280             /* Offset of the start of this stub from the start of the stubs.  */
6281             + my_offset
6282             /* Address of the start of the current section.  */
6283             + s->output_section->vma)
6284            /* The branch instruction is 4 bytes into the stub.  */
6285            + 4
6286            /* ARM branches work from the pc of the instruction + 8.  */
6287            + 8);
6288
6289       put_arm_insn (globals, output_bfd,
6290                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
6291                     s->contents + my_offset + 4);
6292     }
6293
6294   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
6295
6296   /* Now go back and fix up the original BL insn to point to here.  */
6297   ret_offset =
6298     /* Address of where the stub is located.  */
6299     (s->output_section->vma + s->output_offset + my_offset)
6300      /* Address of where the BL is located.  */
6301     - (input_section->output_section->vma + input_section->output_offset
6302        + offset)
6303     /* Addend in the relocation.  */
6304     - addend
6305     /* Biassing for PC-relative addressing.  */
6306     - 8;
6307
6308   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
6309
6310   return TRUE;
6311 }
6312
6313 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
6314
6315 static struct elf_link_hash_entry *
6316 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
6317                              const char *           name,
6318                              bfd *                  input_bfd,
6319                              bfd *                  output_bfd,
6320                              asection *             sym_sec,
6321                              bfd_vma                val,
6322                              asection *             s,
6323                              char **                error_message)
6324 {
6325   bfd_vma my_offset;
6326   long int ret_offset;
6327   struct elf_link_hash_entry * myh;
6328   struct elf32_arm_link_hash_table * globals;
6329
6330   myh = find_arm_glue (info, name, error_message);
6331   if (myh == NULL)
6332     return NULL;
6333
6334   globals = elf32_arm_hash_table (info);
6335
6336   BFD_ASSERT (globals != NULL);
6337   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6338
6339   my_offset = myh->root.u.def.value;
6340
6341   if ((my_offset & 0x01) == 0x01)
6342     {
6343       if (sym_sec != NULL
6344           && sym_sec->owner != NULL
6345           && !INTERWORK_FLAG (sym_sec->owner))
6346         {
6347           (*_bfd_error_handler)
6348             (_("%B(%s): warning: interworking not enabled.\n"
6349                "  first occurrence: %B: arm call to thumb"),
6350              sym_sec->owner, input_bfd, name);
6351         }
6352
6353       --my_offset;
6354       myh->root.u.def.value = my_offset;
6355
6356       if (info->shared || globals->root.is_relocatable_executable
6357           || globals->pic_veneer)
6358         {
6359           /* For relocatable objects we can't use absolute addresses,
6360              so construct the address from a relative offset.  */
6361           /* TODO: If the offset is small it's probably worth
6362              constructing the address with adds.  */
6363           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
6364                         s->contents + my_offset);
6365           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
6366                         s->contents + my_offset + 4);
6367           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
6368                         s->contents + my_offset + 8);
6369           /* Adjust the offset by 4 for the position of the add,
6370              and 8 for the pipeline offset.  */
6371           ret_offset = (val - (s->output_offset
6372                                + s->output_section->vma
6373                                + my_offset + 12))
6374                        | 1;
6375           bfd_put_32 (output_bfd, ret_offset,
6376                       s->contents + my_offset + 12);
6377         }
6378       else if (globals->use_blx)
6379         {
6380           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
6381                         s->contents + my_offset);
6382
6383           /* It's a thumb address.  Add the low order bit.  */
6384           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
6385                       s->contents + my_offset + 4);
6386         }
6387       else
6388         {
6389           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
6390                         s->contents + my_offset);
6391
6392           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
6393                         s->contents + my_offset + 4);
6394
6395           /* It's a thumb address.  Add the low order bit.  */
6396           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
6397                       s->contents + my_offset + 8);
6398
6399           my_offset += 12;
6400         }
6401     }
6402
6403   BFD_ASSERT (my_offset <= globals->arm_glue_size);
6404
6405   return myh;
6406 }
6407
6408 /* Arm code calling a Thumb function.  */
6409
6410 static int
6411 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
6412                          const char *           name,
6413                          bfd *                  input_bfd,
6414                          bfd *                  output_bfd,
6415                          asection *             input_section,
6416                          bfd_byte *             hit_data,
6417                          asection *             sym_sec,
6418                          bfd_vma                offset,
6419                          bfd_signed_vma         addend,
6420                          bfd_vma                val,
6421                          char **error_message)
6422 {
6423   unsigned long int tmp;
6424   bfd_vma my_offset;
6425   asection * s;
6426   long int ret_offset;
6427   struct elf_link_hash_entry * myh;
6428   struct elf32_arm_link_hash_table * globals;
6429
6430   globals = elf32_arm_hash_table (info);
6431
6432   BFD_ASSERT (globals != NULL);
6433   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6434
6435   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
6436                                ARM2THUMB_GLUE_SECTION_NAME);
6437   BFD_ASSERT (s != NULL);
6438   BFD_ASSERT (s->contents != NULL);
6439   BFD_ASSERT (s->output_section != NULL);
6440
6441   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
6442                                      sym_sec, val, s, error_message);
6443   if (!myh)
6444     return FALSE;
6445
6446   my_offset = myh->root.u.def.value;
6447   tmp = bfd_get_32 (input_bfd, hit_data);
6448   tmp = tmp & 0xFF000000;
6449
6450   /* Somehow these are both 4 too far, so subtract 8.  */
6451   ret_offset = (s->output_offset
6452                 + my_offset
6453                 + s->output_section->vma
6454                 - (input_section->output_offset
6455                    + input_section->output_section->vma
6456                    + offset + addend)
6457                 - 8);
6458
6459   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
6460
6461   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
6462
6463   return TRUE;
6464 }
6465
6466 /* Populate Arm stub for an exported Thumb function.  */
6467
6468 static bfd_boolean
6469 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
6470 {
6471   struct bfd_link_info * info = (struct bfd_link_info *) inf;
6472   asection * s;
6473   struct elf_link_hash_entry * myh;
6474   struct elf32_arm_link_hash_entry *eh;
6475   struct elf32_arm_link_hash_table * globals;
6476   asection *sec;
6477   bfd_vma val;
6478   char *error_message;
6479
6480   eh = elf32_arm_hash_entry (h);
6481   /* Allocate stubs for exported Thumb functions on v4t.  */
6482   if (eh->export_glue == NULL)
6483     return TRUE;
6484
6485   globals = elf32_arm_hash_table (info);
6486
6487   BFD_ASSERT (globals != NULL);
6488   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6489
6490   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
6491                                ARM2THUMB_GLUE_SECTION_NAME);
6492   BFD_ASSERT (s != NULL);
6493   BFD_ASSERT (s->contents != NULL);
6494   BFD_ASSERT (s->output_section != NULL);
6495
6496   sec = eh->export_glue->root.u.def.section;
6497
6498   BFD_ASSERT (sec->output_section != NULL);
6499
6500   val = eh->export_glue->root.u.def.value + sec->output_offset
6501         + sec->output_section->vma;
6502
6503   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
6504                                      h->root.u.def.section->owner,
6505                                      globals->obfd, sec, val, s,
6506                                      &error_message);
6507   BFD_ASSERT (myh);
6508   return TRUE;
6509 }
6510
6511 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
6512
6513 static bfd_vma
6514 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
6515 {
6516   bfd_byte *p;
6517   bfd_vma glue_addr;
6518   asection *s;
6519   struct elf32_arm_link_hash_table *globals;
6520
6521   globals = elf32_arm_hash_table (info);
6522
6523   BFD_ASSERT (globals != NULL);
6524   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
6525
6526   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
6527                                ARM_BX_GLUE_SECTION_NAME);
6528   BFD_ASSERT (s != NULL);
6529   BFD_ASSERT (s->contents != NULL);
6530   BFD_ASSERT (s->output_section != NULL);
6531
6532   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
6533
6534   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
6535
6536   if ((globals->bx_glue_offset[reg] & 1) == 0)
6537     {
6538       p = s->contents + glue_addr;
6539       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
6540       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
6541       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
6542       globals->bx_glue_offset[reg] |= 1;
6543     }
6544
6545   return glue_addr + s->output_section->vma + s->output_offset;
6546 }
6547
6548 /* Generate Arm stubs for exported Thumb symbols.  */
6549 static void
6550 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
6551                                   struct bfd_link_info *link_info)
6552 {
6553   struct elf32_arm_link_hash_table * globals;
6554
6555   if (link_info == NULL)
6556     /* Ignore this if we are not called by the ELF backend linker.  */
6557     return;
6558
6559   globals = elf32_arm_hash_table (link_info);
6560   /* If blx is available then exported Thumb symbols are OK and there is
6561      nothing to do.  */
6562   if (globals->use_blx)
6563     return;
6564
6565   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
6566                           link_info);
6567 }
6568
6569 /* Some relocations map to different relocations depending on the
6570    target.  Return the real relocation.  */
6571
6572 static int
6573 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
6574                      int r_type)
6575 {
6576   switch (r_type)
6577     {
6578     case R_ARM_TARGET1:
6579       if (globals->target1_is_rel)
6580         return R_ARM_REL32;
6581       else
6582         return R_ARM_ABS32;
6583
6584     case R_ARM_TARGET2:
6585       return globals->target2_reloc;
6586
6587     default:
6588       return r_type;
6589     }
6590 }
6591
6592 /* Return the base VMA address which should be subtracted from real addresses
6593    when resolving @dtpoff relocation.
6594    This is PT_TLS segment p_vaddr.  */
6595
6596 static bfd_vma
6597 dtpoff_base (struct bfd_link_info *info)
6598 {
6599   /* If tls_sec is NULL, we should have signalled an error already.  */
6600   if (elf_hash_table (info)->tls_sec == NULL)
6601     return 0;
6602   return elf_hash_table (info)->tls_sec->vma;
6603 }
6604
6605 /* Return the relocation value for @tpoff relocation
6606    if STT_TLS virtual address is ADDRESS.  */
6607
6608 static bfd_vma
6609 tpoff (struct bfd_link_info *info, bfd_vma address)
6610 {
6611   struct elf_link_hash_table *htab = elf_hash_table (info);
6612   bfd_vma base;
6613
6614   /* If tls_sec is NULL, we should have signalled an error already.  */
6615   if (htab->tls_sec == NULL)
6616     return 0;
6617   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
6618   return address - htab->tls_sec->vma + base;
6619 }
6620
6621 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
6622    VALUE is the relocation value.  */
6623
6624 static bfd_reloc_status_type
6625 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
6626 {
6627   if (value > 0xfff)
6628     return bfd_reloc_overflow;
6629
6630   value |= bfd_get_32 (abfd, data) & 0xfffff000;
6631   bfd_put_32 (abfd, value, data);
6632   return bfd_reloc_ok;
6633 }
6634
6635 /* For a given value of n, calculate the value of G_n as required to
6636    deal with group relocations.  We return it in the form of an
6637    encoded constant-and-rotation, together with the final residual.  If n is
6638    specified as less than zero, then final_residual is filled with the
6639    input value and no further action is performed.  */
6640
6641 static bfd_vma
6642 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
6643 {
6644   int current_n;
6645   bfd_vma g_n;
6646   bfd_vma encoded_g_n = 0;
6647   bfd_vma residual = value; /* Also known as Y_n.  */
6648
6649   for (current_n = 0; current_n <= n; current_n++)
6650     {
6651       int shift;
6652
6653       /* Calculate which part of the value to mask.  */
6654       if (residual == 0)
6655         shift = 0;
6656       else
6657         {
6658           int msb;
6659
6660           /* Determine the most significant bit in the residual and
6661              align the resulting value to a 2-bit boundary.  */
6662           for (msb = 30; msb >= 0; msb -= 2)
6663             if (residual & (3 << msb))
6664               break;
6665
6666           /* The desired shift is now (msb - 6), or zero, whichever
6667              is the greater.  */
6668           shift = msb - 6;
6669           if (shift < 0)
6670             shift = 0;
6671         }
6672
6673       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
6674       g_n = residual & (0xff << shift);
6675       encoded_g_n = (g_n >> shift)
6676                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
6677
6678       /* Calculate the residual for the next time around.  */
6679       residual &= ~g_n;
6680     }
6681
6682   *final_residual = residual;
6683
6684   return encoded_g_n;
6685 }
6686
6687 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
6688    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
6689
6690 static int
6691 identify_add_or_sub (bfd_vma insn)
6692 {
6693   int opcode = insn & 0x1e00000;
6694
6695   if (opcode == 1 << 23) /* ADD */
6696     return 1;
6697
6698   if (opcode == 1 << 22) /* SUB */
6699     return -1;
6700
6701   return 0;
6702 }
6703
6704 /* Perform a relocation as part of a final link.  */
6705
6706 static bfd_reloc_status_type
6707 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
6708                                bfd *                        input_bfd,
6709                                bfd *                        output_bfd,
6710                                asection *                   input_section,
6711                                bfd_byte *                   contents,
6712                                Elf_Internal_Rela *          rel,
6713                                bfd_vma                      value,
6714                                struct bfd_link_info *       info,
6715                                asection *                   sym_sec,
6716                                const char *                 sym_name,
6717                                int                          sym_flags,
6718                                struct elf_link_hash_entry * h,
6719                                bfd_boolean *                unresolved_reloc_p,
6720                                char **                      error_message)
6721 {
6722   unsigned long                 r_type = howto->type;
6723   unsigned long                 r_symndx;
6724   bfd_byte *                    hit_data = contents + rel->r_offset;
6725   bfd *                         dynobj = NULL;
6726   Elf_Internal_Shdr *           symtab_hdr;
6727   struct elf_link_hash_entry ** sym_hashes;
6728   bfd_vma *                     local_got_offsets;
6729   asection *                    sgot = NULL;
6730   asection *                    splt = NULL;
6731   asection *                    sreloc = NULL;
6732   bfd_vma                       addend;
6733   bfd_signed_vma                signed_addend;
6734   struct elf32_arm_link_hash_table * globals;
6735
6736   globals = elf32_arm_hash_table (info);
6737
6738   BFD_ASSERT (is_arm_elf (input_bfd));
6739
6740   /* Some relocation types map to different relocations depending on the
6741      target.  We pick the right one here.  */
6742   r_type = arm_real_reloc_type (globals, r_type);
6743   if (r_type != howto->type)
6744     howto = elf32_arm_howto_from_type (r_type);
6745
6746   /* If the start address has been set, then set the EF_ARM_HASENTRY
6747      flag.  Setting this more than once is redundant, but the cost is
6748      not too high, and it keeps the code simple.
6749
6750      The test is done  here, rather than somewhere else, because the
6751      start address is only set just before the final link commences.
6752
6753      Note - if the user deliberately sets a start address of 0, the
6754      flag will not be set.  */
6755   if (bfd_get_start_address (output_bfd) != 0)
6756     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
6757
6758   dynobj = elf_hash_table (info)->dynobj;
6759   if (dynobj)
6760     {
6761       sgot = bfd_get_section_by_name (dynobj, ".got");
6762       splt = bfd_get_section_by_name (dynobj, ".plt");
6763     }
6764   symtab_hdr = & elf_symtab_hdr (input_bfd);
6765   sym_hashes = elf_sym_hashes (input_bfd);
6766   local_got_offsets = elf_local_got_offsets (input_bfd);
6767   r_symndx = ELF32_R_SYM (rel->r_info);
6768
6769   if (globals->use_rel)
6770     {
6771       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
6772
6773       if (addend & ((howto->src_mask + 1) >> 1))
6774         {
6775           signed_addend = -1;
6776           signed_addend &= ~ howto->src_mask;
6777           signed_addend |= addend;
6778         }
6779       else
6780         signed_addend = addend;
6781     }
6782   else
6783     addend = signed_addend = rel->r_addend;
6784
6785   switch (r_type)
6786     {
6787     case R_ARM_NONE:
6788       /* We don't need to find a value for this symbol.  It's just a
6789          marker.  */
6790       *unresolved_reloc_p = FALSE;
6791       return bfd_reloc_ok;
6792
6793     case R_ARM_ABS12:
6794       if (!globals->vxworks_p)
6795         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
6796
6797     case R_ARM_PC24:
6798     case R_ARM_ABS32:
6799     case R_ARM_ABS32_NOI:
6800     case R_ARM_REL32:
6801     case R_ARM_REL32_NOI:
6802     case R_ARM_CALL:
6803     case R_ARM_JUMP24:
6804     case R_ARM_XPC25:
6805     case R_ARM_PREL31:
6806     case R_ARM_PLT32:
6807       /* Handle relocations which should use the PLT entry.  ABS32/REL32
6808          will use the symbol's value, which may point to a PLT entry, but we
6809          don't need to handle that here.  If we created a PLT entry, all
6810          branches in this object should go to it, except if the PLT is too
6811          far away, in which case a long branch stub should be inserted.  */
6812       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
6813            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
6814            && r_type != R_ARM_CALL
6815            && r_type != R_ARM_JUMP24
6816            && r_type != R_ARM_PLT32)
6817           && h != NULL
6818           && splt != NULL
6819           && h->plt.offset != (bfd_vma) -1)
6820         {
6821           /* If we've created a .plt section, and assigned a PLT entry to
6822              this function, it should not be known to bind locally.  If
6823              it were, we would have cleared the PLT entry.  */
6824           BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
6825
6826           value = (splt->output_section->vma
6827                    + splt->output_offset
6828                    + h->plt.offset);
6829           *unresolved_reloc_p = FALSE;
6830           return _bfd_final_link_relocate (howto, input_bfd, input_section,
6831                                            contents, rel->r_offset, value,
6832                                            rel->r_addend);
6833         }
6834
6835       /* When generating a shared object or relocatable executable, these
6836          relocations are copied into the output file to be resolved at
6837          run time.  */
6838       if ((info->shared || globals->root.is_relocatable_executable)
6839           && (input_section->flags & SEC_ALLOC)
6840           && !(elf32_arm_hash_table (info)->vxworks_p
6841                && strcmp (input_section->output_section->name,
6842                           ".tls_vars") == 0)
6843           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
6844               || !SYMBOL_CALLS_LOCAL (info, h))
6845           && (h == NULL
6846               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6847               || h->root.type != bfd_link_hash_undefweak)
6848           && r_type != R_ARM_PC24
6849           && r_type != R_ARM_CALL
6850           && r_type != R_ARM_JUMP24
6851           && r_type != R_ARM_PREL31
6852           && r_type != R_ARM_PLT32)
6853         {
6854           Elf_Internal_Rela outrel;
6855           bfd_byte *loc;
6856           bfd_boolean skip, relocate;
6857
6858           *unresolved_reloc_p = FALSE;
6859
6860           if (sreloc == NULL)
6861             {
6862               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
6863                                                            ! globals->use_rel);
6864
6865               if (sreloc == NULL)
6866                 return bfd_reloc_notsupported;
6867             }
6868
6869           skip = FALSE;
6870           relocate = FALSE;
6871
6872           outrel.r_addend = addend;
6873           outrel.r_offset =
6874             _bfd_elf_section_offset (output_bfd, info, input_section,
6875                                      rel->r_offset);
6876           if (outrel.r_offset == (bfd_vma) -1)
6877             skip = TRUE;
6878           else if (outrel.r_offset == (bfd_vma) -2)
6879             skip = TRUE, relocate = TRUE;
6880           outrel.r_offset += (input_section->output_section->vma
6881                               + input_section->output_offset);
6882
6883           if (skip)
6884             memset (&outrel, 0, sizeof outrel);
6885           else if (h != NULL
6886                    && h->dynindx != -1
6887                    && (!info->shared
6888                        || !info->symbolic
6889                        || !h->def_regular))
6890             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
6891           else
6892             {
6893               int symbol;
6894
6895               /* This symbol is local, or marked to become local.  */
6896               if (sym_flags == STT_ARM_TFUNC)
6897                 value |= 1;
6898               if (globals->symbian_p)
6899                 {
6900                   asection *osec;
6901
6902                   /* On Symbian OS, the data segment and text segement
6903                      can be relocated independently.  Therefore, we
6904                      must indicate the segment to which this
6905                      relocation is relative.  The BPABI allows us to
6906                      use any symbol in the right segment; we just use
6907                      the section symbol as it is convenient.  (We
6908                      cannot use the symbol given by "h" directly as it
6909                      will not appear in the dynamic symbol table.)
6910
6911                      Note that the dynamic linker ignores the section
6912                      symbol value, so we don't subtract osec->vma
6913                      from the emitted reloc addend.  */
6914                   if (sym_sec)
6915                     osec = sym_sec->output_section;
6916                   else
6917                     osec = input_section->output_section;
6918                   symbol = elf_section_data (osec)->dynindx;
6919                   if (symbol == 0)
6920                     {
6921                       struct elf_link_hash_table *htab = elf_hash_table (info);
6922
6923                       if ((osec->flags & SEC_READONLY) == 0
6924                           && htab->data_index_section != NULL)
6925                         osec = htab->data_index_section;
6926                       else
6927                         osec = htab->text_index_section;
6928                       symbol = elf_section_data (osec)->dynindx;
6929                     }
6930                   BFD_ASSERT (symbol != 0);
6931                 }
6932               else
6933                 /* On SVR4-ish systems, the dynamic loader cannot
6934                    relocate the text and data segments independently,
6935                    so the symbol does not matter.  */
6936                 symbol = 0;
6937               outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
6938               if (globals->use_rel)
6939                 relocate = TRUE;
6940               else
6941                 outrel.r_addend += value;
6942             }
6943
6944           loc = sreloc->contents;
6945           loc += sreloc->reloc_count++ * RELOC_SIZE (globals);
6946           SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
6947
6948           /* If this reloc is against an external symbol, we do not want to
6949              fiddle with the addend.  Otherwise, we need to include the symbol
6950              value so that it becomes an addend for the dynamic reloc.  */
6951           if (! relocate)
6952             return bfd_reloc_ok;
6953
6954           return _bfd_final_link_relocate (howto, input_bfd, input_section,
6955                                            contents, rel->r_offset, value,
6956                                            (bfd_vma) 0);
6957         }
6958       else switch (r_type)
6959         {
6960         case R_ARM_ABS12:
6961           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
6962
6963         case R_ARM_XPC25:         /* Arm BLX instruction.  */
6964         case R_ARM_CALL:
6965         case R_ARM_JUMP24:
6966         case R_ARM_PC24:          /* Arm B/BL instruction.  */
6967         case R_ARM_PLT32:
6968           {
6969           bfd_signed_vma branch_offset;
6970           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
6971
6972           if (r_type == R_ARM_XPC25)
6973             {
6974               /* Check for Arm calling Arm function.  */
6975               /* FIXME: Should we translate the instruction into a BL
6976                  instruction instead ?  */
6977               if (sym_flags != STT_ARM_TFUNC)
6978                 (*_bfd_error_handler)
6979                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
6980                    input_bfd,
6981                    h ? h->root.root.string : "(local)");
6982             }
6983           else if (r_type == R_ARM_PC24)
6984             {
6985               /* Check for Arm calling Thumb function.  */
6986               if (sym_flags == STT_ARM_TFUNC)
6987                 {
6988                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
6989                                                output_bfd, input_section,
6990                                                hit_data, sym_sec, rel->r_offset,
6991                                                signed_addend, value,
6992                                                error_message))
6993                     return bfd_reloc_ok;
6994                   else
6995                     return bfd_reloc_dangerous;
6996                 }
6997             }
6998
6999           /* Check if a stub has to be inserted because the
7000              destination is too far or we are changing mode.  */
7001           if (   r_type == R_ARM_CALL
7002               || r_type == R_ARM_JUMP24
7003               || r_type == R_ARM_PLT32)
7004             {
7005               bfd_vma from;
7006               
7007               /* If the call goes through a PLT entry, make sure to
7008                  check distance to the right destination address.  */
7009               if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
7010                 {
7011                   value = (splt->output_section->vma
7012                            + splt->output_offset
7013                            + h->plt.offset);
7014                   *unresolved_reloc_p = FALSE;
7015                 }
7016
7017               from = (input_section->output_section->vma
7018                       + input_section->output_offset
7019                       + rel->r_offset);
7020               branch_offset = (bfd_signed_vma)(value - from);
7021
7022               if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
7023                   || branch_offset < ARM_MAX_BWD_BRANCH_OFFSET
7024                   || ((sym_flags == STT_ARM_TFUNC)
7025                       && (((r_type == R_ARM_CALL) && !globals->use_blx)
7026                           || (r_type == R_ARM_JUMP24)
7027                           || (r_type == R_ARM_PLT32) ))
7028                   )
7029                 {
7030                   /* The target is out of reach, so redirect the
7031                      branch to the local stub for this function.  */
7032
7033                   stub_entry = elf32_arm_get_stub_entry (input_section,
7034                                                          sym_sec, h,
7035                                                          rel, globals);
7036                   if (stub_entry != NULL)
7037                     value = (stub_entry->stub_offset
7038                              + stub_entry->stub_sec->output_offset
7039                              + stub_entry->stub_sec->output_section->vma);
7040                 }
7041             }
7042
7043           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
7044              where:
7045               S is the address of the symbol in the relocation.
7046               P is address of the instruction being relocated.
7047               A is the addend (extracted from the instruction) in bytes.
7048
7049              S is held in 'value'.
7050              P is the base address of the section containing the
7051                instruction plus the offset of the reloc into that
7052                section, ie:
7053                  (input_section->output_section->vma +
7054                   input_section->output_offset +
7055                   rel->r_offset).
7056              A is the addend, converted into bytes, ie:
7057                  (signed_addend * 4)
7058
7059              Note: None of these operations have knowledge of the pipeline
7060              size of the processor, thus it is up to the assembler to
7061              encode this information into the addend.  */
7062           value -= (input_section->output_section->vma
7063                     + input_section->output_offset);
7064           value -= rel->r_offset;
7065           if (globals->use_rel)
7066             value += (signed_addend << howto->size);
7067           else
7068             /* RELA addends do not have to be adjusted by howto->size.  */
7069             value += signed_addend;
7070
7071           signed_addend = value;
7072           signed_addend >>= howto->rightshift;
7073
7074           /* A branch to an undefined weak symbol is turned into a jump to
7075              the next instruction unless a PLT entry will be created.
7076              Do the same for local undefined symbols.  */
7077           if (h ? (h->root.type == bfd_link_hash_undefweak
7078                    && !(splt != NULL && h->plt.offset != (bfd_vma) -1))
7079               : bfd_is_und_section (sym_sec))
7080             {
7081               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000)
7082                       | 0x0affffff;
7083             }
7084           else
7085             {
7086               /* Perform a signed range check.  */
7087               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
7088                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
7089                 return bfd_reloc_overflow;
7090
7091               addend = (value & 2);
7092
7093               value = (signed_addend & howto->dst_mask)
7094                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
7095
7096               if (r_type == R_ARM_CALL)
7097                 {
7098                   /* Set the H bit in the BLX instruction.  */
7099                   if (sym_flags == STT_ARM_TFUNC)
7100                     {
7101                       if (addend)
7102                         value |= (1 << 24);
7103                       else
7104                         value &= ~(bfd_vma)(1 << 24);
7105                     }
7106
7107                   /* Select the correct instruction (BL or BLX).  */
7108                   /* Only if we are not handling a BL to a stub. In this
7109                      case, mode switching is performed by the stub.  */
7110                   if (sym_flags == STT_ARM_TFUNC && !stub_entry)
7111                     value |= (1 << 28);
7112                   else
7113                     {
7114                       value &= ~(bfd_vma)(1 << 28);
7115                       value |= (1 << 24);
7116                     }
7117                 }
7118             }
7119           }
7120           break;
7121
7122         case R_ARM_ABS32:
7123           value += addend;
7124           if (sym_flags == STT_ARM_TFUNC)
7125             value |= 1;
7126           break;
7127
7128         case R_ARM_ABS32_NOI:
7129           value += addend;
7130           break;
7131
7132         case R_ARM_REL32:
7133           value += addend;
7134           if (sym_flags == STT_ARM_TFUNC)
7135             value |= 1;
7136           value -= (input_section->output_section->vma
7137                     + input_section->output_offset + rel->r_offset);
7138           break;
7139
7140         case R_ARM_REL32_NOI:
7141           value += addend;
7142           value -= (input_section->output_section->vma
7143                     + input_section->output_offset + rel->r_offset);
7144           break;
7145
7146         case R_ARM_PREL31:
7147           value -= (input_section->output_section->vma
7148                     + input_section->output_offset + rel->r_offset);
7149           value += signed_addend;
7150           if (! h || h->root.type != bfd_link_hash_undefweak)
7151             {
7152               /* Check for overflow.  */
7153               if ((value ^ (value >> 1)) & (1 << 30))
7154                 return bfd_reloc_overflow;
7155             }
7156           value &= 0x7fffffff;
7157           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
7158           if (sym_flags == STT_ARM_TFUNC)
7159             value |= 1;
7160           break;
7161         }
7162
7163       bfd_put_32 (input_bfd, value, hit_data);
7164       return bfd_reloc_ok;
7165
7166     case R_ARM_ABS8:
7167       value += addend;
7168       if ((long) value > 0x7f || (long) value < -0x80)
7169         return bfd_reloc_overflow;
7170
7171       bfd_put_8 (input_bfd, value, hit_data);
7172       return bfd_reloc_ok;
7173
7174     case R_ARM_ABS16:
7175       value += addend;
7176
7177       if ((long) value > 0x7fff || (long) value < -0x8000)
7178         return bfd_reloc_overflow;
7179
7180       bfd_put_16 (input_bfd, value, hit_data);
7181       return bfd_reloc_ok;
7182
7183     case R_ARM_THM_ABS5:
7184       /* Support ldr and str instructions for the thumb.  */
7185       if (globals->use_rel)
7186         {
7187           /* Need to refetch addend.  */
7188           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
7189           /* ??? Need to determine shift amount from operand size.  */
7190           addend >>= howto->rightshift;
7191         }
7192       value += addend;
7193
7194       /* ??? Isn't value unsigned?  */
7195       if ((long) value > 0x1f || (long) value < -0x10)
7196         return bfd_reloc_overflow;
7197
7198       /* ??? Value needs to be properly shifted into place first.  */
7199       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
7200       bfd_put_16 (input_bfd, value, hit_data);
7201       return bfd_reloc_ok;
7202
7203     case R_ARM_THM_ALU_PREL_11_0:
7204       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
7205       {
7206         bfd_vma insn;
7207         bfd_signed_vma relocation;
7208
7209         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
7210              | bfd_get_16 (input_bfd, hit_data + 2);
7211
7212         if (globals->use_rel)
7213           {
7214             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
7215                           | ((insn & (1 << 26)) >> 15);
7216             if (insn & 0xf00000)
7217               signed_addend = -signed_addend;
7218           }
7219
7220         relocation = value + signed_addend;
7221         relocation -= (input_section->output_section->vma
7222                        + input_section->output_offset
7223                        + rel->r_offset);
7224
7225         value = abs (relocation);
7226
7227         if (value >= 0x1000)
7228           return bfd_reloc_overflow;
7229
7230         insn = (insn & 0xfb0f8f00) | (value & 0xff)
7231              | ((value & 0x700) << 4)
7232              | ((value & 0x800) << 15);
7233         if (relocation < 0)
7234           insn |= 0xa00000;
7235
7236         bfd_put_16 (input_bfd, insn >> 16, hit_data);
7237         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
7238
7239         return bfd_reloc_ok;
7240       }
7241
7242     case R_ARM_THM_PC8:
7243       /* PR 10073:  This reloc is not generated by the GNU toolchain,
7244          but it is supported for compatibility with third party libraries
7245          generated by other compilers, specifically the ARM/IAR.  */
7246       {
7247         bfd_vma insn;
7248         bfd_signed_vma relocation;
7249
7250         insn = bfd_get_16 (input_bfd, hit_data);
7251
7252         if (globals->use_rel)
7253           addend = (insn & 0x00ff) << 2;
7254
7255         relocation = value + addend;
7256         relocation -= (input_section->output_section->vma
7257                        + input_section->output_offset
7258                        + rel->r_offset);
7259
7260         value = abs (relocation);
7261
7262         /* We do not check for overflow of this reloc.  Although strictly
7263            speaking this is incorrect, it appears to be necessary in order
7264            to work with IAR generated relocs.  Since GCC and GAS do not
7265            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
7266            a problem for them.  */
7267         value &= 0x3fc;
7268
7269         insn = (insn & 0xff00) | (value >> 2);
7270
7271         bfd_put_16 (input_bfd, insn, hit_data);
7272
7273         return bfd_reloc_ok;
7274       }
7275
7276     case R_ARM_THM_PC12:
7277       /* Corresponds to: ldr.w reg, [pc, #offset].  */
7278       {
7279         bfd_vma insn;
7280         bfd_signed_vma relocation;
7281
7282         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
7283              | bfd_get_16 (input_bfd, hit_data + 2);
7284
7285         if (globals->use_rel)
7286           {
7287             signed_addend = insn & 0xfff;
7288             if (!(insn & (1 << 23)))
7289               signed_addend = -signed_addend;
7290           }
7291
7292         relocation = value + signed_addend;
7293         relocation -= (input_section->output_section->vma
7294                        + input_section->output_offset
7295                        + rel->r_offset);
7296
7297         value = abs (relocation);
7298
7299         if (value >= 0x1000)
7300           return bfd_reloc_overflow;
7301
7302         insn = (insn & 0xff7ff000) | value;
7303         if (relocation >= 0)
7304           insn |= (1 << 23);
7305
7306         bfd_put_16 (input_bfd, insn >> 16, hit_data);
7307         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
7308
7309         return bfd_reloc_ok;
7310       }
7311
7312     case R_ARM_THM_XPC22:
7313     case R_ARM_THM_CALL:
7314     case R_ARM_THM_JUMP24:
7315       /* Thumb BL (branch long instruction).  */
7316       {
7317         bfd_vma relocation;
7318         bfd_vma reloc_sign;
7319         bfd_boolean overflow = FALSE;
7320         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
7321         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
7322         bfd_signed_vma reloc_signed_max;
7323         bfd_signed_vma reloc_signed_min;
7324         bfd_vma check;
7325         bfd_signed_vma signed_check;
7326         int bitsize;
7327         int thumb2 = using_thumb2 (globals);
7328
7329         /* A branch to an undefined weak symbol is turned into a jump to
7330            the next instruction unless a PLT entry will be created.  */
7331         if (h && h->root.type == bfd_link_hash_undefweak
7332             && !(splt != NULL && h->plt.offset != (bfd_vma) -1))
7333           {
7334             bfd_put_16 (input_bfd, 0xe000, hit_data);
7335             bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
7336             return bfd_reloc_ok;
7337           }
7338
7339         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
7340            with Thumb-1) involving the J1 and J2 bits.  */
7341         if (globals->use_rel)
7342           {
7343             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
7344             bfd_vma upper = upper_insn & 0x3ff;
7345             bfd_vma lower = lower_insn & 0x7ff;
7346             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
7347             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
7348             bfd_vma i1 = j1 ^ s ? 0 : 1;
7349             bfd_vma i2 = j2 ^ s ? 0 : 1;
7350
7351             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
7352             /* Sign extend.  */
7353             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
7354
7355             signed_addend = addend;
7356           }
7357
7358         if (r_type == R_ARM_THM_XPC22)
7359           {
7360             /* Check for Thumb to Thumb call.  */
7361             /* FIXME: Should we translate the instruction into a BL
7362                instruction instead ?  */
7363             if (sym_flags == STT_ARM_TFUNC)
7364               (*_bfd_error_handler)
7365                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
7366                  input_bfd,
7367                  h ? h->root.root.string : "(local)");
7368           }
7369         else
7370           {
7371             /* If it is not a call to Thumb, assume call to Arm.
7372                If it is a call relative to a section name, then it is not a
7373                function call at all, but rather a long jump.  Calls through
7374                the PLT do not require stubs.  */
7375             if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
7376                 && (h == NULL || splt == NULL
7377                     || h->plt.offset == (bfd_vma) -1))
7378               {
7379                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
7380                   {
7381                     /* Convert BL to BLX.  */
7382                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
7383                   }
7384                 else if ((   r_type != R_ARM_THM_CALL)
7385                          && (r_type != R_ARM_THM_JUMP24))
7386                   {
7387                     if (elf32_thumb_to_arm_stub
7388                         (info, sym_name, input_bfd, output_bfd, input_section,
7389                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
7390                          error_message))
7391                       return bfd_reloc_ok;
7392                     else
7393                       return bfd_reloc_dangerous;
7394                   }
7395               }
7396             else if (sym_flags == STT_ARM_TFUNC && globals->use_blx
7397                      && r_type == R_ARM_THM_CALL)
7398               {
7399                 /* Make sure this is a BL.  */
7400                 lower_insn |= 0x1800;
7401               }
7402           }
7403
7404         /* Handle calls via the PLT.  */
7405         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
7406           {
7407             value = (splt->output_section->vma
7408                      + splt->output_offset
7409                      + h->plt.offset);
7410             if (globals->use_blx && r_type == R_ARM_THM_CALL)
7411               {
7412                 /* If the Thumb BLX instruction is available, convert the
7413                    BL to a BLX instruction to call the ARM-mode PLT entry.  */
7414                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
7415               }
7416             else
7417               /* Target the Thumb stub before the ARM PLT entry.  */
7418               value -= PLT_THUMB_STUB_SIZE;
7419             *unresolved_reloc_p = FALSE;
7420           }
7421
7422         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
7423           {
7424             /* Check if a stub has to be inserted because the destination
7425                is too far.  */
7426             bfd_vma from;
7427             bfd_signed_vma branch_offset;
7428             struct elf32_arm_stub_hash_entry *stub_entry = NULL;
7429
7430             from = (input_section->output_section->vma
7431                     + input_section->output_offset
7432                     + rel->r_offset);
7433             branch_offset = (bfd_signed_vma)(value - from);
7434
7435             if ((!thumb2
7436                  && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
7437                      || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
7438                 ||
7439                 (thumb2
7440                  && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
7441                      || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
7442                 || ((sym_flags != STT_ARM_TFUNC)
7443                     && (((r_type == R_ARM_THM_CALL) && !globals->use_blx)
7444                         || r_type == R_ARM_THM_JUMP24)))
7445               {
7446                 /* The target is out of reach or we are changing modes, so
7447                    redirect the branch to the local stub for this
7448                    function.  */
7449                 stub_entry = elf32_arm_get_stub_entry (input_section,
7450                                                        sym_sec, h,
7451                                                        rel, globals);
7452                 if (stub_entry != NULL)
7453                   value = (stub_entry->stub_offset
7454                            + stub_entry->stub_sec->output_offset
7455                            + stub_entry->stub_sec->output_section->vma);
7456
7457                 /* If this call becomes a call to Arm, force BLX.  */
7458                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
7459                   {
7460                     if ((stub_entry
7461                          && !arm_stub_is_thumb (stub_entry->stub_type))
7462                         || (sym_flags != STT_ARM_TFUNC))
7463                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
7464                   }
7465               }
7466           }
7467
7468         relocation = value + signed_addend;
7469
7470         relocation -= (input_section->output_section->vma
7471                        + input_section->output_offset
7472                        + rel->r_offset);
7473
7474         check = relocation >> howto->rightshift;
7475
7476         /* If this is a signed value, the rightshift just dropped
7477            leading 1 bits (assuming twos complement).  */
7478         if ((bfd_signed_vma) relocation >= 0)
7479           signed_check = check;
7480         else
7481           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
7482
7483         /* Calculate the permissable maximum and minimum values for
7484            this relocation according to whether we're relocating for
7485            Thumb-2 or not.  */
7486         bitsize = howto->bitsize;
7487         if (!thumb2)
7488           bitsize -= 2;
7489         reloc_signed_max = ((1 << (bitsize - 1)) - 1) >> howto->rightshift;
7490         reloc_signed_min = ~reloc_signed_max;
7491
7492         /* Assumes two's complement.  */
7493         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
7494           overflow = TRUE;
7495
7496         if ((lower_insn & 0x5000) == 0x4000)
7497           /* For a BLX instruction, make sure that the relocation is rounded up
7498              to a word boundary.  This follows the semantics of the instruction
7499              which specifies that bit 1 of the target address will come from bit
7500              1 of the base address.  */
7501           relocation = (relocation + 2) & ~ 3;
7502
7503         /* Put RELOCATION back into the insn.  Assumes two's complement.
7504            We use the Thumb-2 encoding, which is safe even if dealing with
7505            a Thumb-1 instruction by virtue of our overflow check above.  */
7506         reloc_sign = (signed_check < 0) ? 1 : 0;
7507         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
7508                      | ((relocation >> 12) & 0x3ff)
7509                      | (reloc_sign << 10);
7510         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
7511                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
7512                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
7513                      | ((relocation >> 1) & 0x7ff);
7514
7515         /* Put the relocated value back in the object file:  */
7516         bfd_put_16 (input_bfd, upper_insn, hit_data);
7517         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
7518
7519         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
7520       }
7521       break;
7522
7523     case R_ARM_THM_JUMP19:
7524       /* Thumb32 conditional branch instruction.  */
7525       {
7526         bfd_vma relocation;
7527         bfd_boolean overflow = FALSE;
7528         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
7529         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
7530         bfd_signed_vma reloc_signed_max = 0xffffe;
7531         bfd_signed_vma reloc_signed_min = -0x100000;
7532         bfd_signed_vma signed_check;
7533
7534         /* Need to refetch the addend, reconstruct the top three bits,
7535            and squish the two 11 bit pieces together.  */
7536         if (globals->use_rel)
7537           {
7538             bfd_vma S     = (upper_insn & 0x0400) >> 10;
7539             bfd_vma upper = (upper_insn & 0x003f);
7540             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
7541             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
7542             bfd_vma lower = (lower_insn & 0x07ff);
7543
7544             upper |= J1 << 6;
7545             upper |= J2 << 7;
7546             upper |= (!S) << 8;
7547             upper -= 0x0100; /* Sign extend.  */
7548
7549             addend = (upper << 12) | (lower << 1);
7550             signed_addend = addend;
7551           }
7552
7553         /* Handle calls via the PLT.  */
7554         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
7555           {
7556             value = (splt->output_section->vma
7557                      + splt->output_offset
7558                      + h->plt.offset);
7559             /* Target the Thumb stub before the ARM PLT entry.  */
7560             value -= PLT_THUMB_STUB_SIZE;
7561             *unresolved_reloc_p = FALSE;
7562           }
7563
7564         /* ??? Should handle interworking?  GCC might someday try to
7565            use this for tail calls.  */
7566
7567         relocation = value + signed_addend;
7568         relocation -= (input_section->output_section->vma
7569                        + input_section->output_offset
7570                        + rel->r_offset);
7571         signed_check = (bfd_signed_vma) relocation;
7572
7573         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
7574           overflow = TRUE;
7575
7576         /* Put RELOCATION back into the insn.  */
7577         {
7578           bfd_vma S  = (relocation & 0x00100000) >> 20;
7579           bfd_vma J2 = (relocation & 0x00080000) >> 19;
7580           bfd_vma J1 = (relocation & 0x00040000) >> 18;
7581           bfd_vma hi = (relocation & 0x0003f000) >> 12;
7582           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
7583
7584           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
7585           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
7586         }
7587
7588         /* Put the relocated value back in the object file:  */
7589         bfd_put_16 (input_bfd, upper_insn, hit_data);
7590         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
7591
7592         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
7593       }
7594
7595     case R_ARM_THM_JUMP11:
7596     case R_ARM_THM_JUMP8:
7597     case R_ARM_THM_JUMP6:
7598       /* Thumb B (branch) instruction).  */
7599       {
7600         bfd_signed_vma relocation;
7601         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
7602         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
7603         bfd_signed_vma signed_check;
7604
7605         /* CZB cannot jump backward.  */
7606         if (r_type == R_ARM_THM_JUMP6)
7607           reloc_signed_min = 0;
7608
7609         if (globals->use_rel)
7610           {
7611             /* Need to refetch addend.  */
7612             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
7613             if (addend & ((howto->src_mask + 1) >> 1))
7614               {
7615                 signed_addend = -1;
7616                 signed_addend &= ~ howto->src_mask;
7617                 signed_addend |= addend;
7618               }
7619             else
7620               signed_addend = addend;
7621             /* The value in the insn has been right shifted.  We need to
7622                undo this, so that we can perform the address calculation
7623                in terms of bytes.  */
7624             signed_addend <<= howto->rightshift;
7625           }
7626         relocation = value + signed_addend;
7627
7628         relocation -= (input_section->output_section->vma
7629                        + input_section->output_offset
7630                        + rel->r_offset);
7631
7632         relocation >>= howto->rightshift;
7633         signed_check = relocation;
7634
7635         if (r_type == R_ARM_THM_JUMP6)
7636           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
7637         else
7638           relocation &= howto->dst_mask;
7639         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
7640
7641         bfd_put_16 (input_bfd, relocation, hit_data);
7642
7643         /* Assumes two's complement.  */
7644         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
7645           return bfd_reloc_overflow;
7646
7647         return bfd_reloc_ok;
7648       }
7649
7650     case R_ARM_ALU_PCREL7_0:
7651     case R_ARM_ALU_PCREL15_8:
7652     case R_ARM_ALU_PCREL23_15:
7653       {
7654         bfd_vma insn;
7655         bfd_vma relocation;
7656
7657         insn = bfd_get_32 (input_bfd, hit_data);
7658         if (globals->use_rel)
7659           {
7660             /* Extract the addend.  */
7661             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
7662             signed_addend = addend;
7663           }
7664         relocation = value + signed_addend;
7665
7666         relocation -= (input_section->output_section->vma
7667                        + input_section->output_offset
7668                        + rel->r_offset);
7669         insn = (insn & ~0xfff)
7670                | ((howto->bitpos << 7) & 0xf00)
7671                | ((relocation >> howto->bitpos) & 0xff);
7672         bfd_put_32 (input_bfd, value, hit_data);
7673       }
7674       return bfd_reloc_ok;
7675
7676     case R_ARM_GNU_VTINHERIT:
7677     case R_ARM_GNU_VTENTRY:
7678       return bfd_reloc_ok;
7679
7680     case R_ARM_GOTOFF32:
7681       /* Relocation is relative to the start of the
7682          global offset table.  */
7683
7684       BFD_ASSERT (sgot != NULL);
7685       if (sgot == NULL)
7686         return bfd_reloc_notsupported;
7687
7688       /* If we are addressing a Thumb function, we need to adjust the
7689          address by one, so that attempts to call the function pointer will
7690          correctly interpret it as Thumb code.  */
7691       if (sym_flags == STT_ARM_TFUNC)
7692         value += 1;
7693
7694       /* Note that sgot->output_offset is not involved in this
7695          calculation.  We always want the start of .got.  If we
7696          define _GLOBAL_OFFSET_TABLE in a different way, as is
7697          permitted by the ABI, we might have to change this
7698          calculation.  */
7699       value -= sgot->output_section->vma;
7700       return _bfd_final_link_relocate (howto, input_bfd, input_section,
7701                                        contents, rel->r_offset, value,
7702                                        rel->r_addend);
7703
7704     case R_ARM_GOTPC:
7705       /* Use global offset table as symbol value.  */
7706       BFD_ASSERT (sgot != NULL);
7707
7708       if (sgot == NULL)
7709         return bfd_reloc_notsupported;
7710
7711       *unresolved_reloc_p = FALSE;
7712       value = sgot->output_section->vma;
7713       return _bfd_final_link_relocate (howto, input_bfd, input_section,
7714                                        contents, rel->r_offset, value,
7715                                        rel->r_addend);
7716
7717     case R_ARM_GOT32:
7718     case R_ARM_GOT_PREL:
7719       /* Relocation is to the entry for this symbol in the
7720          global offset table.  */
7721       if (sgot == NULL)
7722         return bfd_reloc_notsupported;
7723
7724       if (h != NULL)
7725         {
7726           bfd_vma off;
7727           bfd_boolean dyn;
7728
7729           off = h->got.offset;
7730           BFD_ASSERT (off != (bfd_vma) -1);
7731           dyn = globals->root.dynamic_sections_created;
7732
7733           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7734               || (info->shared
7735                   && SYMBOL_REFERENCES_LOCAL (info, h))
7736               || (ELF_ST_VISIBILITY (h->other)
7737                   && h->root.type == bfd_link_hash_undefweak))
7738             {
7739               /* This is actually a static link, or it is a -Bsymbolic link
7740                  and the symbol is defined locally.  We must initialize this
7741                  entry in the global offset table.  Since the offset must
7742                  always be a multiple of 4, we use the least significant bit
7743                  to record whether we have initialized it already.
7744
7745                  When doing a dynamic link, we create a .rel(a).got relocation
7746                  entry to initialize the value.  This is done in the
7747                  finish_dynamic_symbol routine.  */
7748               if ((off & 1) != 0)
7749                 off &= ~1;
7750               else
7751                 {
7752                   /* If we are addressing a Thumb function, we need to
7753                      adjust the address by one, so that attempts to
7754                      call the function pointer will correctly
7755                      interpret it as Thumb code.  */
7756                   if (sym_flags == STT_ARM_TFUNC)
7757                     value |= 1;
7758
7759                   bfd_put_32 (output_bfd, value, sgot->contents + off);
7760                   h->got.offset |= 1;
7761                 }
7762             }
7763           else
7764             *unresolved_reloc_p = FALSE;
7765
7766           value = sgot->output_offset + off;
7767         }
7768       else
7769         {
7770           bfd_vma off;
7771
7772           BFD_ASSERT (local_got_offsets != NULL &&
7773                       local_got_offsets[r_symndx] != (bfd_vma) -1);
7774
7775           off = local_got_offsets[r_symndx];
7776
7777           /* The offset must always be a multiple of 4.  We use the
7778              least significant bit to record whether we have already
7779              generated the necessary reloc.  */
7780           if ((off & 1) != 0)
7781             off &= ~1;
7782           else
7783             {
7784               /* If we are addressing a Thumb function, we need to
7785                  adjust the address by one, so that attempts to
7786                  call the function pointer will correctly
7787                  interpret it as Thumb code.  */
7788               if (sym_flags == STT_ARM_TFUNC)
7789                 value |= 1;
7790
7791               if (globals->use_rel)
7792                 bfd_put_32 (output_bfd, value, sgot->contents + off);
7793
7794               if (info->shared)
7795                 {
7796                   asection * srelgot;
7797                   Elf_Internal_Rela outrel;
7798                   bfd_byte *loc;
7799
7800                   srelgot = (bfd_get_section_by_name
7801                              (dynobj, RELOC_SECTION (globals, ".got")));
7802                   BFD_ASSERT (srelgot != NULL);
7803
7804                   outrel.r_addend = addend + value;
7805                   outrel.r_offset = (sgot->output_section->vma
7806                                      + sgot->output_offset
7807                                      + off);
7808                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
7809                   loc = srelgot->contents;
7810                   loc += srelgot->reloc_count++ * RELOC_SIZE (globals);
7811                   SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
7812                 }
7813
7814               local_got_offsets[r_symndx] |= 1;
7815             }
7816
7817           value = sgot->output_offset + off;
7818         }
7819       if (r_type != R_ARM_GOT32)
7820         value += sgot->output_section->vma;
7821
7822       return _bfd_final_link_relocate (howto, input_bfd, input_section,
7823                                        contents, rel->r_offset, value,
7824                                        rel->r_addend);
7825
7826     case R_ARM_TLS_LDO32:
7827       value = value - dtpoff_base (info);
7828
7829       return _bfd_final_link_relocate (howto, input_bfd, input_section,
7830                                        contents, rel->r_offset, value,
7831                                        rel->r_addend);
7832
7833     case R_ARM_TLS_LDM32:
7834       {
7835         bfd_vma off;
7836
7837         if (globals->sgot == NULL)
7838           abort ();
7839
7840         off = globals->tls_ldm_got.offset;
7841
7842         if ((off & 1) != 0)
7843           off &= ~1;
7844         else
7845           {
7846             /* If we don't know the module number, create a relocation
7847                for it.  */
7848             if (info->shared)
7849               {
7850                 Elf_Internal_Rela outrel;
7851                 bfd_byte *loc;
7852
7853                 if (globals->srelgot == NULL)
7854                   abort ();
7855
7856                 outrel.r_addend = 0;
7857                 outrel.r_offset = (globals->sgot->output_section->vma
7858                                    + globals->sgot->output_offset + off);
7859                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
7860
7861                 if (globals->use_rel)
7862                   bfd_put_32 (output_bfd, outrel.r_addend,
7863                               globals->sgot->contents + off);
7864
7865                 loc = globals->srelgot->contents;
7866                 loc += globals->srelgot->reloc_count++ * RELOC_SIZE (globals);
7867                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
7868               }
7869             else
7870               bfd_put_32 (output_bfd, 1, globals->sgot->contents + off);
7871
7872             globals->tls_ldm_got.offset |= 1;
7873           }
7874
7875         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
7876           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
7877
7878         return _bfd_final_link_relocate (howto, input_bfd, input_section,
7879                                          contents, rel->r_offset, value,
7880                                          rel->r_addend);
7881       }
7882
7883     case R_ARM_TLS_GD32:
7884     case R_ARM_TLS_IE32:
7885       {
7886         bfd_vma off;
7887         int indx;
7888         char tls_type;
7889
7890         if (globals->sgot == NULL)
7891           abort ();
7892
7893         indx = 0;
7894         if (h != NULL)
7895           {
7896             bfd_boolean dyn;
7897             dyn = globals->root.dynamic_sections_created;
7898             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7899                 && (!info->shared
7900                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
7901               {
7902                 *unresolved_reloc_p = FALSE;
7903                 indx = h->dynindx;
7904               }
7905             off = h->got.offset;
7906             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
7907           }
7908         else
7909           {
7910             if (local_got_offsets == NULL)
7911               abort ();
7912             off = local_got_offsets[r_symndx];
7913             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
7914           }
7915
7916         if (tls_type == GOT_UNKNOWN)
7917           abort ();
7918
7919         if ((off & 1) != 0)
7920           off &= ~1;
7921         else
7922           {
7923             bfd_boolean need_relocs = FALSE;
7924             Elf_Internal_Rela outrel;
7925             bfd_byte *loc = NULL;
7926             int cur_off = off;
7927
7928             /* The GOT entries have not been initialized yet.  Do it
7929                now, and emit any relocations.  If both an IE GOT and a
7930                GD GOT are necessary, we emit the GD first.  */
7931
7932             if ((info->shared || indx != 0)
7933                 && (h == NULL
7934                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7935                     || h->root.type != bfd_link_hash_undefweak))
7936               {
7937                 need_relocs = TRUE;
7938                 if (globals->srelgot == NULL)
7939                   abort ();
7940                 loc = globals->srelgot->contents;
7941                 loc += globals->srelgot->reloc_count * RELOC_SIZE (globals);
7942               }
7943
7944             if (tls_type & GOT_TLS_GD)
7945               {
7946                 if (need_relocs)
7947                   {
7948                     outrel.r_addend = 0;
7949                     outrel.r_offset = (globals->sgot->output_section->vma
7950                                        + globals->sgot->output_offset
7951                                        + cur_off);
7952                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
7953
7954                     if (globals->use_rel)
7955                       bfd_put_32 (output_bfd, outrel.r_addend,
7956                                   globals->sgot->contents + cur_off);
7957
7958                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
7959                     globals->srelgot->reloc_count++;
7960                     loc += RELOC_SIZE (globals);
7961
7962                     if (indx == 0)
7963                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
7964                                   globals->sgot->contents + cur_off + 4);
7965                     else
7966                       {
7967                         outrel.r_addend = 0;
7968                         outrel.r_info = ELF32_R_INFO (indx,
7969                                                       R_ARM_TLS_DTPOFF32);
7970                         outrel.r_offset += 4;
7971
7972                         if (globals->use_rel)
7973                           bfd_put_32 (output_bfd, outrel.r_addend,
7974                                       globals->sgot->contents + cur_off + 4);
7975
7976
7977                         SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
7978                         globals->srelgot->reloc_count++;
7979                         loc += RELOC_SIZE (globals);
7980                       }
7981                   }
7982                 else
7983                   {
7984                     /* If we are not emitting relocations for a
7985                        general dynamic reference, then we must be in a
7986                        static link or an executable link with the
7987                        symbol binding locally.  Mark it as belonging
7988                        to module 1, the executable.  */
7989                     bfd_put_32 (output_bfd, 1,
7990                                 globals->sgot->contents + cur_off);
7991                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
7992                                 globals->sgot->contents + cur_off + 4);
7993                   }
7994
7995                 cur_off += 8;
7996               }
7997
7998             if (tls_type & GOT_TLS_IE)
7999               {
8000                 if (need_relocs)
8001                   {
8002                     if (indx == 0)
8003                       outrel.r_addend = value - dtpoff_base (info);
8004                     else
8005                       outrel.r_addend = 0;
8006                     outrel.r_offset = (globals->sgot->output_section->vma
8007                                        + globals->sgot->output_offset
8008                                        + cur_off);
8009                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
8010
8011                     if (globals->use_rel)
8012                       bfd_put_32 (output_bfd, outrel.r_addend,
8013                                   globals->sgot->contents + cur_off);
8014
8015                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
8016                     globals->srelgot->reloc_count++;
8017                     loc += RELOC_SIZE (globals);
8018                   }
8019                 else
8020                   bfd_put_32 (output_bfd, tpoff (info, value),
8021                               globals->sgot->contents + cur_off);
8022                 cur_off += 4;
8023               }
8024
8025             if (h != NULL)
8026               h->got.offset |= 1;
8027             else
8028               local_got_offsets[r_symndx] |= 1;
8029           }
8030
8031         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
8032           off += 8;
8033         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
8034           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
8035
8036         return _bfd_final_link_relocate (howto, input_bfd, input_section,
8037                                          contents, rel->r_offset, value,
8038                                          rel->r_addend);
8039       }
8040
8041     case R_ARM_TLS_LE32:
8042       if (info->shared)
8043         {
8044           (*_bfd_error_handler)
8045             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
8046              input_bfd, input_section,
8047              (long) rel->r_offset, howto->name);
8048           return FALSE;
8049         }
8050       else
8051         value = tpoff (info, value);
8052
8053       return _bfd_final_link_relocate (howto, input_bfd, input_section,
8054                                        contents, rel->r_offset, value,
8055                                        rel->r_addend);
8056
8057     case R_ARM_V4BX:
8058       if (globals->fix_v4bx)
8059         {
8060           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8061
8062           /* Ensure that we have a BX instruction.  */
8063           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
8064
8065           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
8066             {
8067               /* Branch to veneer.  */
8068               bfd_vma glue_addr;
8069               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
8070               glue_addr -= input_section->output_section->vma
8071                            + input_section->output_offset
8072                            + rel->r_offset + 8;
8073               insn = (insn & 0xf0000000) | 0x0a000000
8074                      | ((glue_addr >> 2) & 0x00ffffff);
8075             }
8076           else
8077             {
8078               /* Preserve Rm (lowest four bits) and the condition code
8079                  (highest four bits). Other bits encode MOV PC,Rm.  */
8080               insn = (insn & 0xf000000f) | 0x01a0f000;
8081             }
8082
8083           bfd_put_32 (input_bfd, insn, hit_data);
8084         }
8085       return bfd_reloc_ok;
8086
8087     case R_ARM_MOVW_ABS_NC:
8088     case R_ARM_MOVT_ABS:
8089     case R_ARM_MOVW_PREL_NC:
8090     case R_ARM_MOVT_PREL:
8091     /* Until we properly support segment-base-relative addressing then
8092        we assume the segment base to be zero, as for the group relocations.
8093        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
8094        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
8095     case R_ARM_MOVW_BREL_NC:
8096     case R_ARM_MOVW_BREL:
8097     case R_ARM_MOVT_BREL:
8098       {
8099         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8100
8101         if (globals->use_rel)
8102           {
8103             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
8104             signed_addend = (addend ^ 0x8000) - 0x8000;
8105           }
8106
8107         value += signed_addend;
8108
8109         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
8110           value -= (input_section->output_section->vma
8111                     + input_section->output_offset + rel->r_offset);
8112
8113         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
8114           return bfd_reloc_overflow;
8115
8116         if (sym_flags == STT_ARM_TFUNC)
8117           value |= 1;
8118
8119         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
8120             || r_type == R_ARM_MOVT_BREL)
8121           value >>= 16;
8122
8123         insn &= 0xfff0f000;
8124         insn |= value & 0xfff;
8125         insn |= (value & 0xf000) << 4;
8126         bfd_put_32 (input_bfd, insn, hit_data);
8127       }
8128       return bfd_reloc_ok;
8129
8130     case R_ARM_THM_MOVW_ABS_NC:
8131     case R_ARM_THM_MOVT_ABS:
8132     case R_ARM_THM_MOVW_PREL_NC:
8133     case R_ARM_THM_MOVT_PREL:
8134     /* Until we properly support segment-base-relative addressing then
8135        we assume the segment base to be zero, as for the above relocations.
8136        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
8137        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
8138        as R_ARM_THM_MOVT_ABS.  */
8139     case R_ARM_THM_MOVW_BREL_NC:
8140     case R_ARM_THM_MOVW_BREL:
8141     case R_ARM_THM_MOVT_BREL:
8142       {
8143         bfd_vma insn;
8144
8145         insn = bfd_get_16 (input_bfd, hit_data) << 16;
8146         insn |= bfd_get_16 (input_bfd, hit_data + 2);
8147
8148         if (globals->use_rel)
8149           {
8150             addend = ((insn >> 4)  & 0xf000)
8151                    | ((insn >> 15) & 0x0800)
8152                    | ((insn >> 4)  & 0x0700)
8153                    | (insn         & 0x00ff);
8154             signed_addend = (addend ^ 0x8000) - 0x8000;
8155           }
8156
8157         value += signed_addend;
8158
8159         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
8160           value -= (input_section->output_section->vma
8161                     + input_section->output_offset + rel->r_offset);
8162
8163         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
8164           return bfd_reloc_overflow;
8165
8166         if (sym_flags == STT_ARM_TFUNC)
8167           value |= 1;
8168
8169         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
8170             || r_type == R_ARM_THM_MOVT_BREL)
8171           value >>= 16;
8172
8173         insn &= 0xfbf08f00;
8174         insn |= (value & 0xf000) << 4;
8175         insn |= (value & 0x0800) << 15;
8176         insn |= (value & 0x0700) << 4;
8177         insn |= (value & 0x00ff);
8178
8179         bfd_put_16 (input_bfd, insn >> 16, hit_data);
8180         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
8181       }
8182       return bfd_reloc_ok;
8183
8184     case R_ARM_ALU_PC_G0_NC:
8185     case R_ARM_ALU_PC_G1_NC:
8186     case R_ARM_ALU_PC_G0:
8187     case R_ARM_ALU_PC_G1:
8188     case R_ARM_ALU_PC_G2:
8189     case R_ARM_ALU_SB_G0_NC:
8190     case R_ARM_ALU_SB_G1_NC:
8191     case R_ARM_ALU_SB_G0:
8192     case R_ARM_ALU_SB_G1:
8193     case R_ARM_ALU_SB_G2:
8194       {
8195         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8196         bfd_vma pc = input_section->output_section->vma
8197                      + input_section->output_offset + rel->r_offset;
8198         /* sb should be the origin of the *segment* containing the symbol.
8199            It is not clear how to obtain this OS-dependent value, so we
8200            make an arbitrary choice of zero.  */
8201         bfd_vma sb = 0;
8202         bfd_vma residual;
8203         bfd_vma g_n;
8204         bfd_signed_vma signed_value;
8205         int group = 0;
8206
8207         /* Determine which group of bits to select.  */
8208         switch (r_type)
8209           {
8210           case R_ARM_ALU_PC_G0_NC:
8211           case R_ARM_ALU_PC_G0:
8212           case R_ARM_ALU_SB_G0_NC:
8213           case R_ARM_ALU_SB_G0:
8214             group = 0;
8215             break;
8216
8217           case R_ARM_ALU_PC_G1_NC:
8218           case R_ARM_ALU_PC_G1:
8219           case R_ARM_ALU_SB_G1_NC:
8220           case R_ARM_ALU_SB_G1:
8221             group = 1;
8222             break;
8223
8224           case R_ARM_ALU_PC_G2:
8225           case R_ARM_ALU_SB_G2:
8226             group = 2;
8227             break;
8228
8229           default:
8230             abort ();
8231           }
8232
8233         /* If REL, extract the addend from the insn.  If RELA, it will
8234            have already been fetched for us.  */
8235         if (globals->use_rel)
8236           {
8237             int negative;
8238             bfd_vma constant = insn & 0xff;
8239             bfd_vma rotation = (insn & 0xf00) >> 8;
8240
8241             if (rotation == 0)
8242               signed_addend = constant;
8243             else
8244               {
8245                 /* Compensate for the fact that in the instruction, the
8246                    rotation is stored in multiples of 2 bits.  */
8247                 rotation *= 2;
8248
8249                 /* Rotate "constant" right by "rotation" bits.  */
8250                 signed_addend = (constant >> rotation) |
8251                                 (constant << (8 * sizeof (bfd_vma) - rotation));
8252               }
8253
8254             /* Determine if the instruction is an ADD or a SUB.
8255                (For REL, this determines the sign of the addend.)  */
8256             negative = identify_add_or_sub (insn);
8257             if (negative == 0)
8258               {
8259                 (*_bfd_error_handler)
8260                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
8261                   input_bfd, input_section,
8262                   (long) rel->r_offset, howto->name);
8263                 return bfd_reloc_overflow;
8264               }
8265
8266             signed_addend *= negative;
8267           }
8268
8269         /* Compute the value (X) to go in the place.  */
8270         if (r_type == R_ARM_ALU_PC_G0_NC
8271             || r_type == R_ARM_ALU_PC_G1_NC
8272             || r_type == R_ARM_ALU_PC_G0
8273             || r_type == R_ARM_ALU_PC_G1
8274             || r_type == R_ARM_ALU_PC_G2)
8275           /* PC relative.  */
8276           signed_value = value - pc + signed_addend;
8277         else
8278           /* Section base relative.  */
8279           signed_value = value - sb + signed_addend;
8280
8281         /* If the target symbol is a Thumb function, then set the
8282            Thumb bit in the address.  */
8283         if (sym_flags == STT_ARM_TFUNC)
8284           signed_value |= 1;
8285
8286         /* Calculate the value of the relevant G_n, in encoded
8287            constant-with-rotation format.  */
8288         g_n = calculate_group_reloc_mask (abs (signed_value), group,
8289                                           &residual);
8290
8291         /* Check for overflow if required.  */
8292         if ((r_type == R_ARM_ALU_PC_G0
8293              || r_type == R_ARM_ALU_PC_G1
8294              || r_type == R_ARM_ALU_PC_G2
8295              || r_type == R_ARM_ALU_SB_G0
8296              || r_type == R_ARM_ALU_SB_G1
8297              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
8298           {
8299             (*_bfd_error_handler)
8300               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
8301               input_bfd, input_section,
8302               (long) rel->r_offset, abs (signed_value), howto->name);
8303             return bfd_reloc_overflow;
8304           }
8305
8306         /* Mask out the value and the ADD/SUB part of the opcode; take care
8307            not to destroy the S bit.  */
8308         insn &= 0xff1ff000;
8309
8310         /* Set the opcode according to whether the value to go in the
8311            place is negative.  */
8312         if (signed_value < 0)
8313           insn |= 1 << 22;
8314         else
8315           insn |= 1 << 23;
8316
8317         /* Encode the offset.  */
8318         insn |= g_n;
8319
8320         bfd_put_32 (input_bfd, insn, hit_data);
8321       }
8322       return bfd_reloc_ok;
8323
8324     case R_ARM_LDR_PC_G0:
8325     case R_ARM_LDR_PC_G1:
8326     case R_ARM_LDR_PC_G2:
8327     case R_ARM_LDR_SB_G0:
8328     case R_ARM_LDR_SB_G1:
8329     case R_ARM_LDR_SB_G2:
8330       {
8331         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8332         bfd_vma pc = input_section->output_section->vma
8333                      + input_section->output_offset + rel->r_offset;
8334         bfd_vma sb = 0; /* See note above.  */
8335         bfd_vma residual;
8336         bfd_signed_vma signed_value;
8337         int group = 0;
8338
8339         /* Determine which groups of bits to calculate.  */
8340         switch (r_type)
8341           {
8342           case R_ARM_LDR_PC_G0:
8343           case R_ARM_LDR_SB_G0:
8344             group = 0;
8345             break;
8346
8347           case R_ARM_LDR_PC_G1:
8348           case R_ARM_LDR_SB_G1:
8349             group = 1;
8350             break;
8351
8352           case R_ARM_LDR_PC_G2:
8353           case R_ARM_LDR_SB_G2:
8354             group = 2;
8355             break;
8356
8357           default:
8358             abort ();
8359           }
8360
8361         /* If REL, extract the addend from the insn.  If RELA, it will
8362            have already been fetched for us.  */
8363         if (globals->use_rel)
8364           {
8365             int negative = (insn & (1 << 23)) ? 1 : -1;
8366             signed_addend = negative * (insn & 0xfff);
8367           }
8368
8369         /* Compute the value (X) to go in the place.  */
8370         if (r_type == R_ARM_LDR_PC_G0
8371             || r_type == R_ARM_LDR_PC_G1
8372             || r_type == R_ARM_LDR_PC_G2)
8373           /* PC relative.  */
8374           signed_value = value - pc + signed_addend;
8375         else
8376           /* Section base relative.  */
8377           signed_value = value - sb + signed_addend;
8378
8379         /* Calculate the value of the relevant G_{n-1} to obtain
8380            the residual at that stage.  */
8381         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
8382
8383         /* Check for overflow.  */
8384         if (residual >= 0x1000)
8385           {
8386             (*_bfd_error_handler)
8387               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
8388               input_bfd, input_section,
8389               (long) rel->r_offset, abs (signed_value), howto->name);
8390             return bfd_reloc_overflow;
8391           }
8392
8393         /* Mask out the value and U bit.  */
8394         insn &= 0xff7ff000;
8395
8396         /* Set the U bit if the value to go in the place is non-negative.  */
8397         if (signed_value >= 0)
8398           insn |= 1 << 23;
8399
8400         /* Encode the offset.  */
8401         insn |= residual;
8402
8403         bfd_put_32 (input_bfd, insn, hit_data);
8404       }
8405       return bfd_reloc_ok;
8406
8407     case R_ARM_LDRS_PC_G0:
8408     case R_ARM_LDRS_PC_G1:
8409     case R_ARM_LDRS_PC_G2:
8410     case R_ARM_LDRS_SB_G0:
8411     case R_ARM_LDRS_SB_G1:
8412     case R_ARM_LDRS_SB_G2:
8413       {
8414         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8415         bfd_vma pc = input_section->output_section->vma
8416                      + input_section->output_offset + rel->r_offset;
8417         bfd_vma sb = 0; /* See note above.  */
8418         bfd_vma residual;
8419         bfd_signed_vma signed_value;
8420         int group = 0;
8421
8422         /* Determine which groups of bits to calculate.  */
8423         switch (r_type)
8424           {
8425           case R_ARM_LDRS_PC_G0:
8426           case R_ARM_LDRS_SB_G0:
8427             group = 0;
8428             break;
8429
8430           case R_ARM_LDRS_PC_G1:
8431           case R_ARM_LDRS_SB_G1:
8432             group = 1;
8433             break;
8434
8435           case R_ARM_LDRS_PC_G2:
8436           case R_ARM_LDRS_SB_G2:
8437             group = 2;
8438             break;
8439
8440           default:
8441             abort ();
8442           }
8443
8444         /* If REL, extract the addend from the insn.  If RELA, it will
8445            have already been fetched for us.  */
8446         if (globals->use_rel)
8447           {
8448             int negative = (insn & (1 << 23)) ? 1 : -1;
8449             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
8450           }
8451
8452         /* Compute the value (X) to go in the place.  */
8453         if (r_type == R_ARM_LDRS_PC_G0
8454             || r_type == R_ARM_LDRS_PC_G1
8455             || r_type == R_ARM_LDRS_PC_G2)
8456           /* PC relative.  */
8457           signed_value = value - pc + signed_addend;
8458         else
8459           /* Section base relative.  */
8460           signed_value = value - sb + signed_addend;
8461
8462         /* Calculate the value of the relevant G_{n-1} to obtain
8463            the residual at that stage.  */
8464         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
8465
8466         /* Check for overflow.  */
8467         if (residual >= 0x100)
8468           {
8469             (*_bfd_error_handler)
8470               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
8471               input_bfd, input_section,
8472               (long) rel->r_offset, abs (signed_value), howto->name);
8473             return bfd_reloc_overflow;
8474           }
8475
8476         /* Mask out the value and U bit.  */
8477         insn &= 0xff7ff0f0;
8478
8479         /* Set the U bit if the value to go in the place is non-negative.  */
8480         if (signed_value >= 0)
8481           insn |= 1 << 23;
8482
8483         /* Encode the offset.  */
8484         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
8485
8486         bfd_put_32 (input_bfd, insn, hit_data);
8487       }
8488       return bfd_reloc_ok;
8489
8490     case R_ARM_LDC_PC_G0:
8491     case R_ARM_LDC_PC_G1:
8492     case R_ARM_LDC_PC_G2:
8493     case R_ARM_LDC_SB_G0:
8494     case R_ARM_LDC_SB_G1:
8495     case R_ARM_LDC_SB_G2:
8496       {
8497         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
8498         bfd_vma pc = input_section->output_section->vma
8499                      + input_section->output_offset + rel->r_offset;
8500         bfd_vma sb = 0; /* See note above.  */
8501         bfd_vma residual;
8502         bfd_signed_vma signed_value;
8503         int group = 0;
8504
8505         /* Determine which groups of bits to calculate.  */
8506         switch (r_type)
8507           {
8508           case R_ARM_LDC_PC_G0:
8509           case R_ARM_LDC_SB_G0:
8510             group = 0;
8511             break;
8512
8513           case R_ARM_LDC_PC_G1:
8514           case R_ARM_LDC_SB_G1:
8515             group = 1;
8516             break;
8517
8518           case R_ARM_LDC_PC_G2:
8519           case R_ARM_LDC_SB_G2:
8520             group = 2;
8521             break;
8522
8523           default:
8524             abort ();
8525           }
8526
8527         /* If REL, extract the addend from the insn.  If RELA, it will
8528            have already been fetched for us.  */
8529         if (globals->use_rel)
8530           {
8531             int negative = (insn & (1 << 23)) ? 1 : -1;
8532             signed_addend = negative * ((insn & 0xff) << 2);
8533           }
8534
8535         /* Compute the value (X) to go in the place.  */
8536         if (r_type == R_ARM_LDC_PC_G0
8537             || r_type == R_ARM_LDC_PC_G1
8538             || r_type == R_ARM_LDC_PC_G2)
8539           /* PC relative.  */
8540           signed_value = value - pc + signed_addend;
8541         else
8542           /* Section base relative.  */
8543           signed_value = value - sb + signed_addend;
8544
8545         /* Calculate the value of the relevant G_{n-1} to obtain
8546            the residual at that stage.  */
8547         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
8548
8549         /* Check for overflow.  (The absolute value to go in the place must be
8550            divisible by four and, after having been divided by four, must
8551            fit in eight bits.)  */
8552         if ((residual & 0x3) != 0 || residual >= 0x400)
8553           {
8554             (*_bfd_error_handler)
8555               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
8556               input_bfd, input_section,
8557               (long) rel->r_offset, abs (signed_value), howto->name);
8558             return bfd_reloc_overflow;
8559           }
8560
8561         /* Mask out the value and U bit.  */
8562         insn &= 0xff7fff00;
8563
8564         /* Set the U bit if the value to go in the place is non-negative.  */
8565         if (signed_value >= 0)
8566           insn |= 1 << 23;
8567
8568         /* Encode the offset.  */
8569         insn |= residual >> 2;
8570
8571         bfd_put_32 (input_bfd, insn, hit_data);
8572       }
8573       return bfd_reloc_ok;
8574
8575     default:
8576       return bfd_reloc_notsupported;
8577     }
8578 }
8579
8580 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
8581 static void
8582 arm_add_to_rel (bfd *              abfd,
8583                 bfd_byte *         address,
8584                 reloc_howto_type * howto,
8585                 bfd_signed_vma     increment)
8586 {
8587   bfd_signed_vma addend;
8588
8589   if (howto->type == R_ARM_THM_CALL
8590       || howto->type == R_ARM_THM_JUMP24)
8591     {
8592       int upper_insn, lower_insn;
8593       int upper, lower;
8594
8595       upper_insn = bfd_get_16 (abfd, address);
8596       lower_insn = bfd_get_16 (abfd, address + 2);
8597       upper = upper_insn & 0x7ff;
8598       lower = lower_insn & 0x7ff;
8599
8600       addend = (upper << 12) | (lower << 1);
8601       addend += increment;
8602       addend >>= 1;
8603
8604       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
8605       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
8606
8607       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
8608       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
8609     }
8610   else
8611     {
8612       bfd_vma        contents;
8613
8614       contents = bfd_get_32 (abfd, address);
8615
8616       /* Get the (signed) value from the instruction.  */
8617       addend = contents & howto->src_mask;
8618       if (addend & ((howto->src_mask + 1) >> 1))
8619         {
8620           bfd_signed_vma mask;
8621
8622           mask = -1;
8623           mask &= ~ howto->src_mask;
8624           addend |= mask;
8625         }
8626
8627       /* Add in the increment, (which is a byte value).  */
8628       switch (howto->type)
8629         {
8630         default:
8631           addend += increment;
8632           break;
8633
8634         case R_ARM_PC24:
8635         case R_ARM_PLT32:
8636         case R_ARM_CALL:
8637         case R_ARM_JUMP24:
8638           addend <<= howto->size;
8639           addend += increment;
8640
8641           /* Should we check for overflow here ?  */
8642
8643           /* Drop any undesired bits.  */
8644           addend >>= howto->rightshift;
8645           break;
8646         }
8647
8648       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
8649
8650       bfd_put_32 (abfd, contents, address);
8651     }
8652 }
8653
8654 #define IS_ARM_TLS_RELOC(R_TYPE)        \
8655   ((R_TYPE) == R_ARM_TLS_GD32           \
8656    || (R_TYPE) == R_ARM_TLS_LDO32       \
8657    || (R_TYPE) == R_ARM_TLS_LDM32       \
8658    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
8659    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
8660    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
8661    || (R_TYPE) == R_ARM_TLS_LE32        \
8662    || (R_TYPE) == R_ARM_TLS_IE32)
8663
8664 /* Relocate an ARM ELF section.  */
8665
8666 static bfd_boolean
8667 elf32_arm_relocate_section (bfd *                  output_bfd,
8668                             struct bfd_link_info * info,
8669                             bfd *                  input_bfd,
8670                             asection *             input_section,
8671                             bfd_byte *             contents,
8672                             Elf_Internal_Rela *    relocs,
8673                             Elf_Internal_Sym *     local_syms,
8674                             asection **            local_sections)
8675 {
8676   Elf_Internal_Shdr *symtab_hdr;
8677   struct elf_link_hash_entry **sym_hashes;
8678   Elf_Internal_Rela *rel;
8679   Elf_Internal_Rela *relend;
8680   const char *name;
8681   struct elf32_arm_link_hash_table * globals;
8682
8683   globals = elf32_arm_hash_table (info);
8684
8685   symtab_hdr = & elf_symtab_hdr (input_bfd);
8686   sym_hashes = elf_sym_hashes (input_bfd);
8687
8688   rel = relocs;
8689   relend = relocs + input_section->reloc_count;
8690   for (; rel < relend; rel++)
8691     {
8692       int                          r_type;
8693       reloc_howto_type *           howto;
8694       unsigned long                r_symndx;
8695       Elf_Internal_Sym *           sym;
8696       asection *                   sec;
8697       struct elf_link_hash_entry * h;
8698       bfd_vma                      relocation;
8699       bfd_reloc_status_type        r;
8700       arelent                      bfd_reloc;
8701       char                         sym_type;
8702       bfd_boolean                  unresolved_reloc = FALSE;
8703       char *error_message = NULL;
8704
8705       r_symndx = ELF32_R_SYM (rel->r_info);
8706       r_type   = ELF32_R_TYPE (rel->r_info);
8707       r_type   = arm_real_reloc_type (globals, r_type);
8708
8709       if (   r_type == R_ARM_GNU_VTENTRY
8710           || r_type == R_ARM_GNU_VTINHERIT)
8711         continue;
8712
8713       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
8714       howto = bfd_reloc.howto;
8715
8716       h = NULL;
8717       sym = NULL;
8718       sec = NULL;
8719
8720       if (r_symndx < symtab_hdr->sh_info)
8721         {
8722           sym = local_syms + r_symndx;
8723           sym_type = ELF32_ST_TYPE (sym->st_info);
8724           sec = local_sections[r_symndx];
8725
8726           /* An object file might have a reference to a local
8727              undefined symbol.  This is a daft object file, but we
8728              should at least do something about it.  V4BX & NONE
8729              relocations do not use the symbol and are explicitly
8730              allowed to use the undefined symbol, so allow those.  */
8731           if (r_type != R_ARM_V4BX
8732               && r_type != R_ARM_NONE
8733               && bfd_is_und_section (sec)
8734               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
8735             {
8736               if (!info->callbacks->undefined_symbol
8737                   (info, bfd_elf_string_from_elf_section
8738                    (input_bfd, symtab_hdr->sh_link, sym->st_name),
8739                    input_bfd, input_section,
8740                    rel->r_offset, TRUE))
8741                 return FALSE;
8742             }
8743           
8744           if (globals->use_rel)
8745             {
8746               relocation = (sec->output_section->vma
8747                             + sec->output_offset
8748                             + sym->st_value);
8749               if (!info->relocatable
8750                   && (sec->flags & SEC_MERGE)
8751                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8752                 {
8753                   asection *msec;
8754                   bfd_vma addend, value;
8755
8756                   switch (r_type)
8757                     {
8758                     case R_ARM_MOVW_ABS_NC:
8759                     case R_ARM_MOVT_ABS:
8760                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
8761                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
8762                       addend = (addend ^ 0x8000) - 0x8000;
8763                       break;
8764
8765                     case R_ARM_THM_MOVW_ABS_NC:
8766                     case R_ARM_THM_MOVT_ABS:
8767                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
8768                               << 16;
8769                       value |= bfd_get_16 (input_bfd,
8770                                            contents + rel->r_offset + 2);
8771                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
8772                                | ((value & 0x04000000) >> 15);
8773                       addend = (addend ^ 0x8000) - 0x8000;
8774                       break;
8775
8776                     default:
8777                       if (howto->rightshift
8778                           || (howto->src_mask & (howto->src_mask + 1)))
8779                         {
8780                           (*_bfd_error_handler)
8781                             (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
8782                              input_bfd, input_section,
8783                              (long) rel->r_offset, howto->name);
8784                           return FALSE;
8785                         }
8786
8787                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
8788
8789                       /* Get the (signed) value from the instruction.  */
8790                       addend = value & howto->src_mask;
8791                       if (addend & ((howto->src_mask + 1) >> 1))
8792                         {
8793                           bfd_signed_vma mask;
8794
8795                           mask = -1;
8796                           mask &= ~ howto->src_mask;
8797                           addend |= mask;
8798                         }
8799                       break;
8800                     }
8801
8802                   msec = sec;
8803                   addend =
8804                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
8805                     - relocation;
8806                   addend += msec->output_section->vma + msec->output_offset;
8807
8808                   /* Cases here must match those in the preceeding
8809                      switch statement.  */
8810                   switch (r_type)
8811                     {
8812                     case R_ARM_MOVW_ABS_NC:
8813                     case R_ARM_MOVT_ABS:
8814                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
8815                               | (addend & 0xfff);
8816                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
8817                       break;
8818
8819                     case R_ARM_THM_MOVW_ABS_NC:
8820                     case R_ARM_THM_MOVT_ABS:
8821                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
8822                               | (addend & 0xff) | ((addend & 0x0800) << 15);
8823                       bfd_put_16 (input_bfd, value >> 16,
8824                                   contents + rel->r_offset);
8825                       bfd_put_16 (input_bfd, value,
8826                                   contents + rel->r_offset + 2);
8827                       break;
8828
8829                     default:
8830                       value = (value & ~ howto->dst_mask)
8831                               | (addend & howto->dst_mask);
8832                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
8833                       break;
8834                     }
8835                 }
8836             }
8837           else
8838             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8839         }
8840       else
8841         {
8842           bfd_boolean warned;
8843
8844           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
8845                                    r_symndx, symtab_hdr, sym_hashes,
8846                                    h, sec, relocation,
8847                                    unresolved_reloc, warned);
8848
8849           sym_type = h->type;
8850         }
8851
8852       if (sec != NULL && elf_discarded_section (sec))
8853         {
8854           /* For relocs against symbols from removed linkonce sections,
8855              or sections discarded by a linker script, we just want the
8856              section contents zeroed.  Avoid any special processing.  */
8857           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8858           rel->r_info = 0;
8859           rel->r_addend = 0;
8860           continue;
8861         }
8862
8863       if (info->relocatable)
8864         {
8865           /* This is a relocatable link.  We don't have to change
8866              anything, unless the reloc is against a section symbol,
8867              in which case we have to adjust according to where the
8868              section symbol winds up in the output section.  */
8869           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8870             {
8871               if (globals->use_rel)
8872                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
8873                                 howto, (bfd_signed_vma) sec->output_offset);
8874               else
8875                 rel->r_addend += sec->output_offset;
8876             }
8877           continue;
8878         }
8879
8880       if (h != NULL)
8881         name = h->root.root.string;
8882       else
8883         {
8884           name = (bfd_elf_string_from_elf_section
8885                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
8886           if (name == NULL || *name == '\0')
8887             name = bfd_section_name (input_bfd, sec);
8888         }
8889
8890       if (r_symndx != 0
8891           && r_type != R_ARM_NONE
8892           && (h == NULL
8893               || h->root.type == bfd_link_hash_defined
8894               || h->root.type == bfd_link_hash_defweak)
8895           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
8896         {
8897           (*_bfd_error_handler)
8898             ((sym_type == STT_TLS
8899               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
8900               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
8901              input_bfd,
8902              input_section,
8903              (long) rel->r_offset,
8904              howto->name,
8905              name);
8906         }
8907
8908       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
8909                                          input_section, contents, rel,
8910                                          relocation, info, sec, name,
8911                                          (h ? ELF_ST_TYPE (h->type) :
8912                                           ELF_ST_TYPE (sym->st_info)), h,
8913                                          &unresolved_reloc, &error_message);
8914
8915       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8916          because such sections are not SEC_ALLOC and thus ld.so will
8917          not process them.  */
8918       if (unresolved_reloc
8919           && !((input_section->flags & SEC_DEBUGGING) != 0
8920                && h->def_dynamic))
8921         {
8922           (*_bfd_error_handler)
8923             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8924              input_bfd,
8925              input_section,
8926              (long) rel->r_offset,
8927              howto->name,
8928              h->root.root.string);
8929           return FALSE;
8930         }
8931
8932       if (r != bfd_reloc_ok)
8933         {
8934           switch (r)
8935             {
8936             case bfd_reloc_overflow:
8937               /* If the overflowing reloc was to an undefined symbol,
8938                  we have already printed one error message and there
8939                  is no point complaining again.  */
8940               if ((! h ||
8941                    h->root.type != bfd_link_hash_undefined)
8942                   && (!((*info->callbacks->reloc_overflow)
8943                         (info, (h ? &h->root : NULL), name, howto->name,
8944                          (bfd_vma) 0, input_bfd, input_section,
8945                          rel->r_offset))))
8946                   return FALSE;
8947               break;
8948
8949             case bfd_reloc_undefined:
8950               if (!((*info->callbacks->undefined_symbol)
8951                     (info, name, input_bfd, input_section,
8952                      rel->r_offset, TRUE)))
8953                 return FALSE;
8954               break;
8955
8956             case bfd_reloc_outofrange:
8957               error_message = _("out of range");
8958               goto common_error;
8959
8960             case bfd_reloc_notsupported:
8961               error_message = _("unsupported relocation");
8962               goto common_error;
8963
8964             case bfd_reloc_dangerous:
8965               /* error_message should already be set.  */
8966               goto common_error;
8967
8968             default:
8969               error_message = _("unknown error");
8970               /* Fall through.  */
8971
8972             common_error:
8973               BFD_ASSERT (error_message != NULL);
8974               if (!((*info->callbacks->reloc_dangerous)
8975                     (info, error_message, input_bfd, input_section,
8976                      rel->r_offset)))
8977                 return FALSE;
8978               break;
8979             }
8980         }
8981     }
8982
8983   return TRUE;
8984 }
8985
8986 /* Add a new unwind edit to the list described by HEAD, TAIL.  If INDEX is zero,
8987    adds the edit to the start of the list.  (The list must be built in order of
8988    ascending INDEX: the function's callers are primarily responsible for
8989    maintaining that condition).  */
8990
8991 static void
8992 add_unwind_table_edit (arm_unwind_table_edit **head,
8993                        arm_unwind_table_edit **tail,
8994                        arm_unwind_edit_type type,
8995                        asection *linked_section,
8996                        unsigned int index)
8997 {
8998   arm_unwind_table_edit *new_edit = xmalloc (sizeof (arm_unwind_table_edit));
8999   
9000   new_edit->type = type;
9001   new_edit->linked_section = linked_section;
9002   new_edit->index = index;
9003   
9004   if (index > 0)
9005     {
9006       new_edit->next = NULL;
9007
9008       if (*tail)
9009         (*tail)->next = new_edit;
9010
9011       (*tail) = new_edit;
9012
9013       if (!*head)
9014         (*head) = new_edit;
9015     }
9016   else
9017     {
9018       new_edit->next = *head;
9019
9020       if (!*tail)
9021         *tail = new_edit;
9022
9023       *head = new_edit;
9024     }
9025 }
9026
9027 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
9028
9029 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
9030 static void
9031 adjust_exidx_size(asection *exidx_sec, int adjust)
9032 {
9033   asection *out_sec;
9034
9035   if (!exidx_sec->rawsize)
9036     exidx_sec->rawsize = exidx_sec->size;
9037
9038   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
9039   out_sec = exidx_sec->output_section;
9040   /* Adjust size of output section.  */
9041   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
9042 }
9043
9044 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
9045 static void
9046 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
9047 {
9048   struct _arm_elf_section_data *exidx_arm_data;
9049
9050   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
9051   add_unwind_table_edit (
9052     &exidx_arm_data->u.exidx.unwind_edit_list,
9053     &exidx_arm_data->u.exidx.unwind_edit_tail,
9054     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
9055
9056   adjust_exidx_size(exidx_sec, 8);
9057 }
9058
9059 /* Scan .ARM.exidx tables, and create a list describing edits which should be
9060    made to those tables, such that:
9061    
9062      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
9063      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
9064         codes which have been inlined into the index).
9065
9066    The edits are applied when the tables are written
9067    (in elf32_arm_write_section).
9068 */
9069
9070 bfd_boolean
9071 elf32_arm_fix_exidx_coverage (asection **text_section_order,
9072                               unsigned int num_text_sections,
9073                               struct bfd_link_info *info)
9074 {
9075   bfd *inp;
9076   unsigned int last_second_word = 0, i;
9077   asection *last_exidx_sec = NULL;
9078   asection *last_text_sec = NULL;
9079   int last_unwind_type = -1;
9080
9081   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
9082      text sections.  */
9083   for (inp = info->input_bfds; inp != NULL; inp = inp->link_next)
9084     {
9085       asection *sec;
9086       
9087       for (sec = inp->sections; sec != NULL; sec = sec->next)
9088         {
9089           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
9090           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
9091           
9092           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
9093             continue;
9094           
9095           if (elf_sec->linked_to)
9096             {
9097               Elf_Internal_Shdr *linked_hdr
9098                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
9099               struct _arm_elf_section_data *linked_sec_arm_data
9100                 = get_arm_elf_section_data (linked_hdr->bfd_section);
9101
9102               if (linked_sec_arm_data == NULL)
9103                 continue;
9104
9105               /* Link this .ARM.exidx section back from the text section it
9106                  describes.  */
9107               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
9108             }
9109         }
9110     }
9111
9112   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
9113      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
9114      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.
9115    */
9116
9117   for (i = 0; i < num_text_sections; i++)
9118     {
9119       asection *sec = text_section_order[i];
9120       asection *exidx_sec;
9121       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
9122       struct _arm_elf_section_data *exidx_arm_data;
9123       bfd_byte *contents = NULL;
9124       int deleted_exidx_bytes = 0;
9125       bfd_vma j;
9126       arm_unwind_table_edit *unwind_edit_head = NULL;
9127       arm_unwind_table_edit *unwind_edit_tail = NULL;
9128       Elf_Internal_Shdr *hdr;
9129       bfd *ibfd;
9130
9131       if (arm_data == NULL)
9132         continue;
9133
9134       exidx_sec = arm_data->u.text.arm_exidx_sec;
9135       if (exidx_sec == NULL)
9136         {
9137           /* Section has no unwind data.  */
9138           if (last_unwind_type == 0 || !last_exidx_sec)
9139             continue;
9140
9141           /* Ignore zero sized sections.  */
9142           if (sec->size == 0)
9143             continue;
9144
9145           insert_cantunwind_after(last_text_sec, last_exidx_sec);
9146           last_unwind_type = 0;
9147           continue;
9148         }
9149
9150       /* Skip /DISCARD/ sections.  */
9151       if (bfd_is_abs_section (exidx_sec->output_section))
9152         continue;
9153
9154       hdr = &elf_section_data (exidx_sec)->this_hdr;
9155       if (hdr->sh_type != SHT_ARM_EXIDX)
9156         continue;
9157       
9158       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
9159       if (exidx_arm_data == NULL)
9160         continue;
9161       
9162       ibfd = exidx_sec->owner;
9163           
9164       if (hdr->contents != NULL)
9165         contents = hdr->contents;
9166       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
9167         /* An error?  */
9168         continue;
9169
9170       for (j = 0; j < hdr->sh_size; j += 8)
9171         {
9172           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
9173           int unwind_type;
9174           int elide = 0;
9175
9176           /* An EXIDX_CANTUNWIND entry.  */
9177           if (second_word == 1)
9178             {
9179               if (last_unwind_type == 0)
9180                 elide = 1;
9181               unwind_type = 0;
9182             }
9183           /* Inlined unwinding data.  Merge if equal to previous.  */
9184           else if ((second_word & 0x80000000) != 0)
9185             {
9186               if (last_second_word == second_word && last_unwind_type == 1)
9187                 elide = 1;
9188               unwind_type = 1;
9189               last_second_word = second_word;
9190             }
9191           /* Normal table entry.  In theory we could merge these too,
9192              but duplicate entries are likely to be much less common.  */
9193           else
9194             unwind_type = 2;
9195
9196           if (elide)
9197             {
9198               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
9199                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
9200
9201               deleted_exidx_bytes += 8;
9202             }
9203
9204           last_unwind_type = unwind_type;
9205         }
9206
9207       /* Free contents if we allocated it ourselves.  */
9208       if (contents != hdr->contents)
9209         free (contents);
9210
9211       /* Record edits to be applied later (in elf32_arm_write_section).  */
9212       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
9213       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
9214           
9215       if (deleted_exidx_bytes > 0)
9216         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
9217
9218       last_exidx_sec = exidx_sec;
9219       last_text_sec = sec;
9220     }
9221
9222   /* Add terminating CANTUNWIND entry.  */
9223   if (last_exidx_sec && last_unwind_type != 0)
9224     insert_cantunwind_after(last_text_sec, last_exidx_sec);
9225
9226   return TRUE;
9227 }
9228
9229 static bfd_boolean
9230 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
9231                                bfd *ibfd, const char *name)
9232 {
9233   asection *sec, *osec;
9234
9235   sec = bfd_get_section_by_name (ibfd, name);
9236   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
9237     return TRUE;
9238
9239   osec = sec->output_section;
9240   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
9241     return TRUE;
9242
9243   if (! bfd_set_section_contents (obfd, osec, sec->contents,
9244                                   sec->output_offset, sec->size))
9245     return FALSE;
9246
9247   return TRUE;
9248 }
9249
9250 static bfd_boolean
9251 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
9252 {
9253   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
9254
9255   /* Invoke the regular ELF backend linker to do all the work.  */
9256   if (!bfd_elf_final_link (abfd, info))
9257     return FALSE;
9258
9259   /* Write out any glue sections now that we have created all the
9260      stubs.  */
9261   if (globals->bfd_of_glue_owner != NULL)
9262     {
9263       if (! elf32_arm_output_glue_section (info, abfd,
9264                                            globals->bfd_of_glue_owner,
9265                                            ARM2THUMB_GLUE_SECTION_NAME))
9266         return FALSE;
9267
9268       if (! elf32_arm_output_glue_section (info, abfd,
9269                                            globals->bfd_of_glue_owner,
9270                                            THUMB2ARM_GLUE_SECTION_NAME))
9271         return FALSE;
9272
9273       if (! elf32_arm_output_glue_section (info, abfd,
9274                                            globals->bfd_of_glue_owner,
9275                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
9276         return FALSE;
9277
9278       if (! elf32_arm_output_glue_section (info, abfd,
9279                                            globals->bfd_of_glue_owner,
9280                                            ARM_BX_GLUE_SECTION_NAME))
9281         return FALSE;
9282     }
9283
9284   return TRUE;
9285 }
9286
9287 /* Set the right machine number.  */
9288
9289 static bfd_boolean
9290 elf32_arm_object_p (bfd *abfd)
9291 {
9292   unsigned int mach;
9293
9294   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
9295
9296   if (mach != bfd_mach_arm_unknown)
9297     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
9298
9299   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
9300     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
9301
9302   else
9303     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
9304
9305   return TRUE;
9306 }
9307
9308 /* Function to keep ARM specific flags in the ELF header.  */
9309
9310 static bfd_boolean
9311 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
9312 {
9313   if (elf_flags_init (abfd)
9314       && elf_elfheader (abfd)->e_flags != flags)
9315     {
9316       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
9317         {
9318           if (flags & EF_ARM_INTERWORK)
9319             (*_bfd_error_handler)
9320               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
9321                abfd);
9322           else
9323             _bfd_error_handler
9324               (_("Warning: Clearing the interworking flag of %B due to outside request"),
9325                abfd);
9326         }
9327     }
9328   else
9329     {
9330       elf_elfheader (abfd)->e_flags = flags;
9331       elf_flags_init (abfd) = TRUE;
9332     }
9333
9334   return TRUE;
9335 }
9336
9337 /* Copy backend specific data from one object module to another.  */
9338
9339 static bfd_boolean
9340 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
9341 {
9342   flagword in_flags;
9343   flagword out_flags;
9344
9345   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
9346     return TRUE;
9347
9348   in_flags  = elf_elfheader (ibfd)->e_flags;
9349   out_flags = elf_elfheader (obfd)->e_flags;
9350
9351   if (elf_flags_init (obfd)
9352       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
9353       && in_flags != out_flags)
9354     {
9355       /* Cannot mix APCS26 and APCS32 code.  */
9356       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
9357         return FALSE;
9358
9359       /* Cannot mix float APCS and non-float APCS code.  */
9360       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
9361         return FALSE;
9362
9363       /* If the src and dest have different interworking flags
9364          then turn off the interworking bit.  */
9365       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
9366         {
9367           if (out_flags & EF_ARM_INTERWORK)
9368             _bfd_error_handler
9369               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
9370                obfd, ibfd);
9371
9372           in_flags &= ~EF_ARM_INTERWORK;
9373         }
9374
9375       /* Likewise for PIC, though don't warn for this case.  */
9376       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
9377         in_flags &= ~EF_ARM_PIC;
9378     }
9379
9380   elf_elfheader (obfd)->e_flags = in_flags;
9381   elf_flags_init (obfd) = TRUE;
9382
9383   /* Also copy the EI_OSABI field.  */
9384   elf_elfheader (obfd)->e_ident[EI_OSABI] =
9385     elf_elfheader (ibfd)->e_ident[EI_OSABI];
9386
9387   /* Copy object attributes.  */
9388   _bfd_elf_copy_obj_attributes (ibfd, obfd);
9389
9390   return TRUE;
9391 }
9392
9393 /* Values for Tag_ABI_PCS_R9_use.  */
9394 enum
9395 {
9396   AEABI_R9_V6,
9397   AEABI_R9_SB,
9398   AEABI_R9_TLS,
9399   AEABI_R9_unused
9400 };
9401
9402 /* Values for Tag_ABI_PCS_RW_data.  */
9403 enum
9404 {
9405   AEABI_PCS_RW_data_absolute,
9406   AEABI_PCS_RW_data_PCrel,
9407   AEABI_PCS_RW_data_SBrel,
9408   AEABI_PCS_RW_data_unused
9409 };
9410
9411 /* Values for Tag_ABI_enum_size.  */
9412 enum
9413 {
9414   AEABI_enum_unused,
9415   AEABI_enum_short,
9416   AEABI_enum_wide,
9417   AEABI_enum_forced_wide
9418 };
9419
9420 /* Determine whether an object attribute tag takes an integer, a
9421    string or both.  */
9422
9423 static int
9424 elf32_arm_obj_attrs_arg_type (int tag)
9425 {
9426   if (tag == Tag_compatibility)
9427     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
9428   else if (tag == Tag_nodefaults)
9429     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
9430   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
9431     return ATTR_TYPE_FLAG_STR_VAL;
9432   else if (tag < 32)
9433     return ATTR_TYPE_FLAG_INT_VAL;
9434   else
9435     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
9436 }
9437
9438 /* The ABI defines that Tag_conformance should be emitted first, and that
9439    Tag_nodefaults should be second (if either is defined).  This sets those
9440    two positions, and bumps up the position of all the remaining tags to
9441    compensate.  */
9442 static int
9443 elf32_arm_obj_attrs_order (int num)
9444 {
9445   if (num == 4)
9446     return Tag_conformance;
9447   if (num == 5)
9448     return Tag_nodefaults;
9449   if ((num - 2) < Tag_nodefaults)
9450     return num - 2;
9451   if ((num - 1) < Tag_conformance)
9452     return num - 1;
9453   return num;
9454 }
9455
9456 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
9457    Returns -1 if no architecture could be read.  */
9458
9459 static int
9460 get_secondary_compatible_arch (bfd *abfd)
9461 {
9462   obj_attribute *attr =
9463     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
9464
9465   /* Note: the tag and its argument below are uleb128 values, though
9466      currently-defined values fit in one byte for each.  */
9467   if (attr->s
9468       && attr->s[0] == Tag_CPU_arch
9469       && (attr->s[1] & 128) != 128
9470       && attr->s[2] == 0)
9471    return attr->s[1];
9472
9473   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
9474   return -1;
9475 }
9476
9477 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9478    The tag is removed if ARCH is -1.  */
9479
9480 static void
9481 set_secondary_compatible_arch (bfd *abfd, int arch)
9482 {
9483   obj_attribute *attr =
9484     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
9485
9486   if (arch == -1)
9487     {
9488       attr->s = NULL;
9489       return;
9490     }
9491
9492   /* Note: the tag and its argument below are uleb128 values, though
9493      currently-defined values fit in one byte for each.  */
9494   if (!attr->s)
9495     attr->s = bfd_alloc (abfd, 3);
9496   attr->s[0] = Tag_CPU_arch;
9497   attr->s[1] = arch;
9498   attr->s[2] = '\0';
9499 }
9500
9501 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9502    into account.  */
9503
9504 static int
9505 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
9506                       int newtag, int secondary_compat)
9507 {
9508 #define T(X) TAG_CPU_ARCH_##X
9509   int tagl, tagh, result;
9510   const int v6t2[] =
9511     {
9512       T(V6T2),   /* PRE_V4.  */
9513       T(V6T2),   /* V4.  */
9514       T(V6T2),   /* V4T.  */
9515       T(V6T2),   /* V5T.  */
9516       T(V6T2),   /* V5TE.  */
9517       T(V6T2),   /* V5TEJ.  */
9518       T(V6T2),   /* V6.  */
9519       T(V7),     /* V6KZ.  */
9520       T(V6T2)    /* V6T2.  */
9521     };
9522   const int v6k[] =
9523     {
9524       T(V6K),    /* PRE_V4.  */
9525       T(V6K),    /* V4.  */
9526       T(V6K),    /* V4T.  */
9527       T(V6K),    /* V5T.  */
9528       T(V6K),    /* V5TE.  */
9529       T(V6K),    /* V5TEJ.  */
9530       T(V6K),    /* V6.  */
9531       T(V6KZ),   /* V6KZ.  */
9532       T(V7),     /* V6T2.  */
9533       T(V6K)     /* V6K.  */
9534     };
9535   const int v7[] =
9536     {
9537       T(V7),     /* PRE_V4.  */
9538       T(V7),     /* V4.  */
9539       T(V7),     /* V4T.  */
9540       T(V7),     /* V5T.  */
9541       T(V7),     /* V5TE.  */
9542       T(V7),     /* V5TEJ.  */
9543       T(V7),     /* V6.  */
9544       T(V7),     /* V6KZ.  */
9545       T(V7),     /* V6T2.  */
9546       T(V7),     /* V6K.  */
9547       T(V7)      /* V7.  */
9548     };
9549   const int v6_m[] =
9550     {
9551       -1,        /* PRE_V4.  */
9552       -1,        /* V4.  */
9553       T(V6K),    /* V4T.  */
9554       T(V6K),    /* V5T.  */
9555       T(V6K),    /* V5TE.  */
9556       T(V6K),    /* V5TEJ.  */
9557       T(V6K),    /* V6.  */
9558       T(V6KZ),   /* V6KZ.  */
9559       T(V7),     /* V6T2.  */
9560       T(V6K),    /* V6K.  */
9561       T(V7),     /* V7.  */
9562       T(V6_M)    /* V6_M.  */
9563     };
9564   const int v6s_m[] =
9565     {
9566       -1,        /* PRE_V4.  */
9567       -1,        /* V4.  */
9568       T(V6K),    /* V4T.  */
9569       T(V6K),    /* V5T.  */
9570       T(V6K),    /* V5TE.  */
9571       T(V6K),    /* V5TEJ.  */
9572       T(V6K),    /* V6.  */
9573       T(V6KZ),   /* V6KZ.  */
9574       T(V7),     /* V6T2.  */
9575       T(V6K),    /* V6K.  */
9576       T(V7),     /* V7.  */
9577       T(V6S_M),  /* V6_M.  */
9578       T(V6S_M)   /* V6S_M.  */
9579     };
9580   const int v4t_plus_v6_m[] =
9581     {
9582       -1,               /* PRE_V4.  */
9583       -1,               /* V4.  */
9584       T(V4T),           /* V4T.  */
9585       T(V5T),           /* V5T.  */
9586       T(V5TE),          /* V5TE.  */
9587       T(V5TEJ),         /* V5TEJ.  */
9588       T(V6),            /* V6.  */
9589       T(V6KZ),          /* V6KZ.  */
9590       T(V6T2),          /* V6T2.  */
9591       T(V6K),           /* V6K.  */
9592       T(V7),            /* V7.  */
9593       T(V6_M),          /* V6_M.  */
9594       T(V6S_M),         /* V6S_M.  */
9595       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
9596     };
9597   const int *comb[] =
9598     {
9599       v6t2,
9600       v6k,
9601       v7,
9602       v6_m,
9603       v6s_m,
9604       /* Pseudo-architecture.  */
9605       v4t_plus_v6_m
9606     };
9607
9608   /* Check we've not got a higher architecture than we know about.  */
9609
9610   if (oldtag >= MAX_TAG_CPU_ARCH || newtag >= MAX_TAG_CPU_ARCH)
9611     {
9612       _bfd_error_handler (_("error: %B: Unknown CPU architecture"), ibfd);
9613       return -1;
9614     }
9615
9616   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
9617
9618   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9619       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9620     oldtag = T(V4T_PLUS_V6_M);
9621
9622   /* And override the new tag if we have a Tag_also_compatible_with on the
9623      input.  */
9624
9625   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9626       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9627     newtag = T(V4T_PLUS_V6_M);
9628
9629   tagl = (oldtag < newtag) ? oldtag : newtag;
9630   result = tagh = (oldtag > newtag) ? oldtag : newtag;
9631
9632   /* Architectures before V6KZ add features monotonically.  */
9633   if (tagh <= TAG_CPU_ARCH_V6KZ)
9634     return result;
9635
9636   result = comb[tagh - T(V6T2)][tagl];
9637
9638   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9639      as the canonical version.  */
9640   if (result == T(V4T_PLUS_V6_M))
9641     {
9642       result = T(V4T);
9643       *secondary_compat_out = T(V6_M);
9644     }
9645   else
9646     *secondary_compat_out = -1;
9647
9648   if (result == -1)
9649     {
9650       _bfd_error_handler (_("error: %B: Conflicting CPU architectures %d/%d"),
9651                           ibfd, oldtag, newtag);
9652       return -1;
9653     }
9654
9655   return result;
9656 #undef T
9657 }
9658
9659 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
9660    are conflicting attributes.  */
9661
9662 static bfd_boolean
9663 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
9664 {
9665   obj_attribute *in_attr;
9666   obj_attribute *out_attr;
9667   obj_attribute_list *in_list;
9668   obj_attribute_list *out_list;
9669   obj_attribute_list **out_listp;
9670   /* Some tags have 0 = don't care, 1 = strong requirement,
9671      2 = weak requirement.  */
9672   static const int order_021[3] = {0, 2, 1};
9673   /* For use with Tag_VFP_arch.  */
9674   static const int order_01243[5] = {0, 1, 2, 4, 3};
9675   int i;
9676   bfd_boolean result = TRUE;
9677
9678   /* Skip the linker stubs file.  This preserves previous behavior
9679      of accepting unknown attributes in the first input file - but
9680      is that a bug?  */
9681   if (ibfd->flags & BFD_LINKER_CREATED)
9682     return TRUE;
9683
9684   if (!elf_known_obj_attributes_proc (obfd)[0].i)
9685     {
9686       /* This is the first object.  Copy the attributes.  */
9687       _bfd_elf_copy_obj_attributes (ibfd, obfd);
9688
9689       /* Use the Tag_null value to indicate the attributes have been
9690          initialized.  */
9691       elf_known_obj_attributes_proc (obfd)[0].i = 1;
9692
9693       return TRUE;
9694     }
9695
9696   in_attr = elf_known_obj_attributes_proc (ibfd);
9697   out_attr = elf_known_obj_attributes_proc (obfd);
9698   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
9699   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
9700     {
9701       /* Ignore mismatches if the object doesn't use floating point.  */
9702       if (out_attr[Tag_ABI_FP_number_model].i == 0)
9703         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
9704       else if (in_attr[Tag_ABI_FP_number_model].i != 0)
9705         {
9706           _bfd_error_handler
9707             (_("error: %B uses VFP register arguments, %B does not"),
9708              ibfd, obfd);
9709           result = FALSE;
9710         }
9711     }
9712
9713   for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
9714     {
9715       /* Merge this attribute with existing attributes.  */
9716       switch (i)
9717         {
9718         case Tag_CPU_raw_name:
9719         case Tag_CPU_name:
9720           /* These are merged after Tag_CPU_arch. */
9721           break;
9722
9723         case Tag_ABI_optimization_goals:
9724         case Tag_ABI_FP_optimization_goals:
9725           /* Use the first value seen.  */
9726           break;
9727
9728         case Tag_CPU_arch:
9729           {
9730             int secondary_compat = -1, secondary_compat_out = -1;
9731             unsigned int saved_out_attr = out_attr[i].i;
9732             static const char *name_table[] = {
9733                 /* These aren't real CPU names, but we can't guess
9734                    that from the architecture version alone.  */
9735                 "Pre v4",
9736                 "ARM v4",
9737                 "ARM v4T",
9738                 "ARM v5T",
9739                 "ARM v5TE",
9740                 "ARM v5TEJ",
9741                 "ARM v6",
9742                 "ARM v6KZ",
9743                 "ARM v6T2",
9744                 "ARM v6K",
9745                 "ARM v7",
9746                 "ARM v6-M",
9747                 "ARM v6S-M"
9748             };
9749
9750             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
9751             secondary_compat = get_secondary_compatible_arch (ibfd);
9752             secondary_compat_out = get_secondary_compatible_arch (obfd);
9753             out_attr[i].i = tag_cpu_arch_combine (ibfd, out_attr[i].i,
9754                                                   &secondary_compat_out,
9755                                                   in_attr[i].i,
9756                                                   secondary_compat);
9757             set_secondary_compatible_arch (obfd, secondary_compat_out);
9758
9759             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
9760             if (out_attr[i].i == saved_out_attr)
9761               ; /* Leave the names alone.  */
9762             else if (out_attr[i].i == in_attr[i].i)
9763               {
9764                 /* The output architecture has been changed to match the
9765                    input architecture.  Use the input names.  */
9766                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
9767                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
9768                   : NULL;
9769                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
9770                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
9771                   : NULL;
9772               }
9773             else
9774               {
9775                 out_attr[Tag_CPU_name].s = NULL;
9776                 out_attr[Tag_CPU_raw_name].s = NULL;
9777               }
9778
9779             /* If we still don't have a value for Tag_CPU_name,
9780                make one up now.  Tag_CPU_raw_name remains blank.  */
9781             if (out_attr[Tag_CPU_name].s == NULL
9782                 && out_attr[i].i < ARRAY_SIZE (name_table))
9783               out_attr[Tag_CPU_name].s =
9784                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
9785           }
9786           break;
9787
9788         case Tag_ARM_ISA_use:
9789         case Tag_THUMB_ISA_use:
9790         case Tag_WMMX_arch:
9791         case Tag_Advanced_SIMD_arch:
9792           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
9793         case Tag_ABI_FP_rounding:
9794         case Tag_ABI_FP_exceptions:
9795         case Tag_ABI_FP_user_exceptions:
9796         case Tag_ABI_FP_number_model:
9797         case Tag_VFP_HP_extension:
9798         case Tag_CPU_unaligned_access:
9799         case Tag_T2EE_use:
9800         case Tag_Virtualization_use:
9801         case Tag_MPextension_use:
9802           /* Use the largest value specified.  */
9803           if (in_attr[i].i > out_attr[i].i)
9804             out_attr[i].i = in_attr[i].i;
9805           break;
9806
9807         case Tag_ABI_align8_preserved:
9808         case Tag_ABI_PCS_RO_data:
9809           /* Use the smallest value specified.  */
9810           if (in_attr[i].i < out_attr[i].i)
9811             out_attr[i].i = in_attr[i].i;
9812           break;
9813
9814         case Tag_ABI_align8_needed:
9815           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
9816               && (in_attr[Tag_ABI_align8_preserved].i == 0
9817                   || out_attr[Tag_ABI_align8_preserved].i == 0))
9818             {
9819               /* This error message should be enabled once all non-conformant
9820                  binaries in the toolchain have had the attributes set
9821                  properly.
9822               _bfd_error_handler
9823                 (_("error: %B: 8-byte data alignment conflicts with %B"),
9824                  obfd, ibfd);
9825               result = FALSE; */
9826             }
9827           /* Fall through.  */
9828         case Tag_ABI_FP_denormal:
9829         case Tag_ABI_PCS_GOT_use:
9830           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
9831              value if greater than 2 (for future-proofing).  */
9832           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
9833               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
9834                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
9835             out_attr[i].i = in_attr[i].i;
9836           break;
9837
9838
9839         case Tag_CPU_arch_profile:
9840           if (out_attr[i].i != in_attr[i].i)
9841             {
9842               /* 0 will merge with anything.
9843                  'A' and 'S' merge to 'A'.
9844                  'R' and 'S' merge to 'R'.
9845                  'M' and 'A|R|S' is an error.  */
9846               if (out_attr[i].i == 0
9847                   || (out_attr[i].i == 'S'
9848                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
9849                 out_attr[i].i = in_attr[i].i;
9850               else if (in_attr[i].i == 0
9851                        || (in_attr[i].i == 'S'
9852                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
9853                 ; /* Do nothing. */
9854               else
9855                 {
9856                   _bfd_error_handler
9857                     (_("error: %B: Conflicting architecture profiles %c/%c"),
9858                      ibfd,
9859                      in_attr[i].i ? in_attr[i].i : '0',
9860                      out_attr[i].i ? out_attr[i].i : '0');
9861                   result = FALSE;
9862                 }
9863             }
9864           break;
9865         case Tag_VFP_arch:
9866           /* Use the "greatest" from the sequence 0, 1, 2, 4, 3, or the
9867              largest value if greater than 4 (for future-proofing).  */
9868           if ((in_attr[i].i > 4 && in_attr[i].i > out_attr[i].i)
9869               || (in_attr[i].i <= 4 && out_attr[i].i <= 4
9870                   && order_01243[in_attr[i].i] > order_01243[out_attr[i].i]))
9871             out_attr[i].i = in_attr[i].i;
9872           break;
9873         case Tag_PCS_config:
9874           if (out_attr[i].i == 0)
9875             out_attr[i].i = in_attr[i].i;
9876           else if (in_attr[i].i != 0 && out_attr[i].i != 0)
9877             {
9878               /* It's sometimes ok to mix different configs, so this is only
9879                  a warning.  */
9880               _bfd_error_handler
9881                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
9882             }
9883           break;
9884         case Tag_ABI_PCS_R9_use:
9885           if (in_attr[i].i != out_attr[i].i
9886               && out_attr[i].i != AEABI_R9_unused
9887               && in_attr[i].i != AEABI_R9_unused)
9888             {
9889               _bfd_error_handler
9890                 (_("error: %B: Conflicting use of R9"), ibfd);
9891               result = FALSE;
9892             }
9893           if (out_attr[i].i == AEABI_R9_unused)
9894             out_attr[i].i = in_attr[i].i;
9895           break;
9896         case Tag_ABI_PCS_RW_data:
9897           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
9898               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
9899               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
9900             {
9901               _bfd_error_handler
9902                 (_("error: %B: SB relative addressing conflicts with use of R9"),
9903                  ibfd);
9904               result = FALSE;
9905             }
9906           /* Use the smallest value specified.  */
9907           if (in_attr[i].i < out_attr[i].i)
9908             out_attr[i].i = in_attr[i].i;
9909           break;
9910         case Tag_ABI_PCS_wchar_t:
9911           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
9912               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
9913             {
9914               _bfd_error_handler
9915                 (_("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"),
9916                  ibfd, in_attr[i].i, out_attr[i].i);
9917             }
9918           else if (in_attr[i].i && !out_attr[i].i)
9919             out_attr[i].i = in_attr[i].i;
9920           break;
9921         case Tag_ABI_enum_size:
9922           if (in_attr[i].i != AEABI_enum_unused)
9923             {
9924               if (out_attr[i].i == AEABI_enum_unused
9925                   || out_attr[i].i == AEABI_enum_forced_wide)
9926                 {
9927                   /* The existing object is compatible with anything.
9928                      Use whatever requirements the new object has.  */
9929                   out_attr[i].i = in_attr[i].i;
9930                 }
9931               else if (in_attr[i].i != AEABI_enum_forced_wide
9932                        && out_attr[i].i != in_attr[i].i
9933                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
9934                 {
9935                   static const char *aeabi_enum_names[] =
9936                     { "", "variable-size", "32-bit", "" };
9937                   const char *in_name =
9938                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
9939                     ? aeabi_enum_names[in_attr[i].i]
9940                     : "<unknown>";
9941                   const char *out_name =
9942                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
9943                     ? aeabi_enum_names[out_attr[i].i]
9944                     : "<unknown>";
9945                   _bfd_error_handler
9946                     (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
9947                      ibfd, in_name, out_name);
9948                 }
9949             }
9950           break;
9951         case Tag_ABI_VFP_args:
9952           /* Aready done.  */
9953           break;
9954         case Tag_ABI_WMMX_args:
9955           if (in_attr[i].i != out_attr[i].i)
9956             {
9957               _bfd_error_handler
9958                 (_("error: %B uses iWMMXt register arguments, %B does not"),
9959                  ibfd, obfd);
9960               result = FALSE;
9961             }
9962           break;
9963         case Tag_compatibility:
9964           /* Merged in target-independent code.  */
9965           break;
9966         case Tag_ABI_HardFP_use:
9967           /* 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).  */
9968           if ((in_attr[i].i == 1 && out_attr[i].i == 2)
9969               || (in_attr[i].i == 2 && out_attr[i].i == 1))
9970             out_attr[i].i = 3;
9971           else if (in_attr[i].i > out_attr[i].i)
9972             out_attr[i].i = in_attr[i].i;
9973           break;
9974         case Tag_ABI_FP_16bit_format:
9975           if (in_attr[i].i != 0 && out_attr[i].i != 0)
9976             {
9977               if (in_attr[i].i != out_attr[i].i)
9978                 {
9979                   _bfd_error_handler
9980                     (_("error: fp16 format mismatch between %B and %B"),
9981                      ibfd, obfd);
9982                   result = FALSE;
9983                 }
9984             }
9985           if (in_attr[i].i != 0)
9986             out_attr[i].i = in_attr[i].i;
9987           break;
9988
9989         case Tag_nodefaults:
9990           /* This tag is set if it exists, but the value is unused (and is
9991              typically zero).  We don't actually need to do anything here -
9992              the merge happens automatically when the type flags are merged
9993              below.  */
9994           break;
9995         case Tag_also_compatible_with:
9996           /* Already done in Tag_CPU_arch.  */
9997           break;
9998         case Tag_conformance:
9999           /* Keep the attribute if it matches.  Throw it away otherwise.
10000              No attribute means no claim to conform.  */
10001           if (!in_attr[i].s || !out_attr[i].s
10002               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
10003             out_attr[i].s = NULL;
10004           break;
10005
10006         default:
10007           {
10008             bfd *err_bfd = NULL;
10009
10010             /* The "known_obj_attributes" table does contain some undefined
10011                attributes.  Ensure that there are unused.  */
10012             if (out_attr[i].i != 0 || out_attr[i].s != NULL)
10013               err_bfd = obfd;
10014             else if (in_attr[i].i != 0 || in_attr[i].s != NULL)
10015               err_bfd = ibfd;
10016
10017             if (err_bfd != NULL)
10018               {
10019                 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
10020                 if ((i & 127) < 64)
10021                   {
10022                     _bfd_error_handler
10023                       (_("%B: Unknown mandatory EABI object attribute %d"),
10024                        err_bfd, i);
10025                     bfd_set_error (bfd_error_bad_value);
10026                     result = FALSE;
10027                   }
10028                 else
10029                   {
10030                     _bfd_error_handler
10031                       (_("Warning: %B: Unknown EABI object attribute %d"),
10032                        err_bfd, i);
10033                   }
10034               }
10035
10036             /* Only pass on attributes that match in both inputs.  */
10037             if (in_attr[i].i != out_attr[i].i
10038                 || in_attr[i].s != out_attr[i].s
10039                 || (in_attr[i].s != NULL && out_attr[i].s != NULL
10040                     && strcmp (in_attr[i].s, out_attr[i].s) != 0))
10041               {
10042                 out_attr[i].i = 0;
10043                 out_attr[i].s = NULL;
10044               }
10045           }
10046         }
10047
10048       /* If out_attr was copied from in_attr then it won't have a type yet.  */
10049       if (in_attr[i].type && !out_attr[i].type)
10050         out_attr[i].type = in_attr[i].type;
10051     }
10052
10053   /* Merge Tag_compatibility attributes and any common GNU ones.  */
10054   _bfd_elf_merge_object_attributes (ibfd, obfd);
10055
10056   /* Check for any attributes not known on ARM.  */
10057   in_list = elf_other_obj_attributes_proc (ibfd);
10058   out_listp = &elf_other_obj_attributes_proc (obfd);
10059   out_list = *out_listp;
10060
10061   for (; in_list || out_list; )
10062     {
10063       bfd *err_bfd = NULL;
10064       int err_tag = 0;
10065
10066       /* The tags for each list are in numerical order.  */
10067       /* If the tags are equal, then merge.  */
10068       if (out_list && (!in_list || in_list->tag > out_list->tag))
10069         {
10070           /* This attribute only exists in obfd.  We can't merge, and we don't
10071              know what the tag means, so delete it.  */
10072           err_bfd = obfd;
10073           err_tag = out_list->tag;
10074           *out_listp = out_list->next;
10075           out_list = *out_listp;
10076         }
10077       else if (in_list && (!out_list || in_list->tag < out_list->tag))
10078         {
10079           /* This attribute only exists in ibfd. We can't merge, and we don't
10080              know what the tag means, so ignore it.  */
10081           err_bfd = ibfd;
10082           err_tag = in_list->tag;
10083           in_list = in_list->next;
10084         }
10085       else /* The tags are equal.  */
10086         {
10087           /* As present, all attributes in the list are unknown, and
10088              therefore can't be merged meaningfully.  */
10089           err_bfd = obfd;
10090           err_tag = out_list->tag;
10091
10092           /*  Only pass on attributes that match in both inputs.  */
10093           if (in_list->attr.i != out_list->attr.i
10094               || in_list->attr.s != out_list->attr.s
10095               || (in_list->attr.s && out_list->attr.s
10096                   && strcmp (in_list->attr.s, out_list->attr.s) != 0))
10097             {
10098               /* No match.  Delete the attribute.  */
10099               *out_listp = out_list->next;
10100               out_list = *out_listp;
10101             }
10102           else
10103             {
10104               /* Matched.  Keep the attribute and move to the next.  */
10105               out_list = out_list->next;
10106               in_list = in_list->next;
10107             }
10108         }
10109
10110       if (err_bfd)
10111         {
10112           /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
10113           if ((err_tag & 127) < 64)
10114             {
10115               _bfd_error_handler
10116                 (_("%B: Unknown mandatory EABI object attribute %d"),
10117                  err_bfd, err_tag);
10118               bfd_set_error (bfd_error_bad_value);
10119               result = FALSE;
10120             }
10121           else
10122             {
10123               _bfd_error_handler
10124                 (_("Warning: %B: Unknown EABI object attribute %d"),
10125                  err_bfd, err_tag);
10126             }
10127         }
10128     }
10129   return result;
10130 }
10131
10132
10133 /* Return TRUE if the two EABI versions are incompatible.  */
10134
10135 static bfd_boolean
10136 elf32_arm_versions_compatible (unsigned iver, unsigned over)
10137 {
10138   /* v4 and v5 are the same spec before and after it was released,
10139      so allow mixing them.  */
10140   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
10141       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
10142     return TRUE;
10143
10144   return (iver == over);
10145 }
10146
10147 /* Merge backend specific data from an object file to the output
10148    object file when linking.  */
10149
10150 static bfd_boolean
10151 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
10152 {
10153   flagword out_flags;
10154   flagword in_flags;
10155   bfd_boolean flags_compatible = TRUE;
10156   asection *sec;
10157
10158   /* Check if we have the same endianess.  */
10159   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
10160     return FALSE;
10161
10162   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
10163     return TRUE;
10164
10165   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
10166     return FALSE;
10167
10168   /* The input BFD must have had its flags initialised.  */
10169   /* The following seems bogus to me -- The flags are initialized in
10170      the assembler but I don't think an elf_flags_init field is
10171      written into the object.  */
10172   /* BFD_ASSERT (elf_flags_init (ibfd)); */
10173
10174   in_flags  = elf_elfheader (ibfd)->e_flags;
10175   out_flags = elf_elfheader (obfd)->e_flags;
10176
10177   /* In theory there is no reason why we couldn't handle this.  However
10178      in practice it isn't even close to working and there is no real
10179      reason to want it.  */
10180   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
10181       && !(ibfd->flags & DYNAMIC)
10182       && (in_flags & EF_ARM_BE8))
10183     {
10184       _bfd_error_handler (_("error: %B is already in final BE8 format"),
10185                           ibfd);
10186       return FALSE;
10187     }
10188
10189   if (!elf_flags_init (obfd))
10190     {
10191       /* If the input is the default architecture and had the default
10192          flags then do not bother setting the flags for the output
10193          architecture, instead allow future merges to do this.  If no
10194          future merges ever set these flags then they will retain their
10195          uninitialised values, which surprise surprise, correspond
10196          to the default values.  */
10197       if (bfd_get_arch_info (ibfd)->the_default
10198           && elf_elfheader (ibfd)->e_flags == 0)
10199         return TRUE;
10200
10201       elf_flags_init (obfd) = TRUE;
10202       elf_elfheader (obfd)->e_flags = in_flags;
10203
10204       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
10205           && bfd_get_arch_info (obfd)->the_default)
10206         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
10207
10208       return TRUE;
10209     }
10210
10211   /* Determine what should happen if the input ARM architecture
10212      does not match the output ARM architecture.  */
10213   if (! bfd_arm_merge_machines (ibfd, obfd))
10214     return FALSE;
10215
10216   /* Identical flags must be compatible.  */
10217   if (in_flags == out_flags)
10218     return TRUE;
10219
10220   /* Check to see if the input BFD actually contains any sections.  If
10221      not, its flags may not have been initialised either, but it
10222      cannot actually cause any incompatiblity.  Do not short-circuit
10223      dynamic objects; their section list may be emptied by
10224     elf_link_add_object_symbols.
10225
10226     Also check to see if there are no code sections in the input.
10227     In this case there is no need to check for code specific flags.
10228     XXX - do we need to worry about floating-point format compatability
10229     in data sections ?  */
10230   if (!(ibfd->flags & DYNAMIC))
10231     {
10232       bfd_boolean null_input_bfd = TRUE;
10233       bfd_boolean only_data_sections = TRUE;
10234
10235       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
10236         {
10237           /* Ignore synthetic glue sections.  */
10238           if (strcmp (sec->name, ".glue_7")
10239               && strcmp (sec->name, ".glue_7t"))
10240             {
10241               if ((bfd_get_section_flags (ibfd, sec)
10242                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
10243                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
10244                 only_data_sections = FALSE;
10245
10246               null_input_bfd = FALSE;
10247               break;
10248             }
10249         }
10250
10251       if (null_input_bfd || only_data_sections)
10252         return TRUE;
10253     }
10254
10255   /* Complain about various flag mismatches.  */
10256   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
10257                                       EF_ARM_EABI_VERSION (out_flags)))
10258     {
10259       _bfd_error_handler
10260         (_("error: Source object %B has EABI version %d, but target %B has EABI version %d"),
10261          ibfd, obfd,
10262          (in_flags & EF_ARM_EABIMASK) >> 24,
10263          (out_flags & EF_ARM_EABIMASK) >> 24);
10264       return FALSE;
10265     }
10266
10267   /* Not sure what needs to be checked for EABI versions >= 1.  */
10268   /* VxWorks libraries do not use these flags.  */
10269   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
10270       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
10271       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
10272     {
10273       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
10274         {
10275           _bfd_error_handler
10276             (_("error: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
10277              ibfd, obfd,
10278              in_flags & EF_ARM_APCS_26 ? 26 : 32,
10279              out_flags & EF_ARM_APCS_26 ? 26 : 32);
10280           flags_compatible = FALSE;
10281         }
10282
10283       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
10284         {
10285           if (in_flags & EF_ARM_APCS_FLOAT)
10286             _bfd_error_handler
10287               (_("error: %B passes floats in float registers, whereas %B passes them in integer registers"),
10288                ibfd, obfd);
10289           else
10290             _bfd_error_handler
10291               (_("error: %B passes floats in integer registers, whereas %B passes them in float registers"),
10292                ibfd, obfd);
10293
10294           flags_compatible = FALSE;
10295         }
10296
10297       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
10298         {
10299           if (in_flags & EF_ARM_VFP_FLOAT)
10300             _bfd_error_handler
10301               (_("error: %B uses VFP instructions, whereas %B does not"),
10302                ibfd, obfd);
10303           else
10304             _bfd_error_handler
10305               (_("error: %B uses FPA instructions, whereas %B does not"),
10306                ibfd, obfd);
10307
10308           flags_compatible = FALSE;
10309         }
10310
10311       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
10312         {
10313           if (in_flags & EF_ARM_MAVERICK_FLOAT)
10314             _bfd_error_handler
10315               (_("error: %B uses Maverick instructions, whereas %B does not"),
10316                ibfd, obfd);
10317           else
10318             _bfd_error_handler
10319               (_("error: %B does not use Maverick instructions, whereas %B does"),
10320                ibfd, obfd);
10321
10322           flags_compatible = FALSE;
10323         }
10324
10325 #ifdef EF_ARM_SOFT_FLOAT
10326       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
10327         {
10328           /* We can allow interworking between code that is VFP format
10329              layout, and uses either soft float or integer regs for
10330              passing floating point arguments and results.  We already
10331              know that the APCS_FLOAT flags match; similarly for VFP
10332              flags.  */
10333           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
10334               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
10335             {
10336               if (in_flags & EF_ARM_SOFT_FLOAT)
10337                 _bfd_error_handler
10338                   (_("error: %B uses software FP, whereas %B uses hardware FP"),
10339                    ibfd, obfd);
10340               else
10341                 _bfd_error_handler
10342                   (_("error: %B uses hardware FP, whereas %B uses software FP"),
10343                    ibfd, obfd);
10344
10345               flags_compatible = FALSE;
10346             }
10347         }
10348 #endif
10349
10350       /* Interworking mismatch is only a warning.  */
10351       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
10352         {
10353           if (in_flags & EF_ARM_INTERWORK)
10354             {
10355               _bfd_error_handler
10356                 (_("Warning: %B supports interworking, whereas %B does not"),
10357                  ibfd, obfd);
10358             }
10359           else
10360             {
10361               _bfd_error_handler
10362                 (_("Warning: %B does not support interworking, whereas %B does"),
10363                  ibfd, obfd);
10364             }
10365         }
10366     }
10367
10368   return flags_compatible;
10369 }
10370
10371 /* Display the flags field.  */
10372
10373 static bfd_boolean
10374 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
10375 {
10376   FILE * file = (FILE *) ptr;
10377   unsigned long flags;
10378
10379   BFD_ASSERT (abfd != NULL && ptr != NULL);
10380
10381   /* Print normal ELF private data.  */
10382   _bfd_elf_print_private_bfd_data (abfd, ptr);
10383
10384   flags = elf_elfheader (abfd)->e_flags;
10385   /* Ignore init flag - it may not be set, despite the flags field
10386      containing valid data.  */
10387
10388   /* xgettext:c-format */
10389   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
10390
10391   switch (EF_ARM_EABI_VERSION (flags))
10392     {
10393     case EF_ARM_EABI_UNKNOWN:
10394       /* The following flag bits are GNU extensions and not part of the
10395          official ARM ELF extended ABI.  Hence they are only decoded if
10396          the EABI version is not set.  */
10397       if (flags & EF_ARM_INTERWORK)
10398         fprintf (file, _(" [interworking enabled]"));
10399
10400       if (flags & EF_ARM_APCS_26)
10401         fprintf (file, " [APCS-26]");
10402       else
10403         fprintf (file, " [APCS-32]");
10404
10405       if (flags & EF_ARM_VFP_FLOAT)
10406         fprintf (file, _(" [VFP float format]"));
10407       else if (flags & EF_ARM_MAVERICK_FLOAT)
10408         fprintf (file, _(" [Maverick float format]"));
10409       else
10410         fprintf (file, _(" [FPA float format]"));
10411
10412       if (flags & EF_ARM_APCS_FLOAT)
10413         fprintf (file, _(" [floats passed in float registers]"));
10414
10415       if (flags & EF_ARM_PIC)
10416         fprintf (file, _(" [position independent]"));
10417
10418       if (flags & EF_ARM_NEW_ABI)
10419         fprintf (file, _(" [new ABI]"));
10420
10421       if (flags & EF_ARM_OLD_ABI)
10422         fprintf (file, _(" [old ABI]"));
10423
10424       if (flags & EF_ARM_SOFT_FLOAT)
10425         fprintf (file, _(" [software FP]"));
10426
10427       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
10428                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
10429                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
10430                  | EF_ARM_MAVERICK_FLOAT);
10431       break;
10432
10433     case EF_ARM_EABI_VER1:
10434       fprintf (file, _(" [Version1 EABI]"));
10435
10436       if (flags & EF_ARM_SYMSARESORTED)
10437         fprintf (file, _(" [sorted symbol table]"));
10438       else
10439         fprintf (file, _(" [unsorted symbol table]"));
10440
10441       flags &= ~ EF_ARM_SYMSARESORTED;
10442       break;
10443
10444     case EF_ARM_EABI_VER2:
10445       fprintf (file, _(" [Version2 EABI]"));
10446
10447       if (flags & EF_ARM_SYMSARESORTED)
10448         fprintf (file, _(" [sorted symbol table]"));
10449       else
10450         fprintf (file, _(" [unsorted symbol table]"));
10451
10452       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
10453         fprintf (file, _(" [dynamic symbols use segment index]"));
10454
10455       if (flags & EF_ARM_MAPSYMSFIRST)
10456         fprintf (file, _(" [mapping symbols precede others]"));
10457
10458       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
10459                  | EF_ARM_MAPSYMSFIRST);
10460       break;
10461
10462     case EF_ARM_EABI_VER3:
10463       fprintf (file, _(" [Version3 EABI]"));
10464       break;
10465
10466     case EF_ARM_EABI_VER4:
10467       fprintf (file, _(" [Version4 EABI]"));
10468       goto eabi;
10469
10470     case EF_ARM_EABI_VER5:
10471       fprintf (file, _(" [Version5 EABI]"));
10472     eabi:
10473       if (flags & EF_ARM_BE8)
10474         fprintf (file, _(" [BE8]"));
10475
10476       if (flags & EF_ARM_LE8)
10477         fprintf (file, _(" [LE8]"));
10478
10479       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
10480       break;
10481
10482     default:
10483       fprintf (file, _(" <EABI version unrecognised>"));
10484       break;
10485     }
10486
10487   flags &= ~ EF_ARM_EABIMASK;
10488
10489   if (flags & EF_ARM_RELEXEC)
10490     fprintf (file, _(" [relocatable executable]"));
10491
10492   if (flags & EF_ARM_HASENTRY)
10493     fprintf (file, _(" [has entry point]"));
10494
10495   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
10496
10497   if (flags)
10498     fprintf (file, _("<Unrecognised flag bits set>"));
10499
10500   fputc ('\n', file);
10501
10502   return TRUE;
10503 }
10504
10505 static int
10506 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
10507 {
10508   switch (ELF_ST_TYPE (elf_sym->st_info))
10509     {
10510     case STT_ARM_TFUNC:
10511       return ELF_ST_TYPE (elf_sym->st_info);
10512
10513     case STT_ARM_16BIT:
10514       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
10515          This allows us to distinguish between data used by Thumb instructions
10516          and non-data (which is probably code) inside Thumb regions of an
10517          executable.  */
10518       if (type != STT_OBJECT && type != STT_TLS)
10519         return ELF_ST_TYPE (elf_sym->st_info);
10520       break;
10521
10522     default:
10523       break;
10524     }
10525
10526   return type;
10527 }
10528
10529 static asection *
10530 elf32_arm_gc_mark_hook (asection *sec,
10531                         struct bfd_link_info *info,
10532                         Elf_Internal_Rela *rel,
10533                         struct elf_link_hash_entry *h,
10534                         Elf_Internal_Sym *sym)
10535 {
10536   if (h != NULL)
10537     switch (ELF32_R_TYPE (rel->r_info))
10538       {
10539       case R_ARM_GNU_VTINHERIT:
10540       case R_ARM_GNU_VTENTRY:
10541         return NULL;
10542       }
10543
10544   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10545 }
10546
10547 /* Update the got entry reference counts for the section being removed.  */
10548
10549 static bfd_boolean
10550 elf32_arm_gc_sweep_hook (bfd *                     abfd,
10551                          struct bfd_link_info *    info,
10552                          asection *                sec,
10553                          const Elf_Internal_Rela * relocs)
10554 {
10555   Elf_Internal_Shdr *symtab_hdr;
10556   struct elf_link_hash_entry **sym_hashes;
10557   bfd_signed_vma *local_got_refcounts;
10558   const Elf_Internal_Rela *rel, *relend;
10559   struct elf32_arm_link_hash_table * globals;
10560
10561   if (info->relocatable)
10562     return TRUE;
10563
10564   globals = elf32_arm_hash_table (info);
10565
10566   elf_section_data (sec)->local_dynrel = NULL;
10567
10568   symtab_hdr = & elf_symtab_hdr (abfd);
10569   sym_hashes = elf_sym_hashes (abfd);
10570   local_got_refcounts = elf_local_got_refcounts (abfd);
10571
10572   check_use_blx (globals);
10573
10574   relend = relocs + sec->reloc_count;
10575   for (rel = relocs; rel < relend; rel++)
10576     {
10577       unsigned long r_symndx;
10578       struct elf_link_hash_entry *h = NULL;
10579       int r_type;
10580
10581       r_symndx = ELF32_R_SYM (rel->r_info);
10582       if (r_symndx >= symtab_hdr->sh_info)
10583         {
10584           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10585           while (h->root.type == bfd_link_hash_indirect
10586                  || h->root.type == bfd_link_hash_warning)
10587             h = (struct elf_link_hash_entry *) h->root.u.i.link;
10588         }
10589
10590       r_type = ELF32_R_TYPE (rel->r_info);
10591       r_type = arm_real_reloc_type (globals, r_type);
10592       switch (r_type)
10593         {
10594         case R_ARM_GOT32:
10595         case R_ARM_GOT_PREL:
10596         case R_ARM_TLS_GD32:
10597         case R_ARM_TLS_IE32:
10598           if (h != NULL)
10599             {
10600               if (h->got.refcount > 0)
10601                 h->got.refcount -= 1;
10602             }
10603           else if (local_got_refcounts != NULL)
10604             {
10605               if (local_got_refcounts[r_symndx] > 0)
10606                 local_got_refcounts[r_symndx] -= 1;
10607             }
10608           break;
10609
10610         case R_ARM_TLS_LDM32:
10611           elf32_arm_hash_table (info)->tls_ldm_got.refcount -= 1;
10612           break;
10613
10614         case R_ARM_ABS32:
10615         case R_ARM_ABS32_NOI:
10616         case R_ARM_REL32:
10617         case R_ARM_REL32_NOI:
10618         case R_ARM_PC24:
10619         case R_ARM_PLT32:
10620         case R_ARM_CALL:
10621         case R_ARM_JUMP24:
10622         case R_ARM_PREL31:
10623         case R_ARM_THM_CALL:
10624         case R_ARM_THM_JUMP24:
10625         case R_ARM_THM_JUMP19:
10626         case R_ARM_MOVW_ABS_NC:
10627         case R_ARM_MOVT_ABS:
10628         case R_ARM_MOVW_PREL_NC:
10629         case R_ARM_MOVT_PREL:
10630         case R_ARM_THM_MOVW_ABS_NC:
10631         case R_ARM_THM_MOVT_ABS:
10632         case R_ARM_THM_MOVW_PREL_NC:
10633         case R_ARM_THM_MOVT_PREL:
10634           /* Should the interworking branches be here also?  */
10635
10636           if (h != NULL)
10637             {
10638               struct elf32_arm_link_hash_entry *eh;
10639               struct elf32_arm_relocs_copied **pp;
10640               struct elf32_arm_relocs_copied *p;
10641
10642               eh = (struct elf32_arm_link_hash_entry *) h;
10643
10644               if (h->plt.refcount > 0)
10645                 {
10646                   h->plt.refcount -= 1;
10647                   if (r_type == R_ARM_THM_CALL)
10648                     eh->plt_maybe_thumb_refcount--;
10649
10650                   if (r_type == R_ARM_THM_JUMP24
10651                       || r_type == R_ARM_THM_JUMP19)
10652                     eh->plt_thumb_refcount--;
10653                 }
10654
10655               if (r_type == R_ARM_ABS32
10656                   || r_type == R_ARM_REL32
10657                   || r_type == R_ARM_ABS32_NOI
10658                   || r_type == R_ARM_REL32_NOI)
10659                 {
10660                   for (pp = &eh->relocs_copied; (p = *pp) != NULL;
10661                        pp = &p->next)
10662                   if (p->section == sec)
10663                     {
10664                       p->count -= 1;
10665                       if (ELF32_R_TYPE (rel->r_info) == R_ARM_REL32
10666                           || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32_NOI)
10667                         p->pc_count -= 1;
10668                       if (p->count == 0)
10669                         *pp = p->next;
10670                       break;
10671                     }
10672                 }
10673             }
10674           break;
10675
10676         default:
10677           break;
10678         }
10679     }
10680
10681   return TRUE;
10682 }
10683
10684 /* Look through the relocs for a section during the first phase.  */
10685
10686 static bfd_boolean
10687 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
10688                         asection *sec, const Elf_Internal_Rela *relocs)
10689 {
10690   Elf_Internal_Shdr *symtab_hdr;
10691   struct elf_link_hash_entry **sym_hashes;
10692   const Elf_Internal_Rela *rel;
10693   const Elf_Internal_Rela *rel_end;
10694   bfd *dynobj;
10695   asection *sreloc;
10696   bfd_vma *local_got_offsets;
10697   struct elf32_arm_link_hash_table *htab;
10698   bfd_boolean needs_plt;
10699   unsigned long nsyms;
10700
10701   if (info->relocatable)
10702     return TRUE;
10703
10704   BFD_ASSERT (is_arm_elf (abfd));
10705
10706   htab = elf32_arm_hash_table (info);
10707   sreloc = NULL;
10708
10709   /* Create dynamic sections for relocatable executables so that we can
10710      copy relocations.  */
10711   if (htab->root.is_relocatable_executable
10712       && ! htab->root.dynamic_sections_created)
10713     {
10714       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
10715         return FALSE;
10716     }
10717
10718   dynobj = elf_hash_table (info)->dynobj;
10719   local_got_offsets = elf_local_got_offsets (abfd);
10720
10721   symtab_hdr = & elf_symtab_hdr (abfd);
10722   sym_hashes = elf_sym_hashes (abfd);
10723   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
10724   
10725   rel_end = relocs + sec->reloc_count;
10726   for (rel = relocs; rel < rel_end; rel++)
10727     {
10728       struct elf_link_hash_entry *h;
10729       struct elf32_arm_link_hash_entry *eh;
10730       unsigned long r_symndx;
10731       int r_type;
10732
10733       r_symndx = ELF32_R_SYM (rel->r_info);
10734       r_type = ELF32_R_TYPE (rel->r_info);
10735       r_type = arm_real_reloc_type (htab, r_type);
10736
10737       if (r_symndx >= nsyms
10738           /* PR 9934: It is possible to have relocations that do not
10739              refer to symbols, thus it is also possible to have an
10740              object file containing relocations but no symbol table.  */
10741           && (r_symndx > 0 || nsyms > 0))
10742         {
10743           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
10744                                    r_symndx);
10745           return FALSE;
10746         }
10747
10748       if (nsyms == 0 || r_symndx < symtab_hdr->sh_info)
10749         h = NULL;
10750       else
10751         {
10752           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10753           while (h->root.type == bfd_link_hash_indirect
10754                  || h->root.type == bfd_link_hash_warning)
10755             h = (struct elf_link_hash_entry *) h->root.u.i.link;
10756         }
10757
10758       eh = (struct elf32_arm_link_hash_entry *) h;
10759
10760       switch (r_type)
10761         {
10762           case R_ARM_GOT32:
10763           case R_ARM_GOT_PREL:
10764           case R_ARM_TLS_GD32:
10765           case R_ARM_TLS_IE32:
10766             /* This symbol requires a global offset table entry.  */
10767             {
10768               int tls_type, old_tls_type;
10769
10770               switch (r_type)
10771                 {
10772                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
10773                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
10774                 default: tls_type = GOT_NORMAL; break;
10775                 }
10776
10777               if (h != NULL)
10778                 {
10779                   h->got.refcount++;
10780                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
10781                 }
10782               else
10783                 {
10784                   bfd_signed_vma *local_got_refcounts;
10785
10786                   /* This is a global offset table entry for a local symbol.  */
10787                   local_got_refcounts = elf_local_got_refcounts (abfd);
10788                   if (local_got_refcounts == NULL)
10789                     {
10790                       bfd_size_type size;
10791
10792                       size = symtab_hdr->sh_info;
10793                       size *= (sizeof (bfd_signed_vma) + sizeof (char));
10794                       local_got_refcounts = bfd_zalloc (abfd, size);
10795                       if (local_got_refcounts == NULL)
10796                         return FALSE;
10797                       elf_local_got_refcounts (abfd) = local_got_refcounts;
10798                       elf32_arm_local_got_tls_type (abfd)
10799                         = (char *) (local_got_refcounts + symtab_hdr->sh_info);
10800                     }
10801                   local_got_refcounts[r_symndx] += 1;
10802                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
10803                 }
10804
10805               /* We will already have issued an error message if there is a
10806                  TLS / non-TLS mismatch, based on the symbol type.  We don't
10807                  support any linker relaxations.  So just combine any TLS
10808                  types needed.  */
10809               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
10810                   && tls_type != GOT_NORMAL)
10811                 tls_type |= old_tls_type;
10812
10813               if (old_tls_type != tls_type)
10814                 {
10815                   if (h != NULL)
10816                     elf32_arm_hash_entry (h)->tls_type = tls_type;
10817                   else
10818                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
10819                 }
10820             }
10821             /* Fall through.  */
10822
10823           case R_ARM_TLS_LDM32:
10824             if (r_type == R_ARM_TLS_LDM32)
10825                 htab->tls_ldm_got.refcount++;
10826             /* Fall through.  */
10827
10828           case R_ARM_GOTOFF32:
10829           case R_ARM_GOTPC:
10830             if (htab->sgot == NULL)
10831               {
10832                 if (htab->root.dynobj == NULL)
10833                   htab->root.dynobj = abfd;
10834                 if (!create_got_section (htab->root.dynobj, info))
10835                   return FALSE;
10836               }
10837             break;
10838
10839           case R_ARM_ABS12:
10840             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
10841                ldr __GOTT_INDEX__ offsets.  */
10842             if (!htab->vxworks_p)
10843               break;
10844             /* Fall through.  */
10845
10846           case R_ARM_PC24:
10847           case R_ARM_PLT32:
10848           case R_ARM_CALL:
10849           case R_ARM_JUMP24:
10850           case R_ARM_PREL31:
10851           case R_ARM_THM_CALL:
10852           case R_ARM_THM_JUMP24:
10853           case R_ARM_THM_JUMP19:
10854             needs_plt = 1;
10855             goto normal_reloc;
10856
10857           case R_ARM_MOVW_ABS_NC:
10858           case R_ARM_MOVT_ABS:
10859           case R_ARM_THM_MOVW_ABS_NC:
10860           case R_ARM_THM_MOVT_ABS:
10861             if (info->shared)
10862               {
10863                 (*_bfd_error_handler)
10864                   (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
10865                    abfd, elf32_arm_howto_table_1[r_type].name,
10866                    (h) ? h->root.root.string : "a local symbol");
10867                 bfd_set_error (bfd_error_bad_value);
10868                 return FALSE;
10869               }
10870
10871             /* Fall through.  */
10872           case R_ARM_ABS32:
10873           case R_ARM_ABS32_NOI:
10874           case R_ARM_REL32:
10875           case R_ARM_REL32_NOI:
10876           case R_ARM_MOVW_PREL_NC:
10877           case R_ARM_MOVT_PREL:
10878           case R_ARM_THM_MOVW_PREL_NC:
10879           case R_ARM_THM_MOVT_PREL:
10880             needs_plt = 0;
10881           normal_reloc:
10882
10883             /* Should the interworking branches be listed here?  */
10884             if (h != NULL)
10885               {
10886                 /* If this reloc is in a read-only section, we might
10887                    need a copy reloc.  We can't check reliably at this
10888                    stage whether the section is read-only, as input
10889                    sections have not yet been mapped to output sections.
10890                    Tentatively set the flag for now, and correct in
10891                    adjust_dynamic_symbol.  */
10892                 if (!info->shared)
10893                   h->non_got_ref = 1;
10894
10895                 /* We may need a .plt entry if the function this reloc
10896                    refers to is in a different object.  We can't tell for
10897                    sure yet, because something later might force the
10898                    symbol local.  */
10899                 if (needs_plt)
10900                   h->needs_plt = 1;
10901
10902                 /* If we create a PLT entry, this relocation will reference
10903                    it, even if it's an ABS32 relocation.  */
10904                 h->plt.refcount += 1;
10905
10906                 /* It's too early to use htab->use_blx here, so we have to
10907                    record possible blx references separately from
10908                    relocs that definitely need a thumb stub.  */
10909
10910                 if (r_type == R_ARM_THM_CALL)
10911                   eh->plt_maybe_thumb_refcount += 1;
10912
10913                 if (r_type == R_ARM_THM_JUMP24
10914                     || r_type == R_ARM_THM_JUMP19)
10915                   eh->plt_thumb_refcount += 1;
10916               }
10917
10918             /* If we are creating a shared library or relocatable executable,
10919                and this is a reloc against a global symbol, or a non PC
10920                relative reloc against a local symbol, then we need to copy
10921                the reloc into the shared library.  However, if we are linking
10922                with -Bsymbolic, we do not need to copy a reloc against a
10923                global symbol which is defined in an object we are
10924                including in the link (i.e., DEF_REGULAR is set).  At
10925                this point we have not seen all the input files, so it is
10926                possible that DEF_REGULAR is not set now but will be set
10927                later (it is never cleared).  We account for that
10928                possibility below by storing information in the
10929                relocs_copied field of the hash table entry.  */
10930             if ((info->shared || htab->root.is_relocatable_executable)
10931                 && (sec->flags & SEC_ALLOC) != 0
10932                 && ((r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI)
10933                     || (h != NULL && ! h->needs_plt
10934                         && (! info->symbolic || ! h->def_regular))))
10935               {
10936                 struct elf32_arm_relocs_copied *p, **head;
10937
10938                 /* When creating a shared object, we must copy these
10939                    reloc types into the output file.  We create a reloc
10940                    section in dynobj and make room for this reloc.  */
10941                 if (sreloc == NULL)
10942                   {
10943                     sreloc = _bfd_elf_make_dynamic_reloc_section
10944                       (sec, dynobj, 2, abfd, ! htab->use_rel);
10945
10946                     if (sreloc == NULL)
10947                       return FALSE;
10948
10949                     /* BPABI objects never have dynamic relocations mapped.  */
10950                     if (htab->symbian_p)
10951                       {
10952                         flagword flags;
10953
10954                         flags = bfd_get_section_flags (dynobj, sreloc);
10955                         flags &= ~(SEC_LOAD | SEC_ALLOC);
10956                         bfd_set_section_flags (dynobj, sreloc, flags);
10957                       }
10958                   }
10959
10960                 /* If this is a global symbol, we count the number of
10961                    relocations we need for this symbol.  */
10962                 if (h != NULL)
10963                   {
10964                     head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
10965                   }
10966                 else
10967                   {
10968                     /* Track dynamic relocs needed for local syms too.
10969                        We really need local syms available to do this
10970                        easily.  Oh well.  */
10971                     asection *s;
10972                     void *vpp;
10973                     Elf_Internal_Sym *isym;
10974
10975                     isym = bfd_sym_from_r_symndx (&htab->sym_cache,
10976                                                   abfd, r_symndx);
10977                     if (isym == NULL)
10978                       return FALSE;
10979
10980                     s = bfd_section_from_elf_index (abfd, isym->st_shndx);
10981                     if (s == NULL)
10982                       s = sec;
10983
10984                     vpp = &elf_section_data (s)->local_dynrel;
10985                     head = (struct elf32_arm_relocs_copied **) vpp;
10986                   }
10987
10988                 p = *head;
10989                 if (p == NULL || p->section != sec)
10990                   {
10991                     bfd_size_type amt = sizeof *p;
10992
10993                     p = bfd_alloc (htab->root.dynobj, amt);
10994                     if (p == NULL)
10995                       return FALSE;
10996                     p->next = *head;
10997                     *head = p;
10998                     p->section = sec;
10999                     p->count = 0;
11000                     p->pc_count = 0;
11001                   }
11002
11003                 if (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
11004                   p->pc_count += 1;
11005                 p->count += 1;
11006               }
11007             break;
11008
11009         /* This relocation describes the C++ object vtable hierarchy.
11010            Reconstruct it for later use during GC.  */
11011         case R_ARM_GNU_VTINHERIT:
11012           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
11013             return FALSE;
11014           break;
11015
11016         /* This relocation describes which C++ vtable entries are actually
11017            used.  Record for later use during GC.  */
11018         case R_ARM_GNU_VTENTRY:
11019           BFD_ASSERT (h != NULL);
11020           if (h != NULL
11021               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
11022             return FALSE;
11023           break;
11024         }
11025     }
11026
11027   return TRUE;
11028 }
11029
11030 /* Unwinding tables are not referenced directly.  This pass marks them as
11031    required if the corresponding code section is marked.  */
11032
11033 static bfd_boolean
11034 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
11035                                   elf_gc_mark_hook_fn gc_mark_hook)
11036 {
11037   bfd *sub;
11038   Elf_Internal_Shdr **elf_shdrp;
11039   bfd_boolean again;
11040
11041   /* Marking EH data may cause additional code sections to be marked,
11042      requiring multiple passes.  */
11043   again = TRUE;
11044   while (again)
11045     {
11046       again = FALSE;
11047       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11048         {
11049           asection *o;
11050
11051           if (! is_arm_elf (sub))
11052             continue;
11053
11054           elf_shdrp = elf_elfsections (sub);
11055           for (o = sub->sections; o != NULL; o = o->next)
11056             {
11057               Elf_Internal_Shdr *hdr;
11058
11059               hdr = &elf_section_data (o)->this_hdr;
11060               if (hdr->sh_type == SHT_ARM_EXIDX
11061                   && hdr->sh_link
11062                   && hdr->sh_link < elf_numsections (sub)
11063                   && !o->gc_mark
11064                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
11065                 {
11066                   again = TRUE;
11067                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11068                     return FALSE;
11069                 }
11070             }
11071         }
11072     }
11073
11074   return TRUE;
11075 }
11076
11077 /* Treat mapping symbols as special target symbols.  */
11078
11079 static bfd_boolean
11080 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
11081 {
11082   return bfd_is_arm_special_symbol_name (sym->name,
11083                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
11084 }
11085
11086 /* This is a copy of elf_find_function() from elf.c except that
11087    ARM mapping symbols are ignored when looking for function names
11088    and STT_ARM_TFUNC is considered to a function type.  */
11089
11090 static bfd_boolean
11091 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
11092                        asection *    section,
11093                        asymbol **    symbols,
11094                        bfd_vma       offset,
11095                        const char ** filename_ptr,
11096                        const char ** functionname_ptr)
11097 {
11098   const char * filename = NULL;
11099   asymbol * func = NULL;
11100   bfd_vma low_func = 0;
11101   asymbol ** p;
11102
11103   for (p = symbols; *p != NULL; p++)
11104     {
11105       elf_symbol_type *q;
11106
11107       q = (elf_symbol_type *) *p;
11108
11109       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
11110         {
11111         default:
11112           break;
11113         case STT_FILE:
11114           filename = bfd_asymbol_name (&q->symbol);
11115           break;
11116         case STT_FUNC:
11117         case STT_ARM_TFUNC:
11118         case STT_NOTYPE:
11119           /* Skip mapping symbols.  */
11120           if ((q->symbol.flags & BSF_LOCAL)
11121               && bfd_is_arm_special_symbol_name (q->symbol.name,
11122                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
11123             continue;
11124           /* Fall through.  */
11125           if (bfd_get_section (&q->symbol) == section
11126               && q->symbol.value >= low_func
11127               && q->symbol.value <= offset)
11128             {
11129               func = (asymbol *) q;
11130               low_func = q->symbol.value;
11131             }
11132           break;
11133         }
11134     }
11135
11136   if (func == NULL)
11137     return FALSE;
11138
11139   if (filename_ptr)
11140     *filename_ptr = filename;
11141   if (functionname_ptr)
11142     *functionname_ptr = bfd_asymbol_name (func);
11143
11144   return TRUE;
11145 }
11146
11147
11148 /* Find the nearest line to a particular section and offset, for error
11149    reporting.   This code is a duplicate of the code in elf.c, except
11150    that it uses arm_elf_find_function.  */
11151
11152 static bfd_boolean
11153 elf32_arm_find_nearest_line (bfd *          abfd,
11154                              asection *     section,
11155                              asymbol **     symbols,
11156                              bfd_vma        offset,
11157                              const char **  filename_ptr,
11158                              const char **  functionname_ptr,
11159                              unsigned int * line_ptr)
11160 {
11161   bfd_boolean found = FALSE;
11162
11163   /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
11164
11165   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11166                                      filename_ptr, functionname_ptr,
11167                                      line_ptr, 0,
11168                                      & elf_tdata (abfd)->dwarf2_find_line_info))
11169     {
11170       if (!*functionname_ptr)
11171         arm_elf_find_function (abfd, section, symbols, offset,
11172                                *filename_ptr ? NULL : filename_ptr,
11173                                functionname_ptr);
11174
11175       return TRUE;
11176     }
11177
11178   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
11179                                              & found, filename_ptr,
11180                                              functionname_ptr, line_ptr,
11181                                              & elf_tdata (abfd)->line_info))
11182     return FALSE;
11183
11184   if (found && (*functionname_ptr || *line_ptr))
11185     return TRUE;
11186
11187   if (symbols == NULL)
11188     return FALSE;
11189
11190   if (! arm_elf_find_function (abfd, section, symbols, offset,
11191                                filename_ptr, functionname_ptr))
11192     return FALSE;
11193
11194   *line_ptr = 0;
11195   return TRUE;
11196 }
11197
11198 static bfd_boolean
11199 elf32_arm_find_inliner_info (bfd *          abfd,
11200                              const char **  filename_ptr,
11201                              const char **  functionname_ptr,
11202                              unsigned int * line_ptr)
11203 {
11204   bfd_boolean found;
11205   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11206                                          functionname_ptr, line_ptr,
11207                                          & elf_tdata (abfd)->dwarf2_find_line_info);
11208   return found;
11209 }
11210
11211 /* Adjust a symbol defined by a dynamic object and referenced by a
11212    regular object.  The current definition is in some section of the
11213    dynamic object, but we're not including those sections.  We have to
11214    change the definition to something the rest of the link can
11215    understand.  */
11216
11217 static bfd_boolean
11218 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
11219                                  struct elf_link_hash_entry * h)
11220 {
11221   bfd * dynobj;
11222   asection * s;
11223   struct elf32_arm_link_hash_entry * eh;
11224   struct elf32_arm_link_hash_table *globals;
11225
11226   globals = elf32_arm_hash_table (info);
11227   dynobj = elf_hash_table (info)->dynobj;
11228
11229   /* Make sure we know what is going on here.  */
11230   BFD_ASSERT (dynobj != NULL
11231               && (h->needs_plt
11232                   || h->u.weakdef != NULL
11233                   || (h->def_dynamic
11234                       && h->ref_regular
11235                       && !h->def_regular)));
11236
11237   eh = (struct elf32_arm_link_hash_entry *) h;
11238
11239   /* If this is a function, put it in the procedure linkage table.  We
11240      will fill in the contents of the procedure linkage table later,
11241      when we know the address of the .got section.  */
11242   if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
11243       || h->needs_plt)
11244     {
11245       if (h->plt.refcount <= 0
11246           || SYMBOL_CALLS_LOCAL (info, h)
11247           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
11248               && h->root.type == bfd_link_hash_undefweak))
11249         {
11250           /* This case can occur if we saw a PLT32 reloc in an input
11251              file, but the symbol was never referred to by a dynamic
11252              object, or if all references were garbage collected.  In
11253              such a case, we don't actually need to build a procedure
11254              linkage table, and we can just do a PC24 reloc instead.  */
11255           h->plt.offset = (bfd_vma) -1;
11256           eh->plt_thumb_refcount = 0;
11257           eh->plt_maybe_thumb_refcount = 0;
11258           h->needs_plt = 0;
11259         }
11260
11261       return TRUE;
11262     }
11263   else
11264     {
11265       /* It's possible that we incorrectly decided a .plt reloc was
11266          needed for an R_ARM_PC24 or similar reloc to a non-function sym
11267          in check_relocs.  We can't decide accurately between function
11268          and non-function syms in check-relocs; Objects loaded later in
11269          the link may change h->type.  So fix it now.  */
11270       h->plt.offset = (bfd_vma) -1;
11271       eh->plt_thumb_refcount = 0;
11272       eh->plt_maybe_thumb_refcount = 0;
11273     }
11274
11275   /* If this is a weak symbol, and there is a real definition, the
11276      processor independent code will have arranged for us to see the
11277      real definition first, and we can just use the same value.  */
11278   if (h->u.weakdef != NULL)
11279     {
11280       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
11281                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
11282       h->root.u.def.section = h->u.weakdef->root.u.def.section;
11283       h->root.u.def.value = h->u.weakdef->root.u.def.value;
11284       return TRUE;
11285     }
11286
11287   /* If there are no non-GOT references, we do not need a copy
11288      relocation.  */
11289   if (!h->non_got_ref)
11290     return TRUE;
11291
11292   /* This is a reference to a symbol defined by a dynamic object which
11293      is not a function.  */
11294
11295   /* If we are creating a shared library, we must presume that the
11296      only references to the symbol are via the global offset table.
11297      For such cases we need not do anything here; the relocations will
11298      be handled correctly by relocate_section.  Relocatable executables
11299      can reference data in shared objects directly, so we don't need to
11300      do anything here.  */
11301   if (info->shared || globals->root.is_relocatable_executable)
11302     return TRUE;
11303
11304   if (h->size == 0)
11305     {
11306       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
11307                              h->root.root.string);
11308       return TRUE;
11309     }
11310
11311   /* We must allocate the symbol in our .dynbss section, which will
11312      become part of the .bss section of the executable.  There will be
11313      an entry for this symbol in the .dynsym section.  The dynamic
11314      object will contain position independent code, so all references
11315      from the dynamic object to this symbol will go through the global
11316      offset table.  The dynamic linker will use the .dynsym entry to
11317      determine the address it must put in the global offset table, so
11318      both the dynamic object and the regular object will refer to the
11319      same memory location for the variable.  */
11320   s = bfd_get_section_by_name (dynobj, ".dynbss");
11321   BFD_ASSERT (s != NULL);
11322
11323   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
11324      copy the initial value out of the dynamic object and into the
11325      runtime process image.  We need to remember the offset into the
11326      .rel(a).bss section we are going to use.  */
11327   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
11328     {
11329       asection *srel;
11330
11331       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (globals, ".bss"));
11332       BFD_ASSERT (srel != NULL);
11333       srel->size += RELOC_SIZE (globals);
11334       h->needs_copy = 1;
11335     }
11336
11337   return _bfd_elf_adjust_dynamic_copy (h, s);
11338 }
11339
11340 /* Allocate space in .plt, .got and associated reloc sections for
11341    dynamic relocs.  */
11342
11343 static bfd_boolean
11344 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
11345 {
11346   struct bfd_link_info *info;
11347   struct elf32_arm_link_hash_table *htab;
11348   struct elf32_arm_link_hash_entry *eh;
11349   struct elf32_arm_relocs_copied *p;
11350   bfd_signed_vma thumb_refs;
11351
11352   eh = (struct elf32_arm_link_hash_entry *) h;
11353
11354   if (h->root.type == bfd_link_hash_indirect)
11355     return TRUE;
11356
11357   if (h->root.type == bfd_link_hash_warning)
11358     /* When warning symbols are created, they **replace** the "real"
11359        entry in the hash table, thus we never get to see the real
11360        symbol in a hash traversal.  So look at it now.  */
11361     h = (struct elf_link_hash_entry *) h->root.u.i.link;
11362
11363   info = (struct bfd_link_info *) inf;
11364   htab = elf32_arm_hash_table (info);
11365
11366   if (htab->root.dynamic_sections_created
11367       && h->plt.refcount > 0)
11368     {
11369       /* Make sure this symbol is output as a dynamic symbol.
11370          Undefined weak syms won't yet be marked as dynamic.  */
11371       if (h->dynindx == -1
11372           && !h->forced_local)
11373         {
11374           if (! bfd_elf_link_record_dynamic_symbol (info, h))
11375             return FALSE;
11376         }
11377
11378       if (info->shared
11379           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
11380         {
11381           asection *s = htab->splt;
11382
11383           /* If this is the first .plt entry, make room for the special
11384              first entry.  */
11385           if (s->size == 0)
11386             s->size += htab->plt_header_size;
11387
11388           h->plt.offset = s->size;
11389
11390           /* If we will insert a Thumb trampoline before this PLT, leave room
11391              for it.  */
11392           thumb_refs = eh->plt_thumb_refcount;
11393           if (!htab->use_blx)
11394             thumb_refs += eh->plt_maybe_thumb_refcount;
11395
11396           if (thumb_refs > 0)
11397             {
11398               h->plt.offset += PLT_THUMB_STUB_SIZE;
11399               s->size += PLT_THUMB_STUB_SIZE;
11400             }
11401
11402           /* If this symbol is not defined in a regular file, and we are
11403              not generating a shared library, then set the symbol to this
11404              location in the .plt.  This is required to make function
11405              pointers compare as equal between the normal executable and
11406              the shared library.  */
11407           if (! info->shared
11408               && !h->def_regular)
11409             {
11410               h->root.u.def.section = s;
11411               h->root.u.def.value = h->plt.offset;
11412             }
11413
11414           /* Make sure the function is not marked as Thumb, in case
11415              it is the target of an ABS32 relocation, which will
11416              point to the PLT entry.  */
11417           if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
11418             h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
11419
11420           /* Make room for this entry.  */
11421           s->size += htab->plt_entry_size;
11422
11423           if (!htab->symbian_p)
11424             {
11425               /* We also need to make an entry in the .got.plt section, which
11426                  will be placed in the .got section by the linker script.  */
11427               eh->plt_got_offset = htab->sgotplt->size;
11428               htab->sgotplt->size += 4;
11429             }
11430
11431           /* We also need to make an entry in the .rel(a).plt section.  */
11432           htab->srelplt->size += RELOC_SIZE (htab);
11433
11434           /* VxWorks executables have a second set of relocations for
11435              each PLT entry.  They go in a separate relocation section,
11436              which is processed by the kernel loader.  */
11437           if (htab->vxworks_p && !info->shared)
11438             {
11439               /* There is a relocation for the initial PLT entry:
11440                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
11441               if (h->plt.offset == htab->plt_header_size)
11442                 htab->srelplt2->size += RELOC_SIZE (htab);
11443
11444               /* There are two extra relocations for each subsequent
11445                  PLT entry: an R_ARM_32 relocation for the GOT entry,
11446                  and an R_ARM_32 relocation for the PLT entry.  */
11447               htab->srelplt2->size += RELOC_SIZE (htab) * 2;
11448             }
11449         }
11450       else
11451         {
11452           h->plt.offset = (bfd_vma) -1;
11453           h->needs_plt = 0;
11454         }
11455     }
11456   else
11457     {
11458       h->plt.offset = (bfd_vma) -1;
11459       h->needs_plt = 0;
11460     }
11461
11462   if (h->got.refcount > 0)
11463     {
11464       asection *s;
11465       bfd_boolean dyn;
11466       int tls_type = elf32_arm_hash_entry (h)->tls_type;
11467       int indx;
11468
11469       /* Make sure this symbol is output as a dynamic symbol.
11470          Undefined weak syms won't yet be marked as dynamic.  */
11471       if (h->dynindx == -1
11472           && !h->forced_local)
11473         {
11474           if (! bfd_elf_link_record_dynamic_symbol (info, h))
11475             return FALSE;
11476         }
11477
11478       if (!htab->symbian_p)
11479         {
11480           s = htab->sgot;
11481           h->got.offset = s->size;
11482
11483           if (tls_type == GOT_UNKNOWN)
11484             abort ();
11485
11486           if (tls_type == GOT_NORMAL)
11487             /* Non-TLS symbols need one GOT slot.  */
11488             s->size += 4;
11489           else
11490             {
11491               if (tls_type & GOT_TLS_GD)
11492                 /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  */
11493                 s->size += 8;
11494               if (tls_type & GOT_TLS_IE)
11495                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
11496                 s->size += 4;
11497             }
11498
11499           dyn = htab->root.dynamic_sections_created;
11500
11501           indx = 0;
11502           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
11503               && (!info->shared
11504                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
11505             indx = h->dynindx;
11506
11507           if (tls_type != GOT_NORMAL
11508               && (info->shared || indx != 0)
11509               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11510                   || h->root.type != bfd_link_hash_undefweak))
11511             {
11512               if (tls_type & GOT_TLS_IE)
11513                 htab->srelgot->size += RELOC_SIZE (htab);
11514
11515               if (tls_type & GOT_TLS_GD)
11516                 htab->srelgot->size += RELOC_SIZE (htab);
11517
11518               if ((tls_type & GOT_TLS_GD) && indx != 0)
11519                 htab->srelgot->size += RELOC_SIZE (htab);
11520             }
11521           else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11522                     || h->root.type != bfd_link_hash_undefweak)
11523                    && (info->shared
11524                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
11525             htab->srelgot->size += RELOC_SIZE (htab);
11526         }
11527     }
11528   else
11529     h->got.offset = (bfd_vma) -1;
11530
11531   /* Allocate stubs for exported Thumb functions on v4t.  */
11532   if (!htab->use_blx && h->dynindx != -1
11533       && h->def_regular
11534       && ELF_ST_TYPE (h->type) == STT_ARM_TFUNC
11535       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
11536     {
11537       struct elf_link_hash_entry * th;
11538       struct bfd_link_hash_entry * bh;
11539       struct elf_link_hash_entry * myh;
11540       char name[1024];
11541       asection *s;
11542       bh = NULL;
11543       /* Create a new symbol to regist the real location of the function.  */
11544       s = h->root.u.def.section;
11545       sprintf (name, "__real_%s", h->root.root.string);
11546       _bfd_generic_link_add_one_symbol (info, s->owner,
11547                                         name, BSF_GLOBAL, s,
11548                                         h->root.u.def.value,
11549                                         NULL, TRUE, FALSE, &bh);
11550
11551       myh = (struct elf_link_hash_entry *) bh;
11552       myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
11553       myh->forced_local = 1;
11554       eh->export_glue = myh;
11555       th = record_arm_to_thumb_glue (info, h);
11556       /* Point the symbol at the stub.  */
11557       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
11558       h->root.u.def.section = th->root.u.def.section;
11559       h->root.u.def.value = th->root.u.def.value & ~1;
11560     }
11561
11562   if (eh->relocs_copied == NULL)
11563     return TRUE;
11564
11565   /* In the shared -Bsymbolic case, discard space allocated for
11566      dynamic pc-relative relocs against symbols which turn out to be
11567      defined in regular objects.  For the normal shared case, discard
11568      space for pc-relative relocs that have become local due to symbol
11569      visibility changes.  */
11570
11571   if (info->shared || htab->root.is_relocatable_executable)
11572     {
11573       /* The only relocs that use pc_count are R_ARM_REL32 and
11574          R_ARM_REL32_NOI, which will appear on something like
11575          ".long foo - .".  We want calls to protected symbols to resolve
11576          directly to the function rather than going via the plt.  If people
11577          want function pointer comparisons to work as expected then they
11578          should avoid writing assembly like ".long foo - .".  */
11579       if (SYMBOL_CALLS_LOCAL (info, h))
11580         {
11581           struct elf32_arm_relocs_copied **pp;
11582
11583           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
11584             {
11585               p->count -= p->pc_count;
11586               p->pc_count = 0;
11587               if (p->count == 0)
11588                 *pp = p->next;
11589               else
11590                 pp = &p->next;
11591             }
11592         }
11593
11594       if (elf32_arm_hash_table (info)->vxworks_p)
11595         {
11596           struct elf32_arm_relocs_copied **pp;
11597
11598           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
11599             {
11600               if (strcmp (p->section->output_section->name, ".tls_vars") == 0)
11601                 *pp = p->next;
11602               else
11603                 pp = &p->next;
11604             }
11605         }
11606
11607       /* Also discard relocs on undefined weak syms with non-default
11608          visibility.  */
11609       if (eh->relocs_copied != NULL
11610           && h->root.type == bfd_link_hash_undefweak)
11611         {
11612           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
11613             eh->relocs_copied = NULL;
11614
11615           /* Make sure undefined weak symbols are output as a dynamic
11616              symbol in PIEs.  */
11617           else if (h->dynindx == -1
11618                    && !h->forced_local)
11619             {
11620               if (! bfd_elf_link_record_dynamic_symbol (info, h))
11621                 return FALSE;
11622             }
11623         }
11624
11625       else if (htab->root.is_relocatable_executable && h->dynindx == -1
11626                && h->root.type == bfd_link_hash_new)
11627         {
11628           /* Output absolute symbols so that we can create relocations
11629              against them.  For normal symbols we output a relocation
11630              against the section that contains them.  */
11631           if (! bfd_elf_link_record_dynamic_symbol (info, h))
11632             return FALSE;
11633         }
11634
11635     }
11636   else
11637     {
11638       /* For the non-shared case, discard space for relocs against
11639          symbols which turn out to need copy relocs or are not
11640          dynamic.  */
11641
11642       if (!h->non_got_ref
11643           && ((h->def_dynamic
11644                && !h->def_regular)
11645               || (htab->root.dynamic_sections_created
11646                   && (h->root.type == bfd_link_hash_undefweak
11647                       || h->root.type == bfd_link_hash_undefined))))
11648         {
11649           /* Make sure this symbol is output as a dynamic symbol.
11650              Undefined weak syms won't yet be marked as dynamic.  */
11651           if (h->dynindx == -1
11652               && !h->forced_local)
11653             {
11654               if (! bfd_elf_link_record_dynamic_symbol (info, h))
11655                 return FALSE;
11656             }
11657
11658           /* If that succeeded, we know we'll be keeping all the
11659              relocs.  */
11660           if (h->dynindx != -1)
11661             goto keep;
11662         }
11663
11664       eh->relocs_copied = NULL;
11665
11666     keep: ;
11667     }
11668
11669   /* Finally, allocate space.  */
11670   for (p = eh->relocs_copied; p != NULL; p = p->next)
11671     {
11672       asection *sreloc = elf_section_data (p->section)->sreloc;
11673       sreloc->size += p->count * RELOC_SIZE (htab);
11674     }
11675
11676   return TRUE;
11677 }
11678
11679 /* Find any dynamic relocs that apply to read-only sections.  */
11680
11681 static bfd_boolean
11682 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
11683 {
11684   struct elf32_arm_link_hash_entry * eh;
11685   struct elf32_arm_relocs_copied * p;
11686
11687   if (h->root.type == bfd_link_hash_warning)
11688     h = (struct elf_link_hash_entry *) h->root.u.i.link;
11689
11690   eh = (struct elf32_arm_link_hash_entry *) h;
11691   for (p = eh->relocs_copied; p != NULL; p = p->next)
11692     {
11693       asection *s = p->section;
11694
11695       if (s != NULL && (s->flags & SEC_READONLY) != 0)
11696         {
11697           struct bfd_link_info *info = (struct bfd_link_info *) inf;
11698
11699           info->flags |= DF_TEXTREL;
11700
11701           /* Not an error, just cut short the traversal.  */
11702           return FALSE;
11703         }
11704     }
11705   return TRUE;
11706 }
11707
11708 void
11709 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
11710                                  int byteswap_code)
11711 {
11712   struct elf32_arm_link_hash_table *globals;
11713
11714   globals = elf32_arm_hash_table (info);
11715   globals->byteswap_code = byteswap_code;
11716 }
11717
11718 /* Set the sizes of the dynamic sections.  */
11719
11720 static bfd_boolean
11721 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
11722                                  struct bfd_link_info * info)
11723 {
11724   bfd * dynobj;
11725   asection * s;
11726   bfd_boolean plt;
11727   bfd_boolean relocs;
11728   bfd *ibfd;
11729   struct elf32_arm_link_hash_table *htab;
11730
11731   htab = elf32_arm_hash_table (info);
11732   dynobj = elf_hash_table (info)->dynobj;
11733   BFD_ASSERT (dynobj != NULL);
11734   check_use_blx (htab);
11735
11736   if (elf_hash_table (info)->dynamic_sections_created)
11737     {
11738       /* Set the contents of the .interp section to the interpreter.  */
11739       if (info->executable)
11740         {
11741           s = bfd_get_section_by_name (dynobj, ".interp");
11742           BFD_ASSERT (s != NULL);
11743           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
11744           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
11745         }
11746     }
11747
11748   /* Set up .got offsets for local syms, and space for local dynamic
11749      relocs.  */
11750   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11751     {
11752       bfd_signed_vma *local_got;
11753       bfd_signed_vma *end_local_got;
11754       char *local_tls_type;
11755       bfd_size_type locsymcount;
11756       Elf_Internal_Shdr *symtab_hdr;
11757       asection *srel;
11758       bfd_boolean is_vxworks = elf32_arm_hash_table (info)->vxworks_p;
11759
11760       if (! is_arm_elf (ibfd))
11761         continue;
11762
11763       for (s = ibfd->sections; s != NULL; s = s->next)
11764         {
11765           struct elf32_arm_relocs_copied *p;
11766
11767           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
11768             {
11769               if (!bfd_is_abs_section (p->section)
11770                   && bfd_is_abs_section (p->section->output_section))
11771                 {
11772                   /* Input section has been discarded, either because
11773                      it is a copy of a linkonce section or due to
11774                      linker script /DISCARD/, so we'll be discarding
11775                      the relocs too.  */
11776                 }
11777               else if (is_vxworks
11778                        && strcmp (p->section->output_section->name,
11779                                   ".tls_vars") == 0)
11780                 {
11781                   /* Relocations in vxworks .tls_vars sections are
11782                      handled specially by the loader.  */
11783                 }
11784               else if (p->count != 0)
11785                 {
11786                   srel = elf_section_data (p->section)->sreloc;
11787                   srel->size += p->count * RELOC_SIZE (htab);
11788                   if ((p->section->output_section->flags & SEC_READONLY) != 0)
11789                     info->flags |= DF_TEXTREL;
11790                 }
11791             }
11792         }
11793
11794       local_got = elf_local_got_refcounts (ibfd);
11795       if (!local_got)
11796         continue;
11797
11798       symtab_hdr = & elf_symtab_hdr (ibfd);
11799       locsymcount = symtab_hdr->sh_info;
11800       end_local_got = local_got + locsymcount;
11801       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
11802       s = htab->sgot;
11803       srel = htab->srelgot;
11804       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
11805         {
11806           if (*local_got > 0)
11807             {
11808               *local_got = s->size;
11809               if (*local_tls_type & GOT_TLS_GD)
11810                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
11811                 s->size += 8;
11812               if (*local_tls_type & GOT_TLS_IE)
11813                 s->size += 4;
11814               if (*local_tls_type == GOT_NORMAL)
11815                 s->size += 4;
11816
11817               if (info->shared || *local_tls_type == GOT_TLS_GD)
11818                 srel->size += RELOC_SIZE (htab);
11819             }
11820           else
11821             *local_got = (bfd_vma) -1;
11822         }
11823     }
11824
11825   if (htab->tls_ldm_got.refcount > 0)
11826     {
11827       /* Allocate two GOT entries and one dynamic relocation (if necessary)
11828          for R_ARM_TLS_LDM32 relocations.  */
11829       htab->tls_ldm_got.offset = htab->sgot->size;
11830       htab->sgot->size += 8;
11831       if (info->shared)
11832         htab->srelgot->size += RELOC_SIZE (htab);
11833     }
11834   else
11835     htab->tls_ldm_got.offset = -1;
11836
11837   /* Allocate global sym .plt and .got entries, and space for global
11838      sym dynamic relocs.  */
11839   elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
11840
11841   /* Here we rummage through the found bfds to collect glue information.  */
11842   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11843     {
11844       if (! is_arm_elf (ibfd))
11845         continue;
11846
11847       /* Initialise mapping tables for code/data.  */
11848       bfd_elf32_arm_init_maps (ibfd);
11849
11850       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
11851           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info))
11852         /* xgettext:c-format */
11853         _bfd_error_handler (_("Errors encountered processing file %s"),
11854                             ibfd->filename);
11855     }
11856
11857   /* Allocate space for the glue sections now that we've sized them.  */
11858   bfd_elf32_arm_allocate_interworking_sections (info);
11859
11860   /* The check_relocs and adjust_dynamic_symbol entry points have
11861      determined the sizes of the various dynamic sections.  Allocate
11862      memory for them.  */
11863   plt = FALSE;
11864   relocs = FALSE;
11865   for (s = dynobj->sections; s != NULL; s = s->next)
11866     {
11867       const char * name;
11868
11869       if ((s->flags & SEC_LINKER_CREATED) == 0)
11870         continue;
11871
11872       /* It's OK to base decisions on the section name, because none
11873          of the dynobj section names depend upon the input files.  */
11874       name = bfd_get_section_name (dynobj, s);
11875
11876       if (strcmp (name, ".plt") == 0)
11877         {
11878           /* Remember whether there is a PLT.  */
11879           plt = s->size != 0;
11880         }
11881       else if (CONST_STRNEQ (name, ".rel"))
11882         {
11883           if (s->size != 0)
11884             {
11885               /* Remember whether there are any reloc sections other
11886                  than .rel(a).plt and .rela.plt.unloaded.  */
11887               if (s != htab->srelplt && s != htab->srelplt2)
11888                 relocs = TRUE;
11889
11890               /* We use the reloc_count field as a counter if we need
11891                  to copy relocs into the output file.  */
11892               s->reloc_count = 0;
11893             }
11894         }
11895       else if (! CONST_STRNEQ (name, ".got")
11896                && strcmp (name, ".dynbss") != 0)
11897         {
11898           /* It's not one of our sections, so don't allocate space.  */
11899           continue;
11900         }
11901
11902       if (s->size == 0)
11903         {
11904           /* If we don't need this section, strip it from the
11905              output file.  This is mostly to handle .rel(a).bss and
11906              .rel(a).plt.  We must create both sections in
11907              create_dynamic_sections, because they must be created
11908              before the linker maps input sections to output
11909              sections.  The linker does that before
11910              adjust_dynamic_symbol is called, and it is that
11911              function which decides whether anything needs to go
11912              into these sections.  */
11913           s->flags |= SEC_EXCLUDE;
11914           continue;
11915         }
11916
11917       if ((s->flags & SEC_HAS_CONTENTS) == 0)
11918         continue;
11919
11920       /* Allocate memory for the section contents.  */
11921       s->contents = bfd_zalloc (dynobj, s->size);
11922       if (s->contents == NULL)
11923         return FALSE;
11924     }
11925
11926   if (elf_hash_table (info)->dynamic_sections_created)
11927     {
11928       /* Add some entries to the .dynamic section.  We fill in the
11929          values later, in elf32_arm_finish_dynamic_sections, but we
11930          must add the entries now so that we get the correct size for
11931          the .dynamic section.  The DT_DEBUG entry is filled in by the
11932          dynamic linker and used by the debugger.  */
11933 #define add_dynamic_entry(TAG, VAL) \
11934   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
11935
11936      if (info->executable)
11937         {
11938           if (!add_dynamic_entry (DT_DEBUG, 0))
11939             return FALSE;
11940         }
11941
11942       if (plt)
11943         {
11944           if (   !add_dynamic_entry (DT_PLTGOT, 0)
11945               || !add_dynamic_entry (DT_PLTRELSZ, 0)
11946               || !add_dynamic_entry (DT_PLTREL,
11947                                      htab->use_rel ? DT_REL : DT_RELA)
11948               || !add_dynamic_entry (DT_JMPREL, 0))
11949             return FALSE;
11950         }
11951
11952       if (relocs)
11953         {
11954           if (htab->use_rel)
11955             {
11956               if (!add_dynamic_entry (DT_REL, 0)
11957                   || !add_dynamic_entry (DT_RELSZ, 0)
11958                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
11959                 return FALSE;
11960             }
11961           else
11962             {
11963               if (!add_dynamic_entry (DT_RELA, 0)
11964                   || !add_dynamic_entry (DT_RELASZ, 0)
11965                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
11966                 return FALSE;
11967             }
11968         }
11969
11970       /* If any dynamic relocs apply to a read-only section,
11971          then we need a DT_TEXTREL entry.  */
11972       if ((info->flags & DF_TEXTREL) == 0)
11973         elf_link_hash_traverse (& htab->root, elf32_arm_readonly_dynrelocs,
11974                                 info);
11975
11976       if ((info->flags & DF_TEXTREL) != 0)
11977         {
11978           if (!add_dynamic_entry (DT_TEXTREL, 0))
11979             return FALSE;
11980         }
11981       if (htab->vxworks_p
11982           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
11983         return FALSE;
11984     }
11985 #undef add_dynamic_entry
11986
11987   return TRUE;
11988 }
11989
11990 /* Finish up dynamic symbol handling.  We set the contents of various
11991    dynamic sections here.  */
11992
11993 static bfd_boolean
11994 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
11995                                  struct bfd_link_info * info,
11996                                  struct elf_link_hash_entry * h,
11997                                  Elf_Internal_Sym * sym)
11998 {
11999   bfd * dynobj;
12000   struct elf32_arm_link_hash_table *htab;
12001   struct elf32_arm_link_hash_entry *eh;
12002
12003   dynobj = elf_hash_table (info)->dynobj;
12004   htab = elf32_arm_hash_table (info);
12005   eh = (struct elf32_arm_link_hash_entry *) h;
12006
12007   if (h->plt.offset != (bfd_vma) -1)
12008     {
12009       asection * splt;
12010       asection * srel;
12011       bfd_byte *loc;
12012       bfd_vma plt_index;
12013       Elf_Internal_Rela rel;
12014
12015       /* This symbol has an entry in the procedure linkage table.  Set
12016          it up.  */
12017
12018       BFD_ASSERT (h->dynindx != -1);
12019
12020       splt = bfd_get_section_by_name (dynobj, ".plt");
12021       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".plt"));
12022       BFD_ASSERT (splt != NULL && srel != NULL);
12023
12024       /* Fill in the entry in the procedure linkage table.  */
12025       if (htab->symbian_p)
12026         {
12027           put_arm_insn (htab, output_bfd,
12028                       elf32_arm_symbian_plt_entry[0],
12029                       splt->contents + h->plt.offset);
12030           bfd_put_32 (output_bfd,
12031                       elf32_arm_symbian_plt_entry[1],
12032                       splt->contents + h->plt.offset + 4);
12033
12034           /* Fill in the entry in the .rel.plt section.  */
12035           rel.r_offset = (splt->output_section->vma
12036                           + splt->output_offset
12037                           + h->plt.offset + 4);
12038           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
12039
12040           /* Get the index in the procedure linkage table which
12041              corresponds to this symbol.  This is the index of this symbol
12042              in all the symbols for which we are making plt entries.  The
12043              first entry in the procedure linkage table is reserved.  */
12044           plt_index = ((h->plt.offset - htab->plt_header_size)
12045                        / htab->plt_entry_size);
12046         }
12047       else
12048         {
12049           bfd_vma got_offset, got_address, plt_address;
12050           bfd_vma got_displacement;
12051           asection * sgot;
12052           bfd_byte * ptr;
12053
12054           sgot = bfd_get_section_by_name (dynobj, ".got.plt");
12055           BFD_ASSERT (sgot != NULL);
12056
12057           /* Get the offset into the .got.plt table of the entry that
12058              corresponds to this function.  */
12059           got_offset = eh->plt_got_offset;
12060
12061           /* Get the index in the procedure linkage table which
12062              corresponds to this symbol.  This is the index of this symbol
12063              in all the symbols for which we are making plt entries.  The
12064              first three entries in .got.plt are reserved; after that
12065              symbols appear in the same order as in .plt.  */
12066           plt_index = (got_offset - 12) / 4;
12067
12068           /* Calculate the address of the GOT entry.  */
12069           got_address = (sgot->output_section->vma
12070                          + sgot->output_offset
12071                          + got_offset);
12072
12073           /* ...and the address of the PLT entry.  */
12074           plt_address = (splt->output_section->vma
12075                          + splt->output_offset
12076                          + h->plt.offset);
12077
12078           ptr = htab->splt->contents + h->plt.offset;
12079           if (htab->vxworks_p && info->shared)
12080             {
12081               unsigned int i;
12082               bfd_vma val;
12083
12084               for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
12085                 {
12086                   val = elf32_arm_vxworks_shared_plt_entry[i];
12087                   if (i == 2)
12088                     val |= got_address - sgot->output_section->vma;
12089                   if (i == 5)
12090                     val |= plt_index * RELOC_SIZE (htab);
12091                   if (i == 2 || i == 5)
12092                     bfd_put_32 (output_bfd, val, ptr);
12093                   else
12094                     put_arm_insn (htab, output_bfd, val, ptr);
12095                 }
12096             }
12097           else if (htab->vxworks_p)
12098             {
12099               unsigned int i;
12100               bfd_vma val;
12101
12102               for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
12103                 {
12104                   val = elf32_arm_vxworks_exec_plt_entry[i];
12105                   if (i == 2)
12106                     val |= got_address;
12107                   if (i == 4)
12108                     val |= 0xffffff & -((h->plt.offset + i * 4 + 8) >> 2);
12109                   if (i == 5)
12110                     val |= plt_index * RELOC_SIZE (htab);
12111                   if (i == 2 || i == 5)
12112                     bfd_put_32 (output_bfd, val, ptr);
12113                   else
12114                     put_arm_insn (htab, output_bfd, val, ptr);
12115                 }
12116
12117               loc = (htab->srelplt2->contents
12118                      + (plt_index * 2 + 1) * RELOC_SIZE (htab));
12119
12120               /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
12121                  referencing the GOT for this PLT entry.  */
12122               rel.r_offset = plt_address + 8;
12123               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
12124               rel.r_addend = got_offset;
12125               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
12126               loc += RELOC_SIZE (htab);
12127
12128               /* Create the R_ARM_ABS32 relocation referencing the
12129                  beginning of the PLT for this GOT entry.  */
12130               rel.r_offset = got_address;
12131               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
12132               rel.r_addend = 0;
12133               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
12134             }
12135           else
12136             {
12137               bfd_signed_vma thumb_refs;
12138               /* Calculate the displacement between the PLT slot and the
12139                  entry in the GOT.  The eight-byte offset accounts for the
12140                  value produced by adding to pc in the first instruction
12141                  of the PLT stub.  */
12142               got_displacement = got_address - (plt_address + 8);
12143
12144               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
12145
12146               thumb_refs = eh->plt_thumb_refcount;
12147               if (!htab->use_blx)
12148                 thumb_refs += eh->plt_maybe_thumb_refcount;
12149
12150               if (thumb_refs > 0)
12151                 {
12152                   put_thumb_insn (htab, output_bfd,
12153                                   elf32_arm_plt_thumb_stub[0], ptr - 4);
12154                   put_thumb_insn (htab, output_bfd,
12155                                   elf32_arm_plt_thumb_stub[1], ptr - 2);
12156                 }
12157
12158               put_arm_insn (htab, output_bfd,
12159                             elf32_arm_plt_entry[0]
12160                             | ((got_displacement & 0x0ff00000) >> 20),
12161                             ptr + 0);
12162               put_arm_insn (htab, output_bfd,
12163                             elf32_arm_plt_entry[1]
12164                             | ((got_displacement & 0x000ff000) >> 12),
12165                             ptr+ 4);
12166               put_arm_insn (htab, output_bfd,
12167                             elf32_arm_plt_entry[2]
12168                             | (got_displacement & 0x00000fff),
12169                             ptr + 8);
12170 #ifdef FOUR_WORD_PLT
12171               bfd_put_32 (output_bfd, elf32_arm_plt_entry[3], ptr + 12);
12172 #endif
12173             }
12174
12175           /* Fill in the entry in the global offset table.  */
12176           bfd_put_32 (output_bfd,
12177                       (splt->output_section->vma
12178                        + splt->output_offset),
12179                       sgot->contents + got_offset);
12180
12181           /* Fill in the entry in the .rel(a).plt section.  */
12182           rel.r_addend = 0;
12183           rel.r_offset = got_address;
12184           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
12185         }
12186
12187       loc = srel->contents + plt_index * RELOC_SIZE (htab);
12188       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
12189
12190       if (!h->def_regular)
12191         {
12192           /* Mark the symbol as undefined, rather than as defined in
12193              the .plt section.  Leave the value alone.  */
12194           sym->st_shndx = SHN_UNDEF;
12195           /* If the symbol is weak, we do need to clear the value.
12196              Otherwise, the PLT entry would provide a definition for
12197              the symbol even if the symbol wasn't defined anywhere,
12198              and so the symbol would never be NULL.  */
12199           if (!h->ref_regular_nonweak)
12200             sym->st_value = 0;
12201         }
12202     }
12203
12204   if (h->got.offset != (bfd_vma) -1
12205       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
12206       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
12207     {
12208       asection * sgot;
12209       asection * srel;
12210       Elf_Internal_Rela rel;
12211       bfd_byte *loc;
12212       bfd_vma offset;
12213
12214       /* This symbol has an entry in the global offset table.  Set it
12215          up.  */
12216       sgot = bfd_get_section_by_name (dynobj, ".got");
12217       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".got"));
12218       BFD_ASSERT (sgot != NULL && srel != NULL);
12219
12220       offset = (h->got.offset & ~(bfd_vma) 1);
12221       rel.r_addend = 0;
12222       rel.r_offset = (sgot->output_section->vma
12223                       + sgot->output_offset
12224                       + offset);
12225
12226       /* If this is a static link, or it is a -Bsymbolic link and the
12227          symbol is defined locally or was forced to be local because
12228          of a version file, we just want to emit a RELATIVE reloc.
12229          The entry in the global offset table will already have been
12230          initialized in the relocate_section function.  */
12231       if (info->shared
12232           && SYMBOL_REFERENCES_LOCAL (info, h))
12233         {
12234           BFD_ASSERT ((h->got.offset & 1) != 0);
12235           rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12236           if (!htab->use_rel)
12237             {
12238               rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + offset);
12239               bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
12240             }
12241         }
12242       else
12243         {
12244           BFD_ASSERT ((h->got.offset & 1) == 0);
12245           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
12246           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
12247         }
12248
12249       loc = srel->contents + srel->reloc_count++ * RELOC_SIZE (htab);
12250       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
12251     }
12252
12253   if (h->needs_copy)
12254     {
12255       asection * s;
12256       Elf_Internal_Rela rel;
12257       bfd_byte *loc;
12258
12259       /* This symbol needs a copy reloc.  Set it up.  */
12260       BFD_ASSERT (h->dynindx != -1
12261                   && (h->root.type == bfd_link_hash_defined
12262                       || h->root.type == bfd_link_hash_defweak));
12263
12264       s = bfd_get_section_by_name (h->root.u.def.section->owner,
12265                                    RELOC_SECTION (htab, ".bss"));
12266       BFD_ASSERT (s != NULL);
12267
12268       rel.r_addend = 0;
12269       rel.r_offset = (h->root.u.def.value
12270                       + h->root.u.def.section->output_section->vma
12271                       + h->root.u.def.section->output_offset);
12272       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
12273       loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
12274       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
12275     }
12276
12277   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
12278      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
12279      to the ".got" section.  */
12280   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
12281       || (!htab->vxworks_p && h == htab->root.hgot))
12282     sym->st_shndx = SHN_ABS;
12283
12284   return TRUE;
12285 }
12286
12287 /* Finish up the dynamic sections.  */
12288
12289 static bfd_boolean
12290 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
12291 {
12292   bfd * dynobj;
12293   asection * sgot;
12294   asection * sdyn;
12295
12296   dynobj = elf_hash_table (info)->dynobj;
12297
12298   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
12299   BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
12300   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
12301
12302   if (elf_hash_table (info)->dynamic_sections_created)
12303     {
12304       asection *splt;
12305       Elf32_External_Dyn *dyncon, *dynconend;
12306       struct elf32_arm_link_hash_table *htab;
12307
12308       htab = elf32_arm_hash_table (info);
12309       splt = bfd_get_section_by_name (dynobj, ".plt");
12310       BFD_ASSERT (splt != NULL && sdyn != NULL);
12311
12312       dyncon = (Elf32_External_Dyn *) sdyn->contents;
12313       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
12314
12315       for (; dyncon < dynconend; dyncon++)
12316         {
12317           Elf_Internal_Dyn dyn;
12318           const char * name;
12319           asection * s;
12320
12321           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
12322
12323           switch (dyn.d_tag)
12324             {
12325               unsigned int type;
12326
12327             default:
12328               if (htab->vxworks_p
12329                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12330                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12331               break;
12332
12333             case DT_HASH:
12334               name = ".hash";
12335               goto get_vma_if_bpabi;
12336             case DT_STRTAB:
12337               name = ".dynstr";
12338               goto get_vma_if_bpabi;
12339             case DT_SYMTAB:
12340               name = ".dynsym";
12341               goto get_vma_if_bpabi;
12342             case DT_VERSYM:
12343               name = ".gnu.version";
12344               goto get_vma_if_bpabi;
12345             case DT_VERDEF:
12346               name = ".gnu.version_d";
12347               goto get_vma_if_bpabi;
12348             case DT_VERNEED:
12349               name = ".gnu.version_r";
12350               goto get_vma_if_bpabi;
12351
12352             case DT_PLTGOT:
12353               name = ".got";
12354               goto get_vma;
12355             case DT_JMPREL:
12356               name = RELOC_SECTION (htab, ".plt");
12357             get_vma:
12358               s = bfd_get_section_by_name (output_bfd, name);
12359               BFD_ASSERT (s != NULL);
12360               if (!htab->symbian_p)
12361                 dyn.d_un.d_ptr = s->vma;
12362               else
12363                 /* In the BPABI, tags in the PT_DYNAMIC section point
12364                    at the file offset, not the memory address, for the
12365                    convenience of the post linker.  */
12366                 dyn.d_un.d_ptr = s->filepos;
12367               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12368               break;
12369
12370             get_vma_if_bpabi:
12371               if (htab->symbian_p)
12372                 goto get_vma;
12373               break;
12374
12375             case DT_PLTRELSZ:
12376               s = bfd_get_section_by_name (output_bfd,
12377                                            RELOC_SECTION (htab, ".plt"));
12378               BFD_ASSERT (s != NULL);
12379               dyn.d_un.d_val = s->size;
12380               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12381               break;
12382
12383             case DT_RELSZ:
12384             case DT_RELASZ:
12385               if (!htab->symbian_p)
12386                 {
12387                   /* My reading of the SVR4 ABI indicates that the
12388                      procedure linkage table relocs (DT_JMPREL) should be
12389                      included in the overall relocs (DT_REL).  This is
12390                      what Solaris does.  However, UnixWare can not handle
12391                      that case.  Therefore, we override the DT_RELSZ entry
12392                      here to make it not include the JMPREL relocs.  Since
12393                      the linker script arranges for .rel(a).plt to follow all
12394                      other relocation sections, we don't have to worry
12395                      about changing the DT_REL entry.  */
12396                   s = bfd_get_section_by_name (output_bfd,
12397                                                RELOC_SECTION (htab, ".plt"));
12398                   if (s != NULL)
12399                     dyn.d_un.d_val -= s->size;
12400                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12401                   break;
12402                 }
12403               /* Fall through.  */
12404
12405             case DT_REL:
12406             case DT_RELA:
12407               /* In the BPABI, the DT_REL tag must point at the file
12408                  offset, not the VMA, of the first relocation
12409                  section.  So, we use code similar to that in
12410                  elflink.c, but do not check for SHF_ALLOC on the
12411                  relcoation section, since relocations sections are
12412                  never allocated under the BPABI.  The comments above
12413                  about Unixware notwithstanding, we include all of the
12414                  relocations here.  */
12415               if (htab->symbian_p)
12416                 {
12417                   unsigned int i;
12418                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12419                           ? SHT_REL : SHT_RELA);
12420                   dyn.d_un.d_val = 0;
12421                   for (i = 1; i < elf_numsections (output_bfd); i++)
12422                     {
12423                       Elf_Internal_Shdr *hdr
12424                         = elf_elfsections (output_bfd)[i];
12425                       if (hdr->sh_type == type)
12426                         {
12427                           if (dyn.d_tag == DT_RELSZ
12428                               || dyn.d_tag == DT_RELASZ)
12429                             dyn.d_un.d_val += hdr->sh_size;
12430                           else if ((ufile_ptr) hdr->sh_offset
12431                                    <= dyn.d_un.d_val - 1)
12432                             dyn.d_un.d_val = hdr->sh_offset;
12433                         }
12434                     }
12435                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12436                 }
12437               break;
12438
12439               /* Set the bottom bit of DT_INIT/FINI if the
12440                  corresponding function is Thumb.  */
12441             case DT_INIT:
12442               name = info->init_function;
12443               goto get_sym;
12444             case DT_FINI:
12445               name = info->fini_function;
12446             get_sym:
12447               /* If it wasn't set by elf_bfd_final_link
12448                  then there is nothing to adjust.  */
12449               if (dyn.d_un.d_val != 0)
12450                 {
12451                   struct elf_link_hash_entry * eh;
12452
12453                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
12454                                              FALSE, FALSE, TRUE);
12455                   if (eh != NULL
12456                       && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
12457                     {
12458                       dyn.d_un.d_val |= 1;
12459                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
12460                     }
12461                 }
12462               break;
12463             }
12464         }
12465
12466       /* Fill in the first entry in the procedure linkage table.  */
12467       if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
12468         {
12469           const bfd_vma *plt0_entry;
12470           bfd_vma got_address, plt_address, got_displacement;
12471
12472           /* Calculate the addresses of the GOT and PLT.  */
12473           got_address = sgot->output_section->vma + sgot->output_offset;
12474           plt_address = splt->output_section->vma + splt->output_offset;
12475
12476           if (htab->vxworks_p)
12477             {
12478               /* The VxWorks GOT is relocated by the dynamic linker.
12479                  Therefore, we must emit relocations rather than simply
12480                  computing the values now.  */
12481               Elf_Internal_Rela rel;
12482
12483               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
12484               put_arm_insn (htab, output_bfd, plt0_entry[0],
12485                             splt->contents + 0);
12486               put_arm_insn (htab, output_bfd, plt0_entry[1],
12487                             splt->contents + 4);
12488               put_arm_insn (htab, output_bfd, plt0_entry[2],
12489                             splt->contents + 8);
12490               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
12491
12492               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
12493               rel.r_offset = plt_address + 12;
12494               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
12495               rel.r_addend = 0;
12496               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
12497                                      htab->srelplt2->contents);
12498             }
12499           else
12500             {
12501               got_displacement = got_address - (plt_address + 16);
12502
12503               plt0_entry = elf32_arm_plt0_entry;
12504               put_arm_insn (htab, output_bfd, plt0_entry[0],
12505                             splt->contents + 0);
12506               put_arm_insn (htab, output_bfd, plt0_entry[1],
12507                             splt->contents + 4);
12508               put_arm_insn (htab, output_bfd, plt0_entry[2],
12509                             splt->contents + 8);
12510               put_arm_insn (htab, output_bfd, plt0_entry[3],
12511                             splt->contents + 12);
12512
12513 #ifdef FOUR_WORD_PLT
12514               /* The displacement value goes in the otherwise-unused
12515                  last word of the second entry.  */
12516               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
12517 #else
12518               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
12519 #endif
12520             }
12521         }
12522
12523       /* UnixWare sets the entsize of .plt to 4, although that doesn't
12524          really seem like the right value.  */
12525       if (splt->output_section->owner == output_bfd)
12526         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
12527
12528       if (htab->vxworks_p && !info->shared && htab->splt->size > 0)
12529         {
12530           /* Correct the .rel(a).plt.unloaded relocations.  They will have
12531              incorrect symbol indexes.  */
12532           int num_plts;
12533           unsigned char *p;
12534
12535           num_plts = ((htab->splt->size - htab->plt_header_size)
12536                       / htab->plt_entry_size);
12537           p = htab->srelplt2->contents + RELOC_SIZE (htab);
12538
12539           for (; num_plts; num_plts--)
12540             {
12541               Elf_Internal_Rela rel;
12542
12543               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
12544               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
12545               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
12546               p += RELOC_SIZE (htab);
12547
12548               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
12549               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
12550               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
12551               p += RELOC_SIZE (htab);
12552             }
12553         }
12554     }
12555
12556   /* Fill in the first three entries in the global offset table.  */
12557   if (sgot)
12558     {
12559       if (sgot->size > 0)
12560         {
12561           if (sdyn == NULL)
12562             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
12563           else
12564             bfd_put_32 (output_bfd,
12565                         sdyn->output_section->vma + sdyn->output_offset,
12566                         sgot->contents);
12567           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
12568           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
12569         }
12570
12571       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
12572     }
12573
12574   return TRUE;
12575 }
12576
12577 static void
12578 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
12579 {
12580   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
12581   struct elf32_arm_link_hash_table *globals;
12582
12583   i_ehdrp = elf_elfheader (abfd);
12584
12585   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
12586     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
12587   else
12588     i_ehdrp->e_ident[EI_OSABI] = 0;
12589   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
12590
12591   if (link_info)
12592     {
12593       globals = elf32_arm_hash_table (link_info);
12594       if (globals->byteswap_code)
12595         i_ehdrp->e_flags |= EF_ARM_BE8;
12596     }
12597 }
12598
12599 static enum elf_reloc_type_class
12600 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
12601 {
12602   switch ((int) ELF32_R_TYPE (rela->r_info))
12603     {
12604     case R_ARM_RELATIVE:
12605       return reloc_class_relative;
12606     case R_ARM_JUMP_SLOT:
12607       return reloc_class_plt;
12608     case R_ARM_COPY:
12609       return reloc_class_copy;
12610     default:
12611       return reloc_class_normal;
12612     }
12613 }
12614
12615 /* Set the right machine number for an Arm ELF file.  */
12616
12617 static bfd_boolean
12618 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
12619 {
12620   if (hdr->sh_type == SHT_NOTE)
12621     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
12622
12623   return TRUE;
12624 }
12625
12626 static void
12627 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
12628 {
12629   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
12630 }
12631
12632 /* Return TRUE if this is an unwinding table entry.  */
12633
12634 static bfd_boolean
12635 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
12636 {
12637   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
12638           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
12639 }
12640
12641
12642 /* Set the type and flags for an ARM section.  We do this by
12643    the section name, which is a hack, but ought to work.  */
12644
12645 static bfd_boolean
12646 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
12647 {
12648   const char * name;
12649
12650   name = bfd_get_section_name (abfd, sec);
12651
12652   if (is_arm_elf_unwind_section_name (abfd, name))
12653     {
12654       hdr->sh_type = SHT_ARM_EXIDX;
12655       hdr->sh_flags |= SHF_LINK_ORDER;
12656     }
12657   return TRUE;
12658 }
12659
12660 /* Handle an ARM specific section when reading an object file.  This is
12661    called when bfd_section_from_shdr finds a section with an unknown
12662    type.  */
12663
12664 static bfd_boolean
12665 elf32_arm_section_from_shdr (bfd *abfd,
12666                              Elf_Internal_Shdr * hdr,
12667                              const char *name,
12668                              int shindex)
12669 {
12670   /* There ought to be a place to keep ELF backend specific flags, but
12671      at the moment there isn't one.  We just keep track of the
12672      sections by their name, instead.  Fortunately, the ABI gives
12673      names for all the ARM specific sections, so we will probably get
12674      away with this.  */
12675   switch (hdr->sh_type)
12676     {
12677     case SHT_ARM_EXIDX:
12678     case SHT_ARM_PREEMPTMAP:
12679     case SHT_ARM_ATTRIBUTES:
12680       break;
12681
12682     default:
12683       return FALSE;
12684     }
12685
12686   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
12687     return FALSE;
12688
12689   return TRUE;
12690 }
12691
12692 /* A structure used to record a list of sections, independently
12693    of the next and prev fields in the asection structure.  */
12694 typedef struct section_list
12695 {
12696   asection * sec;
12697   struct section_list * next;
12698   struct section_list * prev;
12699 }
12700 section_list;
12701
12702 /* Unfortunately we need to keep a list of sections for which
12703    an _arm_elf_section_data structure has been allocated.  This
12704    is because it is possible for functions like elf32_arm_write_section
12705    to be called on a section which has had an elf_data_structure
12706    allocated for it (and so the used_by_bfd field is valid) but
12707    for which the ARM extended version of this structure - the
12708    _arm_elf_section_data structure - has not been allocated.  */
12709 static section_list * sections_with_arm_elf_section_data = NULL;
12710
12711 static void
12712 record_section_with_arm_elf_section_data (asection * sec)
12713 {
12714   struct section_list * entry;
12715
12716   entry = bfd_malloc (sizeof (* entry));
12717   if (entry == NULL)
12718     return;
12719   entry->sec = sec;
12720   entry->next = sections_with_arm_elf_section_data;
12721   entry->prev = NULL;
12722   if (entry->next != NULL)
12723     entry->next->prev = entry;
12724   sections_with_arm_elf_section_data = entry;
12725 }
12726
12727 static struct section_list *
12728 find_arm_elf_section_entry (asection * sec)
12729 {
12730   struct section_list * entry;
12731   static struct section_list * last_entry = NULL;
12732
12733   /* This is a short cut for the typical case where the sections are added
12734      to the sections_with_arm_elf_section_data list in forward order and
12735      then looked up here in backwards order.  This makes a real difference
12736      to the ld-srec/sec64k.exp linker test.  */
12737   entry = sections_with_arm_elf_section_data;
12738   if (last_entry != NULL)
12739     {
12740       if (last_entry->sec == sec)
12741         entry = last_entry;
12742       else if (last_entry->next != NULL
12743                && last_entry->next->sec == sec)
12744         entry = last_entry->next;
12745     }
12746
12747   for (; entry; entry = entry->next)
12748     if (entry->sec == sec)
12749       break;
12750
12751   if (entry)
12752     /* Record the entry prior to this one - it is the entry we are most
12753        likely to want to locate next time.  Also this way if we have been
12754        called from unrecord_section_with_arm_elf_section_data() we will not
12755        be caching a pointer that is about to be freed.  */
12756     last_entry = entry->prev;
12757
12758   return entry;
12759 }
12760
12761 static _arm_elf_section_data *
12762 get_arm_elf_section_data (asection * sec)
12763 {
12764   struct section_list * entry;
12765
12766   entry = find_arm_elf_section_entry (sec);
12767
12768   if (entry)
12769     return elf32_arm_section_data (entry->sec);
12770   else
12771     return NULL;
12772 }
12773
12774 static void
12775 unrecord_section_with_arm_elf_section_data (asection * sec)
12776 {
12777   struct section_list * entry;
12778
12779   entry = find_arm_elf_section_entry (sec);
12780
12781   if (entry)
12782     {
12783       if (entry->prev != NULL)
12784         entry->prev->next = entry->next;
12785       if (entry->next != NULL)
12786         entry->next->prev = entry->prev;
12787       if (entry == sections_with_arm_elf_section_data)
12788         sections_with_arm_elf_section_data = entry->next;
12789       free (entry);
12790     }
12791 }
12792
12793
12794 typedef struct
12795 {
12796   void *finfo;
12797   struct bfd_link_info *info;
12798   asection *sec;
12799   int sec_shndx;
12800   int (*func) (void *, const char *, Elf_Internal_Sym *,
12801                asection *, struct elf_link_hash_entry *);
12802 } output_arch_syminfo;
12803
12804 enum map_symbol_type
12805 {
12806   ARM_MAP_ARM,
12807   ARM_MAP_THUMB,
12808   ARM_MAP_DATA
12809 };
12810
12811
12812 /* Output a single mapping symbol.  */
12813
12814 static bfd_boolean
12815 elf32_arm_output_map_sym (output_arch_syminfo *osi,
12816                           enum map_symbol_type type,
12817                           bfd_vma offset)
12818 {
12819   static const char *names[3] = {"$a", "$t", "$d"};
12820   struct elf32_arm_link_hash_table *htab;
12821   Elf_Internal_Sym sym;
12822
12823   htab = elf32_arm_hash_table (osi->info);
12824   sym.st_value = osi->sec->output_section->vma
12825                  + osi->sec->output_offset
12826                  + offset;
12827   sym.st_size = 0;
12828   sym.st_other = 0;
12829   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
12830   sym.st_shndx = osi->sec_shndx;
12831   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
12832 }
12833
12834
12835 /* Output mapping symbols for PLT entries associated with H.  */
12836
12837 static bfd_boolean
12838 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
12839 {
12840   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
12841   struct elf32_arm_link_hash_table *htab;
12842   struct elf32_arm_link_hash_entry *eh;
12843   bfd_vma addr;
12844
12845   htab = elf32_arm_hash_table (osi->info);
12846
12847   if (h->root.type == bfd_link_hash_indirect)
12848     return TRUE;
12849
12850   if (h->root.type == bfd_link_hash_warning)
12851     /* When warning symbols are created, they **replace** the "real"
12852        entry in the hash table, thus we never get to see the real
12853        symbol in a hash traversal.  So look at it now.  */
12854     h = (struct elf_link_hash_entry *) h->root.u.i.link;
12855
12856   if (h->plt.offset == (bfd_vma) -1)
12857     return TRUE;
12858
12859   eh = (struct elf32_arm_link_hash_entry *) h;
12860   addr = h->plt.offset;
12861   if (htab->symbian_p)
12862     {
12863       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
12864         return FALSE;
12865       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
12866         return FALSE;
12867     }
12868   else if (htab->vxworks_p)
12869     {
12870       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
12871         return FALSE;
12872       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
12873         return FALSE;
12874       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
12875         return FALSE;
12876       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
12877         return FALSE;
12878     }
12879   else
12880     {
12881       bfd_signed_vma thumb_refs;
12882
12883       thumb_refs = eh->plt_thumb_refcount;
12884       if (!htab->use_blx)
12885         thumb_refs += eh->plt_maybe_thumb_refcount;
12886
12887       if (thumb_refs > 0)
12888         {
12889           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
12890             return FALSE;
12891         }
12892 #ifdef FOUR_WORD_PLT
12893       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
12894         return FALSE;
12895       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
12896         return FALSE;
12897 #else
12898       /* A three-word PLT with no Thumb thunk contains only Arm code,
12899          so only need to output a mapping symbol for the first PLT entry and
12900          entries with thumb thunks.  */
12901       if (thumb_refs > 0 || addr == 20)
12902         {
12903           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
12904             return FALSE;
12905         }
12906 #endif
12907     }
12908
12909   return TRUE;
12910 }
12911
12912 /* Output a single local symbol for a generated stub.  */
12913
12914 static bfd_boolean
12915 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
12916                            bfd_vma offset, bfd_vma size)
12917 {
12918   struct elf32_arm_link_hash_table *htab;
12919   Elf_Internal_Sym sym;
12920
12921   htab = elf32_arm_hash_table (osi->info);
12922   sym.st_value = osi->sec->output_section->vma
12923                  + osi->sec->output_offset
12924                  + offset;
12925   sym.st_size = size;
12926   sym.st_other = 0;
12927   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
12928   sym.st_shndx = osi->sec_shndx;
12929   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
12930 }
12931
12932 static bfd_boolean
12933 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
12934                   void * in_arg)
12935 {
12936   struct elf32_arm_stub_hash_entry *stub_entry;
12937   struct bfd_link_info *info;
12938   struct elf32_arm_link_hash_table *htab;
12939   asection *stub_sec;
12940   bfd_vma addr;
12941   char *stub_name;
12942   output_arch_syminfo *osi;
12943   const insn_sequence *template;
12944   enum stub_insn_type prev_type;
12945   int size;
12946   int i;
12947   enum map_symbol_type sym_type;
12948
12949   /* Massage our args to the form they really have.  */
12950   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
12951   osi = (output_arch_syminfo *) in_arg;
12952
12953   info = osi->info;
12954
12955   htab = elf32_arm_hash_table (info);
12956   stub_sec = stub_entry->stub_sec;
12957
12958   /* Ensure this stub is attached to the current section being
12959      processed.  */
12960   if (stub_sec != osi->sec)
12961     return TRUE;
12962
12963   addr = (bfd_vma) stub_entry->stub_offset;
12964   stub_name = stub_entry->output_name;
12965
12966   template = stub_entry->stub_template;
12967   switch (template[0].type)
12968     {
12969     case ARM_TYPE:
12970       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, stub_entry->stub_size))
12971         return FALSE;
12972       break;
12973     case THUMB16_TYPE:
12974     case THUMB32_TYPE:
12975       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
12976                                       stub_entry->stub_size))
12977         return FALSE;
12978       break;
12979     default:
12980       BFD_FAIL ();
12981       return 0;
12982     }
12983
12984   prev_type = DATA_TYPE;
12985   size = 0;
12986   for (i = 0; i < stub_entry->stub_template_size; i++)
12987     {
12988       switch (template[i].type)
12989         {
12990         case ARM_TYPE:
12991           sym_type = ARM_MAP_ARM;
12992           break;
12993
12994         case THUMB16_TYPE:
12995         case THUMB32_TYPE:
12996           sym_type = ARM_MAP_THUMB;
12997           break;
12998
12999         case DATA_TYPE:
13000           sym_type = ARM_MAP_DATA;
13001           break;
13002
13003         default:
13004           BFD_FAIL ();
13005           return FALSE;
13006         }
13007
13008       if (template[i].type != prev_type)
13009         {
13010           prev_type = template[i].type;
13011           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
13012             return FALSE;
13013         }
13014
13015       switch (template[i].type)
13016         {
13017         case ARM_TYPE:
13018         case THUMB32_TYPE:
13019           size += 4;
13020           break;
13021
13022         case THUMB16_TYPE:
13023           size += 2;
13024           break;
13025
13026         case DATA_TYPE:
13027           size += 4;
13028           break;
13029
13030         default:
13031           BFD_FAIL ();
13032           return FALSE;
13033         }
13034     }
13035
13036   return TRUE;
13037 }
13038
13039 /* Output mapping symbols for linker generated sections.  */
13040
13041 static bfd_boolean
13042 elf32_arm_output_arch_local_syms (bfd *output_bfd,
13043                                   struct bfd_link_info *info,
13044                                   void *finfo,
13045                                   int (*func) (void *, const char *,
13046                                                Elf_Internal_Sym *,
13047                                                asection *,
13048                                                struct elf_link_hash_entry *))
13049 {
13050   output_arch_syminfo osi;
13051   struct elf32_arm_link_hash_table *htab;
13052   bfd_vma offset;
13053   bfd_size_type size;
13054
13055   htab = elf32_arm_hash_table (info);
13056   check_use_blx (htab);
13057
13058   osi.finfo = finfo;
13059   osi.info = info;
13060   osi.func = func;
13061
13062   /* ARM->Thumb glue.  */
13063   if (htab->arm_glue_size > 0)
13064     {
13065       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
13066                                          ARM2THUMB_GLUE_SECTION_NAME);
13067
13068       osi.sec_shndx = _bfd_elf_section_from_bfd_section
13069           (output_bfd, osi.sec->output_section);
13070       if (info->shared || htab->root.is_relocatable_executable
13071           || htab->pic_veneer)
13072         size = ARM2THUMB_PIC_GLUE_SIZE;
13073       else if (htab->use_blx)
13074         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
13075       else
13076         size = ARM2THUMB_STATIC_GLUE_SIZE;
13077
13078       for (offset = 0; offset < htab->arm_glue_size; offset += size)
13079         {
13080           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
13081           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
13082         }
13083     }
13084
13085   /* Thumb->ARM glue.  */
13086   if (htab->thumb_glue_size > 0)
13087     {
13088       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
13089                                          THUMB2ARM_GLUE_SECTION_NAME);
13090
13091       osi.sec_shndx = _bfd_elf_section_from_bfd_section
13092           (output_bfd, osi.sec->output_section);
13093       size = THUMB2ARM_GLUE_SIZE;
13094
13095       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
13096         {
13097           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
13098           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
13099         }
13100     }
13101
13102   /* ARMv4 BX veneers.  */
13103   if (htab->bx_glue_size > 0)
13104     {
13105       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
13106                                          ARM_BX_GLUE_SECTION_NAME);
13107
13108       osi.sec_shndx = _bfd_elf_section_from_bfd_section
13109           (output_bfd, osi.sec->output_section);
13110
13111       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
13112     }
13113
13114   /* Long calls stubs.  */
13115   if (htab->stub_bfd && htab->stub_bfd->sections)
13116     {
13117       asection* stub_sec;
13118
13119       for (stub_sec = htab->stub_bfd->sections;
13120            stub_sec != NULL;
13121            stub_sec = stub_sec->next)
13122         {
13123           /* Ignore non-stub sections.  */
13124           if (!strstr (stub_sec->name, STUB_SUFFIX))
13125             continue;
13126
13127           osi.sec = stub_sec;
13128
13129           osi.sec_shndx = _bfd_elf_section_from_bfd_section
13130             (output_bfd, osi.sec->output_section);
13131
13132           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
13133         }
13134     }
13135
13136   /* Finally, output mapping symbols for the PLT.  */
13137   if (!htab->splt || htab->splt->size == 0)
13138     return TRUE;
13139
13140   osi.sec_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
13141                                                      htab->splt->output_section);
13142   osi.sec = htab->splt;
13143   /* Output mapping symbols for the plt header.  SymbianOS does not have a
13144      plt header.  */
13145   if (htab->vxworks_p)
13146     {
13147       /* VxWorks shared libraries have no PLT header.  */
13148       if (!info->shared)
13149         {
13150           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
13151             return FALSE;
13152           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
13153             return FALSE;
13154         }
13155     }
13156   else if (!htab->symbian_p)
13157     {
13158       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
13159         return FALSE;
13160 #ifndef FOUR_WORD_PLT
13161       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
13162         return FALSE;
13163 #endif
13164     }
13165
13166   elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, (void *) &osi);
13167   return TRUE;
13168 }
13169
13170 /* Allocate target specific section data.  */
13171
13172 static bfd_boolean
13173 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
13174 {
13175   if (!sec->used_by_bfd)
13176     {
13177       _arm_elf_section_data *sdata;
13178       bfd_size_type amt = sizeof (*sdata);
13179
13180       sdata = bfd_zalloc (abfd, amt);
13181       if (sdata == NULL)
13182         return FALSE;
13183       sec->used_by_bfd = sdata;
13184     }
13185
13186   record_section_with_arm_elf_section_data (sec);
13187
13188   return _bfd_elf_new_section_hook (abfd, sec);
13189 }
13190
13191
13192 /* Used to order a list of mapping symbols by address.  */
13193
13194 static int
13195 elf32_arm_compare_mapping (const void * a, const void * b)
13196 {
13197   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
13198   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
13199
13200   if (amap->vma > bmap->vma)
13201     return 1;
13202   else if (amap->vma < bmap->vma)
13203     return -1;
13204   else if (amap->type > bmap->type)
13205     /* Ensure results do not depend on the host qsort for objects with
13206        multiple mapping symbols at the same address by sorting on type
13207        after vma.  */
13208     return 1;
13209   else if (amap->type < bmap->type)
13210     return -1;
13211   else
13212     return 0;
13213 }
13214
13215 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
13216
13217 static unsigned long
13218 offset_prel31 (unsigned long addr, bfd_vma offset)
13219 {
13220   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
13221 }
13222
13223 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
13224    relocations.  */
13225
13226 static void
13227 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
13228 {
13229   unsigned long first_word = bfd_get_32 (output_bfd, from);
13230   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
13231   
13232   /* High bit of first word is supposed to be zero.  */
13233   if ((first_word & 0x80000000ul) == 0)
13234     first_word = offset_prel31 (first_word, offset);
13235   
13236   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
13237      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
13238   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
13239     second_word = offset_prel31 (second_word, offset);
13240   
13241   bfd_put_32 (output_bfd, first_word, to);
13242   bfd_put_32 (output_bfd, second_word, to + 4);
13243 }
13244
13245 /* Data for make_branch_to_a8_stub().  */
13246
13247 struct a8_branch_to_stub_data {
13248   asection *writing_section;
13249   bfd_byte *contents;
13250 };
13251
13252
13253 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
13254    places for a particular section.  */
13255
13256 static bfd_boolean
13257 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
13258                        void *in_arg)
13259 {
13260   struct elf32_arm_stub_hash_entry *stub_entry;
13261   struct a8_branch_to_stub_data *data;
13262   bfd_byte *contents;
13263   unsigned long branch_insn;
13264   bfd_vma veneered_insn_loc, veneer_entry_loc;
13265   bfd_signed_vma branch_offset;
13266   bfd *abfd;
13267   unsigned int index;
13268
13269   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
13270   data = (struct a8_branch_to_stub_data *) in_arg;
13271
13272   if (stub_entry->target_section != data->writing_section
13273       || stub_entry->stub_type < arm_stub_a8_veneer_b_cond)
13274     return TRUE;
13275
13276   contents = data->contents;
13277
13278   veneered_insn_loc = stub_entry->target_section->output_section->vma
13279                       + stub_entry->target_section->output_offset
13280                       + stub_entry->target_value;
13281
13282   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
13283                      + stub_entry->stub_sec->output_offset
13284                      + stub_entry->stub_offset;
13285
13286   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
13287     veneered_insn_loc &= ~3u;
13288
13289   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
13290
13291   abfd = stub_entry->target_section->owner;
13292   index = stub_entry->target_value;
13293
13294   /* We attempt to avoid this condition by setting stubs_always_after_branch
13295      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
13296      This check is just to be on the safe side...  */
13297   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
13298     {
13299       (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub is "
13300                                "allocated in unsafe location"), abfd);
13301       return FALSE;
13302     }
13303
13304   switch (stub_entry->stub_type)
13305     {
13306     case arm_stub_a8_veneer_b:
13307     case arm_stub_a8_veneer_b_cond:
13308       branch_insn = 0xf0009000;
13309       goto jump24;
13310
13311     case arm_stub_a8_veneer_blx:
13312       branch_insn = 0xf000e800;
13313       goto jump24;
13314
13315     case arm_stub_a8_veneer_bl:
13316       {
13317         unsigned int i1, j1, i2, j2, s;
13318
13319         branch_insn = 0xf000d000;
13320
13321       jump24:
13322         if (branch_offset < -16777216 || branch_offset > 16777214)
13323           {
13324             /* There's not much we can do apart from complain if this
13325                happens.  */
13326             (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub out "
13327                                      "of range (input file too large)"), abfd);
13328             return FALSE;
13329           }
13330
13331         /* i1 = not(j1 eor s), so:
13332            not i1 = j1 eor s
13333            j1 = (not i1) eor s.  */
13334
13335         branch_insn |= (branch_offset >> 1) & 0x7ff;
13336         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
13337         i2 = (branch_offset >> 22) & 1;
13338         i1 = (branch_offset >> 23) & 1;
13339         s = (branch_offset >> 24) & 1;
13340         j1 = (!i1) ^ s;
13341         j2 = (!i2) ^ s;
13342         branch_insn |= j2 << 11;
13343         branch_insn |= j1 << 13;
13344         branch_insn |= s << 26;
13345       }
13346       break;
13347
13348     default:
13349       BFD_FAIL ();
13350       return FALSE;
13351     }
13352
13353   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[index]);
13354   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[index + 2]);
13355
13356   return TRUE;
13357 }
13358
13359 /* Do code byteswapping.  Return FALSE afterwards so that the section is
13360    written out as normal.  */
13361
13362 static bfd_boolean
13363 elf32_arm_write_section (bfd *output_bfd,
13364                          struct bfd_link_info *link_info,
13365                          asection *sec,
13366                          bfd_byte *contents)
13367 {
13368   unsigned int mapcount, errcount;
13369   _arm_elf_section_data *arm_data;
13370   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
13371   elf32_arm_section_map *map;
13372   elf32_vfp11_erratum_list *errnode;
13373   bfd_vma ptr;
13374   bfd_vma end;
13375   bfd_vma offset = sec->output_section->vma + sec->output_offset;
13376   bfd_byte tmp;
13377   unsigned int i;
13378
13379   /* If this section has not been allocated an _arm_elf_section_data
13380      structure then we cannot record anything.  */
13381   arm_data = get_arm_elf_section_data (sec);
13382   if (arm_data == NULL)
13383     return FALSE;
13384
13385   mapcount = arm_data->mapcount;
13386   map = arm_data->map;
13387   errcount = arm_data->erratumcount;
13388
13389   if (errcount != 0)
13390     {
13391       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
13392
13393       for (errnode = arm_data->erratumlist; errnode != 0;
13394            errnode = errnode->next)
13395         {
13396           bfd_vma index = errnode->vma - offset;
13397
13398           switch (errnode->type)
13399             {
13400             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
13401               {
13402                 bfd_vma branch_to_veneer;
13403                 /* Original condition code of instruction, plus bit mask for
13404                    ARM B instruction.  */
13405                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
13406                                   | 0x0a000000;
13407
13408                 /* The instruction is before the label.  */
13409                 index -= 4;
13410
13411                 /* Above offset included in -4 below.  */
13412                 branch_to_veneer = errnode->u.b.veneer->vma
13413                                    - errnode->vma - 4;
13414
13415                 if ((signed) branch_to_veneer < -(1 << 25)
13416                     || (signed) branch_to_veneer >= (1 << 25))
13417                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
13418                                            "range"), output_bfd);
13419
13420                 insn |= (branch_to_veneer >> 2) & 0xffffff;
13421                 contents[endianflip ^ index] = insn & 0xff;
13422                 contents[endianflip ^ (index + 1)] = (insn >> 8) & 0xff;
13423                 contents[endianflip ^ (index + 2)] = (insn >> 16) & 0xff;
13424                 contents[endianflip ^ (index + 3)] = (insn >> 24) & 0xff;
13425               }
13426               break;
13427
13428             case VFP11_ERRATUM_ARM_VENEER:
13429               {
13430                 bfd_vma branch_from_veneer;
13431                 unsigned int insn;
13432
13433                 /* Take size of veneer into account.  */
13434                 branch_from_veneer = errnode->u.v.branch->vma
13435                                      - errnode->vma - 12;
13436
13437                 if ((signed) branch_from_veneer < -(1 << 25)
13438                     || (signed) branch_from_veneer >= (1 << 25))
13439                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
13440                                            "range"), output_bfd);
13441
13442                 /* Original instruction.  */
13443                 insn = errnode->u.v.branch->u.b.vfp_insn;
13444                 contents[endianflip ^ index] = insn & 0xff;
13445                 contents[endianflip ^ (index + 1)] = (insn >> 8) & 0xff;
13446                 contents[endianflip ^ (index + 2)] = (insn >> 16) & 0xff;
13447                 contents[endianflip ^ (index + 3)] = (insn >> 24) & 0xff;
13448
13449                 /* Branch back to insn after original insn.  */
13450                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
13451                 contents[endianflip ^ (index + 4)] = insn & 0xff;
13452                 contents[endianflip ^ (index + 5)] = (insn >> 8) & 0xff;
13453                 contents[endianflip ^ (index + 6)] = (insn >> 16) & 0xff;
13454                 contents[endianflip ^ (index + 7)] = (insn >> 24) & 0xff;
13455               }
13456               break;
13457
13458             default:
13459               abort ();
13460             }
13461         }
13462     }
13463
13464   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
13465     {
13466       arm_unwind_table_edit *edit_node
13467         = arm_data->u.exidx.unwind_edit_list;
13468       /* Now, sec->size is the size of the section we will write.  The original
13469          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
13470          markers) was sec->rawsize.  (This isn't the case if we perform no
13471          edits, then rawsize will be zero and we should use size).  */
13472       bfd_byte *edited_contents = bfd_malloc (sec->size);
13473       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
13474       unsigned int in_index, out_index;
13475       bfd_vma add_to_offsets = 0;
13476
13477       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
13478         {
13479           if (edit_node)
13480             {
13481               unsigned int edit_index = edit_node->index;
13482               
13483               if (in_index < edit_index && in_index * 8 < input_size)
13484                 {
13485                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
13486                                     contents + in_index * 8, add_to_offsets);
13487                   out_index++;
13488                   in_index++;
13489                 }
13490               else if (in_index == edit_index
13491                        || (in_index * 8 >= input_size
13492                            && edit_index == UINT_MAX))
13493                 {
13494                   switch (edit_node->type)
13495                     {
13496                     case DELETE_EXIDX_ENTRY:
13497                       in_index++;
13498                       add_to_offsets += 8;
13499                       break;
13500                     
13501                     case INSERT_EXIDX_CANTUNWIND_AT_END:
13502                       {
13503                         asection *text_sec = edit_node->linked_section;
13504                         bfd_vma text_offset = text_sec->output_section->vma
13505                                               + text_sec->output_offset
13506                                               + text_sec->size;
13507                         bfd_vma exidx_offset = offset + out_index * 8;
13508                         unsigned long prel31_offset;
13509
13510                         /* Note: this is meant to be equivalent to an
13511                            R_ARM_PREL31 relocation.  These synthetic
13512                            EXIDX_CANTUNWIND markers are not relocated by the
13513                            usual BFD method.  */
13514                         prel31_offset = (text_offset - exidx_offset)
13515                                         & 0x7ffffffful;
13516
13517                         /* First address we can't unwind.  */
13518                         bfd_put_32 (output_bfd, prel31_offset,
13519                                     &edited_contents[out_index * 8]);
13520
13521                         /* Code for EXIDX_CANTUNWIND.  */
13522                         bfd_put_32 (output_bfd, 0x1,
13523                                     &edited_contents[out_index * 8 + 4]);
13524
13525                         out_index++;
13526                         add_to_offsets -= 8;
13527                       }
13528                       break;
13529                     }
13530                   
13531                   edit_node = edit_node->next;
13532                 }
13533             }
13534           else
13535             {
13536               /* No more edits, copy remaining entries verbatim.  */
13537               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
13538                                 contents + in_index * 8, add_to_offsets);
13539               out_index++;
13540               in_index++;
13541             }
13542         }
13543
13544       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
13545         bfd_set_section_contents (output_bfd, sec->output_section,
13546                                   edited_contents,
13547                                   (file_ptr) sec->output_offset, sec->size);
13548
13549       return TRUE;
13550     }
13551
13552   /* Fix code to point to Cortex-A8 erratum stubs.  */
13553   if (globals->fix_cortex_a8)
13554     {
13555       struct a8_branch_to_stub_data data;
13556
13557       data.writing_section = sec;
13558       data.contents = contents;
13559
13560       bfd_hash_traverse (&globals->stub_hash_table, make_branch_to_a8_stub,
13561                          &data);
13562     }
13563
13564   if (mapcount == 0)
13565     return FALSE;
13566
13567   if (globals->byteswap_code)
13568     {
13569       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
13570
13571       ptr = map[0].vma;
13572       for (i = 0; i < mapcount; i++)
13573         {
13574           if (i == mapcount - 1)
13575             end = sec->size;
13576           else
13577             end = map[i + 1].vma;
13578
13579           switch (map[i].type)
13580             {
13581             case 'a':
13582               /* Byte swap code words.  */
13583               while (ptr + 3 < end)
13584                 {
13585                   tmp = contents[ptr];
13586                   contents[ptr] = contents[ptr + 3];
13587                   contents[ptr + 3] = tmp;
13588                   tmp = contents[ptr + 1];
13589                   contents[ptr + 1] = contents[ptr + 2];
13590                   contents[ptr + 2] = tmp;
13591                   ptr += 4;
13592                 }
13593               break;
13594
13595             case 't':
13596               /* Byte swap code halfwords.  */
13597               while (ptr + 1 < end)
13598                 {
13599                   tmp = contents[ptr];
13600                   contents[ptr] = contents[ptr + 1];
13601                   contents[ptr + 1] = tmp;
13602                   ptr += 2;
13603                 }
13604               break;
13605
13606             case 'd':
13607               /* Leave data alone.  */
13608               break;
13609             }
13610           ptr = end;
13611         }
13612     }
13613
13614   free (map);
13615   arm_data->mapcount = 0;
13616   arm_data->mapsize = 0;
13617   arm_data->map = NULL;
13618   unrecord_section_with_arm_elf_section_data (sec);
13619
13620   return FALSE;
13621 }
13622
13623 static void
13624 unrecord_section_via_map_over_sections (bfd * abfd ATTRIBUTE_UNUSED,
13625                                         asection * sec,
13626                                         void * ignore ATTRIBUTE_UNUSED)
13627 {
13628   unrecord_section_with_arm_elf_section_data (sec);
13629 }
13630
13631 static bfd_boolean
13632 elf32_arm_close_and_cleanup (bfd * abfd)
13633 {
13634   if (abfd->sections)
13635     bfd_map_over_sections (abfd,
13636                            unrecord_section_via_map_over_sections,
13637                            NULL);
13638
13639   return _bfd_elf_close_and_cleanup (abfd);
13640 }
13641
13642 static bfd_boolean
13643 elf32_arm_bfd_free_cached_info (bfd * abfd)
13644 {
13645   if (abfd->sections)
13646     bfd_map_over_sections (abfd,
13647                            unrecord_section_via_map_over_sections,
13648                            NULL);
13649
13650   return _bfd_free_cached_info (abfd);
13651 }
13652
13653 /* Display STT_ARM_TFUNC symbols as functions.  */
13654
13655 static void
13656 elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
13657                              asymbol *asym)
13658 {
13659   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
13660
13661   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
13662     elfsym->symbol.flags |= BSF_FUNCTION;
13663 }
13664
13665
13666 /* Mangle thumb function symbols as we read them in.  */
13667
13668 static bfd_boolean
13669 elf32_arm_swap_symbol_in (bfd * abfd,
13670                           const void *psrc,
13671                           const void *pshn,
13672                           Elf_Internal_Sym *dst)
13673 {
13674   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
13675     return FALSE;
13676
13677   /* New EABI objects mark thumb function symbols by setting the low bit of
13678      the address.  Turn these into STT_ARM_TFUNC.  */
13679   if ((ELF_ST_TYPE (dst->st_info) == STT_FUNC)
13680       && (dst->st_value & 1))
13681     {
13682       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
13683       dst->st_value &= ~(bfd_vma) 1;
13684     }
13685   return TRUE;
13686 }
13687
13688
13689 /* Mangle thumb function symbols as we write them out.  */
13690
13691 static void
13692 elf32_arm_swap_symbol_out (bfd *abfd,
13693                            const Elf_Internal_Sym *src,
13694                            void *cdst,
13695                            void *shndx)
13696 {
13697   Elf_Internal_Sym newsym;
13698
13699   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
13700      of the address set, as per the new EABI.  We do this unconditionally
13701      because objcopy does not set the elf header flags until after
13702      it writes out the symbol table.  */
13703   if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
13704     {
13705       newsym = *src;
13706       newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
13707       if (newsym.st_shndx != SHN_UNDEF)
13708         {
13709           /* Do this only for defined symbols. At link type, the static
13710              linker will simulate the work of dynamic linker of resolving
13711              symbols and will carry over the thumbness of found symbols to
13712              the output symbol table. It's not clear how it happens, but
13713              the thumbness of undefined symbols can well be different at
13714              runtime, and writing '1' for them will be confusing for users
13715              and possibly for dynamic linker itself.
13716           */
13717           newsym.st_value |= 1;
13718         }
13719
13720       src = &newsym;
13721     }
13722   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
13723 }
13724
13725 /* Add the PT_ARM_EXIDX program header.  */
13726
13727 static bfd_boolean
13728 elf32_arm_modify_segment_map (bfd *abfd,
13729                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
13730 {
13731   struct elf_segment_map *m;
13732   asection *sec;
13733
13734   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
13735   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
13736     {
13737       /* If there is already a PT_ARM_EXIDX header, then we do not
13738          want to add another one.  This situation arises when running
13739          "strip"; the input binary already has the header.  */
13740       m = elf_tdata (abfd)->segment_map;
13741       while (m && m->p_type != PT_ARM_EXIDX)
13742         m = m->next;
13743       if (!m)
13744         {
13745           m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
13746           if (m == NULL)
13747             return FALSE;
13748           m->p_type = PT_ARM_EXIDX;
13749           m->count = 1;
13750           m->sections[0] = sec;
13751
13752           m->next = elf_tdata (abfd)->segment_map;
13753           elf_tdata (abfd)->segment_map = m;
13754         }
13755     }
13756
13757   return TRUE;
13758 }
13759
13760 /* We may add a PT_ARM_EXIDX program header.  */
13761
13762 static int
13763 elf32_arm_additional_program_headers (bfd *abfd,
13764                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
13765 {
13766   asection *sec;
13767
13768   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
13769   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
13770     return 1;
13771   else
13772     return 0;
13773 }
13774
13775 /* We have two function types: STT_FUNC and STT_ARM_TFUNC.  */
13776
13777 static bfd_boolean
13778 elf32_arm_is_function_type (unsigned int type)
13779 {
13780   return (type == STT_FUNC) || (type == STT_ARM_TFUNC);
13781 }
13782
13783 /* We use this to override swap_symbol_in and swap_symbol_out.  */
13784 const struct elf_size_info elf32_arm_size_info =
13785 {
13786   sizeof (Elf32_External_Ehdr),
13787   sizeof (Elf32_External_Phdr),
13788   sizeof (Elf32_External_Shdr),
13789   sizeof (Elf32_External_Rel),
13790   sizeof (Elf32_External_Rela),
13791   sizeof (Elf32_External_Sym),
13792   sizeof (Elf32_External_Dyn),
13793   sizeof (Elf_External_Note),
13794   4,
13795   1,
13796   32, 2,
13797   ELFCLASS32, EV_CURRENT,
13798   bfd_elf32_write_out_phdrs,
13799   bfd_elf32_write_shdrs_and_ehdr,
13800   bfd_elf32_checksum_contents,
13801   bfd_elf32_write_relocs,
13802   elf32_arm_swap_symbol_in,
13803   elf32_arm_swap_symbol_out,
13804   bfd_elf32_slurp_reloc_table,
13805   bfd_elf32_slurp_symbol_table,
13806   bfd_elf32_swap_dyn_in,
13807   bfd_elf32_swap_dyn_out,
13808   bfd_elf32_swap_reloc_in,
13809   bfd_elf32_swap_reloc_out,
13810   bfd_elf32_swap_reloca_in,
13811   bfd_elf32_swap_reloca_out
13812 };
13813
13814 #define ELF_ARCH                        bfd_arch_arm
13815 #define ELF_MACHINE_CODE                EM_ARM
13816 #ifdef __QNXTARGET__
13817 #define ELF_MAXPAGESIZE                 0x1000
13818 #else
13819 #define ELF_MAXPAGESIZE                 0x8000
13820 #endif
13821 #define ELF_MINPAGESIZE                 0x1000
13822 #define ELF_COMMONPAGESIZE              0x1000
13823
13824 #define bfd_elf32_mkobject                      elf32_arm_mkobject
13825
13826 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
13827 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
13828 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
13829 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
13830 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
13831 #define bfd_elf32_bfd_link_hash_table_free      elf32_arm_hash_table_free
13832 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
13833 #define bfd_elf32_bfd_reloc_name_lookup elf32_arm_reloc_name_lookup
13834 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
13835 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
13836 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
13837 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
13838 #define bfd_elf32_close_and_cleanup             elf32_arm_close_and_cleanup
13839 #define bfd_elf32_bfd_free_cached_info          elf32_arm_bfd_free_cached_info
13840 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
13841
13842 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
13843 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
13844 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
13845 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
13846 #define elf_backend_check_relocs                elf32_arm_check_relocs
13847 #define elf_backend_relocate_section            elf32_arm_relocate_section
13848 #define elf_backend_write_section               elf32_arm_write_section
13849 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
13850 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
13851 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
13852 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
13853 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
13854 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
13855 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
13856 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
13857 #define elf_backend_object_p                    elf32_arm_object_p
13858 #define elf_backend_section_flags               elf32_arm_section_flags
13859 #define elf_backend_fake_sections               elf32_arm_fake_sections
13860 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
13861 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
13862 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
13863 #define elf_backend_symbol_processing           elf32_arm_symbol_processing
13864 #define elf_backend_size_info                   elf32_arm_size_info
13865 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
13866 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
13867 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
13868 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
13869 #define elf_backend_is_function_type            elf32_arm_is_function_type
13870
13871 #define elf_backend_can_refcount       1
13872 #define elf_backend_can_gc_sections    1
13873 #define elf_backend_plt_readonly       1
13874 #define elf_backend_want_got_plt       1
13875 #define elf_backend_want_plt_sym       0
13876 #define elf_backend_may_use_rel_p      1
13877 #define elf_backend_may_use_rela_p     0
13878 #define elf_backend_default_use_rela_p 0
13879
13880 #define elf_backend_got_header_size     12
13881
13882 #undef  elf_backend_obj_attrs_vendor
13883 #define elf_backend_obj_attrs_vendor            "aeabi"
13884 #undef  elf_backend_obj_attrs_section
13885 #define elf_backend_obj_attrs_section           ".ARM.attributes"
13886 #undef  elf_backend_obj_attrs_arg_type
13887 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
13888 #undef  elf_backend_obj_attrs_section_type
13889 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
13890 #define elf_backend_obj_attrs_order     elf32_arm_obj_attrs_order
13891
13892 #include "elf32-target.h"
13893
13894 /* VxWorks Targets.  */
13895
13896 #undef  TARGET_LITTLE_SYM
13897 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
13898 #undef  TARGET_LITTLE_NAME
13899 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
13900 #undef  TARGET_BIG_SYM
13901 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
13902 #undef  TARGET_BIG_NAME
13903 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
13904
13905 /* Like elf32_arm_link_hash_table_create -- but overrides
13906    appropriately for VxWorks.  */
13907
13908 static struct bfd_link_hash_table *
13909 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
13910 {
13911   struct bfd_link_hash_table *ret;
13912
13913   ret = elf32_arm_link_hash_table_create (abfd);
13914   if (ret)
13915     {
13916       struct elf32_arm_link_hash_table *htab
13917         = (struct elf32_arm_link_hash_table *) ret;
13918       htab->use_rel = 0;
13919       htab->vxworks_p = 1;
13920     }
13921   return ret;
13922 }
13923
13924 static void
13925 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
13926 {
13927   elf32_arm_final_write_processing (abfd, linker);
13928   elf_vxworks_final_write_processing (abfd, linker);
13929 }
13930
13931 #undef  elf32_bed
13932 #define elf32_bed elf32_arm_vxworks_bed
13933
13934 #undef  bfd_elf32_bfd_link_hash_table_create
13935 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
13936 #undef  elf_backend_add_symbol_hook
13937 #define elf_backend_add_symbol_hook             elf_vxworks_add_symbol_hook
13938 #undef  elf_backend_final_write_processing
13939 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
13940 #undef  elf_backend_emit_relocs
13941 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
13942
13943 #undef  elf_backend_may_use_rel_p
13944 #define elf_backend_may_use_rel_p       0
13945 #undef  elf_backend_may_use_rela_p
13946 #define elf_backend_may_use_rela_p      1
13947 #undef  elf_backend_default_use_rela_p
13948 #define elf_backend_default_use_rela_p  1
13949 #undef  elf_backend_want_plt_sym
13950 #define elf_backend_want_plt_sym        1
13951 #undef  ELF_MAXPAGESIZE
13952 #define ELF_MAXPAGESIZE                 0x1000
13953
13954 #include "elf32-target.h"
13955
13956
13957 /* Symbian OS Targets.  */
13958
13959 #undef  TARGET_LITTLE_SYM
13960 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
13961 #undef  TARGET_LITTLE_NAME
13962 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
13963 #undef  TARGET_BIG_SYM
13964 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
13965 #undef  TARGET_BIG_NAME
13966 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
13967
13968 /* Like elf32_arm_link_hash_table_create -- but overrides
13969    appropriately for Symbian OS.  */
13970
13971 static struct bfd_link_hash_table *
13972 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
13973 {
13974   struct bfd_link_hash_table *ret;
13975
13976   ret = elf32_arm_link_hash_table_create (abfd);
13977   if (ret)
13978     {
13979       struct elf32_arm_link_hash_table *htab
13980         = (struct elf32_arm_link_hash_table *)ret;
13981       /* There is no PLT header for Symbian OS.  */
13982       htab->plt_header_size = 0;
13983       /* The PLT entries are each one instruction and one word.  */
13984       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
13985       htab->symbian_p = 1;
13986       /* Symbian uses armv5t or above, so use_blx is always true.  */
13987       htab->use_blx = 1;
13988       htab->root.is_relocatable_executable = 1;
13989     }
13990   return ret;
13991 }
13992
13993 static const struct bfd_elf_special_section
13994 elf32_arm_symbian_special_sections[] =
13995 {
13996   /* In a BPABI executable, the dynamic linking sections do not go in
13997      the loadable read-only segment.  The post-linker may wish to
13998      refer to these sections, but they are not part of the final
13999      program image.  */
14000   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
14001   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
14002   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
14003   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
14004   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
14005   /* These sections do not need to be writable as the SymbianOS
14006      postlinker will arrange things so that no dynamic relocation is
14007      required.  */
14008   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
14009   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
14010   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
14011   { NULL,                             0, 0, 0,                 0 }
14012 };
14013
14014 static void
14015 elf32_arm_symbian_begin_write_processing (bfd *abfd,
14016                                           struct bfd_link_info *link_info)
14017 {
14018   /* BPABI objects are never loaded directly by an OS kernel; they are
14019      processed by a postlinker first, into an OS-specific format.  If
14020      the D_PAGED bit is set on the file, BFD will align segments on
14021      page boundaries, so that an OS can directly map the file.  With
14022      BPABI objects, that just results in wasted space.  In addition,
14023      because we clear the D_PAGED bit, map_sections_to_segments will
14024      recognize that the program headers should not be mapped into any
14025      loadable segment.  */
14026   abfd->flags &= ~D_PAGED;
14027   elf32_arm_begin_write_processing (abfd, link_info);
14028 }
14029
14030 static bfd_boolean
14031 elf32_arm_symbian_modify_segment_map (bfd *abfd,
14032                                       struct bfd_link_info *info)
14033 {
14034   struct elf_segment_map *m;
14035   asection *dynsec;
14036
14037   /* BPABI shared libraries and executables should have a PT_DYNAMIC
14038      segment.  However, because the .dynamic section is not marked
14039      with SEC_LOAD, the generic ELF code will not create such a
14040      segment.  */
14041   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
14042   if (dynsec)
14043     {
14044       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
14045         if (m->p_type == PT_DYNAMIC)
14046           break;
14047
14048       if (m == NULL)
14049         {
14050           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
14051           m->next = elf_tdata (abfd)->segment_map;
14052           elf_tdata (abfd)->segment_map = m;
14053         }
14054     }
14055
14056   /* Also call the generic arm routine.  */
14057   return elf32_arm_modify_segment_map (abfd, info);
14058 }
14059
14060 /* Return address for Ith PLT stub in section PLT, for relocation REL
14061    or (bfd_vma) -1 if it should not be included.  */
14062
14063 static bfd_vma
14064 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
14065                                const arelent *rel ATTRIBUTE_UNUSED)
14066 {
14067   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
14068 }
14069
14070
14071 #undef  elf32_bed
14072 #define elf32_bed elf32_arm_symbian_bed
14073
14074 /* The dynamic sections are not allocated on SymbianOS; the postlinker
14075    will process them and then discard them.  */
14076 #undef  ELF_DYNAMIC_SEC_FLAGS
14077 #define ELF_DYNAMIC_SEC_FLAGS \
14078   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
14079
14080 #undef elf_backend_add_symbol_hook
14081 #undef elf_backend_emit_relocs
14082
14083 #undef  bfd_elf32_bfd_link_hash_table_create
14084 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
14085 #undef  elf_backend_special_sections
14086 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
14087 #undef  elf_backend_begin_write_processing
14088 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
14089 #undef  elf_backend_final_write_processing
14090 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
14091
14092 #undef  elf_backend_modify_segment_map
14093 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
14094
14095 /* There is no .got section for BPABI objects, and hence no header.  */
14096 #undef  elf_backend_got_header_size
14097 #define elf_backend_got_header_size 0
14098
14099 /* Similarly, there is no .got.plt section.  */
14100 #undef  elf_backend_want_got_plt
14101 #define elf_backend_want_got_plt 0
14102
14103 #undef  elf_backend_plt_sym_val
14104 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
14105
14106 #undef  elf_backend_may_use_rel_p
14107 #define elf_backend_may_use_rel_p       1
14108 #undef  elf_backend_may_use_rela_p
14109 #define elf_backend_may_use_rela_p      0
14110 #undef  elf_backend_default_use_rela_p
14111 #define elf_backend_default_use_rela_p  0
14112 #undef  elf_backend_want_plt_sym
14113 #define elf_backend_want_plt_sym        0
14114 #undef  ELF_MAXPAGESIZE
14115 #define ELF_MAXPAGESIZE                 0x8000
14116
14117 #include "elf32-target.h"