Support R_SPARC_WDISP10 and R_SPARC_H34.
[external/binutils.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, 2010, 2011, 2012 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-nacl.h"
30 #include "elf-vxworks.h"
31 #include "elf/arm.h"
32
33 /* Return the relocation section associated with NAME.  HTAB is the
34    bfd's elf32_arm_link_hash_entry.  */
35 #define RELOC_SECTION(HTAB, NAME) \
36   ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
37
38 /* Return size of a relocation entry.  HTAB is the bfd's
39    elf32_arm_link_hash_entry.  */
40 #define RELOC_SIZE(HTAB) \
41   ((HTAB)->use_rel \
42    ? sizeof (Elf32_External_Rel) \
43    : sizeof (Elf32_External_Rela))
44
45 /* Return function to swap relocations in.  HTAB is the bfd's
46    elf32_arm_link_hash_entry.  */
47 #define SWAP_RELOC_IN(HTAB) \
48   ((HTAB)->use_rel \
49    ? bfd_elf32_swap_reloc_in \
50    : bfd_elf32_swap_reloca_in)
51
52 /* Return function to swap relocations out.  HTAB is the bfd's
53    elf32_arm_link_hash_entry.  */
54 #define SWAP_RELOC_OUT(HTAB) \
55   ((HTAB)->use_rel \
56    ? bfd_elf32_swap_reloc_out \
57    : bfd_elf32_swap_reloca_out)
58
59 #define elf_info_to_howto               0
60 #define elf_info_to_howto_rel           elf32_arm_info_to_howto
61
62 #define ARM_ELF_ABI_VERSION             0
63 #define ARM_ELF_OS_ABI_VERSION          ELFOSABI_ARM
64
65 static bfd_boolean elf32_arm_write_section (bfd *output_bfd,
66                                             struct bfd_link_info *link_info,
67                                             asection *sec,
68                                             bfd_byte *contents);
69
70 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
71    R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
72    in that slot.  */
73
74 static reloc_howto_type elf32_arm_howto_table_1[] =
75 {
76   /* No relocation.  */
77   HOWTO (R_ARM_NONE,            /* type */
78          0,                     /* rightshift */
79          0,                     /* size (0 = byte, 1 = short, 2 = long) */
80          0,                     /* bitsize */
81          FALSE,                 /* pc_relative */
82          0,                     /* bitpos */
83          complain_overflow_dont,/* complain_on_overflow */
84          bfd_elf_generic_reloc, /* special_function */
85          "R_ARM_NONE",          /* name */
86          FALSE,                 /* partial_inplace */
87          0,                     /* src_mask */
88          0,                     /* dst_mask */
89          FALSE),                /* pcrel_offset */
90
91   HOWTO (R_ARM_PC24,            /* type */
92          2,                     /* rightshift */
93          2,                     /* size (0 = byte, 1 = short, 2 = long) */
94          24,                    /* bitsize */
95          TRUE,                  /* pc_relative */
96          0,                     /* bitpos */
97          complain_overflow_signed,/* complain_on_overflow */
98          bfd_elf_generic_reloc, /* special_function */
99          "R_ARM_PC24",          /* name */
100          FALSE,                 /* partial_inplace */
101          0x00ffffff,            /* src_mask */
102          0x00ffffff,            /* dst_mask */
103          TRUE),                 /* pcrel_offset */
104
105   /* 32 bit absolute */
106   HOWTO (R_ARM_ABS32,           /* type */
107          0,                     /* rightshift */
108          2,                     /* size (0 = byte, 1 = short, 2 = long) */
109          32,                    /* bitsize */
110          FALSE,                 /* pc_relative */
111          0,                     /* bitpos */
112          complain_overflow_bitfield,/* complain_on_overflow */
113          bfd_elf_generic_reloc, /* special_function */
114          "R_ARM_ABS32",         /* name */
115          FALSE,                 /* partial_inplace */
116          0xffffffff,            /* src_mask */
117          0xffffffff,            /* dst_mask */
118          FALSE),                /* pcrel_offset */
119
120   /* standard 32bit pc-relative reloc */
121   HOWTO (R_ARM_REL32,           /* type */
122          0,                     /* rightshift */
123          2,                     /* size (0 = byte, 1 = short, 2 = long) */
124          32,                    /* bitsize */
125          TRUE,                  /* pc_relative */
126          0,                     /* bitpos */
127          complain_overflow_bitfield,/* complain_on_overflow */
128          bfd_elf_generic_reloc, /* special_function */
129          "R_ARM_REL32",         /* name */
130          FALSE,                 /* partial_inplace */
131          0xffffffff,            /* src_mask */
132          0xffffffff,            /* dst_mask */
133          TRUE),                 /* pcrel_offset */
134
135   /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
136   HOWTO (R_ARM_LDR_PC_G0,       /* type */
137          0,                     /* rightshift */
138          0,                     /* size (0 = byte, 1 = short, 2 = long) */
139          32,                    /* bitsize */
140          TRUE,                  /* pc_relative */
141          0,                     /* bitpos */
142          complain_overflow_dont,/* complain_on_overflow */
143          bfd_elf_generic_reloc, /* special_function */
144          "R_ARM_LDR_PC_G0",     /* name */
145          FALSE,                 /* partial_inplace */
146          0xffffffff,            /* src_mask */
147          0xffffffff,            /* dst_mask */
148          TRUE),                 /* pcrel_offset */
149
150    /* 16 bit absolute */
151   HOWTO (R_ARM_ABS16,           /* type */
152          0,                     /* rightshift */
153          1,                     /* size (0 = byte, 1 = short, 2 = long) */
154          16,                    /* bitsize */
155          FALSE,                 /* pc_relative */
156          0,                     /* bitpos */
157          complain_overflow_bitfield,/* complain_on_overflow */
158          bfd_elf_generic_reloc, /* special_function */
159          "R_ARM_ABS16",         /* name */
160          FALSE,                 /* partial_inplace */
161          0x0000ffff,            /* src_mask */
162          0x0000ffff,            /* dst_mask */
163          FALSE),                /* pcrel_offset */
164
165   /* 12 bit absolute */
166   HOWTO (R_ARM_ABS12,           /* type */
167          0,                     /* rightshift */
168          2,                     /* size (0 = byte, 1 = short, 2 = long) */
169          12,                    /* bitsize */
170          FALSE,                 /* pc_relative */
171          0,                     /* bitpos */
172          complain_overflow_bitfield,/* complain_on_overflow */
173          bfd_elf_generic_reloc, /* special_function */
174          "R_ARM_ABS12",         /* name */
175          FALSE,                 /* partial_inplace */
176          0x00000fff,            /* src_mask */
177          0x00000fff,            /* dst_mask */
178          FALSE),                /* pcrel_offset */
179
180   HOWTO (R_ARM_THM_ABS5,        /* type */
181          6,                     /* rightshift */
182          1,                     /* size (0 = byte, 1 = short, 2 = long) */
183          5,                     /* bitsize */
184          FALSE,                 /* pc_relative */
185          0,                     /* bitpos */
186          complain_overflow_bitfield,/* complain_on_overflow */
187          bfd_elf_generic_reloc, /* special_function */
188          "R_ARM_THM_ABS5",      /* name */
189          FALSE,                 /* partial_inplace */
190          0x000007e0,            /* src_mask */
191          0x000007e0,            /* dst_mask */
192          FALSE),                /* pcrel_offset */
193
194   /* 8 bit absolute */
195   HOWTO (R_ARM_ABS8,            /* type */
196          0,                     /* rightshift */
197          0,                     /* size (0 = byte, 1 = short, 2 = long) */
198          8,                     /* bitsize */
199          FALSE,                 /* pc_relative */
200          0,                     /* bitpos */
201          complain_overflow_bitfield,/* complain_on_overflow */
202          bfd_elf_generic_reloc, /* special_function */
203          "R_ARM_ABS8",          /* name */
204          FALSE,                 /* partial_inplace */
205          0x000000ff,            /* src_mask */
206          0x000000ff,            /* dst_mask */
207          FALSE),                /* pcrel_offset */
208
209   HOWTO (R_ARM_SBREL32,         /* type */
210          0,                     /* rightshift */
211          2,                     /* size (0 = byte, 1 = short, 2 = long) */
212          32,                    /* bitsize */
213          FALSE,                 /* pc_relative */
214          0,                     /* bitpos */
215          complain_overflow_dont,/* complain_on_overflow */
216          bfd_elf_generic_reloc, /* special_function */
217          "R_ARM_SBREL32",       /* name */
218          FALSE,                 /* partial_inplace */
219          0xffffffff,            /* src_mask */
220          0xffffffff,            /* dst_mask */
221          FALSE),                /* pcrel_offset */
222
223   HOWTO (R_ARM_THM_CALL,        /* type */
224          1,                     /* rightshift */
225          2,                     /* size (0 = byte, 1 = short, 2 = long) */
226          24,                    /* bitsize */
227          TRUE,                  /* pc_relative */
228          0,                     /* bitpos */
229          complain_overflow_signed,/* complain_on_overflow */
230          bfd_elf_generic_reloc, /* special_function */
231          "R_ARM_THM_CALL",      /* name */
232          FALSE,                 /* partial_inplace */
233          0x07ff2fff,            /* src_mask */
234          0x07ff2fff,            /* dst_mask */
235          TRUE),                 /* pcrel_offset */
236
237   HOWTO (R_ARM_THM_PC8,         /* type */
238          1,                     /* rightshift */
239          1,                     /* size (0 = byte, 1 = short, 2 = long) */
240          8,                     /* bitsize */
241          TRUE,                  /* pc_relative */
242          0,                     /* bitpos */
243          complain_overflow_signed,/* complain_on_overflow */
244          bfd_elf_generic_reloc, /* special_function */
245          "R_ARM_THM_PC8",       /* name */
246          FALSE,                 /* partial_inplace */
247          0x000000ff,            /* src_mask */
248          0x000000ff,            /* dst_mask */
249          TRUE),                 /* pcrel_offset */
250
251   HOWTO (R_ARM_BREL_ADJ,        /* type */
252          1,                     /* rightshift */
253          1,                     /* size (0 = byte, 1 = short, 2 = long) */
254          32,                    /* bitsize */
255          FALSE,                 /* pc_relative */
256          0,                     /* bitpos */
257          complain_overflow_signed,/* complain_on_overflow */
258          bfd_elf_generic_reloc, /* special_function */
259          "R_ARM_BREL_ADJ",      /* name */
260          FALSE,                 /* partial_inplace */
261          0xffffffff,            /* src_mask */
262          0xffffffff,            /* dst_mask */
263          FALSE),                /* pcrel_offset */
264
265   HOWTO (R_ARM_TLS_DESC,        /* type */
266          0,                     /* rightshift */
267          2,                     /* size (0 = byte, 1 = short, 2 = long) */
268          32,                    /* bitsize */
269          FALSE,                 /* pc_relative */
270          0,                     /* bitpos */
271          complain_overflow_bitfield,/* complain_on_overflow */
272          bfd_elf_generic_reloc, /* special_function */
273          "R_ARM_TLS_DESC",      /* name */
274          FALSE,                 /* partial_inplace */
275          0xffffffff,            /* src_mask */
276          0xffffffff,            /* dst_mask */
277          FALSE),                /* pcrel_offset */
278
279   HOWTO (R_ARM_THM_SWI8,        /* type */
280          0,                     /* rightshift */
281          0,                     /* size (0 = byte, 1 = short, 2 = long) */
282          0,                     /* bitsize */
283          FALSE,                 /* pc_relative */
284          0,                     /* bitpos */
285          complain_overflow_signed,/* complain_on_overflow */
286          bfd_elf_generic_reloc, /* special_function */
287          "R_ARM_SWI8",          /* name */
288          FALSE,                 /* partial_inplace */
289          0x00000000,            /* src_mask */
290          0x00000000,            /* dst_mask */
291          FALSE),                /* pcrel_offset */
292
293   /* BLX instruction for the ARM.  */
294   HOWTO (R_ARM_XPC25,           /* type */
295          2,                     /* rightshift */
296          2,                     /* size (0 = byte, 1 = short, 2 = long) */
297          24,                    /* bitsize */
298          TRUE,                  /* pc_relative */
299          0,                     /* bitpos */
300          complain_overflow_signed,/* complain_on_overflow */
301          bfd_elf_generic_reloc, /* special_function */
302          "R_ARM_XPC25",         /* name */
303          FALSE,                 /* partial_inplace */
304          0x00ffffff,            /* src_mask */
305          0x00ffffff,            /* dst_mask */
306          TRUE),                 /* pcrel_offset */
307
308   /* BLX instruction for the Thumb.  */
309   HOWTO (R_ARM_THM_XPC22,       /* type */
310          2,                     /* rightshift */
311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
312          24,                    /* bitsize */
313          TRUE,                  /* pc_relative */
314          0,                     /* bitpos */
315          complain_overflow_signed,/* complain_on_overflow */
316          bfd_elf_generic_reloc, /* special_function */
317          "R_ARM_THM_XPC22",     /* name */
318          FALSE,                 /* partial_inplace */
319          0x07ff2fff,            /* src_mask */
320          0x07ff2fff,            /* dst_mask */
321          TRUE),                 /* pcrel_offset */
322
323   /* Dynamic TLS relocations.  */
324
325   HOWTO (R_ARM_TLS_DTPMOD32,    /* type */
326          0,                     /* rightshift */
327          2,                     /* size (0 = byte, 1 = short, 2 = long) */
328          32,                    /* bitsize */
329          FALSE,                 /* pc_relative */
330          0,                     /* bitpos */
331          complain_overflow_bitfield,/* complain_on_overflow */
332          bfd_elf_generic_reloc, /* special_function */
333          "R_ARM_TLS_DTPMOD32",  /* name */
334          TRUE,                  /* partial_inplace */
335          0xffffffff,            /* src_mask */
336          0xffffffff,            /* dst_mask */
337          FALSE),                /* pcrel_offset */
338
339   HOWTO (R_ARM_TLS_DTPOFF32,    /* type */
340          0,                     /* rightshift */
341          2,                     /* size (0 = byte, 1 = short, 2 = long) */
342          32,                    /* bitsize */
343          FALSE,                 /* pc_relative */
344          0,                     /* bitpos */
345          complain_overflow_bitfield,/* complain_on_overflow */
346          bfd_elf_generic_reloc, /* special_function */
347          "R_ARM_TLS_DTPOFF32",  /* name */
348          TRUE,                  /* partial_inplace */
349          0xffffffff,            /* src_mask */
350          0xffffffff,            /* dst_mask */
351          FALSE),                /* pcrel_offset */
352
353   HOWTO (R_ARM_TLS_TPOFF32,     /* type */
354          0,                     /* rightshift */
355          2,                     /* size (0 = byte, 1 = short, 2 = long) */
356          32,                    /* bitsize */
357          FALSE,                 /* pc_relative */
358          0,                     /* bitpos */
359          complain_overflow_bitfield,/* complain_on_overflow */
360          bfd_elf_generic_reloc, /* special_function */
361          "R_ARM_TLS_TPOFF32",   /* name */
362          TRUE,                  /* partial_inplace */
363          0xffffffff,            /* src_mask */
364          0xffffffff,            /* dst_mask */
365          FALSE),                /* pcrel_offset */
366
367   /* Relocs used in ARM Linux */
368
369   HOWTO (R_ARM_COPY,            /* type */
370          0,                     /* rightshift */
371          2,                     /* size (0 = byte, 1 = short, 2 = long) */
372          32,                    /* bitsize */
373          FALSE,                 /* pc_relative */
374          0,                     /* bitpos */
375          complain_overflow_bitfield,/* complain_on_overflow */
376          bfd_elf_generic_reloc, /* special_function */
377          "R_ARM_COPY",          /* name */
378          TRUE,                  /* partial_inplace */
379          0xffffffff,            /* src_mask */
380          0xffffffff,            /* dst_mask */
381          FALSE),                /* pcrel_offset */
382
383   HOWTO (R_ARM_GLOB_DAT,        /* type */
384          0,                     /* rightshift */
385          2,                     /* size (0 = byte, 1 = short, 2 = long) */
386          32,                    /* bitsize */
387          FALSE,                 /* pc_relative */
388          0,                     /* bitpos */
389          complain_overflow_bitfield,/* complain_on_overflow */
390          bfd_elf_generic_reloc, /* special_function */
391          "R_ARM_GLOB_DAT",      /* name */
392          TRUE,                  /* partial_inplace */
393          0xffffffff,            /* src_mask */
394          0xffffffff,            /* dst_mask */
395          FALSE),                /* pcrel_offset */
396
397   HOWTO (R_ARM_JUMP_SLOT,       /* type */
398          0,                     /* rightshift */
399          2,                     /* size (0 = byte, 1 = short, 2 = long) */
400          32,                    /* bitsize */
401          FALSE,                 /* pc_relative */
402          0,                     /* bitpos */
403          complain_overflow_bitfield,/* complain_on_overflow */
404          bfd_elf_generic_reloc, /* special_function */
405          "R_ARM_JUMP_SLOT",     /* name */
406          TRUE,                  /* partial_inplace */
407          0xffffffff,            /* src_mask */
408          0xffffffff,            /* dst_mask */
409          FALSE),                /* pcrel_offset */
410
411   HOWTO (R_ARM_RELATIVE,        /* type */
412          0,                     /* rightshift */
413          2,                     /* size (0 = byte, 1 = short, 2 = long) */
414          32,                    /* bitsize */
415          FALSE,                 /* pc_relative */
416          0,                     /* bitpos */
417          complain_overflow_bitfield,/* complain_on_overflow */
418          bfd_elf_generic_reloc, /* special_function */
419          "R_ARM_RELATIVE",      /* name */
420          TRUE,                  /* partial_inplace */
421          0xffffffff,            /* src_mask */
422          0xffffffff,            /* dst_mask */
423          FALSE),                /* pcrel_offset */
424
425   HOWTO (R_ARM_GOTOFF32,        /* type */
426          0,                     /* rightshift */
427          2,                     /* size (0 = byte, 1 = short, 2 = long) */
428          32,                    /* bitsize */
429          FALSE,                 /* pc_relative */
430          0,                     /* bitpos */
431          complain_overflow_bitfield,/* complain_on_overflow */
432          bfd_elf_generic_reloc, /* special_function */
433          "R_ARM_GOTOFF32",      /* name */
434          TRUE,                  /* partial_inplace */
435          0xffffffff,            /* src_mask */
436          0xffffffff,            /* dst_mask */
437          FALSE),                /* pcrel_offset */
438
439   HOWTO (R_ARM_GOTPC,           /* type */
440          0,                     /* rightshift */
441          2,                     /* size (0 = byte, 1 = short, 2 = long) */
442          32,                    /* bitsize */
443          TRUE,                  /* pc_relative */
444          0,                     /* bitpos */
445          complain_overflow_bitfield,/* complain_on_overflow */
446          bfd_elf_generic_reloc, /* special_function */
447          "R_ARM_GOTPC",         /* name */
448          TRUE,                  /* partial_inplace */
449          0xffffffff,            /* src_mask */
450          0xffffffff,            /* dst_mask */
451          TRUE),                 /* pcrel_offset */
452
453   HOWTO (R_ARM_GOT32,           /* type */
454          0,                     /* rightshift */
455          2,                     /* size (0 = byte, 1 = short, 2 = long) */
456          32,                    /* bitsize */
457          FALSE,                 /* pc_relative */
458          0,                     /* bitpos */
459          complain_overflow_bitfield,/* complain_on_overflow */
460          bfd_elf_generic_reloc, /* special_function */
461          "R_ARM_GOT32",         /* name */
462          TRUE,                  /* partial_inplace */
463          0xffffffff,            /* src_mask */
464          0xffffffff,            /* dst_mask */
465          FALSE),                /* pcrel_offset */
466
467   HOWTO (R_ARM_PLT32,           /* type */
468          2,                     /* rightshift */
469          2,                     /* size (0 = byte, 1 = short, 2 = long) */
470          24,                    /* bitsize */
471          TRUE,                  /* pc_relative */
472          0,                     /* bitpos */
473          complain_overflow_bitfield,/* complain_on_overflow */
474          bfd_elf_generic_reloc, /* special_function */
475          "R_ARM_PLT32",         /* name */
476          FALSE,                 /* partial_inplace */
477          0x00ffffff,            /* src_mask */
478          0x00ffffff,            /* dst_mask */
479          TRUE),                 /* pcrel_offset */
480
481   HOWTO (R_ARM_CALL,            /* type */
482          2,                     /* rightshift */
483          2,                     /* size (0 = byte, 1 = short, 2 = long) */
484          24,                    /* bitsize */
485          TRUE,                  /* pc_relative */
486          0,                     /* bitpos */
487          complain_overflow_signed,/* complain_on_overflow */
488          bfd_elf_generic_reloc, /* special_function */
489          "R_ARM_CALL",          /* name */
490          FALSE,                 /* partial_inplace */
491          0x00ffffff,            /* src_mask */
492          0x00ffffff,            /* dst_mask */
493          TRUE),                 /* pcrel_offset */
494
495   HOWTO (R_ARM_JUMP24,          /* type */
496          2,                     /* rightshift */
497          2,                     /* size (0 = byte, 1 = short, 2 = long) */
498          24,                    /* bitsize */
499          TRUE,                  /* pc_relative */
500          0,                     /* bitpos */
501          complain_overflow_signed,/* complain_on_overflow */
502          bfd_elf_generic_reloc, /* special_function */
503          "R_ARM_JUMP24",        /* name */
504          FALSE,                 /* partial_inplace */
505          0x00ffffff,            /* src_mask */
506          0x00ffffff,            /* dst_mask */
507          TRUE),                 /* pcrel_offset */
508
509   HOWTO (R_ARM_THM_JUMP24,      /* type */
510          1,                     /* rightshift */
511          2,                     /* size (0 = byte, 1 = short, 2 = long) */
512          24,                    /* bitsize */
513          TRUE,                  /* pc_relative */
514          0,                     /* bitpos */
515          complain_overflow_signed,/* complain_on_overflow */
516          bfd_elf_generic_reloc, /* special_function */
517          "R_ARM_THM_JUMP24",    /* name */
518          FALSE,                 /* partial_inplace */
519          0x07ff2fff,            /* src_mask */
520          0x07ff2fff,            /* dst_mask */
521          TRUE),                 /* pcrel_offset */
522
523   HOWTO (R_ARM_BASE_ABS,        /* type */
524          0,                     /* rightshift */
525          2,                     /* size (0 = byte, 1 = short, 2 = long) */
526          32,                    /* bitsize */
527          FALSE,                 /* pc_relative */
528          0,                     /* bitpos */
529          complain_overflow_dont,/* complain_on_overflow */
530          bfd_elf_generic_reloc, /* special_function */
531          "R_ARM_BASE_ABS",      /* name */
532          FALSE,                 /* partial_inplace */
533          0xffffffff,            /* src_mask */
534          0xffffffff,            /* dst_mask */
535          FALSE),                /* pcrel_offset */
536
537   HOWTO (R_ARM_ALU_PCREL7_0,    /* type */
538          0,                     /* rightshift */
539          2,                     /* size (0 = byte, 1 = short, 2 = long) */
540          12,                    /* bitsize */
541          TRUE,                  /* pc_relative */
542          0,                     /* bitpos */
543          complain_overflow_dont,/* complain_on_overflow */
544          bfd_elf_generic_reloc, /* special_function */
545          "R_ARM_ALU_PCREL_7_0", /* name */
546          FALSE,                 /* partial_inplace */
547          0x00000fff,            /* src_mask */
548          0x00000fff,            /* dst_mask */
549          TRUE),                 /* pcrel_offset */
550
551   HOWTO (R_ARM_ALU_PCREL15_8,   /* type */
552          0,                     /* rightshift */
553          2,                     /* size (0 = byte, 1 = short, 2 = long) */
554          12,                    /* bitsize */
555          TRUE,                  /* pc_relative */
556          8,                     /* bitpos */
557          complain_overflow_dont,/* complain_on_overflow */
558          bfd_elf_generic_reloc, /* special_function */
559          "R_ARM_ALU_PCREL_15_8",/* name */
560          FALSE,                 /* partial_inplace */
561          0x00000fff,            /* src_mask */
562          0x00000fff,            /* dst_mask */
563          TRUE),                 /* pcrel_offset */
564
565   HOWTO (R_ARM_ALU_PCREL23_15,  /* type */
566          0,                     /* rightshift */
567          2,                     /* size (0 = byte, 1 = short, 2 = long) */
568          12,                    /* bitsize */
569          TRUE,                  /* pc_relative */
570          16,                    /* bitpos */
571          complain_overflow_dont,/* complain_on_overflow */
572          bfd_elf_generic_reloc, /* special_function */
573          "R_ARM_ALU_PCREL_23_15",/* name */
574          FALSE,                 /* partial_inplace */
575          0x00000fff,            /* src_mask */
576          0x00000fff,            /* dst_mask */
577          TRUE),                 /* pcrel_offset */
578
579   HOWTO (R_ARM_LDR_SBREL_11_0,  /* type */
580          0,                     /* rightshift */
581          2,                     /* size (0 = byte, 1 = short, 2 = long) */
582          12,                    /* bitsize */
583          FALSE,                 /* pc_relative */
584          0,                     /* bitpos */
585          complain_overflow_dont,/* complain_on_overflow */
586          bfd_elf_generic_reloc, /* special_function */
587          "R_ARM_LDR_SBREL_11_0",/* name */
588          FALSE,                 /* partial_inplace */
589          0x00000fff,            /* src_mask */
590          0x00000fff,            /* dst_mask */
591          FALSE),                /* pcrel_offset */
592
593   HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
594          0,                     /* rightshift */
595          2,                     /* size (0 = byte, 1 = short, 2 = long) */
596          8,                     /* bitsize */
597          FALSE,                 /* pc_relative */
598          12,                    /* bitpos */
599          complain_overflow_dont,/* complain_on_overflow */
600          bfd_elf_generic_reloc, /* special_function */
601          "R_ARM_ALU_SBREL_19_12",/* name */
602          FALSE,                 /* partial_inplace */
603          0x000ff000,            /* src_mask */
604          0x000ff000,            /* dst_mask */
605          FALSE),                /* pcrel_offset */
606
607   HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
608          0,                     /* rightshift */
609          2,                     /* size (0 = byte, 1 = short, 2 = long) */
610          8,                     /* bitsize */
611          FALSE,                 /* pc_relative */
612          20,                    /* bitpos */
613          complain_overflow_dont,/* complain_on_overflow */
614          bfd_elf_generic_reloc, /* special_function */
615          "R_ARM_ALU_SBREL_27_20",/* name */
616          FALSE,                 /* partial_inplace */
617          0x0ff00000,            /* src_mask */
618          0x0ff00000,            /* dst_mask */
619          FALSE),                /* pcrel_offset */
620
621   HOWTO (R_ARM_TARGET1,         /* type */
622          0,                     /* rightshift */
623          2,                     /* size (0 = byte, 1 = short, 2 = long) */
624          32,                    /* bitsize */
625          FALSE,                 /* pc_relative */
626          0,                     /* bitpos */
627          complain_overflow_dont,/* complain_on_overflow */
628          bfd_elf_generic_reloc, /* special_function */
629          "R_ARM_TARGET1",       /* name */
630          FALSE,                 /* partial_inplace */
631          0xffffffff,            /* src_mask */
632          0xffffffff,            /* dst_mask */
633          FALSE),                /* pcrel_offset */
634
635   HOWTO (R_ARM_ROSEGREL32,      /* type */
636          0,                     /* rightshift */
637          2,                     /* size (0 = byte, 1 = short, 2 = long) */
638          32,                    /* bitsize */
639          FALSE,                 /* pc_relative */
640          0,                     /* bitpos */
641          complain_overflow_dont,/* complain_on_overflow */
642          bfd_elf_generic_reloc, /* special_function */
643          "R_ARM_ROSEGREL32",    /* name */
644          FALSE,                 /* partial_inplace */
645          0xffffffff,            /* src_mask */
646          0xffffffff,            /* dst_mask */
647          FALSE),                /* pcrel_offset */
648
649   HOWTO (R_ARM_V4BX,            /* type */
650          0,                     /* rightshift */
651          2,                     /* size (0 = byte, 1 = short, 2 = long) */
652          32,                    /* bitsize */
653          FALSE,                 /* pc_relative */
654          0,                     /* bitpos */
655          complain_overflow_dont,/* complain_on_overflow */
656          bfd_elf_generic_reloc, /* special_function */
657          "R_ARM_V4BX",          /* name */
658          FALSE,                 /* partial_inplace */
659          0xffffffff,            /* src_mask */
660          0xffffffff,            /* dst_mask */
661          FALSE),                /* pcrel_offset */
662
663   HOWTO (R_ARM_TARGET2,         /* type */
664          0,                     /* rightshift */
665          2,                     /* size (0 = byte, 1 = short, 2 = long) */
666          32,                    /* bitsize */
667          FALSE,                 /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_signed,/* complain_on_overflow */
670          bfd_elf_generic_reloc, /* special_function */
671          "R_ARM_TARGET2",       /* name */
672          FALSE,                 /* partial_inplace */
673          0xffffffff,            /* src_mask */
674          0xffffffff,            /* dst_mask */
675          TRUE),                 /* pcrel_offset */
676
677   HOWTO (R_ARM_PREL31,          /* type */
678          0,                     /* rightshift */
679          2,                     /* size (0 = byte, 1 = short, 2 = long) */
680          31,                    /* bitsize */
681          TRUE,                  /* pc_relative */
682          0,                     /* bitpos */
683          complain_overflow_signed,/* complain_on_overflow */
684          bfd_elf_generic_reloc, /* special_function */
685          "R_ARM_PREL31",        /* name */
686          FALSE,                 /* partial_inplace */
687          0x7fffffff,            /* src_mask */
688          0x7fffffff,            /* dst_mask */
689          TRUE),                 /* pcrel_offset */
690
691   HOWTO (R_ARM_MOVW_ABS_NC,     /* type */
692          0,                     /* rightshift */
693          2,                     /* size (0 = byte, 1 = short, 2 = long) */
694          16,                    /* bitsize */
695          FALSE,                 /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_dont,/* complain_on_overflow */
698          bfd_elf_generic_reloc, /* special_function */
699          "R_ARM_MOVW_ABS_NC",   /* name */
700          FALSE,                 /* partial_inplace */
701          0x000f0fff,            /* src_mask */
702          0x000f0fff,            /* dst_mask */
703          FALSE),                /* pcrel_offset */
704
705   HOWTO (R_ARM_MOVT_ABS,        /* type */
706          0,                     /* rightshift */
707          2,                     /* size (0 = byte, 1 = short, 2 = long) */
708          16,                    /* bitsize */
709          FALSE,                 /* pc_relative */
710          0,                     /* bitpos */
711          complain_overflow_bitfield,/* complain_on_overflow */
712          bfd_elf_generic_reloc, /* special_function */
713          "R_ARM_MOVT_ABS",      /* name */
714          FALSE,                 /* partial_inplace */
715          0x000f0fff,            /* src_mask */
716          0x000f0fff,            /* dst_mask */
717          FALSE),                /* pcrel_offset */
718
719   HOWTO (R_ARM_MOVW_PREL_NC,    /* type */
720          0,                     /* rightshift */
721          2,                     /* size (0 = byte, 1 = short, 2 = long) */
722          16,                    /* bitsize */
723          TRUE,                  /* pc_relative */
724          0,                     /* bitpos */
725          complain_overflow_dont,/* complain_on_overflow */
726          bfd_elf_generic_reloc, /* special_function */
727          "R_ARM_MOVW_PREL_NC",  /* name */
728          FALSE,                 /* partial_inplace */
729          0x000f0fff,            /* src_mask */
730          0x000f0fff,            /* dst_mask */
731          TRUE),                 /* pcrel_offset */
732
733   HOWTO (R_ARM_MOVT_PREL,       /* type */
734          0,                     /* rightshift */
735          2,                     /* size (0 = byte, 1 = short, 2 = long) */
736          16,                    /* bitsize */
737          TRUE,                  /* pc_relative */
738          0,                     /* bitpos */
739          complain_overflow_bitfield,/* complain_on_overflow */
740          bfd_elf_generic_reloc, /* special_function */
741          "R_ARM_MOVT_PREL",     /* name */
742          FALSE,                 /* partial_inplace */
743          0x000f0fff,            /* src_mask */
744          0x000f0fff,            /* dst_mask */
745          TRUE),                 /* pcrel_offset */
746
747   HOWTO (R_ARM_THM_MOVW_ABS_NC, /* type */
748          0,                     /* rightshift */
749          2,                     /* size (0 = byte, 1 = short, 2 = long) */
750          16,                    /* bitsize */
751          FALSE,                 /* pc_relative */
752          0,                     /* bitpos */
753          complain_overflow_dont,/* complain_on_overflow */
754          bfd_elf_generic_reloc, /* special_function */
755          "R_ARM_THM_MOVW_ABS_NC",/* name */
756          FALSE,                 /* partial_inplace */
757          0x040f70ff,            /* src_mask */
758          0x040f70ff,            /* dst_mask */
759          FALSE),                /* pcrel_offset */
760
761   HOWTO (R_ARM_THM_MOVT_ABS,    /* type */
762          0,                     /* rightshift */
763          2,                     /* size (0 = byte, 1 = short, 2 = long) */
764          16,                    /* bitsize */
765          FALSE,                 /* pc_relative */
766          0,                     /* bitpos */
767          complain_overflow_bitfield,/* complain_on_overflow */
768          bfd_elf_generic_reloc, /* special_function */
769          "R_ARM_THM_MOVT_ABS",  /* name */
770          FALSE,                 /* partial_inplace */
771          0x040f70ff,            /* src_mask */
772          0x040f70ff,            /* dst_mask */
773          FALSE),                /* pcrel_offset */
774
775   HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
776          0,                     /* rightshift */
777          2,                     /* size (0 = byte, 1 = short, 2 = long) */
778          16,                    /* bitsize */
779          TRUE,                  /* pc_relative */
780          0,                     /* bitpos */
781          complain_overflow_dont,/* complain_on_overflow */
782          bfd_elf_generic_reloc, /* special_function */
783          "R_ARM_THM_MOVW_PREL_NC",/* name */
784          FALSE,                 /* partial_inplace */
785          0x040f70ff,            /* src_mask */
786          0x040f70ff,            /* dst_mask */
787          TRUE),                 /* pcrel_offset */
788
789   HOWTO (R_ARM_THM_MOVT_PREL,   /* type */
790          0,                     /* rightshift */
791          2,                     /* size (0 = byte, 1 = short, 2 = long) */
792          16,                    /* bitsize */
793          TRUE,                  /* pc_relative */
794          0,                     /* bitpos */
795          complain_overflow_bitfield,/* complain_on_overflow */
796          bfd_elf_generic_reloc, /* special_function */
797          "R_ARM_THM_MOVT_PREL", /* name */
798          FALSE,                 /* partial_inplace */
799          0x040f70ff,            /* src_mask */
800          0x040f70ff,            /* dst_mask */
801          TRUE),                 /* pcrel_offset */
802
803   HOWTO (R_ARM_THM_JUMP19,      /* type */
804          1,                     /* rightshift */
805          2,                     /* size (0 = byte, 1 = short, 2 = long) */
806          19,                    /* bitsize */
807          TRUE,                  /* pc_relative */
808          0,                     /* bitpos */
809          complain_overflow_signed,/* complain_on_overflow */
810          bfd_elf_generic_reloc, /* special_function */
811          "R_ARM_THM_JUMP19",    /* name */
812          FALSE,                 /* partial_inplace */
813          0x043f2fff,            /* src_mask */
814          0x043f2fff,            /* dst_mask */
815          TRUE),                 /* pcrel_offset */
816
817   HOWTO (R_ARM_THM_JUMP6,       /* type */
818          1,                     /* rightshift */
819          1,                     /* size (0 = byte, 1 = short, 2 = long) */
820          6,                     /* bitsize */
821          TRUE,                  /* pc_relative */
822          0,                     /* bitpos */
823          complain_overflow_unsigned,/* complain_on_overflow */
824          bfd_elf_generic_reloc, /* special_function */
825          "R_ARM_THM_JUMP6",     /* name */
826          FALSE,                 /* partial_inplace */
827          0x02f8,                /* src_mask */
828          0x02f8,                /* dst_mask */
829          TRUE),                 /* pcrel_offset */
830
831   /* These are declared as 13-bit signed relocations because we can
832      address -4095 .. 4095(base) by altering ADDW to SUBW or vice
833      versa.  */
834   HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
835          0,                     /* rightshift */
836          2,                     /* size (0 = byte, 1 = short, 2 = long) */
837          13,                    /* bitsize */
838          TRUE,                  /* pc_relative */
839          0,                     /* bitpos */
840          complain_overflow_dont,/* complain_on_overflow */
841          bfd_elf_generic_reloc, /* special_function */
842          "R_ARM_THM_ALU_PREL_11_0",/* name */
843          FALSE,                 /* partial_inplace */
844          0xffffffff,            /* src_mask */
845          0xffffffff,            /* dst_mask */
846          TRUE),                 /* pcrel_offset */
847
848   HOWTO (R_ARM_THM_PC12,        /* type */
849          0,                     /* rightshift */
850          2,                     /* size (0 = byte, 1 = short, 2 = long) */
851          13,                    /* bitsize */
852          TRUE,                  /* pc_relative */
853          0,                     /* bitpos */
854          complain_overflow_dont,/* complain_on_overflow */
855          bfd_elf_generic_reloc, /* special_function */
856          "R_ARM_THM_PC12",      /* name */
857          FALSE,                 /* partial_inplace */
858          0xffffffff,            /* src_mask */
859          0xffffffff,            /* dst_mask */
860          TRUE),                 /* pcrel_offset */
861
862   HOWTO (R_ARM_ABS32_NOI,       /* type */
863          0,                     /* rightshift */
864          2,                     /* size (0 = byte, 1 = short, 2 = long) */
865          32,                    /* bitsize */
866          FALSE,                 /* pc_relative */
867          0,                     /* bitpos */
868          complain_overflow_dont,/* complain_on_overflow */
869          bfd_elf_generic_reloc, /* special_function */
870          "R_ARM_ABS32_NOI",     /* name */
871          FALSE,                 /* partial_inplace */
872          0xffffffff,            /* src_mask */
873          0xffffffff,            /* dst_mask */
874          FALSE),                /* pcrel_offset */
875
876   HOWTO (R_ARM_REL32_NOI,       /* type */
877          0,                     /* rightshift */
878          2,                     /* size (0 = byte, 1 = short, 2 = long) */
879          32,                    /* bitsize */
880          TRUE,                  /* pc_relative */
881          0,                     /* bitpos */
882          complain_overflow_dont,/* complain_on_overflow */
883          bfd_elf_generic_reloc, /* special_function */
884          "R_ARM_REL32_NOI",     /* name */
885          FALSE,                 /* partial_inplace */
886          0xffffffff,            /* src_mask */
887          0xffffffff,            /* dst_mask */
888          FALSE),                /* pcrel_offset */
889
890   /* Group relocations.  */
891
892   HOWTO (R_ARM_ALU_PC_G0_NC,    /* type */
893          0,                     /* rightshift */
894          2,                     /* size (0 = byte, 1 = short, 2 = long) */
895          32,                    /* bitsize */
896          TRUE,                  /* pc_relative */
897          0,                     /* bitpos */
898          complain_overflow_dont,/* complain_on_overflow */
899          bfd_elf_generic_reloc, /* special_function */
900          "R_ARM_ALU_PC_G0_NC",  /* name */
901          FALSE,                 /* partial_inplace */
902          0xffffffff,            /* src_mask */
903          0xffffffff,            /* dst_mask */
904          TRUE),                 /* pcrel_offset */
905
906   HOWTO (R_ARM_ALU_PC_G0,       /* type */
907          0,                     /* rightshift */
908          2,                     /* size (0 = byte, 1 = short, 2 = long) */
909          32,                    /* bitsize */
910          TRUE,                  /* pc_relative */
911          0,                     /* bitpos */
912          complain_overflow_dont,/* complain_on_overflow */
913          bfd_elf_generic_reloc, /* special_function */
914          "R_ARM_ALU_PC_G0",     /* name */
915          FALSE,                 /* partial_inplace */
916          0xffffffff,            /* src_mask */
917          0xffffffff,            /* dst_mask */
918          TRUE),                 /* pcrel_offset */
919
920   HOWTO (R_ARM_ALU_PC_G1_NC,    /* type */
921          0,                     /* rightshift */
922          2,                     /* size (0 = byte, 1 = short, 2 = long) */
923          32,                    /* bitsize */
924          TRUE,                  /* pc_relative */
925          0,                     /* bitpos */
926          complain_overflow_dont,/* complain_on_overflow */
927          bfd_elf_generic_reloc, /* special_function */
928          "R_ARM_ALU_PC_G1_NC",  /* name */
929          FALSE,                 /* partial_inplace */
930          0xffffffff,            /* src_mask */
931          0xffffffff,            /* dst_mask */
932          TRUE),                 /* pcrel_offset */
933
934   HOWTO (R_ARM_ALU_PC_G1,       /* type */
935          0,                     /* rightshift */
936          2,                     /* size (0 = byte, 1 = short, 2 = long) */
937          32,                    /* bitsize */
938          TRUE,                  /* pc_relative */
939          0,                     /* bitpos */
940          complain_overflow_dont,/* complain_on_overflow */
941          bfd_elf_generic_reloc, /* special_function */
942          "R_ARM_ALU_PC_G1",     /* name */
943          FALSE,                 /* partial_inplace */
944          0xffffffff,            /* src_mask */
945          0xffffffff,            /* dst_mask */
946          TRUE),                 /* pcrel_offset */
947
948   HOWTO (R_ARM_ALU_PC_G2,       /* type */
949          0,                     /* rightshift */
950          2,                     /* size (0 = byte, 1 = short, 2 = long) */
951          32,                    /* bitsize */
952          TRUE,                  /* pc_relative */
953          0,                     /* bitpos */
954          complain_overflow_dont,/* complain_on_overflow */
955          bfd_elf_generic_reloc, /* special_function */
956          "R_ARM_ALU_PC_G2",     /* name */
957          FALSE,                 /* partial_inplace */
958          0xffffffff,            /* src_mask */
959          0xffffffff,            /* dst_mask */
960          TRUE),                 /* pcrel_offset */
961
962   HOWTO (R_ARM_LDR_PC_G1,       /* type */
963          0,                     /* rightshift */
964          2,                     /* size (0 = byte, 1 = short, 2 = long) */
965          32,                    /* bitsize */
966          TRUE,                  /* pc_relative */
967          0,                     /* bitpos */
968          complain_overflow_dont,/* complain_on_overflow */
969          bfd_elf_generic_reloc, /* special_function */
970          "R_ARM_LDR_PC_G1",     /* name */
971          FALSE,                 /* partial_inplace */
972          0xffffffff,            /* src_mask */
973          0xffffffff,            /* dst_mask */
974          TRUE),                 /* pcrel_offset */
975
976   HOWTO (R_ARM_LDR_PC_G2,       /* type */
977          0,                     /* rightshift */
978          2,                     /* size (0 = byte, 1 = short, 2 = long) */
979          32,                    /* bitsize */
980          TRUE,                  /* pc_relative */
981          0,                     /* bitpos */
982          complain_overflow_dont,/* complain_on_overflow */
983          bfd_elf_generic_reloc, /* special_function */
984          "R_ARM_LDR_PC_G2",     /* name */
985          FALSE,                 /* partial_inplace */
986          0xffffffff,            /* src_mask */
987          0xffffffff,            /* dst_mask */
988          TRUE),                 /* pcrel_offset */
989
990   HOWTO (R_ARM_LDRS_PC_G0,      /* type */
991          0,                     /* rightshift */
992          2,                     /* size (0 = byte, 1 = short, 2 = long) */
993          32,                    /* bitsize */
994          TRUE,                  /* pc_relative */
995          0,                     /* bitpos */
996          complain_overflow_dont,/* complain_on_overflow */
997          bfd_elf_generic_reloc, /* special_function */
998          "R_ARM_LDRS_PC_G0",    /* name */
999          FALSE,                 /* partial_inplace */
1000          0xffffffff,            /* src_mask */
1001          0xffffffff,            /* dst_mask */
1002          TRUE),                 /* pcrel_offset */
1003
1004   HOWTO (R_ARM_LDRS_PC_G1,      /* type */
1005          0,                     /* rightshift */
1006          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1007          32,                    /* bitsize */
1008          TRUE,                  /* pc_relative */
1009          0,                     /* bitpos */
1010          complain_overflow_dont,/* complain_on_overflow */
1011          bfd_elf_generic_reloc, /* special_function */
1012          "R_ARM_LDRS_PC_G1",    /* name */
1013          FALSE,                 /* partial_inplace */
1014          0xffffffff,            /* src_mask */
1015          0xffffffff,            /* dst_mask */
1016          TRUE),                 /* pcrel_offset */
1017
1018   HOWTO (R_ARM_LDRS_PC_G2,      /* type */
1019          0,                     /* rightshift */
1020          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1021          32,                    /* bitsize */
1022          TRUE,                  /* pc_relative */
1023          0,                     /* bitpos */
1024          complain_overflow_dont,/* complain_on_overflow */
1025          bfd_elf_generic_reloc, /* special_function */
1026          "R_ARM_LDRS_PC_G2",    /* name */
1027          FALSE,                 /* partial_inplace */
1028          0xffffffff,            /* src_mask */
1029          0xffffffff,            /* dst_mask */
1030          TRUE),                 /* pcrel_offset */
1031
1032   HOWTO (R_ARM_LDC_PC_G0,       /* type */
1033          0,                     /* rightshift */
1034          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1035          32,                    /* bitsize */
1036          TRUE,                  /* pc_relative */
1037          0,                     /* bitpos */
1038          complain_overflow_dont,/* complain_on_overflow */
1039          bfd_elf_generic_reloc, /* special_function */
1040          "R_ARM_LDC_PC_G0",     /* name */
1041          FALSE,                 /* partial_inplace */
1042          0xffffffff,            /* src_mask */
1043          0xffffffff,            /* dst_mask */
1044          TRUE),                 /* pcrel_offset */
1045
1046   HOWTO (R_ARM_LDC_PC_G1,       /* type */
1047          0,                     /* rightshift */
1048          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1049          32,                    /* bitsize */
1050          TRUE,                  /* pc_relative */
1051          0,                     /* bitpos */
1052          complain_overflow_dont,/* complain_on_overflow */
1053          bfd_elf_generic_reloc, /* special_function */
1054          "R_ARM_LDC_PC_G1",     /* name */
1055          FALSE,                 /* partial_inplace */
1056          0xffffffff,            /* src_mask */
1057          0xffffffff,            /* dst_mask */
1058          TRUE),                 /* pcrel_offset */
1059
1060   HOWTO (R_ARM_LDC_PC_G2,       /* type */
1061          0,                     /* rightshift */
1062          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1063          32,                    /* bitsize */
1064          TRUE,                  /* pc_relative */
1065          0,                     /* bitpos */
1066          complain_overflow_dont,/* complain_on_overflow */
1067          bfd_elf_generic_reloc, /* special_function */
1068          "R_ARM_LDC_PC_G2",     /* name */
1069          FALSE,                 /* partial_inplace */
1070          0xffffffff,            /* src_mask */
1071          0xffffffff,            /* dst_mask */
1072          TRUE),                 /* pcrel_offset */
1073
1074   HOWTO (R_ARM_ALU_SB_G0_NC,    /* type */
1075          0,                     /* rightshift */
1076          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1077          32,                    /* bitsize */
1078          TRUE,                  /* pc_relative */
1079          0,                     /* bitpos */
1080          complain_overflow_dont,/* complain_on_overflow */
1081          bfd_elf_generic_reloc, /* special_function */
1082          "R_ARM_ALU_SB_G0_NC",  /* name */
1083          FALSE,                 /* partial_inplace */
1084          0xffffffff,            /* src_mask */
1085          0xffffffff,            /* dst_mask */
1086          TRUE),                 /* pcrel_offset */
1087
1088   HOWTO (R_ARM_ALU_SB_G0,       /* type */
1089          0,                     /* rightshift */
1090          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1091          32,                    /* bitsize */
1092          TRUE,                  /* pc_relative */
1093          0,                     /* bitpos */
1094          complain_overflow_dont,/* complain_on_overflow */
1095          bfd_elf_generic_reloc, /* special_function */
1096          "R_ARM_ALU_SB_G0",     /* name */
1097          FALSE,                 /* partial_inplace */
1098          0xffffffff,            /* src_mask */
1099          0xffffffff,            /* dst_mask */
1100          TRUE),                 /* pcrel_offset */
1101
1102   HOWTO (R_ARM_ALU_SB_G1_NC,    /* type */
1103          0,                     /* rightshift */
1104          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1105          32,                    /* bitsize */
1106          TRUE,                  /* pc_relative */
1107          0,                     /* bitpos */
1108          complain_overflow_dont,/* complain_on_overflow */
1109          bfd_elf_generic_reloc, /* special_function */
1110          "R_ARM_ALU_SB_G1_NC",  /* name */
1111          FALSE,                 /* partial_inplace */
1112          0xffffffff,            /* src_mask */
1113          0xffffffff,            /* dst_mask */
1114          TRUE),                 /* pcrel_offset */
1115
1116   HOWTO (R_ARM_ALU_SB_G1,       /* type */
1117          0,                     /* rightshift */
1118          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1119          32,                    /* bitsize */
1120          TRUE,                  /* pc_relative */
1121          0,                     /* bitpos */
1122          complain_overflow_dont,/* complain_on_overflow */
1123          bfd_elf_generic_reloc, /* special_function */
1124          "R_ARM_ALU_SB_G1",     /* name */
1125          FALSE,                 /* partial_inplace */
1126          0xffffffff,            /* src_mask */
1127          0xffffffff,            /* dst_mask */
1128          TRUE),                 /* pcrel_offset */
1129
1130   HOWTO (R_ARM_ALU_SB_G2,       /* type */
1131          0,                     /* rightshift */
1132          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1133          32,                    /* bitsize */
1134          TRUE,                  /* pc_relative */
1135          0,                     /* bitpos */
1136          complain_overflow_dont,/* complain_on_overflow */
1137          bfd_elf_generic_reloc, /* special_function */
1138          "R_ARM_ALU_SB_G2",     /* name */
1139          FALSE,                 /* partial_inplace */
1140          0xffffffff,            /* src_mask */
1141          0xffffffff,            /* dst_mask */
1142          TRUE),                 /* pcrel_offset */
1143
1144   HOWTO (R_ARM_LDR_SB_G0,       /* type */
1145          0,                     /* rightshift */
1146          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1147          32,                    /* bitsize */
1148          TRUE,                  /* pc_relative */
1149          0,                     /* bitpos */
1150          complain_overflow_dont,/* complain_on_overflow */
1151          bfd_elf_generic_reloc, /* special_function */
1152          "R_ARM_LDR_SB_G0",     /* name */
1153          FALSE,                 /* partial_inplace */
1154          0xffffffff,            /* src_mask */
1155          0xffffffff,            /* dst_mask */
1156          TRUE),                 /* pcrel_offset */
1157
1158   HOWTO (R_ARM_LDR_SB_G1,       /* type */
1159          0,                     /* rightshift */
1160          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1161          32,                    /* bitsize */
1162          TRUE,                  /* pc_relative */
1163          0,                     /* bitpos */
1164          complain_overflow_dont,/* complain_on_overflow */
1165          bfd_elf_generic_reloc, /* special_function */
1166          "R_ARM_LDR_SB_G1",     /* name */
1167          FALSE,                 /* partial_inplace */
1168          0xffffffff,            /* src_mask */
1169          0xffffffff,            /* dst_mask */
1170          TRUE),                 /* pcrel_offset */
1171
1172   HOWTO (R_ARM_LDR_SB_G2,       /* type */
1173          0,                     /* rightshift */
1174          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1175          32,                    /* bitsize */
1176          TRUE,                  /* pc_relative */
1177          0,                     /* bitpos */
1178          complain_overflow_dont,/* complain_on_overflow */
1179          bfd_elf_generic_reloc, /* special_function */
1180          "R_ARM_LDR_SB_G2",     /* name */
1181          FALSE,                 /* partial_inplace */
1182          0xffffffff,            /* src_mask */
1183          0xffffffff,            /* dst_mask */
1184          TRUE),                 /* pcrel_offset */
1185
1186   HOWTO (R_ARM_LDRS_SB_G0,      /* type */
1187          0,                     /* rightshift */
1188          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1189          32,                    /* bitsize */
1190          TRUE,                  /* pc_relative */
1191          0,                     /* bitpos */
1192          complain_overflow_dont,/* complain_on_overflow */
1193          bfd_elf_generic_reloc, /* special_function */
1194          "R_ARM_LDRS_SB_G0",    /* name */
1195          FALSE,                 /* partial_inplace */
1196          0xffffffff,            /* src_mask */
1197          0xffffffff,            /* dst_mask */
1198          TRUE),                 /* pcrel_offset */
1199
1200   HOWTO (R_ARM_LDRS_SB_G1,      /* type */
1201          0,                     /* rightshift */
1202          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1203          32,                    /* bitsize */
1204          TRUE,                  /* pc_relative */
1205          0,                     /* bitpos */
1206          complain_overflow_dont,/* complain_on_overflow */
1207          bfd_elf_generic_reloc, /* special_function */
1208          "R_ARM_LDRS_SB_G1",    /* name */
1209          FALSE,                 /* partial_inplace */
1210          0xffffffff,            /* src_mask */
1211          0xffffffff,            /* dst_mask */
1212          TRUE),                 /* pcrel_offset */
1213
1214   HOWTO (R_ARM_LDRS_SB_G2,      /* type */
1215          0,                     /* rightshift */
1216          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1217          32,                    /* bitsize */
1218          TRUE,                  /* pc_relative */
1219          0,                     /* bitpos */
1220          complain_overflow_dont,/* complain_on_overflow */
1221          bfd_elf_generic_reloc, /* special_function */
1222          "R_ARM_LDRS_SB_G2",    /* name */
1223          FALSE,                 /* partial_inplace */
1224          0xffffffff,            /* src_mask */
1225          0xffffffff,            /* dst_mask */
1226          TRUE),                 /* pcrel_offset */
1227
1228   HOWTO (R_ARM_LDC_SB_G0,       /* type */
1229          0,                     /* rightshift */
1230          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1231          32,                    /* bitsize */
1232          TRUE,                  /* pc_relative */
1233          0,                     /* bitpos */
1234          complain_overflow_dont,/* complain_on_overflow */
1235          bfd_elf_generic_reloc, /* special_function */
1236          "R_ARM_LDC_SB_G0",     /* name */
1237          FALSE,                 /* partial_inplace */
1238          0xffffffff,            /* src_mask */
1239          0xffffffff,            /* dst_mask */
1240          TRUE),                 /* pcrel_offset */
1241
1242   HOWTO (R_ARM_LDC_SB_G1,       /* type */
1243          0,                     /* rightshift */
1244          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1245          32,                    /* bitsize */
1246          TRUE,                  /* pc_relative */
1247          0,                     /* bitpos */
1248          complain_overflow_dont,/* complain_on_overflow */
1249          bfd_elf_generic_reloc, /* special_function */
1250          "R_ARM_LDC_SB_G1",     /* name */
1251          FALSE,                 /* partial_inplace */
1252          0xffffffff,            /* src_mask */
1253          0xffffffff,            /* dst_mask */
1254          TRUE),                 /* pcrel_offset */
1255
1256   HOWTO (R_ARM_LDC_SB_G2,       /* type */
1257          0,                     /* rightshift */
1258          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1259          32,                    /* bitsize */
1260          TRUE,                  /* pc_relative */
1261          0,                     /* bitpos */
1262          complain_overflow_dont,/* complain_on_overflow */
1263          bfd_elf_generic_reloc, /* special_function */
1264          "R_ARM_LDC_SB_G2",     /* name */
1265          FALSE,                 /* partial_inplace */
1266          0xffffffff,            /* src_mask */
1267          0xffffffff,            /* dst_mask */
1268          TRUE),                 /* pcrel_offset */
1269
1270   /* End of group relocations.  */
1271
1272   HOWTO (R_ARM_MOVW_BREL_NC,    /* type */
1273          0,                     /* rightshift */
1274          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1275          16,                    /* bitsize */
1276          FALSE,                 /* pc_relative */
1277          0,                     /* bitpos */
1278          complain_overflow_dont,/* complain_on_overflow */
1279          bfd_elf_generic_reloc, /* special_function */
1280          "R_ARM_MOVW_BREL_NC",  /* name */
1281          FALSE,                 /* partial_inplace */
1282          0x0000ffff,            /* src_mask */
1283          0x0000ffff,            /* dst_mask */
1284          FALSE),                /* pcrel_offset */
1285
1286   HOWTO (R_ARM_MOVT_BREL,       /* type */
1287          0,                     /* rightshift */
1288          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1289          16,                    /* bitsize */
1290          FALSE,                 /* pc_relative */
1291          0,                     /* bitpos */
1292          complain_overflow_bitfield,/* complain_on_overflow */
1293          bfd_elf_generic_reloc, /* special_function */
1294          "R_ARM_MOVT_BREL",     /* name */
1295          FALSE,                 /* partial_inplace */
1296          0x0000ffff,            /* src_mask */
1297          0x0000ffff,            /* dst_mask */
1298          FALSE),                /* pcrel_offset */
1299
1300   HOWTO (R_ARM_MOVW_BREL,       /* type */
1301          0,                     /* rightshift */
1302          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1303          16,                    /* bitsize */
1304          FALSE,                 /* pc_relative */
1305          0,                     /* bitpos */
1306          complain_overflow_dont,/* complain_on_overflow */
1307          bfd_elf_generic_reloc, /* special_function */
1308          "R_ARM_MOVW_BREL",     /* name */
1309          FALSE,                 /* partial_inplace */
1310          0x0000ffff,            /* src_mask */
1311          0x0000ffff,            /* dst_mask */
1312          FALSE),                /* pcrel_offset */
1313
1314   HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
1315          0,                     /* rightshift */
1316          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1317          16,                    /* bitsize */
1318          FALSE,                 /* pc_relative */
1319          0,                     /* bitpos */
1320          complain_overflow_dont,/* complain_on_overflow */
1321          bfd_elf_generic_reloc, /* special_function */
1322          "R_ARM_THM_MOVW_BREL_NC",/* name */
1323          FALSE,                 /* partial_inplace */
1324          0x040f70ff,            /* src_mask */
1325          0x040f70ff,            /* dst_mask */
1326          FALSE),                /* pcrel_offset */
1327
1328   HOWTO (R_ARM_THM_MOVT_BREL,   /* type */
1329          0,                     /* rightshift */
1330          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1331          16,                    /* bitsize */
1332          FALSE,                 /* pc_relative */
1333          0,                     /* bitpos */
1334          complain_overflow_bitfield,/* complain_on_overflow */
1335          bfd_elf_generic_reloc, /* special_function */
1336          "R_ARM_THM_MOVT_BREL", /* name */
1337          FALSE,                 /* partial_inplace */
1338          0x040f70ff,            /* src_mask */
1339          0x040f70ff,            /* dst_mask */
1340          FALSE),                /* pcrel_offset */
1341
1342   HOWTO (R_ARM_THM_MOVW_BREL,   /* type */
1343          0,                     /* rightshift */
1344          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1345          16,                    /* bitsize */
1346          FALSE,                 /* pc_relative */
1347          0,                     /* bitpos */
1348          complain_overflow_dont,/* complain_on_overflow */
1349          bfd_elf_generic_reloc, /* special_function */
1350          "R_ARM_THM_MOVW_BREL", /* name */
1351          FALSE,                 /* partial_inplace */
1352          0x040f70ff,            /* src_mask */
1353          0x040f70ff,            /* dst_mask */
1354          FALSE),                /* pcrel_offset */
1355
1356   HOWTO (R_ARM_TLS_GOTDESC,     /* type */
1357          0,                     /* rightshift */
1358          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1359          32,                    /* bitsize */
1360          FALSE,                 /* pc_relative */
1361          0,                     /* bitpos */
1362          complain_overflow_bitfield,/* complain_on_overflow */
1363          NULL,                  /* special_function */
1364          "R_ARM_TLS_GOTDESC",   /* name */
1365          TRUE,                  /* partial_inplace */
1366          0xffffffff,            /* src_mask */
1367          0xffffffff,            /* dst_mask */
1368          FALSE),                /* pcrel_offset */
1369
1370   HOWTO (R_ARM_TLS_CALL,        /* type */
1371          0,                     /* rightshift */
1372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1373          24,                    /* bitsize */
1374          FALSE,                 /* pc_relative */
1375          0,                     /* bitpos */
1376          complain_overflow_dont,/* complain_on_overflow */
1377          bfd_elf_generic_reloc, /* special_function */
1378          "R_ARM_TLS_CALL",      /* name */
1379          FALSE,                 /* partial_inplace */
1380          0x00ffffff,            /* src_mask */
1381          0x00ffffff,            /* dst_mask */
1382          FALSE),                /* pcrel_offset */
1383
1384   HOWTO (R_ARM_TLS_DESCSEQ,     /* type */
1385          0,                     /* rightshift */
1386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1387          0,                     /* bitsize */
1388          FALSE,                 /* pc_relative */
1389          0,                     /* bitpos */
1390          complain_overflow_bitfield,/* complain_on_overflow */
1391          bfd_elf_generic_reloc, /* special_function */
1392          "R_ARM_TLS_DESCSEQ",   /* name */
1393          FALSE,                 /* partial_inplace */
1394          0x00000000,            /* src_mask */
1395          0x00000000,            /* dst_mask */
1396          FALSE),                /* pcrel_offset */
1397
1398   HOWTO (R_ARM_THM_TLS_CALL,    /* type */
1399          0,                     /* rightshift */
1400          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1401          24,                    /* bitsize */
1402          FALSE,                 /* pc_relative */
1403          0,                     /* bitpos */
1404          complain_overflow_dont,/* complain_on_overflow */
1405          bfd_elf_generic_reloc, /* special_function */
1406          "R_ARM_THM_TLS_CALL",  /* name */
1407          FALSE,                 /* partial_inplace */
1408          0x07ff07ff,            /* src_mask */
1409          0x07ff07ff,            /* dst_mask */
1410          FALSE),                /* pcrel_offset */
1411
1412   HOWTO (R_ARM_PLT32_ABS,       /* type */
1413          0,                     /* rightshift */
1414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1415          32,                    /* bitsize */
1416          FALSE,                 /* pc_relative */
1417          0,                     /* bitpos */
1418          complain_overflow_dont,/* complain_on_overflow */
1419          bfd_elf_generic_reloc, /* special_function */
1420          "R_ARM_PLT32_ABS",     /* name */
1421          FALSE,                 /* partial_inplace */
1422          0xffffffff,            /* src_mask */
1423          0xffffffff,            /* dst_mask */
1424          FALSE),                /* pcrel_offset */
1425
1426   HOWTO (R_ARM_GOT_ABS,         /* type */
1427          0,                     /* rightshift */
1428          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1429          32,                    /* bitsize */
1430          FALSE,                 /* pc_relative */
1431          0,                     /* bitpos */
1432          complain_overflow_dont,/* complain_on_overflow */
1433          bfd_elf_generic_reloc, /* special_function */
1434          "R_ARM_GOT_ABS",       /* name */
1435          FALSE,                 /* partial_inplace */
1436          0xffffffff,            /* src_mask */
1437          0xffffffff,            /* dst_mask */
1438          FALSE),                        /* pcrel_offset */
1439
1440   HOWTO (R_ARM_GOT_PREL,        /* type */
1441          0,                     /* rightshift */
1442          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1443          32,                    /* bitsize */
1444          TRUE,                  /* pc_relative */
1445          0,                     /* bitpos */
1446          complain_overflow_dont,        /* complain_on_overflow */
1447          bfd_elf_generic_reloc, /* special_function */
1448          "R_ARM_GOT_PREL",      /* name */
1449          FALSE,                 /* partial_inplace */
1450          0xffffffff,            /* src_mask */
1451          0xffffffff,            /* dst_mask */
1452          TRUE),                 /* pcrel_offset */
1453
1454   HOWTO (R_ARM_GOT_BREL12,      /* type */
1455          0,                     /* rightshift */
1456          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1457          12,                    /* bitsize */
1458          FALSE,                 /* pc_relative */
1459          0,                     /* bitpos */
1460          complain_overflow_bitfield,/* complain_on_overflow */
1461          bfd_elf_generic_reloc, /* special_function */
1462          "R_ARM_GOT_BREL12",    /* name */
1463          FALSE,                 /* partial_inplace */
1464          0x00000fff,            /* src_mask */
1465          0x00000fff,            /* dst_mask */
1466          FALSE),                /* pcrel_offset */
1467
1468   HOWTO (R_ARM_GOTOFF12,        /* type */
1469          0,                     /* rightshift */
1470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1471          12,                    /* bitsize */
1472          FALSE,                 /* pc_relative */
1473          0,                     /* bitpos */
1474          complain_overflow_bitfield,/* complain_on_overflow */
1475          bfd_elf_generic_reloc, /* special_function */
1476          "R_ARM_GOTOFF12",      /* name */
1477          FALSE,                 /* partial_inplace */
1478          0x00000fff,            /* src_mask */
1479          0x00000fff,            /* dst_mask */
1480          FALSE),                /* pcrel_offset */
1481
1482   EMPTY_HOWTO (R_ARM_GOTRELAX),  /* reserved for future GOT-load optimizations */
1483
1484   /* GNU extension to record C++ vtable member usage */
1485   HOWTO (R_ARM_GNU_VTENTRY,     /* type */
1486          0,                     /* rightshift */
1487          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1488          0,                     /* bitsize */
1489          FALSE,                 /* pc_relative */
1490          0,                     /* bitpos */
1491          complain_overflow_dont, /* complain_on_overflow */
1492          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
1493          "R_ARM_GNU_VTENTRY",   /* name */
1494          FALSE,                 /* partial_inplace */
1495          0,                     /* src_mask */
1496          0,                     /* dst_mask */
1497          FALSE),                /* pcrel_offset */
1498
1499   /* GNU extension to record C++ vtable hierarchy */
1500   HOWTO (R_ARM_GNU_VTINHERIT, /* type */
1501          0,                     /* rightshift */
1502          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1503          0,                     /* bitsize */
1504          FALSE,                 /* pc_relative */
1505          0,                     /* bitpos */
1506          complain_overflow_dont, /* complain_on_overflow */
1507          NULL,                  /* special_function */
1508          "R_ARM_GNU_VTINHERIT", /* name */
1509          FALSE,                 /* partial_inplace */
1510          0,                     /* src_mask */
1511          0,                     /* dst_mask */
1512          FALSE),                /* pcrel_offset */
1513
1514   HOWTO (R_ARM_THM_JUMP11,      /* type */
1515          1,                     /* rightshift */
1516          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1517          11,                    /* bitsize */
1518          TRUE,                  /* pc_relative */
1519          0,                     /* bitpos */
1520          complain_overflow_signed,      /* complain_on_overflow */
1521          bfd_elf_generic_reloc, /* special_function */
1522          "R_ARM_THM_JUMP11",    /* name */
1523          FALSE,                 /* partial_inplace */
1524          0x000007ff,            /* src_mask */
1525          0x000007ff,            /* dst_mask */
1526          TRUE),                 /* pcrel_offset */
1527
1528   HOWTO (R_ARM_THM_JUMP8,       /* type */
1529          1,                     /* rightshift */
1530          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1531          8,                     /* bitsize */
1532          TRUE,                  /* pc_relative */
1533          0,                     /* bitpos */
1534          complain_overflow_signed,      /* complain_on_overflow */
1535          bfd_elf_generic_reloc, /* special_function */
1536          "R_ARM_THM_JUMP8",     /* name */
1537          FALSE,                 /* partial_inplace */
1538          0x000000ff,            /* src_mask */
1539          0x000000ff,            /* dst_mask */
1540          TRUE),                 /* pcrel_offset */
1541
1542   /* TLS relocations */
1543   HOWTO (R_ARM_TLS_GD32,        /* type */
1544          0,                     /* rightshift */
1545          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1546          32,                    /* bitsize */
1547          FALSE,                 /* pc_relative */
1548          0,                     /* bitpos */
1549          complain_overflow_bitfield,/* complain_on_overflow */
1550          NULL,                  /* special_function */
1551          "R_ARM_TLS_GD32",      /* name */
1552          TRUE,                  /* partial_inplace */
1553          0xffffffff,            /* src_mask */
1554          0xffffffff,            /* dst_mask */
1555          FALSE),                /* pcrel_offset */
1556
1557   HOWTO (R_ARM_TLS_LDM32,       /* type */
1558          0,                     /* rightshift */
1559          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1560          32,                    /* bitsize */
1561          FALSE,                 /* pc_relative */
1562          0,                     /* bitpos */
1563          complain_overflow_bitfield,/* complain_on_overflow */
1564          bfd_elf_generic_reloc, /* special_function */
1565          "R_ARM_TLS_LDM32",     /* name */
1566          TRUE,                  /* partial_inplace */
1567          0xffffffff,            /* src_mask */
1568          0xffffffff,            /* dst_mask */
1569          FALSE),                /* pcrel_offset */
1570
1571   HOWTO (R_ARM_TLS_LDO32,       /* type */
1572          0,                     /* rightshift */
1573          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1574          32,                    /* bitsize */
1575          FALSE,                 /* pc_relative */
1576          0,                     /* bitpos */
1577          complain_overflow_bitfield,/* complain_on_overflow */
1578          bfd_elf_generic_reloc, /* special_function */
1579          "R_ARM_TLS_LDO32",     /* name */
1580          TRUE,                  /* partial_inplace */
1581          0xffffffff,            /* src_mask */
1582          0xffffffff,            /* dst_mask */
1583          FALSE),                /* pcrel_offset */
1584
1585   HOWTO (R_ARM_TLS_IE32,        /* type */
1586          0,                     /* rightshift */
1587          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1588          32,                    /* bitsize */
1589          FALSE,                  /* pc_relative */
1590          0,                     /* bitpos */
1591          complain_overflow_bitfield,/* complain_on_overflow */
1592          NULL,                  /* special_function */
1593          "R_ARM_TLS_IE32",      /* name */
1594          TRUE,                  /* partial_inplace */
1595          0xffffffff,            /* src_mask */
1596          0xffffffff,            /* dst_mask */
1597          FALSE),                /* pcrel_offset */
1598
1599   HOWTO (R_ARM_TLS_LE32,        /* type */
1600          0,                     /* rightshift */
1601          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1602          32,                    /* bitsize */
1603          FALSE,                 /* pc_relative */
1604          0,                     /* bitpos */
1605          complain_overflow_bitfield,/* complain_on_overflow */
1606          bfd_elf_generic_reloc, /* special_function */
1607          "R_ARM_TLS_LE32",      /* name */
1608          TRUE,                  /* partial_inplace */
1609          0xffffffff,            /* src_mask */
1610          0xffffffff,            /* dst_mask */
1611          FALSE),                /* pcrel_offset */
1612
1613   HOWTO (R_ARM_TLS_LDO12,       /* type */
1614          0,                     /* rightshift */
1615          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1616          12,                    /* bitsize */
1617          FALSE,                 /* pc_relative */
1618          0,                     /* bitpos */
1619          complain_overflow_bitfield,/* complain_on_overflow */
1620          bfd_elf_generic_reloc, /* special_function */
1621          "R_ARM_TLS_LDO12",     /* name */
1622          FALSE,                 /* partial_inplace */
1623          0x00000fff,            /* src_mask */
1624          0x00000fff,            /* dst_mask */
1625          FALSE),                /* pcrel_offset */
1626
1627   HOWTO (R_ARM_TLS_LE12,        /* type */
1628          0,                     /* rightshift */
1629          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1630          12,                    /* bitsize */
1631          FALSE,                 /* pc_relative */
1632          0,                     /* bitpos */
1633          complain_overflow_bitfield,/* complain_on_overflow */
1634          bfd_elf_generic_reloc, /* special_function */
1635          "R_ARM_TLS_LE12",      /* name */
1636          FALSE,                 /* partial_inplace */
1637          0x00000fff,            /* src_mask */
1638          0x00000fff,            /* dst_mask */
1639          FALSE),                /* pcrel_offset */
1640
1641   HOWTO (R_ARM_TLS_IE12GP,      /* type */
1642          0,                     /* rightshift */
1643          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1644          12,                    /* bitsize */
1645          FALSE,                 /* pc_relative */
1646          0,                     /* bitpos */
1647          complain_overflow_bitfield,/* complain_on_overflow */
1648          bfd_elf_generic_reloc, /* special_function */
1649          "R_ARM_TLS_IE12GP",    /* name */
1650          FALSE,                 /* partial_inplace */
1651          0x00000fff,            /* src_mask */
1652          0x00000fff,            /* dst_mask */
1653          FALSE),                /* pcrel_offset */
1654
1655   /* 112-127 private relocations.  */
1656   EMPTY_HOWTO (112),
1657   EMPTY_HOWTO (113),
1658   EMPTY_HOWTO (114),
1659   EMPTY_HOWTO (115),
1660   EMPTY_HOWTO (116),
1661   EMPTY_HOWTO (117),
1662   EMPTY_HOWTO (118),
1663   EMPTY_HOWTO (119),
1664   EMPTY_HOWTO (120),
1665   EMPTY_HOWTO (121),
1666   EMPTY_HOWTO (122),
1667   EMPTY_HOWTO (123),
1668   EMPTY_HOWTO (124),
1669   EMPTY_HOWTO (125),
1670   EMPTY_HOWTO (126),
1671   EMPTY_HOWTO (127),
1672
1673   /* R_ARM_ME_TOO, obsolete.  */
1674   EMPTY_HOWTO (128),
1675
1676   HOWTO (R_ARM_THM_TLS_DESCSEQ, /* type */
1677          0,                     /* rightshift */
1678          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1679          0,                     /* bitsize */
1680          FALSE,                 /* pc_relative */
1681          0,                     /* bitpos */
1682          complain_overflow_bitfield,/* complain_on_overflow */
1683          bfd_elf_generic_reloc, /* special_function */
1684          "R_ARM_THM_TLS_DESCSEQ",/* name */
1685          FALSE,                 /* partial_inplace */
1686          0x00000000,            /* src_mask */
1687          0x00000000,            /* dst_mask */
1688          FALSE),                /* pcrel_offset */
1689 };
1690
1691 /* 160 onwards: */
1692 static reloc_howto_type elf32_arm_howto_table_2[1] =
1693 {
1694   HOWTO (R_ARM_IRELATIVE,       /* type */
1695          0,                     /* rightshift */
1696          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1697          32,                    /* bitsize */
1698          FALSE,                 /* pc_relative */
1699          0,                     /* bitpos */
1700          complain_overflow_bitfield,/* complain_on_overflow */
1701          bfd_elf_generic_reloc, /* special_function */
1702          "R_ARM_IRELATIVE",     /* name */
1703          TRUE,                  /* partial_inplace */
1704          0xffffffff,            /* src_mask */
1705          0xffffffff,            /* dst_mask */
1706          FALSE)                 /* pcrel_offset */
1707 };
1708
1709 /* 249-255 extended, currently unused, relocations:  */
1710 static reloc_howto_type elf32_arm_howto_table_3[4] =
1711 {
1712   HOWTO (R_ARM_RREL32,          /* type */
1713          0,                     /* rightshift */
1714          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1715          0,                     /* bitsize */
1716          FALSE,                 /* pc_relative */
1717          0,                     /* bitpos */
1718          complain_overflow_dont,/* complain_on_overflow */
1719          bfd_elf_generic_reloc, /* special_function */
1720          "R_ARM_RREL32",        /* name */
1721          FALSE,                 /* partial_inplace */
1722          0,                     /* src_mask */
1723          0,                     /* dst_mask */
1724          FALSE),                /* pcrel_offset */
1725
1726   HOWTO (R_ARM_RABS32,          /* type */
1727          0,                     /* rightshift */
1728          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1729          0,                     /* bitsize */
1730          FALSE,                 /* pc_relative */
1731          0,                     /* bitpos */
1732          complain_overflow_dont,/* complain_on_overflow */
1733          bfd_elf_generic_reloc, /* special_function */
1734          "R_ARM_RABS32",        /* name */
1735          FALSE,                 /* partial_inplace */
1736          0,                     /* src_mask */
1737          0,                     /* dst_mask */
1738          FALSE),                /* pcrel_offset */
1739
1740   HOWTO (R_ARM_RPC24,           /* type */
1741          0,                     /* rightshift */
1742          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1743          0,                     /* bitsize */
1744          FALSE,                 /* pc_relative */
1745          0,                     /* bitpos */
1746          complain_overflow_dont,/* complain_on_overflow */
1747          bfd_elf_generic_reloc, /* special_function */
1748          "R_ARM_RPC24",         /* name */
1749          FALSE,                 /* partial_inplace */
1750          0,                     /* src_mask */
1751          0,                     /* dst_mask */
1752          FALSE),                /* pcrel_offset */
1753
1754   HOWTO (R_ARM_RBASE,           /* type */
1755          0,                     /* rightshift */
1756          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1757          0,                     /* bitsize */
1758          FALSE,                 /* pc_relative */
1759          0,                     /* bitpos */
1760          complain_overflow_dont,/* complain_on_overflow */
1761          bfd_elf_generic_reloc, /* special_function */
1762          "R_ARM_RBASE",         /* name */
1763          FALSE,                 /* partial_inplace */
1764          0,                     /* src_mask */
1765          0,                     /* dst_mask */
1766          FALSE)                 /* pcrel_offset */
1767 };
1768
1769 static reloc_howto_type *
1770 elf32_arm_howto_from_type (unsigned int r_type)
1771 {
1772   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1773     return &elf32_arm_howto_table_1[r_type];
1774
1775   if (r_type == R_ARM_IRELATIVE)
1776     return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
1777
1778   if (r_type >= R_ARM_RREL32
1779       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
1780     return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1781
1782   return NULL;
1783 }
1784
1785 static void
1786 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
1787                          Elf_Internal_Rela * elf_reloc)
1788 {
1789   unsigned int r_type;
1790
1791   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1792   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
1793 }
1794
1795 struct elf32_arm_reloc_map
1796   {
1797     bfd_reloc_code_real_type  bfd_reloc_val;
1798     unsigned char             elf_reloc_val;
1799   };
1800
1801 /* All entries in this list must also be present in elf32_arm_howto_table.  */
1802 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1803   {
1804     {BFD_RELOC_NONE,                 R_ARM_NONE},
1805     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1806     {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
1807     {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
1808     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1809     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1810     {BFD_RELOC_32,                   R_ARM_ABS32},
1811     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1812     {BFD_RELOC_8,                    R_ARM_ABS8},
1813     {BFD_RELOC_16,                   R_ARM_ABS16},
1814     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1815     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1816     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1817     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1818     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1819     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1820     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1821     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1822     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1823     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1824     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1825     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1826     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1827     {BFD_RELOC_ARM_GOT_PREL,         R_ARM_GOT_PREL},
1828     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1829     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1830     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
1831     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
1832     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
1833     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
1834     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
1835     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1836     {BFD_RELOC_ARM_TLS_GOTDESC,      R_ARM_TLS_GOTDESC},
1837     {BFD_RELOC_ARM_TLS_CALL,         R_ARM_TLS_CALL},
1838     {BFD_RELOC_ARM_THM_TLS_CALL,     R_ARM_THM_TLS_CALL},
1839     {BFD_RELOC_ARM_TLS_DESCSEQ,      R_ARM_TLS_DESCSEQ},
1840     {BFD_RELOC_ARM_THM_TLS_DESCSEQ,  R_ARM_THM_TLS_DESCSEQ},
1841     {BFD_RELOC_ARM_TLS_DESC,         R_ARM_TLS_DESC},
1842     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
1843     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
1844     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
1845     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
1846     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
1847     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
1848     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
1849     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
1850     {BFD_RELOC_ARM_IRELATIVE,        R_ARM_IRELATIVE},
1851     {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
1852     {BFD_RELOC_VTABLE_ENTRY,         R_ARM_GNU_VTENTRY},
1853     {BFD_RELOC_ARM_MOVW,             R_ARM_MOVW_ABS_NC},
1854     {BFD_RELOC_ARM_MOVT,             R_ARM_MOVT_ABS},
1855     {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
1856     {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
1857     {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
1858     {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
1859     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
1860     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
1861     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
1862     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
1863     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
1864     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
1865     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
1866     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
1867     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
1868     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
1869     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
1870     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
1871     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
1872     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
1873     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
1874     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
1875     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
1876     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
1877     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
1878     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
1879     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
1880     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
1881     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
1882     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
1883     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
1884     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
1885     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
1886     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
1887     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
1888     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
1889     {BFD_RELOC_ARM_V4BX,             R_ARM_V4BX}
1890   };
1891
1892 static reloc_howto_type *
1893 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1894                              bfd_reloc_code_real_type code)
1895 {
1896   unsigned int i;
1897
1898   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
1899     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1900       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
1901
1902   return NULL;
1903 }
1904
1905 static reloc_howto_type *
1906 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1907                              const char *r_name)
1908 {
1909   unsigned int i;
1910
1911   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
1912     if (elf32_arm_howto_table_1[i].name != NULL
1913         && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
1914       return &elf32_arm_howto_table_1[i];
1915
1916   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
1917     if (elf32_arm_howto_table_2[i].name != NULL
1918         && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
1919       return &elf32_arm_howto_table_2[i];
1920
1921   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_3); i++)
1922     if (elf32_arm_howto_table_3[i].name != NULL
1923         && strcasecmp (elf32_arm_howto_table_3[i].name, r_name) == 0)
1924       return &elf32_arm_howto_table_3[i];
1925
1926   return NULL;
1927 }
1928
1929 /* Support for core dump NOTE sections.  */
1930
1931 static bfd_boolean
1932 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1933 {
1934   int offset;
1935   size_t size;
1936
1937   switch (note->descsz)
1938     {
1939       default:
1940         return FALSE;
1941
1942       case 148:         /* Linux/ARM 32-bit.  */
1943         /* pr_cursig */
1944         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1945
1946         /* pr_pid */
1947         elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
1948
1949         /* pr_reg */
1950         offset = 72;
1951         size = 72;
1952
1953         break;
1954     }
1955
1956   /* Make a ".reg/999" section.  */
1957   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1958                                           size, note->descpos + offset);
1959 }
1960
1961 static bfd_boolean
1962 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1963 {
1964   switch (note->descsz)
1965     {
1966       default:
1967         return FALSE;
1968
1969       case 124:         /* Linux/ARM elf_prpsinfo.  */
1970         elf_tdata (abfd)->core_pid
1971          = bfd_get_32 (abfd, note->descdata + 12);
1972         elf_tdata (abfd)->core_program
1973          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1974         elf_tdata (abfd)->core_command
1975          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1976     }
1977
1978   /* Note that for some reason, a spurious space is tacked
1979      onto the end of the args in some (at least one anyway)
1980      implementations, so strip it off if it exists.  */
1981   {
1982     char *command = elf_tdata (abfd)->core_command;
1983     int n = strlen (command);
1984
1985     if (0 < n && command[n - 1] == ' ')
1986       command[n - 1] = '\0';
1987   }
1988
1989   return TRUE;
1990 }
1991
1992 static char *
1993 elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
1994                                 int note_type, ...)
1995 {
1996   switch (note_type)
1997     {
1998     default:
1999       return NULL;
2000
2001     case NT_PRPSINFO:
2002       {
2003         char data[124];
2004         va_list ap;
2005
2006         va_start (ap, note_type);
2007         memset (data, 0, sizeof (data));
2008         strncpy (data + 28, va_arg (ap, const char *), 16);
2009         strncpy (data + 44, va_arg (ap, const char *), 80);
2010         va_end (ap);
2011
2012         return elfcore_write_note (abfd, buf, bufsiz,
2013                                    "CORE", note_type, data, sizeof (data));
2014       }
2015
2016     case NT_PRSTATUS:
2017       {
2018         char data[148];
2019         va_list ap;
2020         long pid;
2021         int cursig;
2022         const void *greg;
2023
2024         va_start (ap, note_type);
2025         memset (data, 0, sizeof (data));
2026         pid = va_arg (ap, long);
2027         bfd_put_32 (abfd, pid, data + 24);
2028         cursig = va_arg (ap, int);
2029         bfd_put_16 (abfd, cursig, data + 12);
2030         greg = va_arg (ap, const void *);
2031         memcpy (data + 72, greg, 72);
2032         va_end (ap);
2033
2034         return elfcore_write_note (abfd, buf, bufsiz,
2035                                    "CORE", note_type, data, sizeof (data));
2036       }
2037     }
2038 }
2039
2040 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
2041 #define TARGET_LITTLE_NAME              "elf32-littlearm"
2042 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
2043 #define TARGET_BIG_NAME                 "elf32-bigarm"
2044
2045 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
2046 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
2047 #define elf_backend_write_core_note     elf32_arm_nabi_write_core_note
2048
2049 typedef unsigned long int insn32;
2050 typedef unsigned short int insn16;
2051
2052 /* In lieu of proper flags, assume all EABIv4 or later objects are
2053    interworkable.  */
2054 #define INTERWORK_FLAG(abfd)  \
2055   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
2056   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
2057   || ((abfd)->flags & BFD_LINKER_CREATED))
2058
2059 /* The linker script knows the section names for placement.
2060    The entry_names are used to do simple name mangling on the stubs.
2061    Given a function name, and its type, the stub can be found. The
2062    name can be changed. The only requirement is the %s be present.  */
2063 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
2064 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
2065
2066 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
2067 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
2068
2069 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
2070 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
2071
2072 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
2073 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
2074
2075 #define STUB_ENTRY_NAME   "__%s_veneer"
2076
2077 /* The name of the dynamic interpreter.  This is put in the .interp
2078    section.  */
2079 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
2080
2081 static const unsigned long tls_trampoline [] =
2082 {
2083   0xe08e0000,           /* add r0, lr, r0 */
2084   0xe5901004,           /* ldr r1, [r0,#4] */
2085   0xe12fff11,           /* bx  r1 */
2086 };
2087
2088 static const unsigned long dl_tlsdesc_lazy_trampoline [] =
2089 {
2090   0xe52d2004, /*        push    {r2}                    */
2091   0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8]     */
2092   0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8]     */
2093   0xe79f2002, /* 1:   ldr     r2, [pc, r2]              */
2094   0xe081100f, /* 2:   add     r1, pc                    */
2095   0xe12fff12, /*      bx      r2                        */
2096   0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
2097                                 + dl_tlsdesc_lazy_resolver(GOT)   */
2098   0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
2099 };
2100
2101 #ifdef FOUR_WORD_PLT
2102
2103 /* The first entry in a procedure linkage table looks like
2104    this.  It is set up so that any shared library function that is
2105    called before the relocation has been set up calls the dynamic
2106    linker first.  */
2107 static const bfd_vma elf32_arm_plt0_entry [] =
2108 {
2109   0xe52de004,           /* str   lr, [sp, #-4]! */
2110   0xe59fe010,           /* ldr   lr, [pc, #16]  */
2111   0xe08fe00e,           /* add   lr, pc, lr     */
2112   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2113 };
2114
2115 /* Subsequent entries in a procedure linkage table look like
2116    this.  */
2117 static const bfd_vma elf32_arm_plt_entry [] =
2118 {
2119   0xe28fc600,           /* add   ip, pc, #NN    */
2120   0xe28cca00,           /* add   ip, ip, #NN    */
2121   0xe5bcf000,           /* ldr   pc, [ip, #NN]! */
2122   0x00000000,           /* unused               */
2123 };
2124
2125 #else
2126
2127 /* The first entry in a procedure linkage table looks like
2128    this.  It is set up so that any shared library function that is
2129    called before the relocation has been set up calls the dynamic
2130    linker first.  */
2131 static const bfd_vma elf32_arm_plt0_entry [] =
2132 {
2133   0xe52de004,           /* str   lr, [sp, #-4]! */
2134   0xe59fe004,           /* ldr   lr, [pc, #4]   */
2135   0xe08fe00e,           /* add   lr, pc, lr     */
2136   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2137   0x00000000,           /* &GOT[0] - .          */
2138 };
2139
2140 /* Subsequent entries in a procedure linkage table look like
2141    this.  */
2142 static const bfd_vma elf32_arm_plt_entry [] =
2143 {
2144   0xe28fc600,           /* add   ip, pc, #0xNN00000 */
2145   0xe28cca00,           /* add   ip, ip, #0xNN000   */
2146   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!  */
2147 };
2148
2149 #endif
2150
2151 /* The format of the first entry in the procedure linkage table
2152    for a VxWorks executable.  */
2153 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
2154 {
2155   0xe52dc008,           /* str    ip,[sp,#-8]!                  */
2156   0xe59fc000,           /* ldr    ip,[pc]                       */
2157   0xe59cf008,           /* ldr    pc,[ip,#8]                    */
2158   0x00000000,           /* .long  _GLOBAL_OFFSET_TABLE_         */
2159 };
2160
2161 /* The format of subsequent entries in a VxWorks executable.  */
2162 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
2163 {
2164   0xe59fc000,         /* ldr    ip,[pc]                 */
2165   0xe59cf000,         /* ldr    pc,[ip]                 */
2166   0x00000000,         /* .long  @got                            */
2167   0xe59fc000,         /* ldr    ip,[pc]                 */
2168   0xea000000,         /* b      _PLT                            */
2169   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2170 };
2171
2172 /* The format of entries in a VxWorks shared library.  */
2173 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
2174 {
2175   0xe59fc000,         /* ldr    ip,[pc]                 */
2176   0xe79cf009,         /* ldr    pc,[ip,r9]                      */
2177   0x00000000,         /* .long  @got                            */
2178   0xe59fc000,         /* ldr    ip,[pc]                 */
2179   0xe599f008,         /* ldr    pc,[r9,#8]                      */
2180   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2181 };
2182
2183 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
2184 #define PLT_THUMB_STUB_SIZE 4
2185 static const bfd_vma elf32_arm_plt_thumb_stub [] =
2186 {
2187   0x4778,               /* bx pc */
2188   0x46c0                /* nop   */
2189 };
2190
2191 /* The entries in a PLT when using a DLL-based target with multiple
2192    address spaces.  */
2193 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2194 {
2195   0xe51ff004,         /* ldr   pc, [pc, #-4] */
2196   0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2197 };
2198
2199 /* The first entry in a procedure linkage table looks like
2200    this.  It is set up so that any shared library function that is
2201    called before the relocation has been set up calls the dynamic
2202    linker first.  */
2203 static const bfd_vma elf32_arm_nacl_plt0_entry [] =
2204 {
2205   /* First bundle: */
2206   0xe300c000,           /* movw ip, #:lower16:&GOT[2]-.+8       */
2207   0xe340c000,           /* movt ip, #:upper16:&GOT[2]-.+8       */
2208   0xe08cc00f,           /* add  ip, ip, pc                      */
2209   0xe52dc008,           /* str  ip, [sp, #-8]!                  */
2210   /* Second bundle: */
2211   0xe7dfcf1f,   /* bfc  ip, #30, #2                     */
2212   0xe59cc000,   /* ldr  ip, [ip]                        */
2213   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2214   0xe12fff1c,   /* bx   ip                              */
2215   /* Third bundle: */
2216   0xe320f000,   /* nop                                  */
2217   0xe320f000,   /* nop                                  */
2218   0xe320f000,   /* nop                                  */
2219   /* .Lplt_tail: */
2220   0xe50dc004,           /* str  ip, [sp, #-4]                   */
2221   /* Fourth bundle: */
2222   0xe7dfcf1f,           /* bfc  ip, #30, #2                     */
2223   0xe59cc000,   /* ldr  ip, [ip]                        */
2224   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2225   0xe12fff1c,   /* bx   ip                              */
2226 };
2227 #define ARM_NACL_PLT_TAIL_OFFSET        (11 * 4)
2228
2229 /* Subsequent entries in a procedure linkage table look like this.  */
2230 static const bfd_vma elf32_arm_nacl_plt_entry [] =
2231 {
2232   0xe300c000,           /* movw ip, #:lower16:&GOT[n]-.+8       */
2233   0xe340c000,           /* movt ip, #:upper16:&GOT[n]-.+8       */
2234   0xe08cc00f,           /* add  ip, ip, pc                      */
2235   0xea000000,           /* b    .Lplt_tail                      */
2236 };
2237
2238 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2239 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2240 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2241 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2242 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2243 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2244
2245 enum stub_insn_type
2246 {
2247   THUMB16_TYPE = 1,
2248   THUMB32_TYPE,
2249   ARM_TYPE,
2250   DATA_TYPE
2251 };
2252
2253 #define THUMB16_INSN(X)         {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2254 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2255    is inserted in arm_build_one_stub().  */
2256 #define THUMB16_BCOND_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2257 #define THUMB32_INSN(X)         {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2258 #define THUMB32_B_INSN(X, Z)    {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2259 #define ARM_INSN(X)             {(X), ARM_TYPE, R_ARM_NONE, 0}
2260 #define ARM_REL_INSN(X, Z)      {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2261 #define DATA_WORD(X,Y,Z)        {(X), DATA_TYPE, (Y), (Z)}
2262
2263 typedef struct
2264 {
2265   bfd_vma              data;
2266   enum stub_insn_type  type;
2267   unsigned int         r_type;
2268   int                  reloc_addend;
2269 }  insn_sequence;
2270
2271 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2272    to reach the stub if necessary.  */
2273 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2274 {
2275   ARM_INSN (0xe51ff004),            /* ldr   pc, [pc, #-4] */
2276   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2277 };
2278
2279 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2280    available.  */
2281 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2282 {
2283   ARM_INSN (0xe59fc000),            /* ldr   ip, [pc, #0] */
2284   ARM_INSN (0xe12fff1c),            /* bx    ip */
2285   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2286 };
2287
2288 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2289 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2290 {
2291   THUMB16_INSN (0xb401),             /* push {r0} */
2292   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2293   THUMB16_INSN (0x4684),             /* mov  ip, r0 */
2294   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2295   THUMB16_INSN (0x4760),             /* bx   ip */
2296   THUMB16_INSN (0xbf00),             /* nop */
2297   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2298 };
2299
2300 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
2301    allowed.  */
2302 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2303 {
2304   THUMB16_INSN (0x4778),             /* bx   pc */
2305   THUMB16_INSN (0x46c0),             /* nop */
2306   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2307   ARM_INSN (0xe12fff1c),             /* bx   ip */
2308   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2309 };
2310
2311 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2312    available.  */
2313 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2314 {
2315   THUMB16_INSN (0x4778),             /* bx   pc */
2316   THUMB16_INSN (0x46c0),             /* nop   */
2317   ARM_INSN (0xe51ff004),             /* ldr   pc, [pc, #-4] */
2318   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2319 };
2320
2321 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2322    one, when the destination is close enough.  */
2323 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2324 {
2325   THUMB16_INSN (0x4778),             /* bx   pc */
2326   THUMB16_INSN (0x46c0),             /* nop   */
2327   ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
2328 };
2329
2330 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2331    blx to reach the stub if necessary.  */
2332 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2333 {
2334   ARM_INSN (0xe59fc000),             /* ldr   ip, [pc] */
2335   ARM_INSN (0xe08ff00c),             /* add   pc, pc, ip */
2336   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2337 };
2338
2339 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2340    blx to reach the stub if necessary.  We can not add into pc;
2341    it is not guaranteed to mode switch (different in ARMv6 and
2342    ARMv7).  */
2343 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2344 {
2345   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2346   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2347   ARM_INSN (0xe12fff1c),             /* bx    ip */
2348   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2349 };
2350
2351 /* V4T ARM -> ARM long branch stub, PIC.  */
2352 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2353 {
2354   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2355   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2356   ARM_INSN (0xe12fff1c),             /* bx    ip */
2357   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2358 };
2359
2360 /* V4T Thumb -> ARM long branch stub, PIC.  */
2361 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2362 {
2363   THUMB16_INSN (0x4778),             /* bx   pc */
2364   THUMB16_INSN (0x46c0),             /* nop  */
2365   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2366   ARM_INSN (0xe08cf00f),             /* add  pc, ip, pc */
2367   DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2368 };
2369
2370 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2371    architectures.  */
2372 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2373 {
2374   THUMB16_INSN (0xb401),             /* push {r0} */
2375   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2376   THUMB16_INSN (0x46fc),             /* mov  ip, pc */
2377   THUMB16_INSN (0x4484),             /* add  ip, r0 */
2378   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2379   THUMB16_INSN (0x4760),             /* bx   ip */
2380   DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2381 };
2382
2383 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2384    allowed.  */
2385 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2386 {
2387   THUMB16_INSN (0x4778),             /* bx   pc */
2388   THUMB16_INSN (0x46c0),             /* nop */
2389   ARM_INSN (0xe59fc004),             /* ldr  ip, [pc, #4] */
2390   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2391   ARM_INSN (0xe12fff1c),             /* bx   ip */
2392   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2393 };
2394
2395 /* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
2396    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2397 static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
2398 {
2399   ARM_INSN (0xe59f1000),             /* ldr   r1, [pc] */
2400   ARM_INSN (0xe08ff001),             /* add   pc, pc, r1 */
2401   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2402 };
2403
2404 /* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
2405    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2406 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
2407 {
2408   THUMB16_INSN (0x4778),             /* bx   pc */
2409   THUMB16_INSN (0x46c0),             /* nop */
2410   ARM_INSN (0xe59f1000),             /* ldr  r1, [pc, #0] */
2411   ARM_INSN (0xe081f00f),             /* add  pc, r1, pc */
2412   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
2413 };
2414
2415 /* Cortex-A8 erratum-workaround stubs.  */
2416
2417 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2418    can't use a conditional branch to reach this stub).  */
2419
2420 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2421 {
2422   THUMB16_BCOND_INSN (0xd001),         /* b<cond>.n true.  */
2423   THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2424   THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2425 };
2426
2427 /* Stub used for b.w and bl.w instructions.  */
2428
2429 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2430 {
2431   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2432 };
2433
2434 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2435 {
2436   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2437 };
2438
2439 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2440    instruction (which switches to ARM mode) to point to this stub.  Jump to the
2441    real destination using an ARM-mode branch.  */
2442
2443 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2444 {
2445   ARM_REL_INSN (0xea000000, -8) /* b original_branch_dest.  */
2446 };
2447
2448 /* For each section group there can be a specially created linker section
2449    to hold the stubs for that group.  The name of the stub section is based
2450    upon the name of another section within that group with the suffix below
2451    applied.
2452
2453    PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
2454    create what appeared to be a linker stub section when it actually
2455    contained user code/data.  For example, consider this fragment:
2456
2457      const char * stubborn_problems[] = { "np" };
2458
2459    If this is compiled with "-fPIC -fdata-sections" then gcc produces a
2460    section called:
2461
2462      .data.rel.local.stubborn_problems
2463
2464    This then causes problems in arm32_arm_build_stubs() as it triggers:
2465
2466       // Ignore non-stub sections.
2467       if (!strstr (stub_sec->name, STUB_SUFFIX))
2468         continue;
2469
2470    And so the section would be ignored instead of being processed.  Hence
2471    the change in definition of STUB_SUFFIX to a name that cannot be a valid
2472    C identifier.  */
2473 #define STUB_SUFFIX ".__stub"
2474
2475 /* One entry per long/short branch stub defined above.  */
2476 #define DEF_STUBS \
2477   DEF_STUB(long_branch_any_any) \
2478   DEF_STUB(long_branch_v4t_arm_thumb) \
2479   DEF_STUB(long_branch_thumb_only) \
2480   DEF_STUB(long_branch_v4t_thumb_thumb) \
2481   DEF_STUB(long_branch_v4t_thumb_arm) \
2482   DEF_STUB(short_branch_v4t_thumb_arm) \
2483   DEF_STUB(long_branch_any_arm_pic) \
2484   DEF_STUB(long_branch_any_thumb_pic) \
2485   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
2486   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
2487   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
2488   DEF_STUB(long_branch_thumb_only_pic) \
2489   DEF_STUB(long_branch_any_tls_pic) \
2490   DEF_STUB(long_branch_v4t_thumb_tls_pic) \
2491   DEF_STUB(a8_veneer_b_cond) \
2492   DEF_STUB(a8_veneer_b) \
2493   DEF_STUB(a8_veneer_bl) \
2494   DEF_STUB(a8_veneer_blx)
2495
2496 #define DEF_STUB(x) arm_stub_##x,
2497 enum elf32_arm_stub_type
2498 {
2499   arm_stub_none,
2500   DEF_STUBS
2501   /* Note the first a8_veneer type */
2502   arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond
2503 };
2504 #undef DEF_STUB
2505
2506 typedef struct
2507 {
2508   const insn_sequence* template_sequence;
2509   int template_size;
2510 } stub_def;
2511
2512 #define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
2513 static const stub_def stub_definitions[] =
2514 {
2515   {NULL, 0},
2516   DEF_STUBS
2517 };
2518
2519 struct elf32_arm_stub_hash_entry
2520 {
2521   /* Base hash table entry structure.  */
2522   struct bfd_hash_entry root;
2523
2524   /* The stub section.  */
2525   asection *stub_sec;
2526
2527   /* Offset within stub_sec of the beginning of this stub.  */
2528   bfd_vma stub_offset;
2529
2530   /* Given the symbol's value and its section we can determine its final
2531      value when building the stubs (so the stub knows where to jump).  */
2532   bfd_vma target_value;
2533   asection *target_section;
2534
2535   /* Offset to apply to relocation referencing target_value.  */
2536   bfd_vma target_addend;
2537
2538   /* The instruction which caused this stub to be generated (only valid for
2539      Cortex-A8 erratum workaround stubs at present).  */
2540   unsigned long orig_insn;
2541
2542   /* The stub type.  */
2543   enum elf32_arm_stub_type stub_type;
2544   /* Its encoding size in bytes.  */
2545   int stub_size;
2546   /* Its template.  */
2547   const insn_sequence *stub_template;
2548   /* The size of the template (number of entries).  */
2549   int stub_template_size;
2550
2551   /* The symbol table entry, if any, that this was derived from.  */
2552   struct elf32_arm_link_hash_entry *h;
2553
2554   /* Type of branch.  */
2555   enum arm_st_branch_type branch_type;
2556
2557   /* Where this stub is being called from, or, in the case of combined
2558      stub sections, the first input section in the group.  */
2559   asection *id_sec;
2560
2561   /* The name for the local symbol at the start of this stub.  The
2562      stub name in the hash table has to be unique; this does not, so
2563      it can be friendlier.  */
2564   char *output_name;
2565 };
2566
2567 /* Used to build a map of a section.  This is required for mixed-endian
2568    code/data.  */
2569
2570 typedef struct elf32_elf_section_map
2571 {
2572   bfd_vma vma;
2573   char type;
2574 }
2575 elf32_arm_section_map;
2576
2577 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2578
2579 typedef enum
2580 {
2581   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2582   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2583   VFP11_ERRATUM_ARM_VENEER,
2584   VFP11_ERRATUM_THUMB_VENEER
2585 }
2586 elf32_vfp11_erratum_type;
2587
2588 typedef struct elf32_vfp11_erratum_list
2589 {
2590   struct elf32_vfp11_erratum_list *next;
2591   bfd_vma vma;
2592   union
2593   {
2594     struct
2595     {
2596       struct elf32_vfp11_erratum_list *veneer;
2597       unsigned int vfp_insn;
2598     } b;
2599     struct
2600     {
2601       struct elf32_vfp11_erratum_list *branch;
2602       unsigned int id;
2603     } v;
2604   } u;
2605   elf32_vfp11_erratum_type type;
2606 }
2607 elf32_vfp11_erratum_list;
2608
2609 typedef enum
2610 {
2611   DELETE_EXIDX_ENTRY,
2612   INSERT_EXIDX_CANTUNWIND_AT_END
2613 }
2614 arm_unwind_edit_type;
2615
2616 /* A (sorted) list of edits to apply to an unwind table.  */
2617 typedef struct arm_unwind_table_edit
2618 {
2619   arm_unwind_edit_type type;
2620   /* Note: we sometimes want to insert an unwind entry corresponding to a
2621      section different from the one we're currently writing out, so record the
2622      (text) section this edit relates to here.  */
2623   asection *linked_section;
2624   unsigned int index;
2625   struct arm_unwind_table_edit *next;
2626 }
2627 arm_unwind_table_edit;
2628
2629 typedef struct _arm_elf_section_data
2630 {
2631   /* Information about mapping symbols.  */
2632   struct bfd_elf_section_data elf;
2633   unsigned int mapcount;
2634   unsigned int mapsize;
2635   elf32_arm_section_map *map;
2636   /* Information about CPU errata.  */
2637   unsigned int erratumcount;
2638   elf32_vfp11_erratum_list *erratumlist;
2639   /* Information about unwind tables.  */
2640   union
2641   {
2642     /* Unwind info attached to a text section.  */
2643     struct
2644     {
2645       asection *arm_exidx_sec;
2646     } text;
2647
2648     /* Unwind info attached to an .ARM.exidx section.  */
2649     struct
2650     {
2651       arm_unwind_table_edit *unwind_edit_list;
2652       arm_unwind_table_edit *unwind_edit_tail;
2653     } exidx;
2654   } u;
2655 }
2656 _arm_elf_section_data;
2657
2658 #define elf32_arm_section_data(sec) \
2659   ((_arm_elf_section_data *) elf_section_data (sec))
2660
2661 /* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
2662    These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
2663    so may be created multiple times: we use an array of these entries whilst
2664    relaxing which we can refresh easily, then create stubs for each potentially
2665    erratum-triggering instruction once we've settled on a solution.  */
2666
2667 struct a8_erratum_fix
2668 {
2669   bfd *input_bfd;
2670   asection *section;
2671   bfd_vma offset;
2672   bfd_vma addend;
2673   unsigned long orig_insn;
2674   char *stub_name;
2675   enum elf32_arm_stub_type stub_type;
2676   enum arm_st_branch_type branch_type;
2677 };
2678
2679 /* A table of relocs applied to branches which might trigger Cortex-A8
2680    erratum.  */
2681
2682 struct a8_erratum_reloc
2683 {
2684   bfd_vma from;
2685   bfd_vma destination;
2686   struct elf32_arm_link_hash_entry *hash;
2687   const char *sym_name;
2688   unsigned int r_type;
2689   enum arm_st_branch_type branch_type;
2690   bfd_boolean non_a8_stub;
2691 };
2692
2693 /* The size of the thread control block.  */
2694 #define TCB_SIZE        8
2695
2696 /* ARM-specific information about a PLT entry, over and above the usual
2697    gotplt_union.  */
2698 struct arm_plt_info
2699 {
2700   /* We reference count Thumb references to a PLT entry separately,
2701      so that we can emit the Thumb trampoline only if needed.  */
2702   bfd_signed_vma thumb_refcount;
2703
2704   /* Some references from Thumb code may be eliminated by BL->BLX
2705      conversion, so record them separately.  */
2706   bfd_signed_vma maybe_thumb_refcount;
2707
2708   /* How many of the recorded PLT accesses were from non-call relocations.
2709      This information is useful when deciding whether anything takes the
2710      address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
2711      non-call references to the function should resolve directly to the
2712      real runtime target.  */
2713   unsigned int noncall_refcount;
2714
2715   /* Since PLT entries have variable size if the Thumb prologue is
2716      used, we need to record the index into .got.plt instead of
2717      recomputing it from the PLT offset.  */
2718   bfd_signed_vma got_offset;
2719 };
2720
2721 /* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
2722 struct arm_local_iplt_info
2723 {
2724   /* The information that is usually found in the generic ELF part of
2725      the hash table entry.  */
2726   union gotplt_union root;
2727
2728   /* The information that is usually found in the ARM-specific part of
2729      the hash table entry.  */
2730   struct arm_plt_info arm;
2731
2732   /* A list of all potential dynamic relocations against this symbol.  */
2733   struct elf_dyn_relocs *dyn_relocs;
2734 };
2735
2736 struct elf_arm_obj_tdata
2737 {
2738   struct elf_obj_tdata root;
2739
2740   /* tls_type for each local got entry.  */
2741   char *local_got_tls_type;
2742
2743   /* GOTPLT entries for TLS descriptors.  */
2744   bfd_vma *local_tlsdesc_gotent;
2745
2746   /* Information for local symbols that need entries in .iplt.  */
2747   struct arm_local_iplt_info **local_iplt;
2748
2749   /* Zero to warn when linking objects with incompatible enum sizes.  */
2750   int no_enum_size_warning;
2751
2752   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
2753   int no_wchar_size_warning;
2754 };
2755
2756 #define elf_arm_tdata(bfd) \
2757   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
2758
2759 #define elf32_arm_local_got_tls_type(bfd) \
2760   (elf_arm_tdata (bfd)->local_got_tls_type)
2761
2762 #define elf32_arm_local_tlsdesc_gotent(bfd) \
2763   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
2764
2765 #define elf32_arm_local_iplt(bfd) \
2766   (elf_arm_tdata (bfd)->local_iplt)
2767
2768 #define is_arm_elf(bfd) \
2769   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2770    && elf_tdata (bfd) != NULL \
2771    && elf_object_id (bfd) == ARM_ELF_DATA)
2772
2773 static bfd_boolean
2774 elf32_arm_mkobject (bfd *abfd)
2775 {
2776   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
2777                                   ARM_ELF_DATA);
2778 }
2779
2780 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2781
2782 /* Arm ELF linker hash entry.  */
2783 struct elf32_arm_link_hash_entry
2784 {
2785   struct elf_link_hash_entry root;
2786
2787   /* Track dynamic relocs copied for this symbol.  */
2788   struct elf_dyn_relocs *dyn_relocs;
2789
2790   /* ARM-specific PLT information.  */
2791   struct arm_plt_info plt;
2792
2793 #define GOT_UNKNOWN     0
2794 #define GOT_NORMAL      1
2795 #define GOT_TLS_GD      2
2796 #define GOT_TLS_IE      4
2797 #define GOT_TLS_GDESC   8
2798 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
2799   unsigned int tls_type : 8;
2800
2801   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
2802   unsigned int is_iplt : 1;
2803
2804   unsigned int unused : 23;
2805
2806   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
2807      starting at the end of the jump table.  */
2808   bfd_vma tlsdesc_got;
2809
2810   /* The symbol marking the real symbol location for exported thumb
2811      symbols with Arm stubs.  */
2812   struct elf_link_hash_entry *export_glue;
2813
2814   /* A pointer to the most recently used stub hash entry against this
2815      symbol.  */
2816   struct elf32_arm_stub_hash_entry *stub_cache;
2817 };
2818
2819 /* Traverse an arm ELF linker hash table.  */
2820 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2821   (elf_link_hash_traverse                                               \
2822    (&(table)->root,                                                     \
2823     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2824     (info)))
2825
2826 /* Get the ARM elf linker hash table from a link_info structure.  */
2827 #define elf32_arm_hash_table(info) \
2828   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
2829   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
2830
2831 #define arm_stub_hash_lookup(table, string, create, copy) \
2832   ((struct elf32_arm_stub_hash_entry *) \
2833    bfd_hash_lookup ((table), (string), (create), (copy)))
2834
2835 /* Array to keep track of which stub sections have been created, and
2836    information on stub grouping.  */
2837 struct map_stub
2838 {
2839   /* This is the section to which stubs in the group will be
2840      attached.  */
2841   asection *link_sec;
2842   /* The stub section.  */
2843   asection *stub_sec;
2844 };
2845
2846 #define elf32_arm_compute_jump_table_size(htab) \
2847   ((htab)->next_tls_desc_index * 4)
2848
2849 /* ARM ELF linker hash table.  */
2850 struct elf32_arm_link_hash_table
2851 {
2852   /* The main hash table.  */
2853   struct elf_link_hash_table root;
2854
2855   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
2856   bfd_size_type thumb_glue_size;
2857
2858   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
2859   bfd_size_type arm_glue_size;
2860
2861   /* The size in bytes of section containing the ARMv4 BX veneers.  */
2862   bfd_size_type bx_glue_size;
2863
2864   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
2865      veneer has been populated.  */
2866   bfd_vma bx_glue_offset[15];
2867
2868   /* The size in bytes of the section containing glue for VFP11 erratum
2869      veneers.  */
2870   bfd_size_type vfp11_erratum_glue_size;
2871
2872   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
2873      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
2874      elf32_arm_write_section().  */
2875   struct a8_erratum_fix *a8_erratum_fixes;
2876   unsigned int num_a8_erratum_fixes;
2877
2878   /* An arbitrary input BFD chosen to hold the glue sections.  */
2879   bfd * bfd_of_glue_owner;
2880
2881   /* Nonzero to output a BE8 image.  */
2882   int byteswap_code;
2883
2884   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
2885      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
2886   int target1_is_rel;
2887
2888   /* The relocation to use for R_ARM_TARGET2 relocations.  */
2889   int target2_reloc;
2890
2891   /* 0 = Ignore R_ARM_V4BX.
2892      1 = Convert BX to MOV PC.
2893      2 = Generate v4 interworing stubs.  */
2894   int fix_v4bx;
2895
2896   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
2897   int fix_cortex_a8;
2898
2899   /* Whether we should fix the ARM1176 BLX immediate issue.  */
2900   int fix_arm1176;
2901
2902   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
2903   int use_blx;
2904
2905   /* What sort of code sequences we should look for which may trigger the
2906      VFP11 denorm erratum.  */
2907   bfd_arm_vfp11_fix vfp11_fix;
2908
2909   /* Global counter for the number of fixes we have emitted.  */
2910   int num_vfp11_fixes;
2911
2912   /* Nonzero to force PIC branch veneers.  */
2913   int pic_veneer;
2914
2915   /* The number of bytes in the initial entry in the PLT.  */
2916   bfd_size_type plt_header_size;
2917
2918   /* The number of bytes in the subsequent PLT etries.  */
2919   bfd_size_type plt_entry_size;
2920
2921   /* True if the target system is VxWorks.  */
2922   int vxworks_p;
2923
2924   /* True if the target system is Symbian OS.  */
2925   int symbian_p;
2926
2927   /* True if the target system is Native Client.  */
2928   int nacl_p;
2929
2930   /* True if the target uses REL relocations.  */
2931   int use_rel;
2932
2933   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
2934   bfd_vma next_tls_desc_index;
2935
2936   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
2937   bfd_vma num_tls_desc;
2938
2939   /* Short-cuts to get to dynamic linker sections.  */
2940   asection *sdynbss;
2941   asection *srelbss;
2942
2943   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2944   asection *srelplt2;
2945
2946   /* The offset into splt of the PLT entry for the TLS descriptor
2947      resolver.  Special values are 0, if not necessary (or not found
2948      to be necessary yet), and -1 if needed but not determined
2949      yet.  */
2950   bfd_vma dt_tlsdesc_plt;
2951
2952   /* The offset into sgot of the GOT entry used by the PLT entry
2953      above.  */
2954   bfd_vma dt_tlsdesc_got;
2955
2956   /* Offset in .plt section of tls_arm_trampoline.  */
2957   bfd_vma tls_trampoline;
2958
2959   /* Data for R_ARM_TLS_LDM32 relocations.  */
2960   union
2961   {
2962     bfd_signed_vma refcount;
2963     bfd_vma offset;
2964   } tls_ldm_got;
2965
2966   /* Small local sym cache.  */
2967   struct sym_cache sym_cache;
2968
2969   /* For convenience in allocate_dynrelocs.  */
2970   bfd * obfd;
2971
2972   /* The amount of space used by the reserved portion of the sgotplt
2973      section, plus whatever space is used by the jump slots.  */
2974   bfd_vma sgotplt_jump_table_size;
2975
2976   /* The stub hash table.  */
2977   struct bfd_hash_table stub_hash_table;
2978
2979   /* Linker stub bfd.  */
2980   bfd *stub_bfd;
2981
2982   /* Linker call-backs.  */
2983   asection * (*add_stub_section) (const char *, asection *);
2984   void (*layout_sections_again) (void);
2985
2986   /* Array to keep track of which stub sections have been created, and
2987      information on stub grouping.  */
2988   struct map_stub *stub_group;
2989
2990   /* Number of elements in stub_group.  */
2991   int top_id;
2992
2993   /* Assorted information used by elf32_arm_size_stubs.  */
2994   unsigned int bfd_count;
2995   int top_index;
2996   asection **input_list;
2997 };
2998
2999 /* Create an entry in an ARM ELF linker hash table.  */
3000
3001 static struct bfd_hash_entry *
3002 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3003                              struct bfd_hash_table * table,
3004                              const char * string)
3005 {
3006   struct elf32_arm_link_hash_entry * ret =
3007     (struct elf32_arm_link_hash_entry *) entry;
3008
3009   /* Allocate the structure if it has not already been allocated by a
3010      subclass.  */
3011   if (ret == NULL)
3012     ret = (struct elf32_arm_link_hash_entry *)
3013         bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3014   if (ret == NULL)
3015     return (struct bfd_hash_entry *) ret;
3016
3017   /* Call the allocation method of the superclass.  */
3018   ret = ((struct elf32_arm_link_hash_entry *)
3019          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3020                                      table, string));
3021   if (ret != NULL)
3022     {
3023       ret->dyn_relocs = NULL;
3024       ret->tls_type = GOT_UNKNOWN;
3025       ret->tlsdesc_got = (bfd_vma) -1;
3026       ret->plt.thumb_refcount = 0;
3027       ret->plt.maybe_thumb_refcount = 0;
3028       ret->plt.noncall_refcount = 0;
3029       ret->plt.got_offset = -1;
3030       ret->is_iplt = FALSE;
3031       ret->export_glue = NULL;
3032
3033       ret->stub_cache = NULL;
3034     }
3035
3036   return (struct bfd_hash_entry *) ret;
3037 }
3038
3039 /* Ensure that we have allocated bookkeeping structures for ABFD's local
3040    symbols.  */
3041
3042 static bfd_boolean
3043 elf32_arm_allocate_local_sym_info (bfd *abfd)
3044 {
3045   if (elf_local_got_refcounts (abfd) == NULL)
3046     {
3047       bfd_size_type num_syms;
3048       bfd_size_type size;
3049       char *data;
3050
3051       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3052       size = num_syms * (sizeof (bfd_signed_vma)
3053                          + sizeof (struct arm_local_iplt_info *)
3054                          + sizeof (bfd_vma)
3055                          + sizeof (char));
3056       data = bfd_zalloc (abfd, size);
3057       if (data == NULL)
3058         return FALSE;
3059
3060       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
3061       data += num_syms * sizeof (bfd_signed_vma);
3062
3063       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
3064       data += num_syms * sizeof (struct arm_local_iplt_info *);
3065
3066       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
3067       data += num_syms * sizeof (bfd_vma);
3068
3069       elf32_arm_local_got_tls_type (abfd) = data;
3070     }
3071   return TRUE;
3072 }
3073
3074 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
3075    to input bfd ABFD.  Create the information if it doesn't already exist.
3076    Return null if an allocation fails.  */
3077
3078 static struct arm_local_iplt_info *
3079 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3080 {
3081   struct arm_local_iplt_info **ptr;
3082
3083   if (!elf32_arm_allocate_local_sym_info (abfd))
3084     return NULL;
3085
3086   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3087   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3088   if (*ptr == NULL)
3089     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3090   return *ptr;
3091 }
3092
3093 /* Try to obtain PLT information for the symbol with index R_SYMNDX
3094    in ABFD's symbol table.  If the symbol is global, H points to its
3095    hash table entry, otherwise H is null.
3096
3097    Return true if the symbol does have PLT information.  When returning
3098    true, point *ROOT_PLT at the target-independent reference count/offset
3099    union and *ARM_PLT at the ARM-specific information.  */
3100
3101 static bfd_boolean
3102 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_entry *h,
3103                         unsigned long r_symndx, union gotplt_union **root_plt,
3104                         struct arm_plt_info **arm_plt)
3105 {
3106   struct arm_local_iplt_info *local_iplt;
3107
3108   if (h != NULL)
3109     {
3110       *root_plt = &h->root.plt;
3111       *arm_plt = &h->plt;
3112       return TRUE;
3113     }
3114
3115   if (elf32_arm_local_iplt (abfd) == NULL)
3116     return FALSE;
3117
3118   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3119   if (local_iplt == NULL)
3120     return FALSE;
3121
3122   *root_plt = &local_iplt->root;
3123   *arm_plt = &local_iplt->arm;
3124   return TRUE;
3125 }
3126
3127 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
3128    before it.  */
3129
3130 static bfd_boolean
3131 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3132                                   struct arm_plt_info *arm_plt)
3133 {
3134   struct elf32_arm_link_hash_table *htab;
3135
3136   htab = elf32_arm_hash_table (info);
3137   return (arm_plt->thumb_refcount != 0
3138           || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0));
3139 }
3140
3141 /* Return a pointer to the head of the dynamic reloc list that should
3142    be used for local symbol ISYM, which is symbol number R_SYMNDX in
3143    ABFD's symbol table.  Return null if an error occurs.  */
3144
3145 static struct elf_dyn_relocs **
3146 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3147                                    Elf_Internal_Sym *isym)
3148 {
3149   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3150     {
3151       struct arm_local_iplt_info *local_iplt;
3152
3153       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3154       if (local_iplt == NULL)
3155         return NULL;
3156       return &local_iplt->dyn_relocs;
3157     }
3158   else
3159     {
3160       /* Track dynamic relocs needed for local syms too.
3161          We really need local syms available to do this
3162          easily.  Oh well.  */
3163       asection *s;
3164       void *vpp;
3165
3166       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3167       if (s == NULL)
3168         abort ();
3169
3170       vpp = &elf_section_data (s)->local_dynrel;
3171       return (struct elf_dyn_relocs **) vpp;
3172     }
3173 }
3174
3175 /* Initialize an entry in the stub hash table.  */
3176
3177 static struct bfd_hash_entry *
3178 stub_hash_newfunc (struct bfd_hash_entry *entry,
3179                    struct bfd_hash_table *table,
3180                    const char *string)
3181 {
3182   /* Allocate the structure if it has not already been allocated by a
3183      subclass.  */
3184   if (entry == NULL)
3185     {
3186       entry = (struct bfd_hash_entry *)
3187           bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3188       if (entry == NULL)
3189         return entry;
3190     }
3191
3192   /* Call the allocation method of the superclass.  */
3193   entry = bfd_hash_newfunc (entry, table, string);
3194   if (entry != NULL)
3195     {
3196       struct elf32_arm_stub_hash_entry *eh;
3197
3198       /* Initialize the local fields.  */
3199       eh = (struct elf32_arm_stub_hash_entry *) entry;
3200       eh->stub_sec = NULL;
3201       eh->stub_offset = 0;
3202       eh->target_value = 0;
3203       eh->target_section = NULL;
3204       eh->target_addend = 0;
3205       eh->orig_insn = 0;
3206       eh->stub_type = arm_stub_none;
3207       eh->stub_size = 0;
3208       eh->stub_template = NULL;
3209       eh->stub_template_size = 0;
3210       eh->h = NULL;
3211       eh->id_sec = NULL;
3212       eh->output_name = NULL;
3213     }
3214
3215   return entry;
3216 }
3217
3218 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3219    shortcuts to them in our hash table.  */
3220
3221 static bfd_boolean
3222 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3223 {
3224   struct elf32_arm_link_hash_table *htab;
3225
3226   htab = elf32_arm_hash_table (info);
3227   if (htab == NULL)
3228     return FALSE;
3229
3230   /* BPABI objects never have a GOT, or associated sections.  */
3231   if (htab->symbian_p)
3232     return TRUE;
3233
3234   if (! _bfd_elf_create_got_section (dynobj, info))
3235     return FALSE;
3236
3237   return TRUE;
3238 }
3239
3240 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3241
3242 static bfd_boolean
3243 create_ifunc_sections (struct bfd_link_info *info)
3244 {
3245   struct elf32_arm_link_hash_table *htab;
3246   const struct elf_backend_data *bed;
3247   bfd *dynobj;
3248   asection *s;
3249   flagword flags;
3250
3251   htab = elf32_arm_hash_table (info);
3252   dynobj = htab->root.dynobj;
3253   bed = get_elf_backend_data (dynobj);
3254   flags = bed->dynamic_sec_flags;
3255
3256   if (htab->root.iplt == NULL)
3257     {
3258       s = bfd_make_section_with_flags (dynobj, ".iplt",
3259                                        flags | SEC_READONLY | SEC_CODE);
3260       if (s == NULL
3261           || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3262         return FALSE;
3263       htab->root.iplt = s;
3264     }
3265
3266   if (htab->root.irelplt == NULL)
3267     {
3268       s = bfd_make_section_with_flags (dynobj, RELOC_SECTION (htab, ".iplt"),
3269                                        flags | SEC_READONLY);
3270       if (s == NULL
3271           || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
3272         return FALSE;
3273       htab->root.irelplt = s;
3274     }
3275
3276   if (htab->root.igotplt == NULL)
3277     {
3278       s = bfd_make_section_with_flags (dynobj, ".igot.plt", flags);
3279       if (s == NULL
3280           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3281         return FALSE;
3282       htab->root.igotplt = s;
3283     }
3284   return TRUE;
3285 }
3286
3287 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3288    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3289    hash table.  */
3290
3291 static bfd_boolean
3292 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3293 {
3294   struct elf32_arm_link_hash_table *htab;
3295
3296   htab = elf32_arm_hash_table (info);
3297   if (htab == NULL)
3298     return FALSE;
3299
3300   if (!htab->root.sgot && !create_got_section (dynobj, info))
3301     return FALSE;
3302
3303   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3304     return FALSE;
3305
3306   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3307   if (!info->shared)
3308     htab->srelbss = bfd_get_section_by_name (dynobj,
3309                                              RELOC_SECTION (htab, ".bss"));
3310
3311   if (htab->vxworks_p)
3312     {
3313       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3314         return FALSE;
3315
3316       if (info->shared)
3317         {
3318           htab->plt_header_size = 0;
3319           htab->plt_entry_size
3320             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3321         }
3322       else
3323         {
3324           htab->plt_header_size
3325             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3326           htab->plt_entry_size
3327             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3328         }
3329     }
3330
3331   if (!htab->root.splt
3332       || !htab->root.srelplt
3333       || !htab->sdynbss
3334       || (!info->shared && !htab->srelbss))
3335     abort ();
3336
3337   return TRUE;
3338 }
3339
3340 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3341
3342 static void
3343 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3344                                 struct elf_link_hash_entry *dir,
3345                                 struct elf_link_hash_entry *ind)
3346 {
3347   struct elf32_arm_link_hash_entry *edir, *eind;
3348
3349   edir = (struct elf32_arm_link_hash_entry *) dir;
3350   eind = (struct elf32_arm_link_hash_entry *) ind;
3351
3352   if (eind->dyn_relocs != NULL)
3353     {
3354       if (edir->dyn_relocs != NULL)
3355         {
3356           struct elf_dyn_relocs **pp;
3357           struct elf_dyn_relocs *p;
3358
3359           /* Add reloc counts against the indirect sym to the direct sym
3360              list.  Merge any entries against the same section.  */
3361           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3362             {
3363               struct elf_dyn_relocs *q;
3364
3365               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3366                 if (q->sec == p->sec)
3367                   {
3368                     q->pc_count += p->pc_count;
3369                     q->count += p->count;
3370                     *pp = p->next;
3371                     break;
3372                   }
3373               if (q == NULL)
3374                 pp = &p->next;
3375             }
3376           *pp = edir->dyn_relocs;
3377         }
3378
3379       edir->dyn_relocs = eind->dyn_relocs;
3380       eind->dyn_relocs = NULL;
3381     }
3382
3383   if (ind->root.type == bfd_link_hash_indirect)
3384     {
3385       /* Copy over PLT info.  */
3386       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3387       eind->plt.thumb_refcount = 0;
3388       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3389       eind->plt.maybe_thumb_refcount = 0;
3390       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3391       eind->plt.noncall_refcount = 0;
3392
3393       /* We should only allocate a function to .iplt once the final
3394          symbol information is known.  */
3395       BFD_ASSERT (!eind->is_iplt);
3396
3397       if (dir->got.refcount <= 0)
3398         {
3399           edir->tls_type = eind->tls_type;
3400           eind->tls_type = GOT_UNKNOWN;
3401         }
3402     }
3403
3404   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3405 }
3406
3407 /* Create an ARM elf linker hash table.  */
3408
3409 static struct bfd_link_hash_table *
3410 elf32_arm_link_hash_table_create (bfd *abfd)
3411 {
3412   struct elf32_arm_link_hash_table *ret;
3413   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
3414
3415   ret = (struct elf32_arm_link_hash_table *) bfd_malloc (amt);
3416   if (ret == NULL)
3417     return NULL;
3418
3419   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
3420                                       elf32_arm_link_hash_newfunc,
3421                                       sizeof (struct elf32_arm_link_hash_entry),
3422                                       ARM_ELF_DATA))
3423     {
3424       free (ret);
3425       return NULL;
3426     }
3427
3428   ret->sdynbss = NULL;
3429   ret->srelbss = NULL;
3430   ret->srelplt2 = NULL;
3431   ret->dt_tlsdesc_plt = 0;
3432   ret->dt_tlsdesc_got = 0;
3433   ret->tls_trampoline = 0;
3434   ret->next_tls_desc_index = 0;
3435   ret->num_tls_desc = 0;
3436   ret->thumb_glue_size = 0;
3437   ret->arm_glue_size = 0;
3438   ret->bx_glue_size = 0;
3439   memset (ret->bx_glue_offset, 0, sizeof (ret->bx_glue_offset));
3440   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
3441   ret->vfp11_erratum_glue_size = 0;
3442   ret->num_vfp11_fixes = 0;
3443   ret->fix_cortex_a8 = 0;
3444   ret->fix_arm1176 = 0;
3445   ret->bfd_of_glue_owner = NULL;
3446   ret->byteswap_code = 0;
3447   ret->target1_is_rel = 0;
3448   ret->target2_reloc = R_ARM_NONE;
3449 #ifdef FOUR_WORD_PLT
3450   ret->plt_header_size = 16;
3451   ret->plt_entry_size = 16;
3452 #else
3453   ret->plt_header_size = 20;
3454   ret->plt_entry_size = 12;
3455 #endif
3456   ret->fix_v4bx = 0;
3457   ret->use_blx = 0;
3458   ret->vxworks_p = 0;
3459   ret->symbian_p = 0;
3460   ret->nacl_p = 0;
3461   ret->use_rel = 1;
3462   ret->sym_cache.abfd = NULL;
3463   ret->obfd = abfd;
3464   ret->tls_ldm_got.refcount = 0;
3465   ret->stub_bfd = NULL;
3466   ret->add_stub_section = NULL;
3467   ret->layout_sections_again = NULL;
3468   ret->stub_group = NULL;
3469   ret->top_id = 0;
3470   ret->bfd_count = 0;
3471   ret->top_index = 0;
3472   ret->input_list = NULL;
3473
3474   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
3475                             sizeof (struct elf32_arm_stub_hash_entry)))
3476     {
3477       free (ret);
3478       return NULL;
3479     }
3480
3481   return &ret->root.root;
3482 }
3483
3484 /* Free the derived linker hash table.  */
3485
3486 static void
3487 elf32_arm_hash_table_free (struct bfd_link_hash_table *hash)
3488 {
3489   struct elf32_arm_link_hash_table *ret
3490     = (struct elf32_arm_link_hash_table *) hash;
3491
3492   bfd_hash_table_free (&ret->stub_hash_table);
3493   _bfd_generic_link_hash_table_free (hash);
3494 }
3495
3496 /* Determine if we're dealing with a Thumb only architecture.  */
3497
3498 static bfd_boolean
3499 using_thumb_only (struct elf32_arm_link_hash_table *globals)
3500 {
3501   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3502                                        Tag_CPU_arch);
3503   int profile;
3504
3505   if (arch == TAG_CPU_ARCH_V6_M || arch == TAG_CPU_ARCH_V6S_M)
3506     return TRUE;
3507
3508   if (arch != TAG_CPU_ARCH_V7 && arch != TAG_CPU_ARCH_V7E_M)
3509     return FALSE;
3510
3511   profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3512                                       Tag_CPU_arch_profile);
3513
3514   return profile == 'M';
3515 }
3516
3517 /* Determine if we're dealing with a Thumb-2 object.  */
3518
3519 static bfd_boolean
3520 using_thumb2 (struct elf32_arm_link_hash_table *globals)
3521 {
3522   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3523                                        Tag_CPU_arch);
3524   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
3525 }
3526
3527 /* Determine what kind of NOPs are available.  */
3528
3529 static bfd_boolean
3530 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
3531 {
3532   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3533                                              Tag_CPU_arch);
3534   return arch == TAG_CPU_ARCH_V6T2
3535          || arch == TAG_CPU_ARCH_V6K
3536          || arch == TAG_CPU_ARCH_V7
3537          || arch == TAG_CPU_ARCH_V7E_M;
3538 }
3539
3540 static bfd_boolean
3541 arch_has_thumb2_nop (struct elf32_arm_link_hash_table *globals)
3542 {
3543   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3544                                              Tag_CPU_arch);
3545   return (arch == TAG_CPU_ARCH_V6T2 || arch == TAG_CPU_ARCH_V7
3546           || arch == TAG_CPU_ARCH_V7E_M);
3547 }
3548
3549 static bfd_boolean
3550 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
3551 {
3552   switch (stub_type)
3553     {
3554     case arm_stub_long_branch_thumb_only:
3555     case arm_stub_long_branch_v4t_thumb_arm:
3556     case arm_stub_short_branch_v4t_thumb_arm:
3557     case arm_stub_long_branch_v4t_thumb_arm_pic:
3558     case arm_stub_long_branch_v4t_thumb_tls_pic:
3559     case arm_stub_long_branch_thumb_only_pic:
3560       return TRUE;
3561     case arm_stub_none:
3562       BFD_FAIL ();
3563       return FALSE;
3564       break;
3565     default:
3566       return FALSE;
3567     }
3568 }
3569
3570 /* Determine the type of stub needed, if any, for a call.  */
3571
3572 static enum elf32_arm_stub_type
3573 arm_type_of_stub (struct bfd_link_info *info,
3574                   asection *input_sec,
3575                   const Elf_Internal_Rela *rel,
3576                   unsigned char st_type,
3577                   enum arm_st_branch_type *actual_branch_type,
3578                   struct elf32_arm_link_hash_entry *hash,
3579                   bfd_vma destination,
3580                   asection *sym_sec,
3581                   bfd *input_bfd,
3582                   const char *name)
3583 {
3584   bfd_vma location;
3585   bfd_signed_vma branch_offset;
3586   unsigned int r_type;
3587   struct elf32_arm_link_hash_table * globals;
3588   int thumb2;
3589   int thumb_only;
3590   enum elf32_arm_stub_type stub_type = arm_stub_none;
3591   int use_plt = 0;
3592   enum arm_st_branch_type branch_type = *actual_branch_type;
3593   union gotplt_union *root_plt;
3594   struct arm_plt_info *arm_plt;
3595
3596   if (branch_type == ST_BRANCH_LONG)
3597     return stub_type;
3598
3599   globals = elf32_arm_hash_table (info);
3600   if (globals == NULL)
3601     return stub_type;
3602
3603   thumb_only = using_thumb_only (globals);
3604
3605   thumb2 = using_thumb2 (globals);
3606
3607   /* Determine where the call point is.  */
3608   location = (input_sec->output_offset
3609               + input_sec->output_section->vma
3610               + rel->r_offset);
3611
3612   r_type = ELF32_R_TYPE (rel->r_info);
3613
3614   /* For TLS call relocs, it is the caller's responsibility to provide
3615      the address of the appropriate trampoline.  */
3616   if (r_type != R_ARM_TLS_CALL
3617       && r_type != R_ARM_THM_TLS_CALL
3618       && elf32_arm_get_plt_info (input_bfd, hash, ELF32_R_SYM (rel->r_info),
3619                                  &root_plt, &arm_plt)
3620       && root_plt->offset != (bfd_vma) -1)
3621     {
3622       asection *splt;
3623
3624       if (hash == NULL || hash->is_iplt)
3625         splt = globals->root.iplt;
3626       else
3627         splt = globals->root.splt;
3628       if (splt != NULL)
3629         {
3630           use_plt = 1;
3631
3632           /* Note when dealing with PLT entries: the main PLT stub is in
3633              ARM mode, so if the branch is in Thumb mode, another
3634              Thumb->ARM stub will be inserted later just before the ARM
3635              PLT stub. We don't take this extra distance into account
3636              here, because if a long branch stub is needed, we'll add a
3637              Thumb->Arm one and branch directly to the ARM PLT entry
3638              because it avoids spreading offset corrections in several
3639              places.  */
3640
3641           destination = (splt->output_section->vma
3642                          + splt->output_offset
3643                          + root_plt->offset);
3644           st_type = STT_FUNC;
3645           branch_type = ST_BRANCH_TO_ARM;
3646         }
3647     }
3648   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
3649   BFD_ASSERT (st_type != STT_GNU_IFUNC);
3650
3651   branch_offset = (bfd_signed_vma)(destination - location);
3652
3653   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
3654       || r_type == R_ARM_THM_TLS_CALL)
3655     {
3656       /* Handle cases where:
3657          - this call goes too far (different Thumb/Thumb2 max
3658            distance)
3659          - it's a Thumb->Arm call and blx is not available, or it's a
3660            Thumb->Arm branch (not bl). A stub is needed in this case,
3661            but only if this call is not through a PLT entry. Indeed,
3662            PLT stubs handle mode switching already.
3663       */
3664       if ((!thumb2
3665             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3666                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3667           || (thumb2
3668               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3669                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3670           || (branch_type == ST_BRANCH_TO_ARM
3671               && (((r_type == R_ARM_THM_CALL
3672                     || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
3673                   || (r_type == R_ARM_THM_JUMP24))
3674               && !use_plt))
3675         {
3676           if (branch_type == ST_BRANCH_TO_THUMB)
3677             {
3678               /* Thumb to thumb.  */
3679               if (!thumb_only)
3680                 {
3681                   stub_type = (info->shared | globals->pic_veneer)
3682                     /* PIC stubs.  */
3683                     ? ((globals->use_blx
3684                         && (r_type == R_ARM_THM_CALL))
3685                        /* V5T and above. Stub starts with ARM code, so
3686                           we must be able to switch mode before
3687                           reaching it, which is only possible for 'bl'
3688                           (ie R_ARM_THM_CALL relocation).  */
3689                        ? arm_stub_long_branch_any_thumb_pic
3690                        /* On V4T, use Thumb code only.  */
3691                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
3692
3693                     /* non-PIC stubs.  */
3694                     : ((globals->use_blx
3695                         && (r_type == R_ARM_THM_CALL))
3696                        /* V5T and above.  */
3697                        ? arm_stub_long_branch_any_any
3698                        /* V4T.  */
3699                        : arm_stub_long_branch_v4t_thumb_thumb);
3700                 }
3701               else
3702                 {
3703                   stub_type = (info->shared | globals->pic_veneer)
3704                     /* PIC stub.  */
3705                     ? arm_stub_long_branch_thumb_only_pic
3706                     /* non-PIC stub.  */
3707                     : arm_stub_long_branch_thumb_only;
3708                 }
3709             }
3710           else
3711             {
3712               /* Thumb to arm.  */
3713               if (sym_sec != NULL
3714                   && sym_sec->owner != NULL
3715                   && !INTERWORK_FLAG (sym_sec->owner))
3716                 {
3717                   (*_bfd_error_handler)
3718                     (_("%B(%s): warning: interworking not enabled.\n"
3719                        "  first occurrence: %B: Thumb call to ARM"),
3720                      sym_sec->owner, input_bfd, name);
3721                 }
3722
3723               stub_type =
3724                 (info->shared | globals->pic_veneer)
3725                 /* PIC stubs.  */
3726                 ? (r_type == R_ARM_THM_TLS_CALL
3727                    /* TLS PIC stubs */
3728                    ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
3729                       : arm_stub_long_branch_v4t_thumb_tls_pic)
3730                    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3731                       /* V5T PIC and above.  */
3732                       ? arm_stub_long_branch_any_arm_pic
3733                       /* V4T PIC stub.  */
3734                       : arm_stub_long_branch_v4t_thumb_arm_pic))
3735
3736                 /* non-PIC stubs.  */
3737                 : ((globals->use_blx && r_type == R_ARM_THM_CALL)
3738                    /* V5T and above.  */
3739                    ? arm_stub_long_branch_any_any
3740                    /* V4T.  */
3741                    : arm_stub_long_branch_v4t_thumb_arm);
3742
3743               /* Handle v4t short branches.  */
3744               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3745                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3746                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3747                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3748             }
3749         }
3750     }
3751   else if (r_type == R_ARM_CALL
3752            || r_type == R_ARM_JUMP24
3753            || r_type == R_ARM_PLT32
3754            || r_type == R_ARM_TLS_CALL)
3755     {
3756       if (branch_type == ST_BRANCH_TO_THUMB)
3757         {
3758           /* Arm to thumb.  */
3759
3760           if (sym_sec != NULL
3761               && sym_sec->owner != NULL
3762               && !INTERWORK_FLAG (sym_sec->owner))
3763             {
3764               (*_bfd_error_handler)
3765                 (_("%B(%s): warning: interworking not enabled.\n"
3766                    "  first occurrence: %B: ARM call to Thumb"),
3767                  sym_sec->owner, input_bfd, name);
3768             }
3769
3770           /* We have an extra 2-bytes reach because of
3771              the mode change (bit 24 (H) of BLX encoding).  */
3772           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3773               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3774               || (r_type == R_ARM_CALL && !globals->use_blx)
3775               || (r_type == R_ARM_JUMP24)
3776               || (r_type == R_ARM_PLT32))
3777             {
3778               stub_type = (info->shared | globals->pic_veneer)
3779                 /* PIC stubs.  */
3780                 ? ((globals->use_blx)
3781                    /* V5T and above.  */
3782                    ? arm_stub_long_branch_any_thumb_pic
3783                    /* V4T stub.  */
3784                    : arm_stub_long_branch_v4t_arm_thumb_pic)
3785
3786                 /* non-PIC stubs.  */
3787                 : ((globals->use_blx)
3788                    /* V5T and above.  */
3789                    ? arm_stub_long_branch_any_any
3790                    /* V4T.  */
3791                    : arm_stub_long_branch_v4t_arm_thumb);
3792             }
3793         }
3794       else
3795         {
3796           /* Arm to arm.  */
3797           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3798               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3799             {
3800               stub_type =
3801                 (info->shared | globals->pic_veneer)
3802                 /* PIC stubs.  */
3803                 ? (r_type == R_ARM_TLS_CALL
3804                    /* TLS PIC Stub */
3805                    ? arm_stub_long_branch_any_tls_pic
3806                    : arm_stub_long_branch_any_arm_pic)
3807                 /* non-PIC stubs.  */
3808                 : arm_stub_long_branch_any_any;
3809             }
3810         }
3811     }
3812
3813   /* If a stub is needed, record the actual destination type.  */
3814   if (stub_type != arm_stub_none)
3815     *actual_branch_type = branch_type;
3816
3817   return stub_type;
3818 }
3819
3820 /* Build a name for an entry in the stub hash table.  */
3821
3822 static char *
3823 elf32_arm_stub_name (const asection *input_section,
3824                      const asection *sym_sec,
3825                      const struct elf32_arm_link_hash_entry *hash,
3826                      const Elf_Internal_Rela *rel,
3827                      enum elf32_arm_stub_type stub_type)
3828 {
3829   char *stub_name;
3830   bfd_size_type len;
3831
3832   if (hash)
3833     {
3834       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
3835       stub_name = (char *) bfd_malloc (len);
3836       if (stub_name != NULL)
3837         sprintf (stub_name, "%08x_%s+%x_%d",
3838                  input_section->id & 0xffffffff,
3839                  hash->root.root.root.string,
3840                  (int) rel->r_addend & 0xffffffff,
3841                  (int) stub_type);
3842     }
3843   else
3844     {
3845       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
3846       stub_name = (char *) bfd_malloc (len);
3847       if (stub_name != NULL)
3848         sprintf (stub_name, "%08x_%x:%x+%x_%d",
3849                  input_section->id & 0xffffffff,
3850                  sym_sec->id & 0xffffffff,
3851                  ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
3852                  || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
3853                  ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
3854                  (int) rel->r_addend & 0xffffffff,
3855                  (int) stub_type);
3856     }
3857
3858   return stub_name;
3859 }
3860
3861 /* Look up an entry in the stub hash.  Stub entries are cached because
3862    creating the stub name takes a bit of time.  */
3863
3864 static struct elf32_arm_stub_hash_entry *
3865 elf32_arm_get_stub_entry (const asection *input_section,
3866                           const asection *sym_sec,
3867                           struct elf_link_hash_entry *hash,
3868                           const Elf_Internal_Rela *rel,
3869                           struct elf32_arm_link_hash_table *htab,
3870                           enum elf32_arm_stub_type stub_type)
3871 {
3872   struct elf32_arm_stub_hash_entry *stub_entry;
3873   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
3874   const asection *id_sec;
3875
3876   if ((input_section->flags & SEC_CODE) == 0)
3877     return NULL;
3878
3879   /* If this input section is part of a group of sections sharing one
3880      stub section, then use the id of the first section in the group.
3881      Stub names need to include a section id, as there may well be
3882      more than one stub used to reach say, printf, and we need to
3883      distinguish between them.  */
3884   id_sec = htab->stub_group[input_section->id].link_sec;
3885
3886   if (h != NULL && h->stub_cache != NULL
3887       && h->stub_cache->h == h
3888       && h->stub_cache->id_sec == id_sec
3889       && h->stub_cache->stub_type == stub_type)
3890     {
3891       stub_entry = h->stub_cache;
3892     }
3893   else
3894     {
3895       char *stub_name;
3896
3897       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
3898       if (stub_name == NULL)
3899         return NULL;
3900
3901       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
3902                                         stub_name, FALSE, FALSE);
3903       if (h != NULL)
3904         h->stub_cache = stub_entry;
3905
3906       free (stub_name);
3907     }
3908
3909   return stub_entry;
3910 }
3911
3912 /* Find or create a stub section.  Returns a pointer to the stub section, and
3913    the section to which the stub section will be attached (in *LINK_SEC_P).
3914    LINK_SEC_P may be NULL.  */
3915
3916 static asection *
3917 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
3918                                    struct elf32_arm_link_hash_table *htab)
3919 {
3920   asection *link_sec;
3921   asection *stub_sec;
3922
3923   link_sec = htab->stub_group[section->id].link_sec;
3924   BFD_ASSERT (link_sec != NULL);
3925   stub_sec = htab->stub_group[section->id].stub_sec;
3926
3927   if (stub_sec == NULL)
3928     {
3929       stub_sec = htab->stub_group[link_sec->id].stub_sec;
3930       if (stub_sec == NULL)
3931         {
3932           size_t namelen;
3933           bfd_size_type len;
3934           char *s_name;
3935
3936           namelen = strlen (link_sec->name);
3937           len = namelen + sizeof (STUB_SUFFIX);
3938           s_name = (char *) bfd_alloc (htab->stub_bfd, len);
3939           if (s_name == NULL)
3940             return NULL;
3941
3942           memcpy (s_name, link_sec->name, namelen);
3943           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3944           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3945           if (stub_sec == NULL)
3946             return NULL;
3947           htab->stub_group[link_sec->id].stub_sec = stub_sec;
3948         }
3949       htab->stub_group[section->id].stub_sec = stub_sec;
3950     }
3951
3952   if (link_sec_p)
3953     *link_sec_p = link_sec;
3954
3955   return stub_sec;
3956 }
3957
3958 /* Add a new stub entry to the stub hash.  Not all fields of the new
3959    stub entry are initialised.  */
3960
3961 static struct elf32_arm_stub_hash_entry *
3962 elf32_arm_add_stub (const char *stub_name,
3963                     asection *section,
3964                     struct elf32_arm_link_hash_table *htab)
3965 {
3966   asection *link_sec;
3967   asection *stub_sec;
3968   struct elf32_arm_stub_hash_entry *stub_entry;
3969
3970   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab);
3971   if (stub_sec == NULL)
3972     return NULL;
3973
3974   /* Enter this entry into the linker stub hash table.  */
3975   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3976                                      TRUE, FALSE);
3977   if (stub_entry == NULL)
3978     {
3979       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3980                              section->owner,
3981                              stub_name);
3982       return NULL;
3983     }
3984
3985   stub_entry->stub_sec = stub_sec;
3986   stub_entry->stub_offset = 0;
3987   stub_entry->id_sec = link_sec;
3988
3989   return stub_entry;
3990 }
3991
3992 /* Store an Arm insn into an output section not processed by
3993    elf32_arm_write_section.  */
3994
3995 static void
3996 put_arm_insn (struct elf32_arm_link_hash_table * htab,
3997               bfd * output_bfd, bfd_vma val, void * ptr)
3998 {
3999   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4000     bfd_putl32 (val, ptr);
4001   else
4002     bfd_putb32 (val, ptr);
4003 }
4004
4005 /* Store a 16-bit Thumb insn into an output section not processed by
4006    elf32_arm_write_section.  */
4007
4008 static void
4009 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4010                 bfd * output_bfd, bfd_vma val, void * ptr)
4011 {
4012   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4013     bfd_putl16 (val, ptr);
4014   else
4015     bfd_putb16 (val, ptr);
4016 }
4017
4018 /* If it's possible to change R_TYPE to a more efficient access
4019    model, return the new reloc type.  */
4020
4021 static unsigned
4022 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4023                           struct elf_link_hash_entry *h)
4024 {
4025   int is_local = (h == NULL);
4026
4027   if (info->shared || (h && h->root.type == bfd_link_hash_undefweak))
4028     return r_type;
4029
4030   /* We do not support relaxations for Old TLS models.  */
4031   switch (r_type)
4032     {
4033     case R_ARM_TLS_GOTDESC:
4034     case R_ARM_TLS_CALL:
4035     case R_ARM_THM_TLS_CALL:
4036     case R_ARM_TLS_DESCSEQ:
4037     case R_ARM_THM_TLS_DESCSEQ:
4038       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4039     }
4040
4041   return r_type;
4042 }
4043
4044 static bfd_reloc_status_type elf32_arm_final_link_relocate
4045   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4046    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4047    const char *, unsigned char, enum arm_st_branch_type,
4048    struct elf_link_hash_entry *, bfd_boolean *, char **);
4049
4050 static unsigned int
4051 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4052 {
4053   switch (stub_type)
4054     {
4055     case arm_stub_a8_veneer_b_cond:
4056     case arm_stub_a8_veneer_b:
4057     case arm_stub_a8_veneer_bl:
4058       return 2;
4059
4060     case arm_stub_long_branch_any_any:
4061     case arm_stub_long_branch_v4t_arm_thumb:
4062     case arm_stub_long_branch_thumb_only:
4063     case arm_stub_long_branch_v4t_thumb_thumb:
4064     case arm_stub_long_branch_v4t_thumb_arm:
4065     case arm_stub_short_branch_v4t_thumb_arm:
4066     case arm_stub_long_branch_any_arm_pic:
4067     case arm_stub_long_branch_any_thumb_pic:
4068     case arm_stub_long_branch_v4t_thumb_thumb_pic:
4069     case arm_stub_long_branch_v4t_arm_thumb_pic:
4070     case arm_stub_long_branch_v4t_thumb_arm_pic:
4071     case arm_stub_long_branch_thumb_only_pic:
4072     case arm_stub_long_branch_any_tls_pic:
4073     case arm_stub_long_branch_v4t_thumb_tls_pic:
4074     case arm_stub_a8_veneer_blx:
4075       return 4;
4076
4077     default:
4078       abort ();  /* Should be unreachable.  */
4079     }
4080 }
4081
4082 static bfd_boolean
4083 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4084                     void * in_arg)
4085 {
4086 #define MAXRELOCS 2
4087   struct elf32_arm_stub_hash_entry *stub_entry;
4088   struct elf32_arm_link_hash_table *globals;
4089   struct bfd_link_info *info;
4090   asection *stub_sec;
4091   bfd *stub_bfd;
4092   bfd_byte *loc;
4093   bfd_vma sym_value;
4094   int template_size;
4095   int size;
4096   const insn_sequence *template_sequence;
4097   int i;
4098   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4099   int stub_reloc_offset[MAXRELOCS] = {0, 0};
4100   int nrelocs = 0;
4101
4102   /* Massage our args to the form they really have.  */
4103   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4104   info = (struct bfd_link_info *) in_arg;
4105
4106   globals = elf32_arm_hash_table (info);
4107   if (globals == NULL)
4108     return FALSE;
4109
4110   stub_sec = stub_entry->stub_sec;
4111
4112   if ((globals->fix_cortex_a8 < 0)
4113       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4114     /* We have to do less-strictly-aligned fixes last.  */
4115     return TRUE;
4116
4117   /* Make a note of the offset within the stubs for this entry.  */
4118   stub_entry->stub_offset = stub_sec->size;
4119   loc = stub_sec->contents + stub_entry->stub_offset;
4120
4121   stub_bfd = stub_sec->owner;
4122
4123   /* This is the address of the stub destination.  */
4124   sym_value = (stub_entry->target_value
4125                + stub_entry->target_section->output_offset
4126                + stub_entry->target_section->output_section->vma);
4127
4128   template_sequence = stub_entry->stub_template;
4129   template_size = stub_entry->stub_template_size;
4130
4131   size = 0;
4132   for (i = 0; i < template_size; i++)
4133     {
4134       switch (template_sequence[i].type)
4135         {
4136         case THUMB16_TYPE:
4137           {
4138             bfd_vma data = (bfd_vma) template_sequence[i].data;
4139             if (template_sequence[i].reloc_addend != 0)
4140               {
4141                 /* We've borrowed the reloc_addend field to mean we should
4142                    insert a condition code into this (Thumb-1 branch)
4143                    instruction.  See THUMB16_BCOND_INSN.  */
4144                 BFD_ASSERT ((data & 0xff00) == 0xd000);
4145                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
4146               }
4147             bfd_put_16 (stub_bfd, data, loc + size);
4148             size += 2;
4149           }
4150           break;
4151
4152         case THUMB32_TYPE:
4153           bfd_put_16 (stub_bfd,
4154                       (template_sequence[i].data >> 16) & 0xffff,
4155                       loc + size);
4156           bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
4157                       loc + size + 2);
4158           if (template_sequence[i].r_type != R_ARM_NONE)
4159             {
4160               stub_reloc_idx[nrelocs] = i;
4161               stub_reloc_offset[nrelocs++] = size;
4162             }
4163           size += 4;
4164           break;
4165
4166         case ARM_TYPE:
4167           bfd_put_32 (stub_bfd, template_sequence[i].data,
4168                       loc + size);
4169           /* Handle cases where the target is encoded within the
4170              instruction.  */
4171           if (template_sequence[i].r_type == R_ARM_JUMP24)
4172             {
4173               stub_reloc_idx[nrelocs] = i;
4174               stub_reloc_offset[nrelocs++] = size;
4175             }
4176           size += 4;
4177           break;
4178
4179         case DATA_TYPE:
4180           bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
4181           stub_reloc_idx[nrelocs] = i;
4182           stub_reloc_offset[nrelocs++] = size;
4183           size += 4;
4184           break;
4185
4186         default:
4187           BFD_FAIL ();
4188           return FALSE;
4189         }
4190     }
4191
4192   stub_sec->size += size;
4193
4194   /* Stub size has already been computed in arm_size_one_stub. Check
4195      consistency.  */
4196   BFD_ASSERT (size == stub_entry->stub_size);
4197
4198   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
4199   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
4200     sym_value |= 1;
4201
4202   /* Assume there is at least one and at most MAXRELOCS entries to relocate
4203      in each stub.  */
4204   BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
4205
4206   for (i = 0; i < nrelocs; i++)
4207     if (template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP24
4208         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_JUMP19
4209         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_CALL
4210         || template_sequence[stub_reloc_idx[i]].r_type == R_ARM_THM_XPC22)
4211       {
4212         Elf_Internal_Rela rel;
4213         bfd_boolean unresolved_reloc;
4214         char *error_message;
4215         enum arm_st_branch_type branch_type
4216           = (template_sequence[stub_reloc_idx[i]].r_type != R_ARM_THM_XPC22
4217              ? ST_BRANCH_TO_THUMB : ST_BRANCH_TO_ARM);
4218         bfd_vma points_to = sym_value + stub_entry->target_addend;
4219
4220         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4221         rel.r_info = ELF32_R_INFO (0,
4222                                    template_sequence[stub_reloc_idx[i]].r_type);
4223         rel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
4224
4225         if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
4226           /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
4227              template should refer back to the instruction after the original
4228              branch.  */
4229           points_to = sym_value;
4230
4231         /* There may be unintended consequences if this is not true.  */
4232         BFD_ASSERT (stub_entry->h == NULL);
4233
4234         /* Note: _bfd_final_link_relocate doesn't handle these relocations
4235            properly.  We should probably use this function unconditionally,
4236            rather than only for certain relocations listed in the enclosing
4237            conditional, for the sake of consistency.  */
4238         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4239             (template_sequence[stub_reloc_idx[i]].r_type),
4240           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4241           points_to, info, stub_entry->target_section, "", STT_FUNC,
4242           branch_type, (struct elf_link_hash_entry *) stub_entry->h,
4243           &unresolved_reloc, &error_message);
4244       }
4245     else
4246       {
4247         Elf_Internal_Rela rel;
4248         bfd_boolean unresolved_reloc;
4249         char *error_message;
4250         bfd_vma points_to = sym_value + stub_entry->target_addend
4251           + template_sequence[stub_reloc_idx[i]].reloc_addend;
4252
4253         rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
4254         rel.r_info = ELF32_R_INFO (0,
4255                                    template_sequence[stub_reloc_idx[i]].r_type);
4256         rel.r_addend = 0;
4257
4258         elf32_arm_final_link_relocate (elf32_arm_howto_from_type
4259             (template_sequence[stub_reloc_idx[i]].r_type),
4260           stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
4261           points_to, info, stub_entry->target_section, "", STT_FUNC,
4262           stub_entry->branch_type,
4263           (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
4264           &error_message);
4265       }
4266
4267   return TRUE;
4268 #undef MAXRELOCS
4269 }
4270
4271 /* Calculate the template, template size and instruction size for a stub.
4272    Return value is the instruction size.  */
4273
4274 static unsigned int
4275 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
4276                              const insn_sequence **stub_template,
4277                              int *stub_template_size)
4278 {
4279   const insn_sequence *template_sequence = NULL;
4280   int template_size = 0, i;
4281   unsigned int size;
4282
4283   template_sequence = stub_definitions[stub_type].template_sequence;
4284   if (stub_template)
4285     *stub_template = template_sequence;
4286
4287   template_size = stub_definitions[stub_type].template_size;
4288   if (stub_template_size)
4289     *stub_template_size = template_size;
4290
4291   size = 0;
4292   for (i = 0; i < template_size; i++)
4293     {
4294       switch (template_sequence[i].type)
4295         {
4296         case THUMB16_TYPE:
4297           size += 2;
4298           break;
4299
4300         case ARM_TYPE:
4301         case THUMB32_TYPE:
4302         case DATA_TYPE:
4303           size += 4;
4304           break;
4305
4306         default:
4307           BFD_FAIL ();
4308           return 0;
4309         }
4310     }
4311
4312   return size;
4313 }
4314
4315 /* As above, but don't actually build the stub.  Just bump offset so
4316    we know stub section sizes.  */
4317
4318 static bfd_boolean
4319 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
4320                    void *in_arg ATTRIBUTE_UNUSED)
4321 {
4322   struct elf32_arm_stub_hash_entry *stub_entry;
4323   const insn_sequence *template_sequence;
4324   int template_size, size;
4325
4326   /* Massage our args to the form they really have.  */
4327   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4328
4329   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
4330              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
4331
4332   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
4333                                       &template_size);
4334
4335   stub_entry->stub_size = size;
4336   stub_entry->stub_template = template_sequence;
4337   stub_entry->stub_template_size = template_size;
4338
4339   size = (size + 7) & ~7;
4340   stub_entry->stub_sec->size += size;
4341
4342   return TRUE;
4343 }
4344
4345 /* External entry points for sizing and building linker stubs.  */
4346
4347 /* Set up various things so that we can make a list of input sections
4348    for each output section included in the link.  Returns -1 on error,
4349    0 when no stubs will be needed, and 1 on success.  */
4350
4351 int
4352 elf32_arm_setup_section_lists (bfd *output_bfd,
4353                                struct bfd_link_info *info)
4354 {
4355   bfd *input_bfd;
4356   unsigned int bfd_count;
4357   int top_id, top_index;
4358   asection *section;
4359   asection **input_list, **list;
4360   bfd_size_type amt;
4361   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4362
4363   if (htab == NULL)
4364     return 0;
4365   if (! is_elf_hash_table (htab))
4366     return 0;
4367
4368   /* Count the number of input BFDs and find the top input section id.  */
4369   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
4370        input_bfd != NULL;
4371        input_bfd = input_bfd->link_next)
4372     {
4373       bfd_count += 1;
4374       for (section = input_bfd->sections;
4375            section != NULL;
4376            section = section->next)
4377         {
4378           if (top_id < section->id)
4379             top_id = section->id;
4380         }
4381     }
4382   htab->bfd_count = bfd_count;
4383
4384   amt = sizeof (struct map_stub) * (top_id + 1);
4385   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4386   if (htab->stub_group == NULL)
4387     return -1;
4388   htab->top_id = top_id;
4389
4390   /* We can't use output_bfd->section_count here to find the top output
4391      section index as some sections may have been removed, and
4392      _bfd_strip_section_from_output doesn't renumber the indices.  */
4393   for (section = output_bfd->sections, top_index = 0;
4394        section != NULL;
4395        section = section->next)
4396     {
4397       if (top_index < section->index)
4398         top_index = section->index;
4399     }
4400
4401   htab->top_index = top_index;
4402   amt = sizeof (asection *) * (top_index + 1);
4403   input_list = (asection **) bfd_malloc (amt);
4404   htab->input_list = input_list;
4405   if (input_list == NULL)
4406     return -1;
4407
4408   /* For sections we aren't interested in, mark their entries with a
4409      value we can check later.  */
4410   list = input_list + top_index;
4411   do
4412     *list = bfd_abs_section_ptr;
4413   while (list-- != input_list);
4414
4415   for (section = output_bfd->sections;
4416        section != NULL;
4417        section = section->next)
4418     {
4419       if ((section->flags & SEC_CODE) != 0)
4420         input_list[section->index] = NULL;
4421     }
4422
4423   return 1;
4424 }
4425
4426 /* The linker repeatedly calls this function for each input section,
4427    in the order that input sections are linked into output sections.
4428    Build lists of input sections to determine groupings between which
4429    we may insert linker stubs.  */
4430
4431 void
4432 elf32_arm_next_input_section (struct bfd_link_info *info,
4433                               asection *isec)
4434 {
4435   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4436
4437   if (htab == NULL)
4438     return;
4439
4440   if (isec->output_section->index <= htab->top_index)
4441     {
4442       asection **list = htab->input_list + isec->output_section->index;
4443
4444       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
4445         {
4446           /* Steal the link_sec pointer for our list.  */
4447 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4448           /* This happens to make the list in reverse order,
4449              which we reverse later.  */
4450           PREV_SEC (isec) = *list;
4451           *list = isec;
4452         }
4453     }
4454 }
4455
4456 /* See whether we can group stub sections together.  Grouping stub
4457    sections may result in fewer stubs.  More importantly, we need to
4458    put all .init* and .fini* stubs at the end of the .init or
4459    .fini output sections respectively, because glibc splits the
4460    _init and _fini functions into multiple parts.  Putting a stub in
4461    the middle of a function is not a good idea.  */
4462
4463 static void
4464 group_sections (struct elf32_arm_link_hash_table *htab,
4465                 bfd_size_type stub_group_size,
4466                 bfd_boolean stubs_always_after_branch)
4467 {
4468   asection **list = htab->input_list;
4469
4470   do
4471     {
4472       asection *tail = *list;
4473       asection *head;
4474
4475       if (tail == bfd_abs_section_ptr)
4476         continue;
4477
4478       /* Reverse the list: we must avoid placing stubs at the
4479          beginning of the section because the beginning of the text
4480          section may be required for an interrupt vector in bare metal
4481          code.  */
4482 #define NEXT_SEC PREV_SEC
4483       head = NULL;
4484       while (tail != NULL)
4485         {
4486           /* Pop from tail.  */
4487           asection *item = tail;
4488           tail = PREV_SEC (item);
4489
4490           /* Push on head.  */
4491           NEXT_SEC (item) = head;
4492           head = item;
4493         }
4494
4495       while (head != NULL)
4496         {
4497           asection *curr;
4498           asection *next;
4499           bfd_vma stub_group_start = head->output_offset;
4500           bfd_vma end_of_next;
4501
4502           curr = head;
4503           while (NEXT_SEC (curr) != NULL)
4504             {
4505               next = NEXT_SEC (curr);
4506               end_of_next = next->output_offset + next->size;
4507               if (end_of_next - stub_group_start >= stub_group_size)
4508                 /* End of NEXT is too far from start, so stop.  */
4509                 break;
4510               /* Add NEXT to the group.  */
4511               curr = next;
4512             }
4513
4514           /* OK, the size from the start to the start of CURR is less
4515              than stub_group_size and thus can be handled by one stub
4516              section.  (Or the head section is itself larger than
4517              stub_group_size, in which case we may be toast.)
4518              We should really be keeping track of the total size of
4519              stubs added here, as stubs contribute to the final output
4520              section size.  */
4521           do
4522             {
4523               next = NEXT_SEC (head);
4524               /* Set up this stub group.  */
4525               htab->stub_group[head->id].link_sec = curr;
4526             }
4527           while (head != curr && (head = next) != NULL);
4528
4529           /* But wait, there's more!  Input sections up to stub_group_size
4530              bytes after the stub section can be handled by it too.  */
4531           if (!stubs_always_after_branch)
4532             {
4533               stub_group_start = curr->output_offset + curr->size;
4534
4535               while (next != NULL)
4536                 {
4537                   end_of_next = next->output_offset + next->size;
4538                   if (end_of_next - stub_group_start >= stub_group_size)
4539                     /* End of NEXT is too far from stubs, so stop.  */
4540                     break;
4541                   /* Add NEXT to the stub group.  */
4542                   head = next;
4543                   next = NEXT_SEC (head);
4544                   htab->stub_group[head->id].link_sec = curr;
4545                 }
4546             }
4547           head = next;
4548         }
4549     }
4550   while (list++ != htab->input_list + htab->top_index);
4551
4552   free (htab->input_list);
4553 #undef PREV_SEC
4554 #undef NEXT_SEC
4555 }
4556
4557 /* Comparison function for sorting/searching relocations relating to Cortex-A8
4558    erratum fix.  */
4559
4560 static int
4561 a8_reloc_compare (const void *a, const void *b)
4562 {
4563   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
4564   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
4565
4566   if (ra->from < rb->from)
4567     return -1;
4568   else if (ra->from > rb->from)
4569     return 1;
4570   else
4571     return 0;
4572 }
4573
4574 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
4575                                                     const char *, char **);
4576
4577 /* Helper function to scan code for sequences which might trigger the Cortex-A8
4578    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
4579    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
4580    otherwise.  */
4581
4582 static bfd_boolean
4583 cortex_a8_erratum_scan (bfd *input_bfd,
4584                         struct bfd_link_info *info,
4585                         struct a8_erratum_fix **a8_fixes_p,
4586                         unsigned int *num_a8_fixes_p,
4587                         unsigned int *a8_fix_table_size_p,
4588                         struct a8_erratum_reloc *a8_relocs,
4589                         unsigned int num_a8_relocs,
4590                         unsigned prev_num_a8_fixes,
4591                         bfd_boolean *stub_changed_p)
4592 {
4593   asection *section;
4594   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4595   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
4596   unsigned int num_a8_fixes = *num_a8_fixes_p;
4597   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
4598
4599   if (htab == NULL)
4600     return FALSE;
4601
4602   for (section = input_bfd->sections;
4603        section != NULL;
4604        section = section->next)
4605     {
4606       bfd_byte *contents = NULL;
4607       struct _arm_elf_section_data *sec_data;
4608       unsigned int span;
4609       bfd_vma base_vma;
4610
4611       if (elf_section_type (section) != SHT_PROGBITS
4612           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4613           || (section->flags & SEC_EXCLUDE) != 0
4614           || (section->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
4615           || (section->output_section == bfd_abs_section_ptr))
4616         continue;
4617
4618       base_vma = section->output_section->vma + section->output_offset;
4619
4620       if (elf_section_data (section)->this_hdr.contents != NULL)
4621         contents = elf_section_data (section)->this_hdr.contents;
4622       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4623         return TRUE;
4624
4625       sec_data = elf32_arm_section_data (section);
4626
4627       for (span = 0; span < sec_data->mapcount; span++)
4628         {
4629           unsigned int span_start = sec_data->map[span].vma;
4630           unsigned int span_end = (span == sec_data->mapcount - 1)
4631             ? section->size : sec_data->map[span + 1].vma;
4632           unsigned int i;
4633           char span_type = sec_data->map[span].type;
4634           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
4635
4636           if (span_type != 't')
4637             continue;
4638
4639           /* Span is entirely within a single 4KB region: skip scanning.  */
4640           if (((base_vma + span_start) & ~0xfff)
4641               == ((base_vma + span_end) & ~0xfff))
4642             continue;
4643
4644           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
4645
4646                * The opcode is BLX.W, BL.W, B.W, Bcc.W
4647                * The branch target is in the same 4KB region as the
4648                  first half of the branch.
4649                * The instruction before the branch is a 32-bit
4650                  length non-branch instruction.  */
4651           for (i = span_start; i < span_end;)
4652             {
4653               unsigned int insn = bfd_getl16 (&contents[i]);
4654               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
4655               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
4656
4657               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
4658                 insn_32bit = TRUE;
4659
4660               if (insn_32bit)
4661                 {
4662                   /* Load the rest of the insn (in manual-friendly order).  */
4663                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
4664
4665                   /* Encoding T4: B<c>.W.  */
4666                   is_b = (insn & 0xf800d000) == 0xf0009000;
4667                   /* Encoding T1: BL<c>.W.  */
4668                   is_bl = (insn & 0xf800d000) == 0xf000d000;
4669                   /* Encoding T2: BLX<c>.W.  */
4670                   is_blx = (insn & 0xf800d000) == 0xf000c000;
4671                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
4672                   is_bcc = (insn & 0xf800d000) == 0xf0008000
4673                            && (insn & 0x07f00000) != 0x03800000;
4674                 }
4675
4676               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
4677
4678               if (((base_vma + i) & 0xfff) == 0xffe
4679                   && insn_32bit
4680                   && is_32bit_branch
4681                   && last_was_32bit
4682                   && ! last_was_branch)
4683                 {
4684                   bfd_signed_vma offset = 0;
4685                   bfd_boolean force_target_arm = FALSE;
4686                   bfd_boolean force_target_thumb = FALSE;
4687                   bfd_vma target;
4688                   enum elf32_arm_stub_type stub_type = arm_stub_none;
4689                   struct a8_erratum_reloc key, *found;
4690                   bfd_boolean use_plt = FALSE;
4691
4692                   key.from = base_vma + i;
4693                   found = (struct a8_erratum_reloc *)
4694                       bsearch (&key, a8_relocs, num_a8_relocs,
4695                                sizeof (struct a8_erratum_reloc),
4696                                &a8_reloc_compare);
4697
4698                   if (found)
4699                     {
4700                       char *error_message = NULL;
4701                       struct elf_link_hash_entry *entry;
4702
4703                       /* We don't care about the error returned from this
4704                          function, only if there is glue or not.  */
4705                       entry = find_thumb_glue (info, found->sym_name,
4706                                                &error_message);
4707
4708                       if (entry)
4709                         found->non_a8_stub = TRUE;
4710
4711                       /* Keep a simpler condition, for the sake of clarity.  */
4712                       if (htab->root.splt != NULL && found->hash != NULL
4713                           && found->hash->root.plt.offset != (bfd_vma) -1)
4714                         use_plt = TRUE;
4715
4716                       if (found->r_type == R_ARM_THM_CALL)
4717                         {
4718                           if (found->branch_type == ST_BRANCH_TO_ARM
4719                               || use_plt)
4720                             force_target_arm = TRUE;
4721                           else
4722                             force_target_thumb = TRUE;
4723                         }
4724                     }
4725
4726                   /* Check if we have an offending branch instruction.  */
4727
4728                   if (found && found->non_a8_stub)
4729                     /* We've already made a stub for this instruction, e.g.
4730                        it's a long branch or a Thumb->ARM stub.  Assume that
4731                        stub will suffice to work around the A8 erratum (see
4732                        setting of always_after_branch above).  */
4733                     ;
4734                   else if (is_bcc)
4735                     {
4736                       offset = (insn & 0x7ff) << 1;
4737                       offset |= (insn & 0x3f0000) >> 4;
4738                       offset |= (insn & 0x2000) ? 0x40000 : 0;
4739                       offset |= (insn & 0x800) ? 0x80000 : 0;
4740                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
4741                       if (offset & 0x100000)
4742                         offset |= ~ ((bfd_signed_vma) 0xfffff);
4743                       stub_type = arm_stub_a8_veneer_b_cond;
4744                     }
4745                   else if (is_b || is_bl || is_blx)
4746                     {
4747                       int s = (insn & 0x4000000) != 0;
4748                       int j1 = (insn & 0x2000) != 0;
4749                       int j2 = (insn & 0x800) != 0;
4750                       int i1 = !(j1 ^ s);
4751                       int i2 = !(j2 ^ s);
4752
4753                       offset = (insn & 0x7ff) << 1;
4754                       offset |= (insn & 0x3ff0000) >> 4;
4755                       offset |= i2 << 22;
4756                       offset |= i1 << 23;
4757                       offset |= s << 24;
4758                       if (offset & 0x1000000)
4759                         offset |= ~ ((bfd_signed_vma) 0xffffff);
4760
4761                       if (is_blx)
4762                         offset &= ~ ((bfd_signed_vma) 3);
4763
4764                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
4765                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
4766                     }
4767
4768                   if (stub_type != arm_stub_none)
4769                     {
4770                       bfd_vma pc_for_insn = base_vma + i + 4;
4771
4772                       /* The original instruction is a BL, but the target is
4773                          an ARM instruction.  If we were not making a stub,
4774                          the BL would have been converted to a BLX.  Use the
4775                          BLX stub instead in that case.  */
4776                       if (htab->use_blx && force_target_arm
4777                           && stub_type == arm_stub_a8_veneer_bl)
4778                         {
4779                           stub_type = arm_stub_a8_veneer_blx;
4780                           is_blx = TRUE;
4781                           is_bl = FALSE;
4782                         }
4783                       /* Conversely, if the original instruction was
4784                          BLX but the target is Thumb mode, use the BL
4785                          stub.  */
4786                       else if (force_target_thumb
4787                                && stub_type == arm_stub_a8_veneer_blx)
4788                         {
4789                           stub_type = arm_stub_a8_veneer_bl;
4790                           is_blx = FALSE;
4791                           is_bl = TRUE;
4792                         }
4793
4794                       if (is_blx)
4795                         pc_for_insn &= ~ ((bfd_vma) 3);
4796
4797                       /* If we found a relocation, use the proper destination,
4798                          not the offset in the (unrelocated) instruction.
4799                          Note this is always done if we switched the stub type
4800                          above.  */
4801                       if (found)
4802                         offset =
4803                           (bfd_signed_vma) (found->destination - pc_for_insn);
4804
4805                       /* If the stub will use a Thumb-mode branch to a
4806                          PLT target, redirect it to the preceding Thumb
4807                          entry point.  */
4808                       if (stub_type != arm_stub_a8_veneer_blx && use_plt)
4809                         offset -= PLT_THUMB_STUB_SIZE;
4810
4811                       target = pc_for_insn + offset;
4812
4813                       /* The BLX stub is ARM-mode code.  Adjust the offset to
4814                          take the different PC value (+8 instead of +4) into
4815                          account.  */
4816                       if (stub_type == arm_stub_a8_veneer_blx)
4817                         offset += 4;
4818
4819                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
4820                         {
4821                           char *stub_name = NULL;
4822
4823                           if (num_a8_fixes == a8_fix_table_size)
4824                             {
4825                               a8_fix_table_size *= 2;
4826                               a8_fixes = (struct a8_erratum_fix *)
4827                                   bfd_realloc (a8_fixes,
4828                                                sizeof (struct a8_erratum_fix)
4829                                                * a8_fix_table_size);
4830                             }
4831
4832                           if (num_a8_fixes < prev_num_a8_fixes)
4833                             {
4834                               /* If we're doing a subsequent scan,
4835                                  check if we've found the same fix as
4836                                  before, and try and reuse the stub
4837                                  name.  */
4838                               stub_name = a8_fixes[num_a8_fixes].stub_name;
4839                               if ((a8_fixes[num_a8_fixes].section != section)
4840                                   || (a8_fixes[num_a8_fixes].offset != i))
4841                                 {
4842                                   free (stub_name);
4843                                   stub_name = NULL;
4844                                   *stub_changed_p = TRUE;
4845                                 }
4846                             }
4847
4848                           if (!stub_name)
4849                             {
4850                               stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
4851                               if (stub_name != NULL)
4852                                 sprintf (stub_name, "%x:%x", section->id, i);
4853                             }
4854
4855                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
4856                           a8_fixes[num_a8_fixes].section = section;
4857                           a8_fixes[num_a8_fixes].offset = i;
4858                           a8_fixes[num_a8_fixes].addend = offset;
4859                           a8_fixes[num_a8_fixes].orig_insn = insn;
4860                           a8_fixes[num_a8_fixes].stub_name = stub_name;
4861                           a8_fixes[num_a8_fixes].stub_type = stub_type;
4862                           a8_fixes[num_a8_fixes].branch_type =
4863                             is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
4864
4865                           num_a8_fixes++;
4866                         }
4867                     }
4868                 }
4869
4870               i += insn_32bit ? 4 : 2;
4871               last_was_32bit = insn_32bit;
4872               last_was_branch = is_32bit_branch;
4873             }
4874         }
4875
4876       if (elf_section_data (section)->this_hdr.contents == NULL)
4877         free (contents);
4878     }
4879
4880   *a8_fixes_p = a8_fixes;
4881   *num_a8_fixes_p = num_a8_fixes;
4882   *a8_fix_table_size_p = a8_fix_table_size;
4883
4884   return FALSE;
4885 }
4886
4887 /* Determine and set the size of the stub section for a final link.
4888
4889    The basic idea here is to examine all the relocations looking for
4890    PC-relative calls to a target that is unreachable with a "bl"
4891    instruction.  */
4892
4893 bfd_boolean
4894 elf32_arm_size_stubs (bfd *output_bfd,
4895                       bfd *stub_bfd,
4896                       struct bfd_link_info *info,
4897                       bfd_signed_vma group_size,
4898                       asection * (*add_stub_section) (const char *, asection *),
4899                       void (*layout_sections_again) (void))
4900 {
4901   bfd_size_type stub_group_size;
4902   bfd_boolean stubs_always_after_branch;
4903   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
4904   struct a8_erratum_fix *a8_fixes = NULL;
4905   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
4906   struct a8_erratum_reloc *a8_relocs = NULL;
4907   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
4908
4909   if (htab == NULL)
4910     return FALSE;
4911
4912   if (htab->fix_cortex_a8)
4913     {
4914       a8_fixes = (struct a8_erratum_fix *)
4915           bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
4916       a8_relocs = (struct a8_erratum_reloc *)
4917           bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
4918     }
4919
4920   /* Propagate mach to stub bfd, because it may not have been
4921      finalized when we created stub_bfd.  */
4922   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
4923                      bfd_get_mach (output_bfd));
4924
4925   /* Stash our params away.  */
4926   htab->stub_bfd = stub_bfd;
4927   htab->add_stub_section = add_stub_section;
4928   htab->layout_sections_again = layout_sections_again;
4929   stubs_always_after_branch = group_size < 0;
4930
4931   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
4932      as the first half of a 32-bit branch straddling two 4K pages.  This is a
4933      crude way of enforcing that.  */
4934   if (htab->fix_cortex_a8)
4935     stubs_always_after_branch = 1;
4936
4937   if (group_size < 0)
4938     stub_group_size = -group_size;
4939   else
4940     stub_group_size = group_size;
4941
4942   if (stub_group_size == 1)
4943     {
4944       /* Default values.  */
4945       /* Thumb branch range is +-4MB has to be used as the default
4946          maximum size (a given section can contain both ARM and Thumb
4947          code, so the worst case has to be taken into account).
4948
4949          This value is 24K less than that, which allows for 2025
4950          12-byte stubs.  If we exceed that, then we will fail to link.
4951          The user will have to relink with an explicit group size
4952          option.  */
4953       stub_group_size = 4170000;
4954     }
4955
4956   group_sections (htab, stub_group_size, stubs_always_after_branch);
4957
4958   /* If we're applying the cortex A8 fix, we need to determine the
4959      program header size now, because we cannot change it later --
4960      that could alter section placements.  Notice the A8 erratum fix
4961      ends up requiring the section addresses to remain unchanged
4962      modulo the page size.  That's something we cannot represent
4963      inside BFD, and we don't want to force the section alignment to
4964      be the page size.  */
4965   if (htab->fix_cortex_a8)
4966     (*htab->layout_sections_again) ();
4967
4968   while (1)
4969     {
4970       bfd *input_bfd;
4971       unsigned int bfd_indx;
4972       asection *stub_sec;
4973       bfd_boolean stub_changed = FALSE;
4974       unsigned prev_num_a8_fixes = num_a8_fixes;
4975
4976       num_a8_fixes = 0;
4977       for (input_bfd = info->input_bfds, bfd_indx = 0;
4978            input_bfd != NULL;
4979            input_bfd = input_bfd->link_next, bfd_indx++)
4980         {
4981           Elf_Internal_Shdr *symtab_hdr;
4982           asection *section;
4983           Elf_Internal_Sym *local_syms = NULL;
4984
4985           num_a8_relocs = 0;
4986
4987           /* We'll need the symbol table in a second.  */
4988           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4989           if (symtab_hdr->sh_info == 0)
4990             continue;
4991
4992           /* Walk over each section attached to the input bfd.  */
4993           for (section = input_bfd->sections;
4994                section != NULL;
4995                section = section->next)
4996             {
4997               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4998
4999               /* If there aren't any relocs, then there's nothing more
5000                  to do.  */
5001               if ((section->flags & SEC_RELOC) == 0
5002                   || section->reloc_count == 0
5003                   || (section->flags & SEC_CODE) == 0)
5004                 continue;
5005
5006               /* If this section is a link-once section that will be
5007                  discarded, then don't create any stubs.  */
5008               if (section->output_section == NULL
5009                   || section->output_section->owner != output_bfd)
5010                 continue;
5011
5012               /* Get the relocs.  */
5013               internal_relocs
5014                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
5015                                              NULL, info->keep_memory);
5016               if (internal_relocs == NULL)
5017                 goto error_ret_free_local;
5018
5019               /* Now examine each relocation.  */
5020               irela = internal_relocs;
5021               irelaend = irela + section->reloc_count;
5022               for (; irela < irelaend; irela++)
5023                 {
5024                   unsigned int r_type, r_indx;
5025                   enum elf32_arm_stub_type stub_type;
5026                   struct elf32_arm_stub_hash_entry *stub_entry;
5027                   asection *sym_sec;
5028                   bfd_vma sym_value;
5029                   bfd_vma destination;
5030                   struct elf32_arm_link_hash_entry *hash;
5031                   const char *sym_name;
5032                   char *stub_name;
5033                   const asection *id_sec;
5034                   unsigned char st_type;
5035                   enum arm_st_branch_type branch_type;
5036                   bfd_boolean created_stub = FALSE;
5037
5038                   r_type = ELF32_R_TYPE (irela->r_info);
5039                   r_indx = ELF32_R_SYM (irela->r_info);
5040
5041                   if (r_type >= (unsigned int) R_ARM_max)
5042                     {
5043                       bfd_set_error (bfd_error_bad_value);
5044                     error_ret_free_internal:
5045                       if (elf_section_data (section)->relocs == NULL)
5046                         free (internal_relocs);
5047                       goto error_ret_free_local;
5048                     }
5049
5050                   hash = NULL;
5051                   if (r_indx >= symtab_hdr->sh_info)
5052                     hash = elf32_arm_hash_entry
5053                       (elf_sym_hashes (input_bfd)
5054                        [r_indx - symtab_hdr->sh_info]);
5055
5056                   /* Only look for stubs on branch instructions, or
5057                      non-relaxed TLSCALL  */
5058                   if ((r_type != (unsigned int) R_ARM_CALL)
5059                       && (r_type != (unsigned int) R_ARM_THM_CALL)
5060                       && (r_type != (unsigned int) R_ARM_JUMP24)
5061                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
5062                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
5063                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
5064                       && (r_type != (unsigned int) R_ARM_PLT32)
5065                       && !((r_type == (unsigned int) R_ARM_TLS_CALL
5066                             || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5067                            && r_type == elf32_arm_tls_transition
5068                                (info, r_type, &hash->root)
5069                            && ((hash ? hash->tls_type
5070                                 : (elf32_arm_local_got_tls_type
5071                                    (input_bfd)[r_indx]))
5072                                & GOT_TLS_GDESC) != 0))
5073                     continue;
5074
5075                   /* Now determine the call target, its name, value,
5076                      section.  */
5077                   sym_sec = NULL;
5078                   sym_value = 0;
5079                   destination = 0;
5080                   sym_name = NULL;
5081
5082                   if (r_type == (unsigned int) R_ARM_TLS_CALL
5083                       || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
5084                     {
5085                       /* A non-relaxed TLS call.  The target is the
5086                          plt-resident trampoline and nothing to do
5087                          with the symbol.  */
5088                       BFD_ASSERT (htab->tls_trampoline > 0);
5089                       sym_sec = htab->root.splt;
5090                       sym_value = htab->tls_trampoline;
5091                       hash = 0;
5092                       st_type = STT_FUNC;
5093                       branch_type = ST_BRANCH_TO_ARM;
5094                     }
5095                   else if (!hash)
5096                     {
5097                       /* It's a local symbol.  */
5098                       Elf_Internal_Sym *sym;
5099
5100                       if (local_syms == NULL)
5101                         {
5102                           local_syms
5103                             = (Elf_Internal_Sym *) symtab_hdr->contents;
5104                           if (local_syms == NULL)
5105                             local_syms
5106                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5107                                                       symtab_hdr->sh_info, 0,
5108                                                       NULL, NULL, NULL);
5109                           if (local_syms == NULL)
5110                             goto error_ret_free_internal;
5111                         }
5112
5113                       sym = local_syms + r_indx;
5114                       if (sym->st_shndx == SHN_UNDEF)
5115                         sym_sec = bfd_und_section_ptr;
5116                       else if (sym->st_shndx == SHN_ABS)
5117                         sym_sec = bfd_abs_section_ptr;
5118                       else if (sym->st_shndx == SHN_COMMON)
5119                         sym_sec = bfd_com_section_ptr;
5120                       else
5121                         sym_sec =
5122                           bfd_section_from_elf_index (input_bfd, sym->st_shndx);
5123
5124                       if (!sym_sec)
5125                         /* This is an undefined symbol.  It can never
5126                            be resolved. */
5127                         continue;
5128
5129                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5130                         sym_value = sym->st_value;
5131                       destination = (sym_value + irela->r_addend
5132                                      + sym_sec->output_offset
5133                                      + sym_sec->output_section->vma);
5134                       st_type = ELF_ST_TYPE (sym->st_info);
5135                       branch_type = ARM_SYM_BRANCH_TYPE (sym);
5136                       sym_name
5137                         = bfd_elf_string_from_elf_section (input_bfd,
5138                                                            symtab_hdr->sh_link,
5139                                                            sym->st_name);
5140                     }
5141                   else
5142                     {
5143                       /* It's an external symbol.  */
5144                       while (hash->root.root.type == bfd_link_hash_indirect
5145                              || hash->root.root.type == bfd_link_hash_warning)
5146                         hash = ((struct elf32_arm_link_hash_entry *)
5147                                 hash->root.root.u.i.link);
5148
5149                       if (hash->root.root.type == bfd_link_hash_defined
5150                           || hash->root.root.type == bfd_link_hash_defweak)
5151                         {
5152                           sym_sec = hash->root.root.u.def.section;
5153                           sym_value = hash->root.root.u.def.value;
5154
5155                           struct elf32_arm_link_hash_table *globals =
5156                                                   elf32_arm_hash_table (info);
5157
5158                           /* For a destination in a shared library,
5159                              use the PLT stub as target address to
5160                              decide whether a branch stub is
5161                              needed.  */
5162                           if (globals != NULL
5163                               && globals->root.splt != NULL
5164                               && hash != NULL
5165                               && hash->root.plt.offset != (bfd_vma) -1)
5166                             {
5167                               sym_sec = globals->root.splt;
5168                               sym_value = hash->root.plt.offset;
5169                               if (sym_sec->output_section != NULL)
5170                                 destination = (sym_value
5171                                                + sym_sec->output_offset
5172                                                + sym_sec->output_section->vma);
5173                             }
5174                           else if (sym_sec->output_section != NULL)
5175                             destination = (sym_value + irela->r_addend
5176                                            + sym_sec->output_offset
5177                                            + sym_sec->output_section->vma);
5178                         }
5179                       else if ((hash->root.root.type == bfd_link_hash_undefined)
5180                                || (hash->root.root.type == bfd_link_hash_undefweak))
5181                         {
5182                           /* For a shared library, use the PLT stub as
5183                              target address to decide whether a long
5184                              branch stub is needed.
5185                              For absolute code, they cannot be handled.  */
5186                           struct elf32_arm_link_hash_table *globals =
5187                             elf32_arm_hash_table (info);
5188
5189                           if (globals != NULL
5190                               && globals->root.splt != NULL
5191                               && hash != NULL
5192                               && hash->root.plt.offset != (bfd_vma) -1)
5193                             {
5194                               sym_sec = globals->root.splt;
5195                               sym_value = hash->root.plt.offset;
5196                               if (sym_sec->output_section != NULL)
5197                                 destination = (sym_value
5198                                                + sym_sec->output_offset
5199                                                + sym_sec->output_section->vma);
5200                             }
5201                           else
5202                             continue;
5203                         }
5204                       else
5205                         {
5206                           bfd_set_error (bfd_error_bad_value);
5207                           goto error_ret_free_internal;
5208                         }
5209                       st_type = hash->root.type;
5210                       branch_type = hash->root.target_internal;
5211                       sym_name = hash->root.root.root.string;
5212                     }
5213
5214                   do
5215                     {
5216                       /* Determine what (if any) linker stub is needed.  */
5217                       stub_type = arm_type_of_stub (info, section, irela,
5218                                                     st_type, &branch_type,
5219                                                     hash, destination, sym_sec,
5220                                                     input_bfd, sym_name);
5221                       if (stub_type == arm_stub_none)
5222                         break;
5223
5224                       /* Support for grouping stub sections.  */
5225                       id_sec = htab->stub_group[section->id].link_sec;
5226
5227                       /* Get the name of this stub.  */
5228                       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash,
5229                                                        irela, stub_type);
5230                       if (!stub_name)
5231                         goto error_ret_free_internal;
5232
5233                       /* We've either created a stub for this reloc already,
5234                          or we are about to.  */
5235                       created_stub = TRUE;
5236
5237                       stub_entry = arm_stub_hash_lookup
5238                                      (&htab->stub_hash_table, stub_name,
5239                                       FALSE, FALSE);
5240                       if (stub_entry != NULL)
5241                         {
5242                           /* The proper stub has already been created.  */
5243                           free (stub_name);
5244                           stub_entry->target_value = sym_value;
5245                           break;
5246                         }
5247
5248                       stub_entry = elf32_arm_add_stub (stub_name, section,
5249                                                        htab);
5250                       if (stub_entry == NULL)
5251                         {
5252                           free (stub_name);
5253                           goto error_ret_free_internal;
5254                         }
5255
5256                       stub_entry->target_value = sym_value;
5257                       stub_entry->target_section = sym_sec;
5258                       stub_entry->stub_type = stub_type;
5259                       stub_entry->h = hash;
5260                       stub_entry->branch_type = branch_type;
5261
5262                       if (sym_name == NULL)
5263                         sym_name = "unnamed";
5264                       stub_entry->output_name = (char *)
5265                           bfd_alloc (htab->stub_bfd,
5266                                      sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5267                                      + strlen (sym_name));
5268                       if (stub_entry->output_name == NULL)
5269                         {
5270                           free (stub_name);
5271                           goto error_ret_free_internal;
5272                         }
5273
5274                       /* For historical reasons, use the existing names for
5275                          ARM-to-Thumb and Thumb-to-ARM stubs.  */
5276                       if ((r_type == (unsigned int) R_ARM_THM_CALL
5277                            || r_type == (unsigned int) R_ARM_THM_JUMP24)
5278                           && branch_type == ST_BRANCH_TO_ARM)
5279                         sprintf (stub_entry->output_name,
5280                                  THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5281                       else if ((r_type == (unsigned int) R_ARM_CALL
5282                                || r_type == (unsigned int) R_ARM_JUMP24)
5283                                && branch_type == ST_BRANCH_TO_THUMB)
5284                         sprintf (stub_entry->output_name,
5285                                  ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5286                       else
5287                         sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
5288                                  sym_name);
5289
5290                       stub_changed = TRUE;
5291                     }
5292                   while (0);
5293
5294                   /* Look for relocations which might trigger Cortex-A8
5295                      erratum.  */
5296                   if (htab->fix_cortex_a8
5297                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
5298                           || r_type == (unsigned int) R_ARM_THM_JUMP19
5299                           || r_type == (unsigned int) R_ARM_THM_CALL
5300                           || r_type == (unsigned int) R_ARM_THM_XPC22))
5301                     {
5302                       bfd_vma from = section->output_section->vma
5303                                      + section->output_offset
5304                                      + irela->r_offset;
5305
5306                       if ((from & 0xfff) == 0xffe)
5307                         {
5308                           /* Found a candidate.  Note we haven't checked the
5309                              destination is within 4K here: if we do so (and
5310                              don't create an entry in a8_relocs) we can't tell
5311                              that a branch should have been relocated when
5312                              scanning later.  */
5313                           if (num_a8_relocs == a8_reloc_table_size)
5314                             {
5315                               a8_reloc_table_size *= 2;
5316                               a8_relocs = (struct a8_erratum_reloc *)
5317                                   bfd_realloc (a8_relocs,
5318                                                sizeof (struct a8_erratum_reloc)
5319                                                * a8_reloc_table_size);
5320                             }
5321
5322                           a8_relocs[num_a8_relocs].from = from;
5323                           a8_relocs[num_a8_relocs].destination = destination;
5324                           a8_relocs[num_a8_relocs].r_type = r_type;
5325                           a8_relocs[num_a8_relocs].branch_type = branch_type;
5326                           a8_relocs[num_a8_relocs].sym_name = sym_name;
5327                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
5328                           a8_relocs[num_a8_relocs].hash = hash;
5329
5330                           num_a8_relocs++;
5331                         }
5332                     }
5333                 }
5334
5335               /* We're done with the internal relocs, free them.  */
5336               if (elf_section_data (section)->relocs == NULL)
5337                 free (internal_relocs);
5338             }
5339
5340           if (htab->fix_cortex_a8)
5341             {
5342               /* Sort relocs which might apply to Cortex-A8 erratum.  */
5343               qsort (a8_relocs, num_a8_relocs,
5344                      sizeof (struct a8_erratum_reloc),
5345                      &a8_reloc_compare);
5346
5347               /* Scan for branches which might trigger Cortex-A8 erratum.  */
5348               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
5349                                           &num_a8_fixes, &a8_fix_table_size,
5350                                           a8_relocs, num_a8_relocs,
5351                                           prev_num_a8_fixes, &stub_changed)
5352                   != 0)
5353                 goto error_ret_free_local;
5354             }
5355         }
5356
5357       if (prev_num_a8_fixes != num_a8_fixes)
5358         stub_changed = TRUE;
5359
5360       if (!stub_changed)
5361         break;
5362
5363       /* OK, we've added some stubs.  Find out the new size of the
5364          stub sections.  */
5365       for (stub_sec = htab->stub_bfd->sections;
5366            stub_sec != NULL;
5367            stub_sec = stub_sec->next)
5368         {
5369           /* Ignore non-stub sections.  */
5370           if (!strstr (stub_sec->name, STUB_SUFFIX))
5371             continue;
5372
5373           stub_sec->size = 0;
5374         }
5375
5376       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
5377
5378       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
5379       if (htab->fix_cortex_a8)
5380         for (i = 0; i < num_a8_fixes; i++)
5381           {
5382             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
5383                          a8_fixes[i].section, htab);
5384
5385             if (stub_sec == NULL)
5386               goto error_ret_free_local;
5387
5388             stub_sec->size
5389               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
5390                                               NULL);
5391           }
5392
5393
5394       /* Ask the linker to do its stuff.  */
5395       (*htab->layout_sections_again) ();
5396     }
5397
5398   /* Add stubs for Cortex-A8 erratum fixes now.  */
5399   if (htab->fix_cortex_a8)
5400     {
5401       for (i = 0; i < num_a8_fixes; i++)
5402         {
5403           struct elf32_arm_stub_hash_entry *stub_entry;
5404           char *stub_name = a8_fixes[i].stub_name;
5405           asection *section = a8_fixes[i].section;
5406           unsigned int section_id = a8_fixes[i].section->id;
5407           asection *link_sec = htab->stub_group[section_id].link_sec;
5408           asection *stub_sec = htab->stub_group[section_id].stub_sec;
5409           const insn_sequence *template_sequence;
5410           int template_size, size = 0;
5411
5412           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
5413                                              TRUE, FALSE);
5414           if (stub_entry == NULL)
5415             {
5416               (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
5417                                      section->owner,
5418                                      stub_name);
5419               return FALSE;
5420             }
5421
5422           stub_entry->stub_sec = stub_sec;
5423           stub_entry->stub_offset = 0;
5424           stub_entry->id_sec = link_sec;
5425           stub_entry->stub_type = a8_fixes[i].stub_type;
5426           stub_entry->target_section = a8_fixes[i].section;
5427           stub_entry->target_value = a8_fixes[i].offset;
5428           stub_entry->target_addend = a8_fixes[i].addend;
5429           stub_entry->orig_insn = a8_fixes[i].orig_insn;
5430           stub_entry->branch_type = a8_fixes[i].branch_type;
5431
5432           size = find_stub_size_and_template (a8_fixes[i].stub_type,
5433                                               &template_sequence,
5434                                               &template_size);
5435
5436           stub_entry->stub_size = size;
5437           stub_entry->stub_template = template_sequence;
5438           stub_entry->stub_template_size = template_size;
5439         }
5440
5441       /* Stash the Cortex-A8 erratum fix array for use later in
5442          elf32_arm_write_section().  */
5443       htab->a8_erratum_fixes = a8_fixes;
5444       htab->num_a8_erratum_fixes = num_a8_fixes;
5445     }
5446   else
5447     {
5448       htab->a8_erratum_fixes = NULL;
5449       htab->num_a8_erratum_fixes = 0;
5450     }
5451   return TRUE;
5452
5453  error_ret_free_local:
5454   return FALSE;
5455 }
5456
5457 /* Build all the stubs associated with the current output file.  The
5458    stubs are kept in a hash table attached to the main linker hash
5459    table.  We also set up the .plt entries for statically linked PIC
5460    functions here.  This function is called via arm_elf_finish in the
5461    linker.  */
5462
5463 bfd_boolean
5464 elf32_arm_build_stubs (struct bfd_link_info *info)
5465 {
5466   asection *stub_sec;
5467   struct bfd_hash_table *table;
5468   struct elf32_arm_link_hash_table *htab;
5469
5470   htab = elf32_arm_hash_table (info);
5471   if (htab == NULL)
5472     return FALSE;
5473
5474   for (stub_sec = htab->stub_bfd->sections;
5475        stub_sec != NULL;
5476        stub_sec = stub_sec->next)
5477     {
5478       bfd_size_type size;
5479
5480       /* Ignore non-stub sections.  */
5481       if (!strstr (stub_sec->name, STUB_SUFFIX))
5482         continue;
5483
5484       /* Allocate memory to hold the linker stubs.  */
5485       size = stub_sec->size;
5486       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
5487       if (stub_sec->contents == NULL && size != 0)
5488         return FALSE;
5489       stub_sec->size = 0;
5490     }
5491
5492   /* Build the stubs as directed by the stub hash table.  */
5493   table = &htab->stub_hash_table;
5494   bfd_hash_traverse (table, arm_build_one_stub, info);
5495   if (htab->fix_cortex_a8)
5496     {
5497       /* Place the cortex a8 stubs last.  */
5498       htab->fix_cortex_a8 = -1;
5499       bfd_hash_traverse (table, arm_build_one_stub, info);
5500     }
5501
5502   return TRUE;
5503 }
5504
5505 /* Locate the Thumb encoded calling stub for NAME.  */
5506
5507 static struct elf_link_hash_entry *
5508 find_thumb_glue (struct bfd_link_info *link_info,
5509                  const char *name,
5510                  char **error_message)
5511 {
5512   char *tmp_name;
5513   struct elf_link_hash_entry *hash;
5514   struct elf32_arm_link_hash_table *hash_table;
5515
5516   /* We need a pointer to the armelf specific hash table.  */
5517   hash_table = elf32_arm_hash_table (link_info);
5518   if (hash_table == NULL)
5519     return NULL;
5520
5521   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5522                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
5523
5524   BFD_ASSERT (tmp_name);
5525
5526   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
5527
5528   hash = elf_link_hash_lookup
5529     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5530
5531   if (hash == NULL
5532       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
5533                    tmp_name, name) == -1)
5534     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5535
5536   free (tmp_name);
5537
5538   return hash;
5539 }
5540
5541 /* Locate the ARM encoded calling stub for NAME.  */
5542
5543 static struct elf_link_hash_entry *
5544 find_arm_glue (struct bfd_link_info *link_info,
5545                const char *name,
5546                char **error_message)
5547 {
5548   char *tmp_name;
5549   struct elf_link_hash_entry *myh;
5550   struct elf32_arm_link_hash_table *hash_table;
5551
5552   /* We need a pointer to the elfarm specific hash table.  */
5553   hash_table = elf32_arm_hash_table (link_info);
5554   if (hash_table == NULL)
5555     return NULL;
5556
5557   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5558                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5559
5560   BFD_ASSERT (tmp_name);
5561
5562   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5563
5564   myh = elf_link_hash_lookup
5565     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
5566
5567   if (myh == NULL
5568       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
5569                    tmp_name, name) == -1)
5570     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
5571
5572   free (tmp_name);
5573
5574   return myh;
5575 }
5576
5577 /* ARM->Thumb glue (static images):
5578
5579    .arm
5580    __func_from_arm:
5581    ldr r12, __func_addr
5582    bx  r12
5583    __func_addr:
5584    .word func    @ behave as if you saw a ARM_32 reloc.
5585
5586    (v5t static images)
5587    .arm
5588    __func_from_arm:
5589    ldr pc, __func_addr
5590    __func_addr:
5591    .word func    @ behave as if you saw a ARM_32 reloc.
5592
5593    (relocatable images)
5594    .arm
5595    __func_from_arm:
5596    ldr r12, __func_offset
5597    add r12, r12, pc
5598    bx  r12
5599    __func_offset:
5600    .word func - .   */
5601
5602 #define ARM2THUMB_STATIC_GLUE_SIZE 12
5603 static const insn32 a2t1_ldr_insn = 0xe59fc000;
5604 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
5605 static const insn32 a2t3_func_addr_insn = 0x00000001;
5606
5607 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
5608 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
5609 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
5610
5611 #define ARM2THUMB_PIC_GLUE_SIZE 16
5612 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
5613 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
5614 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
5615
5616 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
5617
5618      .thumb                             .thumb
5619      .align 2                           .align 2
5620  __func_from_thumb:                 __func_from_thumb:
5621      bx pc                              push {r6, lr}
5622      nop                                ldr  r6, __func_addr
5623      .arm                               mov  lr, pc
5624      b func                             bx   r6
5625                                         .arm
5626                                     ;; back_to_thumb
5627                                         ldmia r13! {r6, lr}
5628                                         bx    lr
5629                                     __func_addr:
5630                                         .word        func  */
5631
5632 #define THUMB2ARM_GLUE_SIZE 8
5633 static const insn16 t2a1_bx_pc_insn = 0x4778;
5634 static const insn16 t2a2_noop_insn = 0x46c0;
5635 static const insn32 t2a3_b_insn = 0xea000000;
5636
5637 #define VFP11_ERRATUM_VENEER_SIZE 8
5638
5639 #define ARM_BX_VENEER_SIZE 12
5640 static const insn32 armbx1_tst_insn = 0xe3100001;
5641 static const insn32 armbx2_moveq_insn = 0x01a0f000;
5642 static const insn32 armbx3_bx_insn = 0xe12fff10;
5643
5644 #ifndef ELFARM_NABI_C_INCLUDED
5645 static void
5646 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
5647 {
5648   asection * s;
5649   bfd_byte * contents;
5650
5651   if (size == 0)
5652     {
5653       /* Do not include empty glue sections in the output.  */
5654       if (abfd != NULL)
5655         {
5656           s = bfd_get_section_by_name (abfd, name);
5657           if (s != NULL)
5658             s->flags |= SEC_EXCLUDE;
5659         }
5660       return;
5661     }
5662
5663   BFD_ASSERT (abfd != NULL);
5664
5665   s = bfd_get_section_by_name (abfd, name);
5666   BFD_ASSERT (s != NULL);
5667
5668   contents = (bfd_byte *) bfd_alloc (abfd, size);
5669
5670   BFD_ASSERT (s->size == size);
5671   s->contents = contents;
5672 }
5673
5674 bfd_boolean
5675 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
5676 {
5677   struct elf32_arm_link_hash_table * globals;
5678
5679   globals = elf32_arm_hash_table (info);
5680   BFD_ASSERT (globals != NULL);
5681
5682   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5683                                    globals->arm_glue_size,
5684                                    ARM2THUMB_GLUE_SECTION_NAME);
5685
5686   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5687                                    globals->thumb_glue_size,
5688                                    THUMB2ARM_GLUE_SECTION_NAME);
5689
5690   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5691                                    globals->vfp11_erratum_glue_size,
5692                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
5693
5694   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
5695                                    globals->bx_glue_size,
5696                                    ARM_BX_GLUE_SECTION_NAME);
5697
5698   return TRUE;
5699 }
5700
5701 /* Allocate space and symbols for calling a Thumb function from Arm mode.
5702    returns the symbol identifying the stub.  */
5703
5704 static struct elf_link_hash_entry *
5705 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
5706                           struct elf_link_hash_entry * h)
5707 {
5708   const char * name = h->root.root.string;
5709   asection * s;
5710   char * tmp_name;
5711   struct elf_link_hash_entry * myh;
5712   struct bfd_link_hash_entry * bh;
5713   struct elf32_arm_link_hash_table * globals;
5714   bfd_vma val;
5715   bfd_size_type size;
5716
5717   globals = elf32_arm_hash_table (link_info);
5718   BFD_ASSERT (globals != NULL);
5719   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5720
5721   s = bfd_get_section_by_name
5722     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
5723
5724   BFD_ASSERT (s != NULL);
5725
5726   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
5727                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
5728
5729   BFD_ASSERT (tmp_name);
5730
5731   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
5732
5733   myh = elf_link_hash_lookup
5734     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
5735
5736   if (myh != NULL)
5737     {
5738       /* We've already seen this guy.  */
5739       free (tmp_name);
5740       return myh;
5741     }
5742
5743   /* The only trick here is using hash_table->arm_glue_size as the value.
5744      Even though the section isn't allocated yet, this is where we will be
5745      putting it.  The +1 on the value marks that the stub has not been
5746      output yet - not that it is a Thumb function.  */
5747   bh = NULL;
5748   val = globals->arm_glue_size + 1;
5749   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
5750                                     tmp_name, BSF_GLOBAL, s, val,
5751                                     NULL, TRUE, FALSE, &bh);
5752
5753   myh = (struct elf_link_hash_entry *) bh;
5754   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5755   myh->forced_local = 1;
5756
5757   free (tmp_name);
5758
5759   if (link_info->shared || globals->root.is_relocatable_executable
5760       || globals->pic_veneer)
5761     size = ARM2THUMB_PIC_GLUE_SIZE;
5762   else if (globals->use_blx)
5763     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
5764   else
5765     size = ARM2THUMB_STATIC_GLUE_SIZE;
5766
5767   s->size += size;
5768   globals->arm_glue_size += size;
5769
5770   return myh;
5771 }
5772
5773 /* Allocate space for ARMv4 BX veneers.  */
5774
5775 static void
5776 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
5777 {
5778   asection * s;
5779   struct elf32_arm_link_hash_table *globals;
5780   char *tmp_name;
5781   struct elf_link_hash_entry *myh;
5782   struct bfd_link_hash_entry *bh;
5783   bfd_vma val;
5784
5785   /* BX PC does not need a veneer.  */
5786   if (reg == 15)
5787     return;
5788
5789   globals = elf32_arm_hash_table (link_info);
5790   BFD_ASSERT (globals != NULL);
5791   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5792
5793   /* Check if this veneer has already been allocated.  */
5794   if (globals->bx_glue_offset[reg])
5795     return;
5796
5797   s = bfd_get_section_by_name
5798     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
5799
5800   BFD_ASSERT (s != NULL);
5801
5802   /* Add symbol for veneer.  */
5803   tmp_name = (char *)
5804       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
5805
5806   BFD_ASSERT (tmp_name);
5807
5808   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
5809
5810   myh = elf_link_hash_lookup
5811     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
5812
5813   BFD_ASSERT (myh == NULL);
5814
5815   bh = NULL;
5816   val = globals->bx_glue_size;
5817   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
5818                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
5819                                     NULL, TRUE, FALSE, &bh);
5820
5821   myh = (struct elf_link_hash_entry *) bh;
5822   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5823   myh->forced_local = 1;
5824
5825   s->size += ARM_BX_VENEER_SIZE;
5826   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
5827   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
5828 }
5829
5830
5831 /* Add an entry to the code/data map for section SEC.  */
5832
5833 static void
5834 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
5835 {
5836   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
5837   unsigned int newidx;
5838
5839   if (sec_data->map == NULL)
5840     {
5841       sec_data->map = (elf32_arm_section_map *)
5842           bfd_malloc (sizeof (elf32_arm_section_map));
5843       sec_data->mapcount = 0;
5844       sec_data->mapsize = 1;
5845     }
5846
5847   newidx = sec_data->mapcount++;
5848
5849   if (sec_data->mapcount > sec_data->mapsize)
5850     {
5851       sec_data->mapsize *= 2;
5852       sec_data->map = (elf32_arm_section_map *)
5853           bfd_realloc_or_free (sec_data->map, sec_data->mapsize
5854                                * sizeof (elf32_arm_section_map));
5855     }
5856
5857   if (sec_data->map)
5858     {
5859       sec_data->map[newidx].vma = vma;
5860       sec_data->map[newidx].type = type;
5861     }
5862 }
5863
5864
5865 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
5866    veneers are handled for now.  */
5867
5868 static bfd_vma
5869 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
5870                              elf32_vfp11_erratum_list *branch,
5871                              bfd *branch_bfd,
5872                              asection *branch_sec,
5873                              unsigned int offset)
5874 {
5875   asection *s;
5876   struct elf32_arm_link_hash_table *hash_table;
5877   char *tmp_name;
5878   struct elf_link_hash_entry *myh;
5879   struct bfd_link_hash_entry *bh;
5880   bfd_vma val;
5881   struct _arm_elf_section_data *sec_data;
5882   elf32_vfp11_erratum_list *newerr;
5883
5884   hash_table = elf32_arm_hash_table (link_info);
5885   BFD_ASSERT (hash_table != NULL);
5886   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
5887
5888   s = bfd_get_section_by_name
5889     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
5890
5891   sec_data = elf32_arm_section_data (s);
5892
5893   BFD_ASSERT (s != NULL);
5894
5895   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
5896                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
5897
5898   BFD_ASSERT (tmp_name);
5899
5900   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
5901            hash_table->num_vfp11_fixes);
5902
5903   myh = elf_link_hash_lookup
5904     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
5905
5906   BFD_ASSERT (myh == NULL);
5907
5908   bh = NULL;
5909   val = hash_table->vfp11_erratum_glue_size;
5910   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
5911                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
5912                                     NULL, TRUE, FALSE, &bh);
5913
5914   myh = (struct elf_link_hash_entry *) bh;
5915   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5916   myh->forced_local = 1;
5917
5918   /* Link veneer back to calling location.  */
5919   sec_data->erratumcount += 1;
5920   newerr = (elf32_vfp11_erratum_list *)
5921       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
5922
5923   newerr->type = VFP11_ERRATUM_ARM_VENEER;
5924   newerr->vma = -1;
5925   newerr->u.v.branch = branch;
5926   newerr->u.v.id = hash_table->num_vfp11_fixes;
5927   branch->u.b.veneer = newerr;
5928
5929   newerr->next = sec_data->erratumlist;
5930   sec_data->erratumlist = newerr;
5931
5932   /* A symbol for the return from the veneer.  */
5933   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
5934            hash_table->num_vfp11_fixes);
5935
5936   myh = elf_link_hash_lookup
5937     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
5938
5939   if (myh != NULL)
5940     abort ();
5941
5942   bh = NULL;
5943   val = offset + 4;
5944   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
5945                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
5946
5947   myh = (struct elf_link_hash_entry *) bh;
5948   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5949   myh->forced_local = 1;
5950
5951   free (tmp_name);
5952
5953   /* Generate a mapping symbol for the veneer section, and explicitly add an
5954      entry for that symbol to the code/data map for the section.  */
5955   if (hash_table->vfp11_erratum_glue_size == 0)
5956     {
5957       bh = NULL;
5958       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
5959          ever requires this erratum fix.  */
5960       _bfd_generic_link_add_one_symbol (link_info,
5961                                         hash_table->bfd_of_glue_owner, "$a",
5962                                         BSF_LOCAL, s, 0, NULL,
5963                                         TRUE, FALSE, &bh);
5964
5965       myh = (struct elf_link_hash_entry *) bh;
5966       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5967       myh->forced_local = 1;
5968
5969       /* The elf32_arm_init_maps function only cares about symbols from input
5970          BFDs.  We must make a note of this generated mapping symbol
5971          ourselves so that code byteswapping works properly in
5972          elf32_arm_write_section.  */
5973       elf32_arm_section_map_add (s, 'a', 0);
5974     }
5975
5976   s->size += VFP11_ERRATUM_VENEER_SIZE;
5977   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
5978   hash_table->num_vfp11_fixes++;
5979
5980   /* The offset of the veneer.  */
5981   return val;
5982 }
5983
5984 #define ARM_GLUE_SECTION_FLAGS \
5985   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
5986    | SEC_READONLY | SEC_LINKER_CREATED)
5987
5988 /* Create a fake section for use by the ARM backend of the linker.  */
5989
5990 static bfd_boolean
5991 arm_make_glue_section (bfd * abfd, const char * name)
5992 {
5993   asection * sec;
5994
5995   sec = bfd_get_section_by_name (abfd, name);
5996   if (sec != NULL)
5997     /* Already made.  */
5998     return TRUE;
5999
6000   sec = bfd_make_section_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
6001
6002   if (sec == NULL
6003       || !bfd_set_section_alignment (abfd, sec, 2))
6004     return FALSE;
6005
6006   /* Set the gc mark to prevent the section from being removed by garbage
6007      collection, despite the fact that no relocs refer to this section.  */
6008   sec->gc_mark = 1;
6009
6010   return TRUE;
6011 }
6012
6013 /* Add the glue sections to ABFD.  This function is called from the
6014    linker scripts in ld/emultempl/{armelf}.em.  */
6015
6016 bfd_boolean
6017 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
6018                                         struct bfd_link_info *info)
6019 {
6020   /* If we are only performing a partial
6021      link do not bother adding the glue.  */
6022   if (info->relocatable)
6023     return TRUE;
6024
6025   return arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
6026     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
6027     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
6028     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
6029 }
6030
6031 /* Select a BFD to be used to hold the sections used by the glue code.
6032    This function is called from the linker scripts in ld/emultempl/
6033    {armelf/pe}.em.  */
6034
6035 bfd_boolean
6036 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
6037 {
6038   struct elf32_arm_link_hash_table *globals;
6039
6040   /* If we are only performing a partial link
6041      do not bother getting a bfd to hold the glue.  */
6042   if (info->relocatable)
6043     return TRUE;
6044
6045   /* Make sure we don't attach the glue sections to a dynamic object.  */
6046   BFD_ASSERT (!(abfd->flags & DYNAMIC));
6047
6048   globals = elf32_arm_hash_table (info);
6049   BFD_ASSERT (globals != NULL);
6050
6051   if (globals->bfd_of_glue_owner != NULL)
6052     return TRUE;
6053
6054   /* Save the bfd for later use.  */
6055   globals->bfd_of_glue_owner = abfd;
6056
6057   return TRUE;
6058 }
6059
6060 static void
6061 check_use_blx (struct elf32_arm_link_hash_table *globals)
6062 {
6063   int cpu_arch;
6064
6065   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
6066                                        Tag_CPU_arch);
6067
6068   if (globals->fix_arm1176)
6069     {
6070       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
6071         globals->use_blx = 1;
6072     }
6073   else
6074     {
6075       if (cpu_arch > TAG_CPU_ARCH_V4T)
6076         globals->use_blx = 1;
6077     }
6078 }
6079
6080 bfd_boolean
6081 bfd_elf32_arm_process_before_allocation (bfd *abfd,
6082                                          struct bfd_link_info *link_info)
6083 {
6084   Elf_Internal_Shdr *symtab_hdr;
6085   Elf_Internal_Rela *internal_relocs = NULL;
6086   Elf_Internal_Rela *irel, *irelend;
6087   bfd_byte *contents = NULL;
6088
6089   asection *sec;
6090   struct elf32_arm_link_hash_table *globals;
6091
6092   /* If we are only performing a partial link do not bother
6093      to construct any glue.  */
6094   if (link_info->relocatable)
6095     return TRUE;
6096
6097   /* Here we have a bfd that is to be included on the link.  We have a
6098      hook to do reloc rummaging, before section sizes are nailed down.  */
6099   globals = elf32_arm_hash_table (link_info);
6100   BFD_ASSERT (globals != NULL);
6101
6102   check_use_blx (globals);
6103
6104   if (globals->byteswap_code && !bfd_big_endian (abfd))
6105     {
6106       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
6107                           abfd);
6108       return FALSE;
6109     }
6110
6111   /* PR 5398: If we have not decided to include any loadable sections in
6112      the output then we will not have a glue owner bfd.  This is OK, it
6113      just means that there is nothing else for us to do here.  */
6114   if (globals->bfd_of_glue_owner == NULL)
6115     return TRUE;
6116
6117   /* Rummage around all the relocs and map the glue vectors.  */
6118   sec = abfd->sections;
6119
6120   if (sec == NULL)
6121     return TRUE;
6122
6123   for (; sec != NULL; sec = sec->next)
6124     {
6125       if (sec->reloc_count == 0)
6126         continue;
6127
6128       if ((sec->flags & SEC_EXCLUDE) != 0)
6129         continue;
6130
6131       symtab_hdr = & elf_symtab_hdr (abfd);
6132
6133       /* Load the relocs.  */
6134       internal_relocs
6135         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
6136
6137       if (internal_relocs == NULL)
6138         goto error_return;
6139
6140       irelend = internal_relocs + sec->reloc_count;
6141       for (irel = internal_relocs; irel < irelend; irel++)
6142         {
6143           long r_type;
6144           unsigned long r_index;
6145
6146           struct elf_link_hash_entry *h;
6147
6148           r_type = ELF32_R_TYPE (irel->r_info);
6149           r_index = ELF32_R_SYM (irel->r_info);
6150
6151           /* These are the only relocation types we care about.  */
6152           if (   r_type != R_ARM_PC24
6153               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
6154             continue;
6155
6156           /* Get the section contents if we haven't done so already.  */
6157           if (contents == NULL)
6158             {
6159               /* Get cached copy if it exists.  */
6160               if (elf_section_data (sec)->this_hdr.contents != NULL)
6161                 contents = elf_section_data (sec)->this_hdr.contents;
6162               else
6163                 {
6164                   /* Go get them off disk.  */
6165                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
6166                     goto error_return;
6167                 }
6168             }
6169
6170           if (r_type == R_ARM_V4BX)
6171             {
6172               int reg;
6173
6174               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
6175               record_arm_bx_glue (link_info, reg);
6176               continue;
6177             }
6178
6179           /* If the relocation is not against a symbol it cannot concern us.  */
6180           h = NULL;
6181
6182           /* We don't care about local symbols.  */
6183           if (r_index < symtab_hdr->sh_info)
6184             continue;
6185
6186           /* This is an external symbol.  */
6187           r_index -= symtab_hdr->sh_info;
6188           h = (struct elf_link_hash_entry *)
6189             elf_sym_hashes (abfd)[r_index];
6190
6191           /* If the relocation is against a static symbol it must be within
6192              the current section and so cannot be a cross ARM/Thumb relocation.  */
6193           if (h == NULL)
6194             continue;
6195
6196           /* If the call will go through a PLT entry then we do not need
6197              glue.  */
6198           if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
6199             continue;
6200
6201           switch (r_type)
6202             {
6203             case R_ARM_PC24:
6204               /* This one is a call from arm code.  We need to look up
6205                  the target of the call.  If it is a thumb target, we
6206                  insert glue.  */
6207               if (h->target_internal == ST_BRANCH_TO_THUMB)
6208                 record_arm_to_thumb_glue (link_info, h);
6209               break;
6210
6211             default:
6212               abort ();
6213             }
6214         }
6215
6216       if (contents != NULL
6217           && elf_section_data (sec)->this_hdr.contents != contents)
6218         free (contents);
6219       contents = NULL;
6220
6221       if (internal_relocs != NULL
6222           && elf_section_data (sec)->relocs != internal_relocs)
6223         free (internal_relocs);
6224       internal_relocs = NULL;
6225     }
6226
6227   return TRUE;
6228
6229 error_return:
6230   if (contents != NULL
6231       && elf_section_data (sec)->this_hdr.contents != contents)
6232     free (contents);
6233   if (internal_relocs != NULL
6234       && elf_section_data (sec)->relocs != internal_relocs)
6235     free (internal_relocs);
6236
6237   return FALSE;
6238 }
6239 #endif
6240
6241
6242 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
6243
6244 void
6245 bfd_elf32_arm_init_maps (bfd *abfd)
6246 {
6247   Elf_Internal_Sym *isymbuf;
6248   Elf_Internal_Shdr *hdr;
6249   unsigned int i, localsyms;
6250
6251   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
6252   if (! is_arm_elf (abfd))
6253     return;
6254
6255   if ((abfd->flags & DYNAMIC) != 0)
6256     return;
6257
6258   hdr = & elf_symtab_hdr (abfd);
6259   localsyms = hdr->sh_info;
6260
6261   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
6262      should contain the number of local symbols, which should come before any
6263      global symbols.  Mapping symbols are always local.  */
6264   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
6265                                   NULL);
6266
6267   /* No internal symbols read?  Skip this BFD.  */
6268   if (isymbuf == NULL)
6269     return;
6270
6271   for (i = 0; i < localsyms; i++)
6272     {
6273       Elf_Internal_Sym *isym = &isymbuf[i];
6274       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6275       const char *name;
6276
6277       if (sec != NULL
6278           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
6279         {
6280           name = bfd_elf_string_from_elf_section (abfd,
6281             hdr->sh_link, isym->st_name);
6282
6283           if (bfd_is_arm_special_symbol_name (name,
6284                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
6285             elf32_arm_section_map_add (sec, name[1], isym->st_value);
6286         }
6287     }
6288 }
6289
6290
6291 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
6292    say what they wanted.  */
6293
6294 void
6295 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
6296 {
6297   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6298   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6299
6300   if (globals == NULL)
6301     return;
6302
6303   if (globals->fix_cortex_a8 == -1)
6304     {
6305       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
6306       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
6307           && (out_attr[Tag_CPU_arch_profile].i == 'A'
6308               || out_attr[Tag_CPU_arch_profile].i == 0))
6309         globals->fix_cortex_a8 = 1;
6310       else
6311         globals->fix_cortex_a8 = 0;
6312     }
6313 }
6314
6315
6316 void
6317 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
6318 {
6319   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6320   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
6321
6322   if (globals == NULL)
6323     return;
6324   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
6325   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
6326     {
6327       switch (globals->vfp11_fix)
6328         {
6329         case BFD_ARM_VFP11_FIX_DEFAULT:
6330         case BFD_ARM_VFP11_FIX_NONE:
6331           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6332           break;
6333
6334         default:
6335           /* Give a warning, but do as the user requests anyway.  */
6336           (*_bfd_error_handler) (_("%B: warning: selected VFP11 erratum "
6337             "workaround is not necessary for target architecture"), obfd);
6338         }
6339     }
6340   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
6341     /* For earlier architectures, we might need the workaround, but do not
6342        enable it by default.  If users is running with broken hardware, they
6343        must enable the erratum fix explicitly.  */
6344     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
6345 }
6346
6347
6348 enum bfd_arm_vfp11_pipe
6349 {
6350   VFP11_FMAC,
6351   VFP11_LS,
6352   VFP11_DS,
6353   VFP11_BAD
6354 };
6355
6356 /* Return a VFP register number.  This is encoded as RX:X for single-precision
6357    registers, or X:RX for double-precision registers, where RX is the group of
6358    four bits in the instruction encoding and X is the single extension bit.
6359    RX and X fields are specified using their lowest (starting) bit.  The return
6360    value is:
6361
6362      0...31: single-precision registers s0...s31
6363      32...63: double-precision registers d0...d31.
6364
6365    Although X should be zero for VFP11 (encoding d0...d15 only), we might
6366    encounter VFP3 instructions, so we allow the full range for DP registers.  */
6367
6368 static unsigned int
6369 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
6370                      unsigned int x)
6371 {
6372   if (is_double)
6373     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
6374   else
6375     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
6376 }
6377
6378 /* Set bits in *WMASK according to a register number REG as encoded by
6379    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
6380
6381 static void
6382 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
6383 {
6384   if (reg < 32)
6385     *wmask |= 1 << reg;
6386   else if (reg < 48)
6387     *wmask |= 3 << ((reg - 32) * 2);
6388 }
6389
6390 /* Return TRUE if WMASK overwrites anything in REGS.  */
6391
6392 static bfd_boolean
6393 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
6394 {
6395   int i;
6396
6397   for (i = 0; i < numregs; i++)
6398     {
6399       unsigned int reg = regs[i];
6400
6401       if (reg < 32 && (wmask & (1 << reg)) != 0)
6402         return TRUE;
6403
6404       reg -= 32;
6405
6406       if (reg >= 16)
6407         continue;
6408
6409       if ((wmask & (3 << (reg * 2))) != 0)
6410         return TRUE;
6411     }
6412
6413   return FALSE;
6414 }
6415
6416 /* In this function, we're interested in two things: finding input registers
6417    for VFP data-processing instructions, and finding the set of registers which
6418    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
6419    hold the written set, so FLDM etc. are easy to deal with (we're only
6420    interested in 32 SP registers or 16 dp registers, due to the VFP version
6421    implemented by the chip in question).  DP registers are marked by setting
6422    both SP registers in the write mask).  */
6423
6424 static enum bfd_arm_vfp11_pipe
6425 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
6426                            int *numregs)
6427 {
6428   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
6429   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
6430
6431   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
6432     {
6433       unsigned int pqrs;
6434       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6435       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6436
6437       pqrs = ((insn & 0x00800000) >> 20)
6438            | ((insn & 0x00300000) >> 19)
6439            | ((insn & 0x00000040) >> 6);
6440
6441       switch (pqrs)
6442         {
6443         case 0: /* fmac[sd].  */
6444         case 1: /* fnmac[sd].  */
6445         case 2: /* fmsc[sd].  */
6446         case 3: /* fnmsc[sd].  */
6447           vpipe = VFP11_FMAC;
6448           bfd_arm_vfp11_write_mask (destmask, fd);
6449           regs[0] = fd;
6450           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
6451           regs[2] = fm;
6452           *numregs = 3;
6453           break;
6454
6455         case 4: /* fmul[sd].  */
6456         case 5: /* fnmul[sd].  */
6457         case 6: /* fadd[sd].  */
6458         case 7: /* fsub[sd].  */
6459           vpipe = VFP11_FMAC;
6460           goto vfp_binop;
6461
6462         case 8: /* fdiv[sd].  */
6463           vpipe = VFP11_DS;
6464           vfp_binop:
6465           bfd_arm_vfp11_write_mask (destmask, fd);
6466           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
6467           regs[1] = fm;
6468           *numregs = 2;
6469           break;
6470
6471         case 15: /* extended opcode.  */
6472           {
6473             unsigned int extn = ((insn >> 15) & 0x1e)
6474                               | ((insn >> 7) & 1);
6475
6476             switch (extn)
6477               {
6478               case 0: /* fcpy[sd].  */
6479               case 1: /* fabs[sd].  */
6480               case 2: /* fneg[sd].  */
6481               case 8: /* fcmp[sd].  */
6482               case 9: /* fcmpe[sd].  */
6483               case 10: /* fcmpz[sd].  */
6484               case 11: /* fcmpez[sd].  */
6485               case 16: /* fuito[sd].  */
6486               case 17: /* fsito[sd].  */
6487               case 24: /* ftoui[sd].  */
6488               case 25: /* ftouiz[sd].  */
6489               case 26: /* ftosi[sd].  */
6490               case 27: /* ftosiz[sd].  */
6491                 /* These instructions will not bounce due to underflow.  */
6492                 *numregs = 0;
6493                 vpipe = VFP11_FMAC;
6494                 break;
6495
6496               case 3: /* fsqrt[sd].  */
6497                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
6498                    registers to cause the erratum in previous instructions.  */
6499                 bfd_arm_vfp11_write_mask (destmask, fd);
6500                 vpipe = VFP11_DS;
6501                 break;
6502
6503               case 15: /* fcvt{ds,sd}.  */
6504                 {
6505                   int rnum = 0;
6506
6507                   bfd_arm_vfp11_write_mask (destmask, fd);
6508
6509                   /* Only FCVTSD can underflow.  */
6510                   if ((insn & 0x100) != 0)
6511                     regs[rnum++] = fm;
6512
6513                   *numregs = rnum;
6514
6515                   vpipe = VFP11_FMAC;
6516                 }
6517                 break;
6518
6519               default:
6520                 return VFP11_BAD;
6521               }
6522           }
6523           break;
6524
6525         default:
6526           return VFP11_BAD;
6527         }
6528     }
6529   /* Two-register transfer.  */
6530   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
6531     {
6532       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
6533
6534       if ((insn & 0x100000) == 0)
6535         {
6536           if (is_double)
6537             bfd_arm_vfp11_write_mask (destmask, fm);
6538           else
6539             {
6540               bfd_arm_vfp11_write_mask (destmask, fm);
6541               bfd_arm_vfp11_write_mask (destmask, fm + 1);
6542             }
6543         }
6544
6545       vpipe = VFP11_LS;
6546     }
6547   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
6548     {
6549       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
6550       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
6551
6552       switch (puw)
6553         {
6554         case 0: /* Two-reg transfer.  We should catch these above.  */
6555           abort ();
6556
6557         case 2: /* fldm[sdx].  */
6558         case 3:
6559         case 5:
6560           {
6561             unsigned int i, offset = insn & 0xff;
6562
6563             if (is_double)
6564               offset >>= 1;
6565
6566             for (i = fd; i < fd + offset; i++)
6567               bfd_arm_vfp11_write_mask (destmask, i);
6568           }
6569           break;
6570
6571         case 4: /* fld[sd].  */
6572         case 6:
6573           bfd_arm_vfp11_write_mask (destmask, fd);
6574           break;
6575
6576         default:
6577           return VFP11_BAD;
6578         }
6579
6580       vpipe = VFP11_LS;
6581     }
6582   /* Single-register transfer. Note L==0.  */
6583   else if ((insn & 0x0f100e10) == 0x0e000a10)
6584     {
6585       unsigned int opcode = (insn >> 21) & 7;
6586       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
6587
6588       switch (opcode)
6589         {
6590         case 0: /* fmsr/fmdlr.  */
6591         case 1: /* fmdhr.  */
6592           /* Mark fmdhr and fmdlr as writing to the whole of the DP
6593              destination register.  I don't know if this is exactly right,
6594              but it is the conservative choice.  */
6595           bfd_arm_vfp11_write_mask (destmask, fn);
6596           break;
6597
6598         case 7: /* fmxr.  */
6599           break;
6600         }
6601
6602       vpipe = VFP11_LS;
6603     }
6604
6605   return vpipe;
6606 }
6607
6608
6609 static int elf32_arm_compare_mapping (const void * a, const void * b);
6610
6611
6612 /* Look for potentially-troublesome code sequences which might trigger the
6613    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
6614    (available from ARM) for details of the erratum.  A short version is
6615    described in ld.texinfo.  */
6616
6617 bfd_boolean
6618 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
6619 {
6620   asection *sec;
6621   bfd_byte *contents = NULL;
6622   int state = 0;
6623   int regs[3], numregs = 0;
6624   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
6625   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
6626
6627   if (globals == NULL)
6628     return FALSE;
6629
6630   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
6631      The states transition as follows:
6632
6633        0 -> 1 (vector) or 0 -> 2 (scalar)
6634            A VFP FMAC-pipeline instruction has been seen. Fill
6635            regs[0]..regs[numregs-1] with its input operands. Remember this
6636            instruction in 'first_fmac'.
6637
6638        1 -> 2
6639            Any instruction, except for a VFP instruction which overwrites
6640            regs[*].
6641
6642        1 -> 3 [ -> 0 ]  or
6643        2 -> 3 [ -> 0 ]
6644            A VFP instruction has been seen which overwrites any of regs[*].
6645            We must make a veneer!  Reset state to 0 before examining next
6646            instruction.
6647
6648        2 -> 0
6649            If we fail to match anything in state 2, reset to state 0 and reset
6650            the instruction pointer to the instruction after 'first_fmac'.
6651
6652      If the VFP11 vector mode is in use, there must be at least two unrelated
6653      instructions between anti-dependent VFP11 instructions to properly avoid
6654      triggering the erratum, hence the use of the extra state 1.  */
6655
6656   /* If we are only performing a partial link do not bother
6657      to construct any glue.  */
6658   if (link_info->relocatable)
6659     return TRUE;
6660
6661   /* Skip if this bfd does not correspond to an ELF image.  */
6662   if (! is_arm_elf (abfd))
6663     return TRUE;
6664
6665   /* We should have chosen a fix type by the time we get here.  */
6666   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
6667
6668   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
6669     return TRUE;
6670
6671   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
6672   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
6673     return TRUE;
6674
6675   for (sec = abfd->sections; sec != NULL; sec = sec->next)
6676     {
6677       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
6678       struct _arm_elf_section_data *sec_data;
6679
6680       /* If we don't have executable progbits, we're not interested in this
6681          section.  Also skip if section is to be excluded.  */
6682       if (elf_section_type (sec) != SHT_PROGBITS
6683           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
6684           || (sec->flags & SEC_EXCLUDE) != 0
6685           || sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
6686           || sec->output_section == bfd_abs_section_ptr
6687           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
6688         continue;
6689
6690       sec_data = elf32_arm_section_data (sec);
6691
6692       if (sec_data->mapcount == 0)
6693         continue;
6694
6695       if (elf_section_data (sec)->this_hdr.contents != NULL)
6696         contents = elf_section_data (sec)->this_hdr.contents;
6697       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
6698         goto error_return;
6699
6700       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
6701              elf32_arm_compare_mapping);
6702
6703       for (span = 0; span < sec_data->mapcount; span++)
6704         {
6705           unsigned int span_start = sec_data->map[span].vma;
6706           unsigned int span_end = (span == sec_data->mapcount - 1)
6707                                   ? sec->size : sec_data->map[span + 1].vma;
6708           char span_type = sec_data->map[span].type;
6709
6710           /* FIXME: Only ARM mode is supported at present.  We may need to
6711              support Thumb-2 mode also at some point.  */
6712           if (span_type != 'a')
6713             continue;
6714
6715           for (i = span_start; i < span_end;)
6716             {
6717               unsigned int next_i = i + 4;
6718               unsigned int insn = bfd_big_endian (abfd)
6719                 ? (contents[i] << 24)
6720                   | (contents[i + 1] << 16)
6721                   | (contents[i + 2] << 8)
6722                   | contents[i + 3]
6723                 : (contents[i + 3] << 24)
6724                   | (contents[i + 2] << 16)
6725                   | (contents[i + 1] << 8)
6726                   | contents[i];
6727               unsigned int writemask = 0;
6728               enum bfd_arm_vfp11_pipe vpipe;
6729
6730               switch (state)
6731                 {
6732                 case 0:
6733                   vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
6734                                                     &numregs);
6735                   /* I'm assuming the VFP11 erratum can trigger with denorm
6736                      operands on either the FMAC or the DS pipeline. This might
6737                      lead to slightly overenthusiastic veneer insertion.  */
6738                   if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
6739                     {
6740                       state = use_vector ? 1 : 2;
6741                       first_fmac = i;
6742                       veneer_of_insn = insn;
6743                     }
6744                   break;
6745
6746                 case 1:
6747                   {
6748                     int other_regs[3], other_numregs;
6749                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
6750                                                       other_regs,
6751                                                       &other_numregs);
6752                     if (vpipe != VFP11_BAD
6753                         && bfd_arm_vfp11_antidependency (writemask, regs,
6754                                                          numregs))
6755                       state = 3;
6756                     else
6757                       state = 2;
6758                   }
6759                   break;
6760
6761                 case 2:
6762                   {
6763                     int other_regs[3], other_numregs;
6764                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
6765                                                       other_regs,
6766                                                       &other_numregs);
6767                     if (vpipe != VFP11_BAD
6768                         && bfd_arm_vfp11_antidependency (writemask, regs,
6769                                                          numregs))
6770                       state = 3;
6771                     else
6772                       {
6773                         state = 0;
6774                         next_i = first_fmac + 4;
6775                       }
6776                   }
6777                   break;
6778
6779                 case 3:
6780                   abort ();  /* Should be unreachable.  */
6781                 }
6782
6783               if (state == 3)
6784                 {
6785                   elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
6786                       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
6787
6788                   elf32_arm_section_data (sec)->erratumcount += 1;
6789
6790                   newerr->u.b.vfp_insn = veneer_of_insn;
6791
6792                   switch (span_type)
6793                     {
6794                     case 'a':
6795                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
6796                       break;
6797
6798                     default:
6799                       abort ();
6800                     }
6801
6802                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
6803                                                first_fmac);
6804
6805                   newerr->vma = -1;
6806
6807                   newerr->next = sec_data->erratumlist;
6808                   sec_data->erratumlist = newerr;
6809
6810                   state = 0;
6811                 }
6812
6813               i = next_i;
6814             }
6815         }
6816
6817       if (contents != NULL
6818           && elf_section_data (sec)->this_hdr.contents != contents)
6819         free (contents);
6820       contents = NULL;
6821     }
6822
6823   return TRUE;
6824
6825 error_return:
6826   if (contents != NULL
6827       && elf_section_data (sec)->this_hdr.contents != contents)
6828     free (contents);
6829
6830   return FALSE;
6831 }
6832
6833 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
6834    after sections have been laid out, using specially-named symbols.  */
6835
6836 void
6837 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
6838                                           struct bfd_link_info *link_info)
6839 {
6840   asection *sec;
6841   struct elf32_arm_link_hash_table *globals;
6842   char *tmp_name;
6843
6844   if (link_info->relocatable)
6845     return;
6846
6847   /* Skip if this bfd does not correspond to an ELF image.  */
6848   if (! is_arm_elf (abfd))
6849     return;
6850
6851   globals = elf32_arm_hash_table (link_info);
6852   if (globals == NULL)
6853     return;
6854
6855   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
6856                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
6857
6858   for (sec = abfd->sections; sec != NULL; sec = sec->next)
6859     {
6860       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
6861       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
6862
6863       for (; errnode != NULL; errnode = errnode->next)
6864         {
6865           struct elf_link_hash_entry *myh;
6866           bfd_vma vma;
6867
6868           switch (errnode->type)
6869             {
6870             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
6871             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
6872               /* Find veneer symbol.  */
6873               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
6874                        errnode->u.b.veneer->u.v.id);
6875
6876               myh = elf_link_hash_lookup
6877                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
6878
6879               if (myh == NULL)
6880                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
6881                                          "`%s'"), abfd, tmp_name);
6882
6883               vma = myh->root.u.def.section->output_section->vma
6884                     + myh->root.u.def.section->output_offset
6885                     + myh->root.u.def.value;
6886
6887               errnode->u.b.veneer->vma = vma;
6888               break;
6889
6890             case VFP11_ERRATUM_ARM_VENEER:
6891             case VFP11_ERRATUM_THUMB_VENEER:
6892               /* Find return location.  */
6893               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
6894                        errnode->u.v.id);
6895
6896               myh = elf_link_hash_lookup
6897                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
6898
6899               if (myh == NULL)
6900                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
6901                                          "`%s'"), abfd, tmp_name);
6902
6903               vma = myh->root.u.def.section->output_section->vma
6904                     + myh->root.u.def.section->output_offset
6905                     + myh->root.u.def.value;
6906
6907               errnode->u.v.branch->vma = vma;
6908               break;
6909
6910             default:
6911               abort ();
6912             }
6913         }
6914     }
6915
6916   free (tmp_name);
6917 }
6918
6919
6920 /* Set target relocation values needed during linking.  */
6921
6922 void
6923 bfd_elf32_arm_set_target_relocs (struct bfd *output_bfd,
6924                                  struct bfd_link_info *link_info,
6925                                  int target1_is_rel,
6926                                  char * target2_type,
6927                                  int fix_v4bx,
6928                                  int use_blx,
6929                                  bfd_arm_vfp11_fix vfp11_fix,
6930                                  int no_enum_warn, int no_wchar_warn,
6931                                  int pic_veneer, int fix_cortex_a8,
6932                                  int fix_arm1176)
6933 {
6934   struct elf32_arm_link_hash_table *globals;
6935
6936   globals = elf32_arm_hash_table (link_info);
6937   if (globals == NULL)
6938     return;
6939
6940   globals->target1_is_rel = target1_is_rel;
6941   if (strcmp (target2_type, "rel") == 0)
6942     globals->target2_reloc = R_ARM_REL32;
6943   else if (strcmp (target2_type, "abs") == 0)
6944     globals->target2_reloc = R_ARM_ABS32;
6945   else if (strcmp (target2_type, "got-rel") == 0)
6946     globals->target2_reloc = R_ARM_GOT_PREL;
6947   else
6948     {
6949       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
6950                           target2_type);
6951     }
6952   globals->fix_v4bx = fix_v4bx;
6953   globals->use_blx |= use_blx;
6954   globals->vfp11_fix = vfp11_fix;
6955   globals->pic_veneer = pic_veneer;
6956   globals->fix_cortex_a8 = fix_cortex_a8;
6957   globals->fix_arm1176 = fix_arm1176;
6958
6959   BFD_ASSERT (is_arm_elf (output_bfd));
6960   elf_arm_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
6961   elf_arm_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
6962 }
6963
6964 /* Replace the target offset of a Thumb bl or b.w instruction.  */
6965
6966 static void
6967 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
6968 {
6969   bfd_vma upper;
6970   bfd_vma lower;
6971   int reloc_sign;
6972
6973   BFD_ASSERT ((offset & 1) == 0);
6974
6975   upper = bfd_get_16 (abfd, insn);
6976   lower = bfd_get_16 (abfd, insn + 2);
6977   reloc_sign = (offset < 0) ? 1 : 0;
6978   upper = (upper & ~(bfd_vma) 0x7ff)
6979           | ((offset >> 12) & 0x3ff)
6980           | (reloc_sign << 10);
6981   lower = (lower & ~(bfd_vma) 0x2fff)
6982           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
6983           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
6984           | ((offset >> 1) & 0x7ff);
6985   bfd_put_16 (abfd, upper, insn);
6986   bfd_put_16 (abfd, lower, insn + 2);
6987 }
6988
6989 /* Thumb code calling an ARM function.  */
6990
6991 static int
6992 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
6993                          const char *           name,
6994                          bfd *                  input_bfd,
6995                          bfd *                  output_bfd,
6996                          asection *             input_section,
6997                          bfd_byte *             hit_data,
6998                          asection *             sym_sec,
6999                          bfd_vma                offset,
7000                          bfd_signed_vma         addend,
7001                          bfd_vma                val,
7002                          char **error_message)
7003 {
7004   asection * s = 0;
7005   bfd_vma my_offset;
7006   long int ret_offset;
7007   struct elf_link_hash_entry * myh;
7008   struct elf32_arm_link_hash_table * globals;
7009
7010   myh = find_thumb_glue (info, name, error_message);
7011   if (myh == NULL)
7012     return FALSE;
7013
7014   globals = elf32_arm_hash_table (info);
7015   BFD_ASSERT (globals != NULL);
7016   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7017
7018   my_offset = myh->root.u.def.value;
7019
7020   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
7021                                THUMB2ARM_GLUE_SECTION_NAME);
7022
7023   BFD_ASSERT (s != NULL);
7024   BFD_ASSERT (s->contents != NULL);
7025   BFD_ASSERT (s->output_section != NULL);
7026
7027   if ((my_offset & 0x01) == 0x01)
7028     {
7029       if (sym_sec != NULL
7030           && sym_sec->owner != NULL
7031           && !INTERWORK_FLAG (sym_sec->owner))
7032         {
7033           (*_bfd_error_handler)
7034             (_("%B(%s): warning: interworking not enabled.\n"
7035                "  first occurrence: %B: Thumb call to ARM"),
7036              sym_sec->owner, input_bfd, name);
7037
7038           return FALSE;
7039         }
7040
7041       --my_offset;
7042       myh->root.u.def.value = my_offset;
7043
7044       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
7045                       s->contents + my_offset);
7046
7047       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
7048                       s->contents + my_offset + 2);
7049
7050       ret_offset =
7051         /* Address of destination of the stub.  */
7052         ((bfd_signed_vma) val)
7053         - ((bfd_signed_vma)
7054            /* Offset from the start of the current section
7055               to the start of the stubs.  */
7056            (s->output_offset
7057             /* Offset of the start of this stub from the start of the stubs.  */
7058             + my_offset
7059             /* Address of the start of the current section.  */
7060             + s->output_section->vma)
7061            /* The branch instruction is 4 bytes into the stub.  */
7062            + 4
7063            /* ARM branches work from the pc of the instruction + 8.  */
7064            + 8);
7065
7066       put_arm_insn (globals, output_bfd,
7067                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
7068                     s->contents + my_offset + 4);
7069     }
7070
7071   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
7072
7073   /* Now go back and fix up the original BL insn to point to here.  */
7074   ret_offset =
7075     /* Address of where the stub is located.  */
7076     (s->output_section->vma + s->output_offset + my_offset)
7077      /* Address of where the BL is located.  */
7078     - (input_section->output_section->vma + input_section->output_offset
7079        + offset)
7080     /* Addend in the relocation.  */
7081     - addend
7082     /* Biassing for PC-relative addressing.  */
7083     - 8;
7084
7085   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
7086
7087   return TRUE;
7088 }
7089
7090 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
7091
7092 static struct elf_link_hash_entry *
7093 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
7094                              const char *           name,
7095                              bfd *                  input_bfd,
7096                              bfd *                  output_bfd,
7097                              asection *             sym_sec,
7098                              bfd_vma                val,
7099                              asection *             s,
7100                              char **                error_message)
7101 {
7102   bfd_vma my_offset;
7103   long int ret_offset;
7104   struct elf_link_hash_entry * myh;
7105   struct elf32_arm_link_hash_table * globals;
7106
7107   myh = find_arm_glue (info, name, error_message);
7108   if (myh == NULL)
7109     return NULL;
7110
7111   globals = elf32_arm_hash_table (info);
7112   BFD_ASSERT (globals != NULL);
7113   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7114
7115   my_offset = myh->root.u.def.value;
7116
7117   if ((my_offset & 0x01) == 0x01)
7118     {
7119       if (sym_sec != NULL
7120           && sym_sec->owner != NULL
7121           && !INTERWORK_FLAG (sym_sec->owner))
7122         {
7123           (*_bfd_error_handler)
7124             (_("%B(%s): warning: interworking not enabled.\n"
7125                "  first occurrence: %B: arm call to thumb"),
7126              sym_sec->owner, input_bfd, name);
7127         }
7128
7129       --my_offset;
7130       myh->root.u.def.value = my_offset;
7131
7132       if (info->shared || globals->root.is_relocatable_executable
7133           || globals->pic_veneer)
7134         {
7135           /* For relocatable objects we can't use absolute addresses,
7136              so construct the address from a relative offset.  */
7137           /* TODO: If the offset is small it's probably worth
7138              constructing the address with adds.  */
7139           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
7140                         s->contents + my_offset);
7141           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
7142                         s->contents + my_offset + 4);
7143           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
7144                         s->contents + my_offset + 8);
7145           /* Adjust the offset by 4 for the position of the add,
7146              and 8 for the pipeline offset.  */
7147           ret_offset = (val - (s->output_offset
7148                                + s->output_section->vma
7149                                + my_offset + 12))
7150                        | 1;
7151           bfd_put_32 (output_bfd, ret_offset,
7152                       s->contents + my_offset + 12);
7153         }
7154       else if (globals->use_blx)
7155         {
7156           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
7157                         s->contents + my_offset);
7158
7159           /* It's a thumb address.  Add the low order bit.  */
7160           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
7161                       s->contents + my_offset + 4);
7162         }
7163       else
7164         {
7165           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
7166                         s->contents + my_offset);
7167
7168           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
7169                         s->contents + my_offset + 4);
7170
7171           /* It's a thumb address.  Add the low order bit.  */
7172           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
7173                       s->contents + my_offset + 8);
7174
7175           my_offset += 12;
7176         }
7177     }
7178
7179   BFD_ASSERT (my_offset <= globals->arm_glue_size);
7180
7181   return myh;
7182 }
7183
7184 /* Arm code calling a Thumb function.  */
7185
7186 static int
7187 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
7188                          const char *           name,
7189                          bfd *                  input_bfd,
7190                          bfd *                  output_bfd,
7191                          asection *             input_section,
7192                          bfd_byte *             hit_data,
7193                          asection *             sym_sec,
7194                          bfd_vma                offset,
7195                          bfd_signed_vma         addend,
7196                          bfd_vma                val,
7197                          char **error_message)
7198 {
7199   unsigned long int tmp;
7200   bfd_vma my_offset;
7201   asection * s;
7202   long int ret_offset;
7203   struct elf_link_hash_entry * myh;
7204   struct elf32_arm_link_hash_table * globals;
7205
7206   globals = elf32_arm_hash_table (info);
7207   BFD_ASSERT (globals != NULL);
7208   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7209
7210   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
7211                                ARM2THUMB_GLUE_SECTION_NAME);
7212   BFD_ASSERT (s != NULL);
7213   BFD_ASSERT (s->contents != NULL);
7214   BFD_ASSERT (s->output_section != NULL);
7215
7216   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
7217                                      sym_sec, val, s, error_message);
7218   if (!myh)
7219     return FALSE;
7220
7221   my_offset = myh->root.u.def.value;
7222   tmp = bfd_get_32 (input_bfd, hit_data);
7223   tmp = tmp & 0xFF000000;
7224
7225   /* Somehow these are both 4 too far, so subtract 8.  */
7226   ret_offset = (s->output_offset
7227                 + my_offset
7228                 + s->output_section->vma
7229                 - (input_section->output_offset
7230                    + input_section->output_section->vma
7231                    + offset + addend)
7232                 - 8);
7233
7234   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
7235
7236   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
7237
7238   return TRUE;
7239 }
7240
7241 /* Populate Arm stub for an exported Thumb function.  */
7242
7243 static bfd_boolean
7244 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
7245 {
7246   struct bfd_link_info * info = (struct bfd_link_info *) inf;
7247   asection * s;
7248   struct elf_link_hash_entry * myh;
7249   struct elf32_arm_link_hash_entry *eh;
7250   struct elf32_arm_link_hash_table * globals;
7251   asection *sec;
7252   bfd_vma val;
7253   char *error_message;
7254
7255   eh = elf32_arm_hash_entry (h);
7256   /* Allocate stubs for exported Thumb functions on v4t.  */
7257   if (eh->export_glue == NULL)
7258     return TRUE;
7259
7260   globals = elf32_arm_hash_table (info);
7261   BFD_ASSERT (globals != NULL);
7262   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7263
7264   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
7265                                ARM2THUMB_GLUE_SECTION_NAME);
7266   BFD_ASSERT (s != NULL);
7267   BFD_ASSERT (s->contents != NULL);
7268   BFD_ASSERT (s->output_section != NULL);
7269
7270   sec = eh->export_glue->root.u.def.section;
7271
7272   BFD_ASSERT (sec->output_section != NULL);
7273
7274   val = eh->export_glue->root.u.def.value + sec->output_offset
7275         + sec->output_section->vma;
7276
7277   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
7278                                      h->root.u.def.section->owner,
7279                                      globals->obfd, sec, val, s,
7280                                      &error_message);
7281   BFD_ASSERT (myh);
7282   return TRUE;
7283 }
7284
7285 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
7286
7287 static bfd_vma
7288 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
7289 {
7290   bfd_byte *p;
7291   bfd_vma glue_addr;
7292   asection *s;
7293   struct elf32_arm_link_hash_table *globals;
7294
7295   globals = elf32_arm_hash_table (info);
7296   BFD_ASSERT (globals != NULL);
7297   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7298
7299   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
7300                                ARM_BX_GLUE_SECTION_NAME);
7301   BFD_ASSERT (s != NULL);
7302   BFD_ASSERT (s->contents != NULL);
7303   BFD_ASSERT (s->output_section != NULL);
7304
7305   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
7306
7307   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
7308
7309   if ((globals->bx_glue_offset[reg] & 1) == 0)
7310     {
7311       p = s->contents + glue_addr;
7312       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
7313       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
7314       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
7315       globals->bx_glue_offset[reg] |= 1;
7316     }
7317
7318   return glue_addr + s->output_section->vma + s->output_offset;
7319 }
7320
7321 /* Generate Arm stubs for exported Thumb symbols.  */
7322 static void
7323 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
7324                                   struct bfd_link_info *link_info)
7325 {
7326   struct elf32_arm_link_hash_table * globals;
7327
7328   if (link_info == NULL)
7329     /* Ignore this if we are not called by the ELF backend linker.  */
7330     return;
7331
7332   globals = elf32_arm_hash_table (link_info);
7333   if (globals == NULL)
7334     return;
7335
7336   /* If blx is available then exported Thumb symbols are OK and there is
7337      nothing to do.  */
7338   if (globals->use_blx)
7339     return;
7340
7341   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
7342                           link_info);
7343 }
7344
7345 /* Reserve space for COUNT dynamic relocations in relocation selection
7346    SRELOC.  */
7347
7348 static void
7349 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
7350                               bfd_size_type count)
7351 {
7352   struct elf32_arm_link_hash_table *htab;
7353
7354   htab = elf32_arm_hash_table (info);
7355   BFD_ASSERT (htab->root.dynamic_sections_created);
7356   if (sreloc == NULL)
7357     abort ();
7358   sreloc->size += RELOC_SIZE (htab) * count;
7359 }
7360
7361 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
7362    dynamic, the relocations should go in SRELOC, otherwise they should
7363    go in the special .rel.iplt section.  */
7364
7365 static void
7366 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
7367                             bfd_size_type count)
7368 {
7369   struct elf32_arm_link_hash_table *htab;
7370
7371   htab = elf32_arm_hash_table (info);
7372   if (!htab->root.dynamic_sections_created)
7373     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
7374   else
7375     {
7376       BFD_ASSERT (sreloc != NULL);
7377       sreloc->size += RELOC_SIZE (htab) * count;
7378     }
7379 }
7380
7381 /* Add relocation REL to the end of relocation section SRELOC.  */
7382
7383 static void
7384 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
7385                         asection *sreloc, Elf_Internal_Rela *rel)
7386 {
7387   bfd_byte *loc;
7388   struct elf32_arm_link_hash_table *htab;
7389
7390   htab = elf32_arm_hash_table (info);
7391   if (!htab->root.dynamic_sections_created
7392       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
7393     sreloc = htab->root.irelplt;
7394   if (sreloc == NULL)
7395     abort ();
7396   loc = sreloc->contents;
7397   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
7398   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
7399     abort ();
7400   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
7401 }
7402
7403 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
7404    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
7405    to .plt.  */
7406
7407 static void
7408 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
7409                               bfd_boolean is_iplt_entry,
7410                               union gotplt_union *root_plt,
7411                               struct arm_plt_info *arm_plt)
7412 {
7413   struct elf32_arm_link_hash_table *htab;
7414   asection *splt;
7415   asection *sgotplt;
7416
7417   htab = elf32_arm_hash_table (info);
7418
7419   if (is_iplt_entry)
7420     {
7421       splt = htab->root.iplt;
7422       sgotplt = htab->root.igotplt;
7423
7424       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
7425       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
7426     }
7427   else
7428     {
7429       splt = htab->root.splt;
7430       sgotplt = htab->root.sgotplt;
7431
7432       /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
7433       elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
7434
7435       /* If this is the first .plt entry, make room for the special
7436          first entry.  */
7437       if (splt->size == 0)
7438         splt->size += htab->plt_header_size;
7439     }
7440
7441   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
7442   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
7443     splt->size += PLT_THUMB_STUB_SIZE;
7444   root_plt->offset = splt->size;
7445   splt->size += htab->plt_entry_size;
7446
7447   if (!htab->symbian_p)
7448     {
7449       /* We also need to make an entry in the .got.plt section, which
7450          will be placed in the .got section by the linker script.  */
7451       arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
7452       sgotplt->size += 4;
7453     }
7454 }
7455
7456 static bfd_vma
7457 arm_movw_immediate (bfd_vma value)
7458 {
7459   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
7460 }
7461
7462 static bfd_vma
7463 arm_movt_immediate (bfd_vma value)
7464 {
7465   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
7466 }
7467
7468 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
7469    the entry lives in .iplt and resolves to (*SYM_VALUE)().
7470    Otherwise, DYNINDX is the index of the symbol in the dynamic
7471    symbol table and SYM_VALUE is undefined.
7472
7473    ROOT_PLT points to the offset of the PLT entry from the start of its
7474    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
7475    bookkeeping information.  */
7476
7477 static void
7478 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
7479                               union gotplt_union *root_plt,
7480                               struct arm_plt_info *arm_plt,
7481                               int dynindx, bfd_vma sym_value)
7482 {
7483   struct elf32_arm_link_hash_table *htab;
7484   asection *sgot;
7485   asection *splt;
7486   asection *srel;
7487   bfd_byte *loc;
7488   bfd_vma plt_index;
7489   Elf_Internal_Rela rel;
7490   bfd_vma plt_header_size;
7491   bfd_vma got_header_size;
7492
7493   htab = elf32_arm_hash_table (info);
7494
7495   /* Pick the appropriate sections and sizes.  */
7496   if (dynindx == -1)
7497     {
7498       splt = htab->root.iplt;
7499       sgot = htab->root.igotplt;
7500       srel = htab->root.irelplt;
7501
7502       /* There are no reserved entries in .igot.plt, and no special
7503          first entry in .iplt.  */
7504       got_header_size = 0;
7505       plt_header_size = 0;
7506     }
7507   else
7508     {
7509       splt = htab->root.splt;
7510       sgot = htab->root.sgotplt;
7511       srel = htab->root.srelplt;
7512
7513       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
7514       plt_header_size = htab->plt_header_size;
7515     }
7516   BFD_ASSERT (splt != NULL && srel != NULL);
7517
7518   /* Fill in the entry in the procedure linkage table.  */
7519   if (htab->symbian_p)
7520     {
7521       BFD_ASSERT (dynindx >= 0);
7522       put_arm_insn (htab, output_bfd,
7523                     elf32_arm_symbian_plt_entry[0],
7524                     splt->contents + root_plt->offset);
7525       bfd_put_32 (output_bfd,
7526                   elf32_arm_symbian_plt_entry[1],
7527                   splt->contents + root_plt->offset + 4);
7528
7529       /* Fill in the entry in the .rel.plt section.  */
7530       rel.r_offset = (splt->output_section->vma
7531                       + splt->output_offset
7532                       + root_plt->offset + 4);
7533       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
7534
7535       /* Get the index in the procedure linkage table which
7536          corresponds to this symbol.  This is the index of this symbol
7537          in all the symbols for which we are making plt entries.  The
7538          first entry in the procedure linkage table is reserved.  */
7539       plt_index = ((root_plt->offset - plt_header_size)
7540                    / htab->plt_entry_size);
7541     }
7542   else
7543     {
7544       bfd_vma got_offset, got_address, plt_address;
7545       bfd_vma got_displacement, initial_got_entry;
7546       bfd_byte * ptr;
7547
7548       BFD_ASSERT (sgot != NULL);
7549
7550       /* Get the offset into the .(i)got.plt table of the entry that
7551          corresponds to this function.  */
7552       got_offset = (arm_plt->got_offset & -2);
7553
7554       /* Get the index in the procedure linkage table which
7555          corresponds to this symbol.  This is the index of this symbol
7556          in all the symbols for which we are making plt entries.
7557          After the reserved .got.plt entries, all symbols appear in
7558          the same order as in .plt.  */
7559       plt_index = (got_offset - got_header_size) / 4;
7560
7561       /* Calculate the address of the GOT entry.  */
7562       got_address = (sgot->output_section->vma
7563                      + sgot->output_offset
7564                      + got_offset);
7565
7566       /* ...and the address of the PLT entry.  */
7567       plt_address = (splt->output_section->vma
7568                      + splt->output_offset
7569                      + root_plt->offset);
7570
7571       ptr = splt->contents + root_plt->offset;
7572       if (htab->vxworks_p && info->shared)
7573         {
7574           unsigned int i;
7575           bfd_vma val;
7576
7577           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
7578             {
7579               val = elf32_arm_vxworks_shared_plt_entry[i];
7580               if (i == 2)
7581                 val |= got_address - sgot->output_section->vma;
7582               if (i == 5)
7583                 val |= plt_index * RELOC_SIZE (htab);
7584               if (i == 2 || i == 5)
7585                 bfd_put_32 (output_bfd, val, ptr);
7586               else
7587                 put_arm_insn (htab, output_bfd, val, ptr);
7588             }
7589         }
7590       else if (htab->vxworks_p)
7591         {
7592           unsigned int i;
7593           bfd_vma val;
7594
7595           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
7596             {
7597               val = elf32_arm_vxworks_exec_plt_entry[i];
7598               if (i == 2)
7599                 val |= got_address;
7600               if (i == 4)
7601                 val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
7602               if (i == 5)
7603                 val |= plt_index * RELOC_SIZE (htab);
7604               if (i == 2 || i == 5)
7605                 bfd_put_32 (output_bfd, val, ptr);
7606               else
7607                 put_arm_insn (htab, output_bfd, val, ptr);
7608             }
7609
7610           loc = (htab->srelplt2->contents
7611                  + (plt_index * 2 + 1) * RELOC_SIZE (htab));
7612
7613           /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
7614              referencing the GOT for this PLT entry.  */
7615           rel.r_offset = plt_address + 8;
7616           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
7617           rel.r_addend = got_offset;
7618           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7619           loc += RELOC_SIZE (htab);
7620
7621           /* Create the R_ARM_ABS32 relocation referencing the
7622              beginning of the PLT for this GOT entry.  */
7623           rel.r_offset = got_address;
7624           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
7625           rel.r_addend = 0;
7626           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7627         }
7628       else if (htab->nacl_p)
7629         {
7630           /* Calculate the displacement between the PLT slot and the
7631              common tail that's part of the special initial PLT slot.  */
7632           bfd_vma tail_displacement
7633             = ((splt->output_section->vma + splt->output_offset
7634                 + ARM_NACL_PLT_TAIL_OFFSET)
7635                - (plt_address + htab->plt_entry_size + 4));
7636           BFD_ASSERT ((tail_displacement & 3) == 0);
7637           tail_displacement >>= 2;
7638
7639           BFD_ASSERT ((tail_displacement & 0xff000000) == 0
7640                       || (-tail_displacement & 0xff000000) == 0);
7641
7642           /* Calculate the displacement between the PLT slot and the entry
7643              in the GOT.  The offset accounts for the value produced by
7644              adding to pc in the penultimate instruction of the PLT stub.  */
7645           got_displacement = got_address - (plt_address + htab->plt_entry_size);
7646
7647           /* NaCl does not support interworking at all.  */
7648           BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
7649
7650           put_arm_insn (htab, output_bfd,
7651                         elf32_arm_nacl_plt_entry[0]
7652                         | arm_movw_immediate (got_displacement),
7653                         ptr + 0);
7654           put_arm_insn (htab, output_bfd,
7655                         elf32_arm_nacl_plt_entry[1]
7656                         | arm_movt_immediate (got_displacement),
7657                         ptr + 4);
7658           put_arm_insn (htab, output_bfd,
7659                         elf32_arm_nacl_plt_entry[2],
7660                         ptr + 8);
7661           put_arm_insn (htab, output_bfd,
7662                         elf32_arm_nacl_plt_entry[3]
7663                         | (tail_displacement & 0x00ffffff),
7664                         ptr + 12);
7665         }
7666       else
7667         {
7668           /* Calculate the displacement between the PLT slot and the
7669              entry in the GOT.  The eight-byte offset accounts for the
7670              value produced by adding to pc in the first instruction
7671              of the PLT stub.  */
7672           got_displacement = got_address - (plt_address + 8);
7673
7674           BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
7675
7676           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
7677             {
7678               put_thumb_insn (htab, output_bfd,
7679                               elf32_arm_plt_thumb_stub[0], ptr - 4);
7680               put_thumb_insn (htab, output_bfd,
7681                               elf32_arm_plt_thumb_stub[1], ptr - 2);
7682             }
7683
7684           put_arm_insn (htab, output_bfd,
7685                         elf32_arm_plt_entry[0]
7686                         | ((got_displacement & 0x0ff00000) >> 20),
7687                         ptr + 0);
7688           put_arm_insn (htab, output_bfd,
7689                         elf32_arm_plt_entry[1]
7690                         | ((got_displacement & 0x000ff000) >> 12),
7691                         ptr+ 4);
7692           put_arm_insn (htab, output_bfd,
7693                         elf32_arm_plt_entry[2]
7694                         | (got_displacement & 0x00000fff),
7695                         ptr + 8);
7696 #ifdef FOUR_WORD_PLT
7697           bfd_put_32 (output_bfd, elf32_arm_plt_entry[3], ptr + 12);
7698 #endif
7699         }
7700
7701       /* Fill in the entry in the .rel(a).(i)plt section.  */
7702       rel.r_offset = got_address;
7703       rel.r_addend = 0;
7704       if (dynindx == -1)
7705         {
7706           /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
7707              The dynamic linker or static executable then calls SYM_VALUE
7708              to determine the correct run-time value of the .igot.plt entry.  */
7709           rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
7710           initial_got_entry = sym_value;
7711         }
7712       else
7713         {
7714           rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
7715           initial_got_entry = (splt->output_section->vma
7716                                + splt->output_offset);
7717         }
7718
7719       /* Fill in the entry in the global offset table.  */
7720       bfd_put_32 (output_bfd, initial_got_entry,
7721                   sgot->contents + got_offset);
7722     }
7723
7724   loc = srel->contents + plt_index * RELOC_SIZE (htab);
7725   SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7726 }
7727
7728 /* Some relocations map to different relocations depending on the
7729    target.  Return the real relocation.  */
7730
7731 static int
7732 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
7733                      int r_type)
7734 {
7735   switch (r_type)
7736     {
7737     case R_ARM_TARGET1:
7738       if (globals->target1_is_rel)
7739         return R_ARM_REL32;
7740       else
7741         return R_ARM_ABS32;
7742
7743     case R_ARM_TARGET2:
7744       return globals->target2_reloc;
7745
7746     default:
7747       return r_type;
7748     }
7749 }
7750
7751 /* Return the base VMA address which should be subtracted from real addresses
7752    when resolving @dtpoff relocation.
7753    This is PT_TLS segment p_vaddr.  */
7754
7755 static bfd_vma
7756 dtpoff_base (struct bfd_link_info *info)
7757 {
7758   /* If tls_sec is NULL, we should have signalled an error already.  */
7759   if (elf_hash_table (info)->tls_sec == NULL)
7760     return 0;
7761   return elf_hash_table (info)->tls_sec->vma;
7762 }
7763
7764 /* Return the relocation value for @tpoff relocation
7765    if STT_TLS virtual address is ADDRESS.  */
7766
7767 static bfd_vma
7768 tpoff (struct bfd_link_info *info, bfd_vma address)
7769 {
7770   struct elf_link_hash_table *htab = elf_hash_table (info);
7771   bfd_vma base;
7772
7773   /* If tls_sec is NULL, we should have signalled an error already.  */
7774   if (htab->tls_sec == NULL)
7775     return 0;
7776   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
7777   return address - htab->tls_sec->vma + base;
7778 }
7779
7780 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
7781    VALUE is the relocation value.  */
7782
7783 static bfd_reloc_status_type
7784 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
7785 {
7786   if (value > 0xfff)
7787     return bfd_reloc_overflow;
7788
7789   value |= bfd_get_32 (abfd, data) & 0xfffff000;
7790   bfd_put_32 (abfd, value, data);
7791   return bfd_reloc_ok;
7792 }
7793
7794 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
7795    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
7796    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
7797
7798    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
7799    is to then call final_link_relocate.  Return other values in the
7800    case of error.
7801
7802    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
7803    the pre-relaxed code.  It would be nice if the relocs were updated
7804    to match the optimization.   */
7805
7806 static bfd_reloc_status_type
7807 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
7808                      bfd *input_bfd, asection *input_sec, bfd_byte *contents,
7809                      Elf_Internal_Rela *rel, unsigned long is_local)
7810 {
7811   unsigned long insn;
7812
7813   switch (ELF32_R_TYPE (rel->r_info))
7814     {
7815     default:
7816       return bfd_reloc_notsupported;
7817
7818     case R_ARM_TLS_GOTDESC:
7819       if (is_local)
7820         insn = 0;
7821       else
7822         {
7823           insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7824           if (insn & 1)
7825             insn -= 5; /* THUMB */
7826           else
7827             insn -= 8; /* ARM */
7828         }
7829       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
7830       return bfd_reloc_continue;
7831
7832     case R_ARM_THM_TLS_DESCSEQ:
7833       /* Thumb insn.  */
7834       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
7835       if ((insn & 0xff78) == 0x4478)      /* add rx, pc */
7836         {
7837           if (is_local)
7838             /* nop */
7839             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
7840         }
7841       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
7842         {
7843           if (is_local)
7844             /* nop */
7845             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
7846           else
7847             /* ldr rx,[ry] */
7848             bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
7849         }
7850       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
7851         {
7852           if (is_local)
7853             /* nop */
7854             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
7855           else
7856             /* mov r0, rx */
7857             bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
7858                         contents + rel->r_offset);
7859         }
7860       else
7861         {
7862           if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
7863             /* It's a 32 bit instruction, fetch the rest of it for
7864                error generation.  */
7865             insn = (insn << 16)
7866               | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
7867           (*_bfd_error_handler)
7868             (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' in TLS trampoline"),
7869              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
7870           return bfd_reloc_notsupported;
7871         }
7872       break;
7873
7874     case R_ARM_TLS_DESCSEQ:
7875       /* arm insn.  */
7876       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7877       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
7878         {
7879           if (is_local)
7880             /* mov rx, ry */
7881             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
7882                         contents + rel->r_offset);
7883         }
7884       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
7885         {
7886           if (is_local)
7887             /* nop */
7888             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
7889           else
7890             /* ldr rx,[ry] */
7891             bfd_put_32 (input_bfd, insn & 0xfffff000,
7892                         contents + rel->r_offset);
7893         }
7894       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
7895         {
7896           if (is_local)
7897             /* nop */
7898             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
7899           else
7900             /* mov r0, rx */
7901             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
7902                         contents + rel->r_offset);
7903         }
7904       else
7905         {
7906           (*_bfd_error_handler)
7907             (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' in TLS trampoline"),
7908              input_bfd, input_sec, (unsigned long)rel->r_offset, insn);
7909           return bfd_reloc_notsupported;
7910         }
7911       break;
7912
7913     case R_ARM_TLS_CALL:
7914       /* GD->IE relaxation, turn the instruction into 'nop' or
7915          'ldr r0, [pc,r0]'  */
7916       insn = is_local ? 0xe1a00000 : 0xe79f0000;
7917       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
7918       break;
7919
7920     case R_ARM_THM_TLS_CALL:
7921       /* GD->IE relaxation */
7922       if (!is_local)
7923         /* add r0,pc; ldr r0, [r0]  */
7924         insn = 0x44786800;
7925       else if (arch_has_thumb2_nop (globals))
7926         /* nop.w */
7927         insn = 0xf3af8000;
7928       else
7929         /* nop; nop */
7930         insn = 0xbf00bf00;
7931
7932       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
7933       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
7934       break;
7935     }
7936   return bfd_reloc_ok;
7937 }
7938
7939 /* For a given value of n, calculate the value of G_n as required to
7940    deal with group relocations.  We return it in the form of an
7941    encoded constant-and-rotation, together with the final residual.  If n is
7942    specified as less than zero, then final_residual is filled with the
7943    input value and no further action is performed.  */
7944
7945 static bfd_vma
7946 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
7947 {
7948   int current_n;
7949   bfd_vma g_n;
7950   bfd_vma encoded_g_n = 0;
7951   bfd_vma residual = value; /* Also known as Y_n.  */
7952
7953   for (current_n = 0; current_n <= n; current_n++)
7954     {
7955       int shift;
7956
7957       /* Calculate which part of the value to mask.  */
7958       if (residual == 0)
7959         shift = 0;
7960       else
7961         {
7962           int msb;
7963
7964           /* Determine the most significant bit in the residual and
7965              align the resulting value to a 2-bit boundary.  */
7966           for (msb = 30; msb >= 0; msb -= 2)
7967             if (residual & (3 << msb))
7968               break;
7969
7970           /* The desired shift is now (msb - 6), or zero, whichever
7971              is the greater.  */
7972           shift = msb - 6;
7973           if (shift < 0)
7974             shift = 0;
7975         }
7976
7977       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
7978       g_n = residual & (0xff << shift);
7979       encoded_g_n = (g_n >> shift)
7980                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
7981
7982       /* Calculate the residual for the next time around.  */
7983       residual &= ~g_n;
7984     }
7985
7986   *final_residual = residual;
7987
7988   return encoded_g_n;
7989 }
7990
7991 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
7992    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
7993
7994 static int
7995 identify_add_or_sub (bfd_vma insn)
7996 {
7997   int opcode = insn & 0x1e00000;
7998
7999   if (opcode == 1 << 23) /* ADD */
8000     return 1;
8001
8002   if (opcode == 1 << 22) /* SUB */
8003     return -1;
8004
8005   return 0;
8006 }
8007
8008 /* Perform a relocation as part of a final link.  */
8009
8010 static bfd_reloc_status_type
8011 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
8012                                bfd *                        input_bfd,
8013                                bfd *                        output_bfd,
8014                                asection *                   input_section,
8015                                bfd_byte *                   contents,
8016                                Elf_Internal_Rela *          rel,
8017                                bfd_vma                      value,
8018                                struct bfd_link_info *       info,
8019                                asection *                   sym_sec,
8020                                const char *                 sym_name,
8021                                unsigned char                st_type,
8022                                enum arm_st_branch_type      branch_type,
8023                                struct elf_link_hash_entry * h,
8024                                bfd_boolean *                unresolved_reloc_p,
8025                                char **                      error_message)
8026 {
8027   unsigned long                 r_type = howto->type;
8028   unsigned long                 r_symndx;
8029   bfd_byte *                    hit_data = contents + rel->r_offset;
8030   bfd_vma *                     local_got_offsets;
8031   bfd_vma *                     local_tlsdesc_gotents;
8032   asection *                    sgot;
8033   asection *                    splt;
8034   asection *                    sreloc = NULL;
8035   asection *                    srelgot;
8036   bfd_vma                       addend;
8037   bfd_signed_vma                signed_addend;
8038   unsigned char                 dynreloc_st_type;
8039   bfd_vma                       dynreloc_value;
8040   struct elf32_arm_link_hash_table * globals;
8041   struct elf32_arm_link_hash_entry *eh;
8042   union gotplt_union           *root_plt;
8043   struct arm_plt_info          *arm_plt;
8044   bfd_vma                       plt_offset;
8045   bfd_vma                       gotplt_offset;
8046   bfd_boolean                   has_iplt_entry;
8047
8048   globals = elf32_arm_hash_table (info);
8049   if (globals == NULL)
8050     return bfd_reloc_notsupported;
8051
8052   BFD_ASSERT (is_arm_elf (input_bfd));
8053
8054   /* Some relocation types map to different relocations depending on the
8055      target.  We pick the right one here.  */
8056   r_type = arm_real_reloc_type (globals, r_type);
8057
8058   /* It is possible to have linker relaxations on some TLS access
8059      models.  Update our information here.  */
8060   r_type = elf32_arm_tls_transition (info, r_type, h);
8061
8062   if (r_type != howto->type)
8063     howto = elf32_arm_howto_from_type (r_type);
8064
8065   /* If the start address has been set, then set the EF_ARM_HASENTRY
8066      flag.  Setting this more than once is redundant, but the cost is
8067      not too high, and it keeps the code simple.
8068
8069      The test is done  here, rather than somewhere else, because the
8070      start address is only set just before the final link commences.
8071
8072      Note - if the user deliberately sets a start address of 0, the
8073      flag will not be set.  */
8074   if (bfd_get_start_address (output_bfd) != 0)
8075     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
8076
8077   eh = (struct elf32_arm_link_hash_entry *) h;
8078   sgot = globals->root.sgot;
8079   local_got_offsets = elf_local_got_offsets (input_bfd);
8080   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
8081
8082   if (globals->root.dynamic_sections_created)
8083     srelgot = globals->root.srelgot;
8084   else
8085     srelgot = NULL;
8086
8087   r_symndx = ELF32_R_SYM (rel->r_info);
8088
8089   if (globals->use_rel)
8090     {
8091       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
8092
8093       if (addend & ((howto->src_mask + 1) >> 1))
8094         {
8095           signed_addend = -1;
8096           signed_addend &= ~ howto->src_mask;
8097           signed_addend |= addend;
8098         }
8099       else
8100         signed_addend = addend;
8101     }
8102   else
8103     addend = signed_addend = rel->r_addend;
8104
8105   /* Record the symbol information that should be used in dynamic
8106      relocations.  */
8107   dynreloc_st_type = st_type;
8108   dynreloc_value = value;
8109   if (branch_type == ST_BRANCH_TO_THUMB)
8110     dynreloc_value |= 1;
8111
8112   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
8113      VALUE appropriately for relocations that we resolve at link time.  */
8114   has_iplt_entry = FALSE;
8115   if (elf32_arm_get_plt_info (input_bfd, eh, r_symndx, &root_plt, &arm_plt)
8116       && root_plt->offset != (bfd_vma) -1)
8117     {
8118       plt_offset = root_plt->offset;
8119       gotplt_offset = arm_plt->got_offset;
8120
8121       if (h == NULL || eh->is_iplt)
8122         {
8123           has_iplt_entry = TRUE;
8124           splt = globals->root.iplt;
8125
8126           /* Populate .iplt entries here, because not all of them will
8127              be seen by finish_dynamic_symbol.  The lower bit is set if
8128              we have already populated the entry.  */
8129           if (plt_offset & 1)
8130             plt_offset--;
8131           else
8132             {
8133               elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
8134                                             -1, dynreloc_value);
8135               root_plt->offset |= 1;
8136             }
8137
8138           /* Static relocations always resolve to the .iplt entry.  */
8139           st_type = STT_FUNC;
8140           value = (splt->output_section->vma
8141                    + splt->output_offset
8142                    + plt_offset);
8143           branch_type = ST_BRANCH_TO_ARM;
8144
8145           /* If there are non-call relocations that resolve to the .iplt
8146              entry, then all dynamic ones must too.  */
8147           if (arm_plt->noncall_refcount != 0)
8148             {
8149               dynreloc_st_type = st_type;
8150               dynreloc_value = value;
8151             }
8152         }
8153       else
8154         /* We populate the .plt entry in finish_dynamic_symbol.  */
8155         splt = globals->root.splt;
8156     }
8157   else
8158     {
8159       splt = NULL;
8160       plt_offset = (bfd_vma) -1;
8161       gotplt_offset = (bfd_vma) -1;
8162     }
8163
8164   switch (r_type)
8165     {
8166     case R_ARM_NONE:
8167       /* We don't need to find a value for this symbol.  It's just a
8168          marker.  */
8169       *unresolved_reloc_p = FALSE;
8170       return bfd_reloc_ok;
8171
8172     case R_ARM_ABS12:
8173       if (!globals->vxworks_p)
8174         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
8175
8176     case R_ARM_PC24:
8177     case R_ARM_ABS32:
8178     case R_ARM_ABS32_NOI:
8179     case R_ARM_REL32:
8180     case R_ARM_REL32_NOI:
8181     case R_ARM_CALL:
8182     case R_ARM_JUMP24:
8183     case R_ARM_XPC25:
8184     case R_ARM_PREL31:
8185     case R_ARM_PLT32:
8186       /* Handle relocations which should use the PLT entry.  ABS32/REL32
8187          will use the symbol's value, which may point to a PLT entry, but we
8188          don't need to handle that here.  If we created a PLT entry, all
8189          branches in this object should go to it, except if the PLT is too
8190          far away, in which case a long branch stub should be inserted.  */
8191       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
8192            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
8193            && r_type != R_ARM_CALL
8194            && r_type != R_ARM_JUMP24
8195            && r_type != R_ARM_PLT32)
8196           && plt_offset != (bfd_vma) -1)
8197         {
8198           /* If we've created a .plt section, and assigned a PLT entry
8199              to this function, it must either be a STT_GNU_IFUNC reference
8200              or not be known to bind locally.  In other cases, we should
8201              have cleared the PLT entry by now.  */
8202           BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
8203
8204           value = (splt->output_section->vma
8205                    + splt->output_offset
8206                    + plt_offset);
8207           *unresolved_reloc_p = FALSE;
8208           return _bfd_final_link_relocate (howto, input_bfd, input_section,
8209                                            contents, rel->r_offset, value,
8210                                            rel->r_addend);
8211         }
8212
8213       /* When generating a shared object or relocatable executable, these
8214          relocations are copied into the output file to be resolved at
8215          run time.  */
8216       if ((info->shared || globals->root.is_relocatable_executable)
8217           && (input_section->flags & SEC_ALLOC)
8218           && !(globals->vxworks_p
8219                && strcmp (input_section->output_section->name,
8220                           ".tls_vars") == 0)
8221           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
8222               || !SYMBOL_CALLS_LOCAL (info, h))
8223           && (!strstr (input_section->name, STUB_SUFFIX))
8224           && (h == NULL
8225               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8226               || h->root.type != bfd_link_hash_undefweak)
8227           && r_type != R_ARM_PC24
8228           && r_type != R_ARM_CALL
8229           && r_type != R_ARM_JUMP24
8230           && r_type != R_ARM_PREL31
8231           && r_type != R_ARM_PLT32)
8232         {
8233           Elf_Internal_Rela outrel;
8234           bfd_boolean skip, relocate;
8235
8236           *unresolved_reloc_p = FALSE;
8237
8238           if (sreloc == NULL && globals->root.dynamic_sections_created)
8239             {
8240               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
8241                                                            ! globals->use_rel);
8242
8243               if (sreloc == NULL)
8244                 return bfd_reloc_notsupported;
8245             }
8246
8247           skip = FALSE;
8248           relocate = FALSE;
8249
8250           outrel.r_addend = addend;
8251           outrel.r_offset =
8252             _bfd_elf_section_offset (output_bfd, info, input_section,
8253                                      rel->r_offset);
8254           if (outrel.r_offset == (bfd_vma) -1)
8255             skip = TRUE;
8256           else if (outrel.r_offset == (bfd_vma) -2)
8257             skip = TRUE, relocate = TRUE;
8258           outrel.r_offset += (input_section->output_section->vma
8259                               + input_section->output_offset);
8260
8261           if (skip)
8262             memset (&outrel, 0, sizeof outrel);
8263           else if (h != NULL
8264                    && h->dynindx != -1
8265                    && (!info->shared
8266                        || !info->symbolic
8267                        || !h->def_regular))
8268             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
8269           else
8270             {
8271               int symbol;
8272
8273               /* This symbol is local, or marked to become local.  */
8274               BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI);
8275               if (globals->symbian_p)
8276                 {
8277                   asection *osec;
8278
8279                   /* On Symbian OS, the data segment and text segement
8280                      can be relocated independently.  Therefore, we
8281                      must indicate the segment to which this
8282                      relocation is relative.  The BPABI allows us to
8283                      use any symbol in the right segment; we just use
8284                      the section symbol as it is convenient.  (We
8285                      cannot use the symbol given by "h" directly as it
8286                      will not appear in the dynamic symbol table.)
8287
8288                      Note that the dynamic linker ignores the section
8289                      symbol value, so we don't subtract osec->vma
8290                      from the emitted reloc addend.  */
8291                   if (sym_sec)
8292                     osec = sym_sec->output_section;
8293                   else
8294                     osec = input_section->output_section;
8295                   symbol = elf_section_data (osec)->dynindx;
8296                   if (symbol == 0)
8297                     {
8298                       struct elf_link_hash_table *htab = elf_hash_table (info);
8299
8300                       if ((osec->flags & SEC_READONLY) == 0
8301                           && htab->data_index_section != NULL)
8302                         osec = htab->data_index_section;
8303                       else
8304                         osec = htab->text_index_section;
8305                       symbol = elf_section_data (osec)->dynindx;
8306                     }
8307                   BFD_ASSERT (symbol != 0);
8308                 }
8309               else
8310                 /* On SVR4-ish systems, the dynamic loader cannot
8311                    relocate the text and data segments independently,
8312                    so the symbol does not matter.  */
8313                 symbol = 0;
8314               if (dynreloc_st_type == STT_GNU_IFUNC)
8315                 /* We have an STT_GNU_IFUNC symbol that doesn't resolve
8316                    to the .iplt entry.  Instead, every non-call reference
8317                    must use an R_ARM_IRELATIVE relocation to obtain the
8318                    correct run-time address.  */
8319                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
8320               else
8321                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
8322               if (globals->use_rel)
8323                 relocate = TRUE;
8324               else
8325                 outrel.r_addend += dynreloc_value;
8326             }
8327
8328           elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
8329
8330           /* If this reloc is against an external symbol, we do not want to
8331              fiddle with the addend.  Otherwise, we need to include the symbol
8332              value so that it becomes an addend for the dynamic reloc.  */
8333           if (! relocate)
8334             return bfd_reloc_ok;
8335
8336           return _bfd_final_link_relocate (howto, input_bfd, input_section,
8337                                            contents, rel->r_offset,
8338                                            dynreloc_value, (bfd_vma) 0);
8339         }
8340       else switch (r_type)
8341         {
8342         case R_ARM_ABS12:
8343           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
8344
8345         case R_ARM_XPC25:         /* Arm BLX instruction.  */
8346         case R_ARM_CALL:
8347         case R_ARM_JUMP24:
8348         case R_ARM_PC24:          /* Arm B/BL instruction.  */
8349         case R_ARM_PLT32:
8350           {
8351           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
8352
8353           if (r_type == R_ARM_XPC25)
8354             {
8355               /* Check for Arm calling Arm function.  */
8356               /* FIXME: Should we translate the instruction into a BL
8357                  instruction instead ?  */
8358               if (branch_type != ST_BRANCH_TO_THUMB)
8359                 (*_bfd_error_handler)
8360                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
8361                    input_bfd,
8362                    h ? h->root.root.string : "(local)");
8363             }
8364           else if (r_type == R_ARM_PC24)
8365             {
8366               /* Check for Arm calling Thumb function.  */
8367               if (branch_type == ST_BRANCH_TO_THUMB)
8368                 {
8369                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
8370                                                output_bfd, input_section,
8371                                                hit_data, sym_sec, rel->r_offset,
8372                                                signed_addend, value,
8373                                                error_message))
8374                     return bfd_reloc_ok;
8375                   else
8376                     return bfd_reloc_dangerous;
8377                 }
8378             }
8379
8380           /* Check if a stub has to be inserted because the
8381              destination is too far or we are changing mode.  */
8382           if (   r_type == R_ARM_CALL
8383               || r_type == R_ARM_JUMP24
8384               || r_type == R_ARM_PLT32)
8385             {
8386               enum elf32_arm_stub_type stub_type = arm_stub_none;
8387               struct elf32_arm_link_hash_entry *hash;
8388
8389               hash = (struct elf32_arm_link_hash_entry *) h;
8390               stub_type = arm_type_of_stub (info, input_section, rel,
8391                                             st_type, &branch_type,
8392                                             hash, value, sym_sec,
8393                                             input_bfd, sym_name);
8394
8395               if (stub_type != arm_stub_none)
8396                 {
8397                   /* The target is out of reach, so redirect the
8398                      branch to the local stub for this function.  */
8399                   stub_entry = elf32_arm_get_stub_entry (input_section,
8400                                                          sym_sec, h,
8401                                                          rel, globals,
8402                                                          stub_type);
8403                   {
8404                     if (stub_entry != NULL)
8405                       value = (stub_entry->stub_offset
8406                                + stub_entry->stub_sec->output_offset
8407                                + stub_entry->stub_sec->output_section->vma);
8408
8409                     if (plt_offset != (bfd_vma) -1)
8410                       *unresolved_reloc_p = FALSE;
8411                   }
8412                 }
8413               else
8414                 {
8415                   /* If the call goes through a PLT entry, make sure to
8416                      check distance to the right destination address.  */
8417                   if (plt_offset != (bfd_vma) -1)
8418                     {
8419                       value = (splt->output_section->vma
8420                                + splt->output_offset
8421                                + plt_offset);
8422                       *unresolved_reloc_p = FALSE;
8423                       /* The PLT entry is in ARM mode, regardless of the
8424                          target function.  */
8425                       branch_type = ST_BRANCH_TO_ARM;
8426                     }
8427                 }
8428             }
8429
8430           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
8431              where:
8432               S is the address of the symbol in the relocation.
8433               P is address of the instruction being relocated.
8434               A is the addend (extracted from the instruction) in bytes.
8435
8436              S is held in 'value'.
8437              P is the base address of the section containing the
8438                instruction plus the offset of the reloc into that
8439                section, ie:
8440                  (input_section->output_section->vma +
8441                   input_section->output_offset +
8442                   rel->r_offset).
8443              A is the addend, converted into bytes, ie:
8444                  (signed_addend * 4)
8445
8446              Note: None of these operations have knowledge of the pipeline
8447              size of the processor, thus it is up to the assembler to
8448              encode this information into the addend.  */
8449           value -= (input_section->output_section->vma
8450                     + input_section->output_offset);
8451           value -= rel->r_offset;
8452           if (globals->use_rel)
8453             value += (signed_addend << howto->size);
8454           else
8455             /* RELA addends do not have to be adjusted by howto->size.  */
8456             value += signed_addend;
8457
8458           signed_addend = value;
8459           signed_addend >>= howto->rightshift;
8460
8461           /* A branch to an undefined weak symbol is turned into a jump to
8462              the next instruction unless a PLT entry will be created.
8463              Do the same for local undefined symbols (but not for STN_UNDEF).
8464              The jump to the next instruction is optimized as a NOP depending
8465              on the architecture.  */
8466           if (h ? (h->root.type == bfd_link_hash_undefweak
8467                    && plt_offset == (bfd_vma) -1)
8468               : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
8469             {
8470               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
8471
8472               if (arch_has_arm_nop (globals))
8473                 value |= 0x0320f000;
8474               else
8475                 value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
8476             }
8477           else
8478             {
8479               /* Perform a signed range check.  */
8480               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
8481                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
8482                 return bfd_reloc_overflow;
8483
8484               addend = (value & 2);
8485
8486               value = (signed_addend & howto->dst_mask)
8487                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
8488
8489               if (r_type == R_ARM_CALL)
8490                 {
8491                   /* Set the H bit in the BLX instruction.  */
8492                   if (branch_type == ST_BRANCH_TO_THUMB)
8493                     {
8494                       if (addend)
8495                         value |= (1 << 24);
8496                       else
8497                         value &= ~(bfd_vma)(1 << 24);
8498                     }
8499
8500                   /* Select the correct instruction (BL or BLX).  */
8501                   /* Only if we are not handling a BL to a stub. In this
8502                      case, mode switching is performed by the stub.  */
8503                   if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
8504                     value |= (1 << 28);
8505                   else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
8506                     {
8507                       value &= ~(bfd_vma)(1 << 28);
8508                       value |= (1 << 24);
8509                     }
8510                 }
8511             }
8512           }
8513           break;
8514
8515         case R_ARM_ABS32:
8516           value += addend;
8517           if (branch_type == ST_BRANCH_TO_THUMB)
8518             value |= 1;
8519           break;
8520
8521         case R_ARM_ABS32_NOI:
8522           value += addend;
8523           break;
8524
8525         case R_ARM_REL32:
8526           value += addend;
8527           if (branch_type == ST_BRANCH_TO_THUMB)
8528             value |= 1;
8529           value -= (input_section->output_section->vma
8530                     + input_section->output_offset + rel->r_offset);
8531           break;
8532
8533         case R_ARM_REL32_NOI:
8534           value += addend;
8535           value -= (input_section->output_section->vma
8536                     + input_section->output_offset + rel->r_offset);
8537           break;
8538
8539         case R_ARM_PREL31:
8540           value -= (input_section->output_section->vma
8541                     + input_section->output_offset + rel->r_offset);
8542           value += signed_addend;
8543           if (! h || h->root.type != bfd_link_hash_undefweak)
8544             {
8545               /* Check for overflow.  */
8546               if ((value ^ (value >> 1)) & (1 << 30))
8547                 return bfd_reloc_overflow;
8548             }
8549           value &= 0x7fffffff;
8550           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
8551           if (branch_type == ST_BRANCH_TO_THUMB)
8552             value |= 1;
8553           break;
8554         }
8555
8556       bfd_put_32 (input_bfd, value, hit_data);
8557       return bfd_reloc_ok;
8558
8559     case R_ARM_ABS8:
8560       value += addend;
8561
8562       /* There is no way to tell whether the user intended to use a signed or
8563          unsigned addend.  When checking for overflow we accept either,
8564          as specified by the AAELF.  */
8565       if ((long) value > 0xff || (long) value < -0x80)
8566         return bfd_reloc_overflow;
8567
8568       bfd_put_8 (input_bfd, value, hit_data);
8569       return bfd_reloc_ok;
8570
8571     case R_ARM_ABS16:
8572       value += addend;
8573
8574       /* See comment for R_ARM_ABS8.  */
8575       if ((long) value > 0xffff || (long) value < -0x8000)
8576         return bfd_reloc_overflow;
8577
8578       bfd_put_16 (input_bfd, value, hit_data);
8579       return bfd_reloc_ok;
8580
8581     case R_ARM_THM_ABS5:
8582       /* Support ldr and str instructions for the thumb.  */
8583       if (globals->use_rel)
8584         {
8585           /* Need to refetch addend.  */
8586           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
8587           /* ??? Need to determine shift amount from operand size.  */
8588           addend >>= howto->rightshift;
8589         }
8590       value += addend;
8591
8592       /* ??? Isn't value unsigned?  */
8593       if ((long) value > 0x1f || (long) value < -0x10)
8594         return bfd_reloc_overflow;
8595
8596       /* ??? Value needs to be properly shifted into place first.  */
8597       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
8598       bfd_put_16 (input_bfd, value, hit_data);
8599       return bfd_reloc_ok;
8600
8601     case R_ARM_THM_ALU_PREL_11_0:
8602       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
8603       {
8604         bfd_vma insn;
8605         bfd_signed_vma relocation;
8606
8607         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
8608              | bfd_get_16 (input_bfd, hit_data + 2);
8609
8610         if (globals->use_rel)
8611           {
8612             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
8613                           | ((insn & (1 << 26)) >> 15);
8614             if (insn & 0xf00000)
8615               signed_addend = -signed_addend;
8616           }
8617
8618         relocation = value + signed_addend;
8619         relocation -= (input_section->output_section->vma
8620                        + input_section->output_offset
8621                        + rel->r_offset);
8622
8623         value = abs (relocation);
8624
8625         if (value >= 0x1000)
8626           return bfd_reloc_overflow;
8627
8628         insn = (insn & 0xfb0f8f00) | (value & 0xff)
8629              | ((value & 0x700) << 4)
8630              | ((value & 0x800) << 15);
8631         if (relocation < 0)
8632           insn |= 0xa00000;
8633
8634         bfd_put_16 (input_bfd, insn >> 16, hit_data);
8635         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
8636
8637         return bfd_reloc_ok;
8638       }
8639
8640     case R_ARM_THM_PC8:
8641       /* PR 10073:  This reloc is not generated by the GNU toolchain,
8642          but it is supported for compatibility with third party libraries
8643          generated by other compilers, specifically the ARM/IAR.  */
8644       {
8645         bfd_vma insn;
8646         bfd_signed_vma relocation;
8647
8648         insn = bfd_get_16 (input_bfd, hit_data);
8649
8650         if (globals->use_rel)
8651           addend = (insn & 0x00ff) << 2;
8652
8653         relocation = value + addend;
8654         relocation -= (input_section->output_section->vma
8655                        + input_section->output_offset
8656                        + rel->r_offset);
8657
8658         value = abs (relocation);
8659
8660         /* We do not check for overflow of this reloc.  Although strictly
8661            speaking this is incorrect, it appears to be necessary in order
8662            to work with IAR generated relocs.  Since GCC and GAS do not
8663            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
8664            a problem for them.  */
8665         value &= 0x3fc;
8666
8667         insn = (insn & 0xff00) | (value >> 2);
8668
8669         bfd_put_16 (input_bfd, insn, hit_data);
8670
8671         return bfd_reloc_ok;
8672       }
8673
8674     case R_ARM_THM_PC12:
8675       /* Corresponds to: ldr.w reg, [pc, #offset].  */
8676       {
8677         bfd_vma insn;
8678         bfd_signed_vma relocation;
8679
8680         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
8681              | bfd_get_16 (input_bfd, hit_data + 2);
8682
8683         if (globals->use_rel)
8684           {
8685             signed_addend = insn & 0xfff;
8686             if (!(insn & (1 << 23)))
8687               signed_addend = -signed_addend;
8688           }
8689
8690         relocation = value + signed_addend;
8691         relocation -= (input_section->output_section->vma
8692                        + input_section->output_offset
8693                        + rel->r_offset);
8694
8695         value = abs (relocation);
8696
8697         if (value >= 0x1000)
8698           return bfd_reloc_overflow;
8699
8700         insn = (insn & 0xff7ff000) | value;
8701         if (relocation >= 0)
8702           insn |= (1 << 23);
8703
8704         bfd_put_16 (input_bfd, insn >> 16, hit_data);
8705         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
8706
8707         return bfd_reloc_ok;
8708       }
8709
8710     case R_ARM_THM_XPC22:
8711     case R_ARM_THM_CALL:
8712     case R_ARM_THM_JUMP24:
8713       /* Thumb BL (branch long instruction).  */
8714       {
8715         bfd_vma relocation;
8716         bfd_vma reloc_sign;
8717         bfd_boolean overflow = FALSE;
8718         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
8719         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
8720         bfd_signed_vma reloc_signed_max;
8721         bfd_signed_vma reloc_signed_min;
8722         bfd_vma check;
8723         bfd_signed_vma signed_check;
8724         int bitsize;
8725         const int thumb2 = using_thumb2 (globals);
8726
8727         /* A branch to an undefined weak symbol is turned into a jump to
8728            the next instruction unless a PLT entry will be created.
8729            The jump to the next instruction is optimized as a NOP.W for
8730            Thumb-2 enabled architectures.  */
8731         if (h && h->root.type == bfd_link_hash_undefweak
8732             && plt_offset == (bfd_vma) -1)
8733           {
8734             if (arch_has_thumb2_nop (globals))
8735               {
8736                 bfd_put_16 (input_bfd, 0xf3af, hit_data);
8737                 bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
8738               }
8739             else
8740               {
8741                 bfd_put_16 (input_bfd, 0xe000, hit_data);
8742                 bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
8743               }
8744             return bfd_reloc_ok;
8745           }
8746
8747         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
8748            with Thumb-1) involving the J1 and J2 bits.  */
8749         if (globals->use_rel)
8750           {
8751             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
8752             bfd_vma upper = upper_insn & 0x3ff;
8753             bfd_vma lower = lower_insn & 0x7ff;
8754             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
8755             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
8756             bfd_vma i1 = j1 ^ s ? 0 : 1;
8757             bfd_vma i2 = j2 ^ s ? 0 : 1;
8758
8759             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
8760             /* Sign extend.  */
8761             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
8762
8763             signed_addend = addend;
8764           }
8765
8766         if (r_type == R_ARM_THM_XPC22)
8767           {
8768             /* Check for Thumb to Thumb call.  */
8769             /* FIXME: Should we translate the instruction into a BL
8770                instruction instead ?  */
8771             if (branch_type == ST_BRANCH_TO_THUMB)
8772               (*_bfd_error_handler)
8773                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
8774                  input_bfd,
8775                  h ? h->root.root.string : "(local)");
8776           }
8777         else
8778           {
8779             /* If it is not a call to Thumb, assume call to Arm.
8780                If it is a call relative to a section name, then it is not a
8781                function call at all, but rather a long jump.  Calls through
8782                the PLT do not require stubs.  */
8783             if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
8784               {
8785                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
8786                   {
8787                     /* Convert BL to BLX.  */
8788                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
8789                   }
8790                 else if ((   r_type != R_ARM_THM_CALL)
8791                          && (r_type != R_ARM_THM_JUMP24))
8792                   {
8793                     if (elf32_thumb_to_arm_stub
8794                         (info, sym_name, input_bfd, output_bfd, input_section,
8795                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
8796                          error_message))
8797                       return bfd_reloc_ok;
8798                     else
8799                       return bfd_reloc_dangerous;
8800                   }
8801               }
8802             else if (branch_type == ST_BRANCH_TO_THUMB
8803                      && globals->use_blx
8804                      && r_type == R_ARM_THM_CALL)
8805               {
8806                 /* Make sure this is a BL.  */
8807                 lower_insn |= 0x1800;
8808               }
8809           }
8810
8811         enum elf32_arm_stub_type stub_type = arm_stub_none;
8812         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
8813           {
8814             /* Check if a stub has to be inserted because the destination
8815                is too far.  */
8816             struct elf32_arm_stub_hash_entry *stub_entry;
8817             struct elf32_arm_link_hash_entry *hash;
8818
8819             hash = (struct elf32_arm_link_hash_entry *) h;
8820
8821             stub_type = arm_type_of_stub (info, input_section, rel,
8822                                           st_type, &branch_type,
8823                                           hash, value, sym_sec,
8824                                           input_bfd, sym_name);
8825
8826             if (stub_type != arm_stub_none)
8827               {
8828                 /* The target is out of reach or we are changing modes, so
8829                    redirect the branch to the local stub for this
8830                    function.  */
8831                 stub_entry = elf32_arm_get_stub_entry (input_section,
8832                                                        sym_sec, h,
8833                                                        rel, globals,
8834                                                        stub_type);
8835                 if (stub_entry != NULL)
8836                   {
8837                     value = (stub_entry->stub_offset
8838                              + stub_entry->stub_sec->output_offset
8839                              + stub_entry->stub_sec->output_section->vma);
8840
8841                     if (plt_offset != (bfd_vma) -1)
8842                       *unresolved_reloc_p = FALSE;
8843                   }
8844
8845                 /* If this call becomes a call to Arm, force BLX.  */
8846                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
8847                   {
8848                     if ((stub_entry
8849                          && !arm_stub_is_thumb (stub_entry->stub_type))
8850                         || branch_type != ST_BRANCH_TO_THUMB)
8851                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
8852                   }
8853               }
8854           }
8855
8856         /* Handle calls via the PLT.  */
8857         if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
8858           {
8859             value = (splt->output_section->vma
8860                      + splt->output_offset
8861                      + plt_offset);
8862
8863             if (globals->use_blx && r_type == R_ARM_THM_CALL)
8864               {
8865                 /* If the Thumb BLX instruction is available, convert
8866                    the BL to a BLX instruction to call the ARM-mode
8867                    PLT entry.  */
8868                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
8869                 branch_type = ST_BRANCH_TO_ARM;
8870               }
8871             else
8872               {
8873                 /* Target the Thumb stub before the ARM PLT entry.  */
8874                 value -= PLT_THUMB_STUB_SIZE;
8875                 branch_type = ST_BRANCH_TO_THUMB;
8876               }
8877             *unresolved_reloc_p = FALSE;
8878           }
8879
8880         relocation = value + signed_addend;
8881
8882         relocation -= (input_section->output_section->vma
8883                        + input_section->output_offset
8884                        + rel->r_offset);
8885
8886         check = relocation >> howto->rightshift;
8887
8888         /* If this is a signed value, the rightshift just dropped
8889            leading 1 bits (assuming twos complement).  */
8890         if ((bfd_signed_vma) relocation >= 0)
8891           signed_check = check;
8892         else
8893           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
8894
8895         /* Calculate the permissable maximum and minimum values for
8896            this relocation according to whether we're relocating for
8897            Thumb-2 or not.  */
8898         bitsize = howto->bitsize;
8899         if (!thumb2)
8900           bitsize -= 2;
8901         reloc_signed_max = (1 << (bitsize - 1)) - 1;
8902         reloc_signed_min = ~reloc_signed_max;
8903
8904         /* Assumes two's complement.  */
8905         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
8906           overflow = TRUE;
8907
8908         if ((lower_insn & 0x5000) == 0x4000)
8909           /* For a BLX instruction, make sure that the relocation is rounded up
8910              to a word boundary.  This follows the semantics of the instruction
8911              which specifies that bit 1 of the target address will come from bit
8912              1 of the base address.  */
8913           relocation = (relocation + 2) & ~ 3;
8914
8915         /* Put RELOCATION back into the insn.  Assumes two's complement.
8916            We use the Thumb-2 encoding, which is safe even if dealing with
8917            a Thumb-1 instruction by virtue of our overflow check above.  */
8918         reloc_sign = (signed_check < 0) ? 1 : 0;
8919         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
8920                      | ((relocation >> 12) & 0x3ff)
8921                      | (reloc_sign << 10);
8922         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
8923                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
8924                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
8925                      | ((relocation >> 1) & 0x7ff);
8926
8927         /* Put the relocated value back in the object file:  */
8928         bfd_put_16 (input_bfd, upper_insn, hit_data);
8929         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
8930
8931         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
8932       }
8933       break;
8934
8935     case R_ARM_THM_JUMP19:
8936       /* Thumb32 conditional branch instruction.  */
8937       {
8938         bfd_vma relocation;
8939         bfd_boolean overflow = FALSE;
8940         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
8941         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
8942         bfd_signed_vma reloc_signed_max = 0xffffe;
8943         bfd_signed_vma reloc_signed_min = -0x100000;
8944         bfd_signed_vma signed_check;
8945
8946         /* Need to refetch the addend, reconstruct the top three bits,
8947            and squish the two 11 bit pieces together.  */
8948         if (globals->use_rel)
8949           {
8950             bfd_vma S     = (upper_insn & 0x0400) >> 10;
8951             bfd_vma upper = (upper_insn & 0x003f);
8952             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
8953             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
8954             bfd_vma lower = (lower_insn & 0x07ff);
8955
8956             upper |= J1 << 6;
8957             upper |= J2 << 7;
8958             upper |= (!S) << 8;
8959             upper -= 0x0100; /* Sign extend.  */
8960
8961             addend = (upper << 12) | (lower << 1);
8962             signed_addend = addend;
8963           }
8964
8965         /* Handle calls via the PLT.  */
8966         if (plt_offset != (bfd_vma) -1)
8967           {
8968             value = (splt->output_section->vma
8969                      + splt->output_offset
8970                      + plt_offset);
8971             /* Target the Thumb stub before the ARM PLT entry.  */
8972             value -= PLT_THUMB_STUB_SIZE;
8973             *unresolved_reloc_p = FALSE;
8974           }
8975
8976         /* ??? Should handle interworking?  GCC might someday try to
8977            use this for tail calls.  */
8978
8979         relocation = value + signed_addend;
8980         relocation -= (input_section->output_section->vma
8981                        + input_section->output_offset
8982                        + rel->r_offset);
8983         signed_check = (bfd_signed_vma) relocation;
8984
8985         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
8986           overflow = TRUE;
8987
8988         /* Put RELOCATION back into the insn.  */
8989         {
8990           bfd_vma S  = (relocation & 0x00100000) >> 20;
8991           bfd_vma J2 = (relocation & 0x00080000) >> 19;
8992           bfd_vma J1 = (relocation & 0x00040000) >> 18;
8993           bfd_vma hi = (relocation & 0x0003f000) >> 12;
8994           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
8995
8996           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
8997           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
8998         }
8999
9000         /* Put the relocated value back in the object file:  */
9001         bfd_put_16 (input_bfd, upper_insn, hit_data);
9002         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9003
9004         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
9005       }
9006
9007     case R_ARM_THM_JUMP11:
9008     case R_ARM_THM_JUMP8:
9009     case R_ARM_THM_JUMP6:
9010       /* Thumb B (branch) instruction).  */
9011       {
9012         bfd_signed_vma relocation;
9013         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
9014         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
9015         bfd_signed_vma signed_check;
9016
9017         /* CZB cannot jump backward.  */
9018         if (r_type == R_ARM_THM_JUMP6)
9019           reloc_signed_min = 0;
9020
9021         if (globals->use_rel)
9022           {
9023             /* Need to refetch addend.  */
9024             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
9025             if (addend & ((howto->src_mask + 1) >> 1))
9026               {
9027                 signed_addend = -1;
9028                 signed_addend &= ~ howto->src_mask;
9029                 signed_addend |= addend;
9030               }
9031             else
9032               signed_addend = addend;
9033             /* The value in the insn has been right shifted.  We need to
9034                undo this, so that we can perform the address calculation
9035                in terms of bytes.  */
9036             signed_addend <<= howto->rightshift;
9037           }
9038         relocation = value + signed_addend;
9039
9040         relocation -= (input_section->output_section->vma
9041                        + input_section->output_offset
9042                        + rel->r_offset);
9043
9044         relocation >>= howto->rightshift;
9045         signed_check = relocation;
9046
9047         if (r_type == R_ARM_THM_JUMP6)
9048           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
9049         else
9050           relocation &= howto->dst_mask;
9051         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
9052
9053         bfd_put_16 (input_bfd, relocation, hit_data);
9054
9055         /* Assumes two's complement.  */
9056         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
9057           return bfd_reloc_overflow;
9058
9059         return bfd_reloc_ok;
9060       }
9061
9062     case R_ARM_ALU_PCREL7_0:
9063     case R_ARM_ALU_PCREL15_8:
9064     case R_ARM_ALU_PCREL23_15:
9065       {
9066         bfd_vma insn;
9067         bfd_vma relocation;
9068
9069         insn = bfd_get_32 (input_bfd, hit_data);
9070         if (globals->use_rel)
9071           {
9072             /* Extract the addend.  */
9073             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
9074             signed_addend = addend;
9075           }
9076         relocation = value + signed_addend;
9077
9078         relocation -= (input_section->output_section->vma
9079                        + input_section->output_offset
9080                        + rel->r_offset);
9081         insn = (insn & ~0xfff)
9082                | ((howto->bitpos << 7) & 0xf00)
9083                | ((relocation >> howto->bitpos) & 0xff);
9084         bfd_put_32 (input_bfd, value, hit_data);
9085       }
9086       return bfd_reloc_ok;
9087
9088     case R_ARM_GNU_VTINHERIT:
9089     case R_ARM_GNU_VTENTRY:
9090       return bfd_reloc_ok;
9091
9092     case R_ARM_GOTOFF32:
9093       /* Relocation is relative to the start of the
9094          global offset table.  */
9095
9096       BFD_ASSERT (sgot != NULL);
9097       if (sgot == NULL)
9098         return bfd_reloc_notsupported;
9099
9100       /* If we are addressing a Thumb function, we need to adjust the
9101          address by one, so that attempts to call the function pointer will
9102          correctly interpret it as Thumb code.  */
9103       if (branch_type == ST_BRANCH_TO_THUMB)
9104         value += 1;
9105
9106       /* Note that sgot->output_offset is not involved in this
9107          calculation.  We always want the start of .got.  If we
9108          define _GLOBAL_OFFSET_TABLE in a different way, as is
9109          permitted by the ABI, we might have to change this
9110          calculation.  */
9111       value -= sgot->output_section->vma;
9112       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9113                                        contents, rel->r_offset, value,
9114                                        rel->r_addend);
9115
9116     case R_ARM_GOTPC:
9117       /* Use global offset table as symbol value.  */
9118       BFD_ASSERT (sgot != NULL);
9119
9120       if (sgot == NULL)
9121         return bfd_reloc_notsupported;
9122
9123       *unresolved_reloc_p = FALSE;
9124       value = sgot->output_section->vma;
9125       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9126                                        contents, rel->r_offset, value,
9127                                        rel->r_addend);
9128
9129     case R_ARM_GOT32:
9130     case R_ARM_GOT_PREL:
9131       /* Relocation is to the entry for this symbol in the
9132          global offset table.  */
9133       if (sgot == NULL)
9134         return bfd_reloc_notsupported;
9135
9136       if (dynreloc_st_type == STT_GNU_IFUNC
9137           && plt_offset != (bfd_vma) -1
9138           && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
9139         {
9140           /* We have a relocation against a locally-binding STT_GNU_IFUNC
9141              symbol, and the relocation resolves directly to the runtime
9142              target rather than to the .iplt entry.  This means that any
9143              .got entry would be the same value as the .igot.plt entry,
9144              so there's no point creating both.  */
9145           sgot = globals->root.igotplt;
9146           value = sgot->output_offset + gotplt_offset;
9147         }
9148       else if (h != NULL)
9149         {
9150           bfd_vma off;
9151
9152           off = h->got.offset;
9153           BFD_ASSERT (off != (bfd_vma) -1);
9154           if ((off & 1) != 0)
9155             {
9156               /* We have already processsed one GOT relocation against
9157                  this symbol.  */
9158               off &= ~1;
9159               if (globals->root.dynamic_sections_created
9160                   && !SYMBOL_REFERENCES_LOCAL (info, h))
9161                 *unresolved_reloc_p = FALSE;
9162             }
9163           else
9164             {
9165               Elf_Internal_Rela outrel;
9166
9167               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9168                 {
9169                   /* If the symbol doesn't resolve locally in a static
9170                      object, we have an undefined reference.  If the
9171                      symbol doesn't resolve locally in a dynamic object,
9172                      it should be resolved by the dynamic linker.  */
9173                   if (globals->root.dynamic_sections_created)
9174                     {
9175                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
9176                       *unresolved_reloc_p = FALSE;
9177                     }
9178                   else
9179                     outrel.r_info = 0;
9180                   outrel.r_addend = 0;
9181                 }
9182               else
9183                 {
9184                   if (dynreloc_st_type == STT_GNU_IFUNC)
9185                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9186                   else if (info->shared)
9187                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
9188                   else
9189                     outrel.r_info = 0;
9190                   outrel.r_addend = dynreloc_value;
9191                 }
9192
9193               /* The GOT entry is initialized to zero by default.
9194                  See if we should install a different value.  */
9195               if (outrel.r_addend != 0
9196                   && (outrel.r_info == 0 || globals->use_rel))
9197                 {
9198                   bfd_put_32 (output_bfd, outrel.r_addend,
9199                               sgot->contents + off);
9200                   outrel.r_addend = 0;
9201                 }
9202
9203               if (outrel.r_info != 0)
9204                 {
9205                   outrel.r_offset = (sgot->output_section->vma
9206                                      + sgot->output_offset
9207                                      + off);
9208                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
9209                 }
9210               h->got.offset |= 1;
9211             }
9212           value = sgot->output_offset + off;
9213         }
9214       else
9215         {
9216           bfd_vma off;
9217
9218           BFD_ASSERT (local_got_offsets != NULL &&
9219                       local_got_offsets[r_symndx] != (bfd_vma) -1);
9220
9221           off = local_got_offsets[r_symndx];
9222
9223           /* The offset must always be a multiple of 4.  We use the
9224              least significant bit to record whether we have already
9225              generated the necessary reloc.  */
9226           if ((off & 1) != 0)
9227             off &= ~1;
9228           else
9229             {
9230               if (globals->use_rel)
9231                 bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
9232
9233               if (info->shared || dynreloc_st_type == STT_GNU_IFUNC)
9234                 {
9235                   Elf_Internal_Rela outrel;
9236
9237                   outrel.r_addend = addend + dynreloc_value;
9238                   outrel.r_offset = (sgot->output_section->vma
9239                                      + sgot->output_offset
9240                                      + off);
9241                   if (dynreloc_st_type == STT_GNU_IFUNC)
9242                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9243                   else
9244                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
9245                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
9246                 }
9247
9248               local_got_offsets[r_symndx] |= 1;
9249             }
9250
9251           value = sgot->output_offset + off;
9252         }
9253       if (r_type != R_ARM_GOT32)
9254         value += sgot->output_section->vma;
9255
9256       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9257                                        contents, rel->r_offset, value,
9258                                        rel->r_addend);
9259
9260     case R_ARM_TLS_LDO32:
9261       value = value - dtpoff_base (info);
9262
9263       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9264                                        contents, rel->r_offset, value,
9265                                        rel->r_addend);
9266
9267     case R_ARM_TLS_LDM32:
9268       {
9269         bfd_vma off;
9270
9271         if (sgot == NULL)
9272           abort ();
9273
9274         off = globals->tls_ldm_got.offset;
9275
9276         if ((off & 1) != 0)
9277           off &= ~1;
9278         else
9279           {
9280             /* If we don't know the module number, create a relocation
9281                for it.  */
9282             if (info->shared)
9283               {
9284                 Elf_Internal_Rela outrel;
9285
9286                 if (srelgot == NULL)
9287                   abort ();
9288
9289                 outrel.r_addend = 0;
9290                 outrel.r_offset = (sgot->output_section->vma
9291                                    + sgot->output_offset + off);
9292                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
9293
9294                 if (globals->use_rel)
9295                   bfd_put_32 (output_bfd, outrel.r_addend,
9296                               sgot->contents + off);
9297
9298                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
9299               }
9300             else
9301               bfd_put_32 (output_bfd, 1, sgot->contents + off);
9302
9303             globals->tls_ldm_got.offset |= 1;
9304           }
9305
9306         value = sgot->output_section->vma + sgot->output_offset + off
9307           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
9308
9309         return _bfd_final_link_relocate (howto, input_bfd, input_section,
9310                                          contents, rel->r_offset, value,
9311                                          rel->r_addend);
9312       }
9313
9314     case R_ARM_TLS_CALL:
9315     case R_ARM_THM_TLS_CALL:
9316     case R_ARM_TLS_GD32:
9317     case R_ARM_TLS_IE32:
9318     case R_ARM_TLS_GOTDESC:
9319     case R_ARM_TLS_DESCSEQ:
9320     case R_ARM_THM_TLS_DESCSEQ:
9321       {
9322         bfd_vma off, offplt;
9323         int indx = 0;
9324         char tls_type;
9325
9326         BFD_ASSERT (sgot != NULL);
9327
9328         if (h != NULL)
9329           {
9330             bfd_boolean dyn;
9331             dyn = globals->root.dynamic_sections_created;
9332             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
9333                 && (!info->shared
9334                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
9335               {
9336                 *unresolved_reloc_p = FALSE;
9337                 indx = h->dynindx;
9338               }
9339             off = h->got.offset;
9340             offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
9341             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
9342           }
9343         else
9344           {
9345             BFD_ASSERT (local_got_offsets != NULL);
9346             off = local_got_offsets[r_symndx];
9347             offplt = local_tlsdesc_gotents[r_symndx];
9348             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
9349           }
9350
9351         /* Linker relaxations happens from one of the
9352            R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
9353         if (ELF32_R_TYPE(rel->r_info) != r_type)
9354           tls_type = GOT_TLS_IE;
9355
9356         BFD_ASSERT (tls_type != GOT_UNKNOWN);
9357
9358         if ((off & 1) != 0)
9359           off &= ~1;
9360         else
9361           {
9362             bfd_boolean need_relocs = FALSE;
9363             Elf_Internal_Rela outrel;
9364             int cur_off = off;
9365
9366             /* The GOT entries have not been initialized yet.  Do it
9367                now, and emit any relocations.  If both an IE GOT and a
9368                GD GOT are necessary, we emit the GD first.  */
9369
9370             if ((info->shared || indx != 0)
9371                 && (h == NULL
9372                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9373                     || h->root.type != bfd_link_hash_undefweak))
9374               {
9375                 need_relocs = TRUE;
9376                 BFD_ASSERT (srelgot != NULL);
9377               }
9378
9379             if (tls_type & GOT_TLS_GDESC)
9380               {
9381                 bfd_byte *loc;
9382
9383                 /* We should have relaxed, unless this is an undefined
9384                    weak symbol.  */
9385                 BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
9386                             || info->shared);
9387                 BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
9388                             <= globals->root.sgotplt->size);
9389
9390                 outrel.r_addend = 0;
9391                 outrel.r_offset = (globals->root.sgotplt->output_section->vma
9392                                    + globals->root.sgotplt->output_offset
9393                                    + offplt
9394                                    + globals->sgotplt_jump_table_size);
9395
9396                 outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
9397                 sreloc = globals->root.srelplt;
9398                 loc = sreloc->contents;
9399                 loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
9400                 BFD_ASSERT (loc + RELOC_SIZE (globals)
9401                            <= sreloc->contents + sreloc->size);
9402
9403                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
9404
9405                 /* For globals, the first word in the relocation gets
9406                    the relocation index and the top bit set, or zero,
9407                    if we're binding now.  For locals, it gets the
9408                    symbol's offset in the tls section.  */
9409                 bfd_put_32 (output_bfd,
9410                             !h ? value - elf_hash_table (info)->tls_sec->vma
9411                             : info->flags & DF_BIND_NOW ? 0
9412                             : 0x80000000 | ELF32_R_SYM (outrel.r_info),
9413                             globals->root.sgotplt->contents + offplt
9414                             + globals->sgotplt_jump_table_size);
9415
9416                 /* Second word in the relocation is always zero.  */
9417                 bfd_put_32 (output_bfd, 0,
9418                             globals->root.sgotplt->contents + offplt
9419                             + globals->sgotplt_jump_table_size + 4);
9420               }
9421             if (tls_type & GOT_TLS_GD)
9422               {
9423                 if (need_relocs)
9424                   {
9425                     outrel.r_addend = 0;
9426                     outrel.r_offset = (sgot->output_section->vma
9427                                        + sgot->output_offset
9428                                        + cur_off);
9429                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
9430
9431                     if (globals->use_rel)
9432                       bfd_put_32 (output_bfd, outrel.r_addend,
9433                                   sgot->contents + cur_off);
9434
9435                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
9436
9437                     if (indx == 0)
9438                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
9439                                   sgot->contents + cur_off + 4);
9440                     else
9441                       {
9442                         outrel.r_addend = 0;
9443                         outrel.r_info = ELF32_R_INFO (indx,
9444                                                       R_ARM_TLS_DTPOFF32);
9445                         outrel.r_offset += 4;
9446
9447                         if (globals->use_rel)
9448                           bfd_put_32 (output_bfd, outrel.r_addend,
9449                                       sgot->contents + cur_off + 4);
9450
9451                         elf32_arm_add_dynreloc (output_bfd, info,
9452                                                 srelgot, &outrel);
9453                       }
9454                   }
9455                 else
9456                   {
9457                     /* If we are not emitting relocations for a
9458                        general dynamic reference, then we must be in a
9459                        static link or an executable link with the
9460                        symbol binding locally.  Mark it as belonging
9461                        to module 1, the executable.  */
9462                     bfd_put_32 (output_bfd, 1,
9463                                 sgot->contents + cur_off);
9464                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
9465                                 sgot->contents + cur_off + 4);
9466                   }
9467
9468                 cur_off += 8;
9469               }
9470
9471             if (tls_type & GOT_TLS_IE)
9472               {
9473                 if (need_relocs)
9474                   {
9475                     if (indx == 0)
9476                       outrel.r_addend = value - dtpoff_base (info);
9477                     else
9478                       outrel.r_addend = 0;
9479                     outrel.r_offset = (sgot->output_section->vma
9480                                        + sgot->output_offset
9481                                        + cur_off);
9482                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
9483
9484                     if (globals->use_rel)
9485                       bfd_put_32 (output_bfd, outrel.r_addend,
9486                                   sgot->contents + cur_off);
9487
9488                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
9489                   }
9490                 else
9491                   bfd_put_32 (output_bfd, tpoff (info, value),
9492                               sgot->contents + cur_off);
9493                 cur_off += 4;
9494               }
9495
9496             if (h != NULL)
9497               h->got.offset |= 1;
9498             else
9499               local_got_offsets[r_symndx] |= 1;
9500           }
9501
9502         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
9503           off += 8;
9504         else if (tls_type & GOT_TLS_GDESC)
9505           off = offplt;
9506
9507         if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
9508             || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
9509           {
9510             bfd_signed_vma offset;
9511             /* TLS stubs are arm mode.  The original symbol is a
9512                data object, so branch_type is bogus.  */
9513             branch_type = ST_BRANCH_TO_ARM;
9514             enum elf32_arm_stub_type stub_type
9515               = arm_type_of_stub (info, input_section, rel,
9516                                   st_type, &branch_type,
9517                                   (struct elf32_arm_link_hash_entry *)h,
9518                                   globals->tls_trampoline, globals->root.splt,
9519                                   input_bfd, sym_name);
9520
9521             if (stub_type != arm_stub_none)
9522               {
9523                 struct elf32_arm_stub_hash_entry *stub_entry
9524                   = elf32_arm_get_stub_entry
9525                   (input_section, globals->root.splt, 0, rel,
9526                    globals, stub_type);
9527                 offset = (stub_entry->stub_offset
9528                           + stub_entry->stub_sec->output_offset
9529                           + stub_entry->stub_sec->output_section->vma);
9530               }
9531             else
9532               offset = (globals->root.splt->output_section->vma
9533                         + globals->root.splt->output_offset
9534                         + globals->tls_trampoline);
9535
9536             if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
9537               {
9538                 unsigned long inst;
9539
9540                 offset -= (input_section->output_section->vma
9541                            + input_section->output_offset
9542                            + rel->r_offset + 8);
9543
9544                 inst = offset >> 2;
9545                 inst &= 0x00ffffff;
9546                 value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
9547               }
9548             else
9549               {
9550                 /* Thumb blx encodes the offset in a complicated
9551                    fashion.  */
9552                 unsigned upper_insn, lower_insn;
9553                 unsigned neg;
9554
9555                 offset -= (input_section->output_section->vma
9556                            + input_section->output_offset
9557                            + rel->r_offset + 4);
9558
9559                 if (stub_type != arm_stub_none
9560                     && arm_stub_is_thumb (stub_type))
9561                   {
9562                     lower_insn = 0xd000;
9563                   }
9564                 else
9565                   {
9566                     lower_insn = 0xc000;
9567                     /* Round up the offset to a word boundary */
9568                     offset = (offset + 2) & ~2;
9569                   }
9570
9571                 neg = offset < 0;
9572                 upper_insn = (0xf000
9573                               | ((offset >> 12) & 0x3ff)
9574                               | (neg << 10));
9575                 lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
9576                               | (((!((offset >> 22) & 1)) ^ neg) << 11)
9577                               | ((offset >> 1) & 0x7ff);
9578                 bfd_put_16 (input_bfd, upper_insn, hit_data);
9579                 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
9580                 return bfd_reloc_ok;
9581               }
9582           }
9583         /* These relocations needs special care, as besides the fact
9584            they point somewhere in .gotplt, the addend must be
9585            adjusted accordingly depending on the type of instruction
9586            we refer to */
9587         else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
9588           {
9589             unsigned long data, insn;
9590             unsigned thumb;
9591
9592             data = bfd_get_32 (input_bfd, hit_data);
9593             thumb = data & 1;
9594             data &= ~1u;
9595
9596             if (thumb)
9597               {
9598                 insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
9599                 if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
9600                   insn = (insn << 16)
9601                     | bfd_get_16 (input_bfd,
9602                                   contents + rel->r_offset - data + 2);
9603                 if ((insn & 0xf800c000) == 0xf000c000)
9604                   /* bl/blx */
9605                   value = -6;
9606                 else if ((insn & 0xffffff00) == 0x4400)
9607                   /* add */
9608                   value = -5;
9609                 else
9610                   {
9611                     (*_bfd_error_handler)
9612                       (_("%B(%A+0x%lx):unexpected Thumb instruction '0x%x' referenced by TLS_GOTDESC"),
9613                        input_bfd, input_section,
9614                        (unsigned long)rel->r_offset, insn);
9615                     return bfd_reloc_notsupported;
9616                   }
9617               }
9618             else
9619               {
9620                 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
9621
9622                 switch (insn >> 24)
9623                   {
9624                   case 0xeb:  /* bl */
9625                   case 0xfa:  /* blx */
9626                     value = -4;
9627                     break;
9628
9629                   case 0xe0:    /* add */
9630                     value = -8;
9631                     break;
9632
9633                   default:
9634                     (*_bfd_error_handler)
9635                       (_("%B(%A+0x%lx):unexpected ARM instruction '0x%x' referenced by TLS_GOTDESC"),
9636                        input_bfd, input_section,
9637                        (unsigned long)rel->r_offset, insn);
9638                     return bfd_reloc_notsupported;
9639                   }
9640               }
9641
9642             value += ((globals->root.sgotplt->output_section->vma
9643                        + globals->root.sgotplt->output_offset + off)
9644                       - (input_section->output_section->vma
9645                          + input_section->output_offset
9646                          + rel->r_offset)
9647                       + globals->sgotplt_jump_table_size);
9648           }
9649         else
9650           value = ((globals->root.sgot->output_section->vma
9651                     + globals->root.sgot->output_offset + off)
9652                    - (input_section->output_section->vma
9653                       + input_section->output_offset + rel->r_offset));
9654
9655         return _bfd_final_link_relocate (howto, input_bfd, input_section,
9656                                          contents, rel->r_offset, value,
9657                                          rel->r_addend);
9658       }
9659
9660     case R_ARM_TLS_LE32:
9661       if (info->shared && !info->pie)
9662         {
9663           (*_bfd_error_handler)
9664             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
9665              input_bfd, input_section,
9666              (long) rel->r_offset, howto->name);
9667           return (bfd_reloc_status_type) FALSE;
9668         }
9669       else
9670         value = tpoff (info, value);
9671
9672       return _bfd_final_link_relocate (howto, input_bfd, input_section,
9673                                        contents, rel->r_offset, value,
9674                                        rel->r_addend);
9675
9676     case R_ARM_V4BX:
9677       if (globals->fix_v4bx)
9678         {
9679           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
9680
9681           /* Ensure that we have a BX instruction.  */
9682           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
9683
9684           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
9685             {
9686               /* Branch to veneer.  */
9687               bfd_vma glue_addr;
9688               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
9689               glue_addr -= input_section->output_section->vma
9690                            + input_section->output_offset
9691                            + rel->r_offset + 8;
9692               insn = (insn & 0xf0000000) | 0x0a000000
9693                      | ((glue_addr >> 2) & 0x00ffffff);
9694             }
9695           else
9696             {
9697               /* Preserve Rm (lowest four bits) and the condition code
9698                  (highest four bits). Other bits encode MOV PC,Rm.  */
9699               insn = (insn & 0xf000000f) | 0x01a0f000;
9700             }
9701
9702           bfd_put_32 (input_bfd, insn, hit_data);
9703         }
9704       return bfd_reloc_ok;
9705
9706     case R_ARM_MOVW_ABS_NC:
9707     case R_ARM_MOVT_ABS:
9708     case R_ARM_MOVW_PREL_NC:
9709     case R_ARM_MOVT_PREL:
9710     /* Until we properly support segment-base-relative addressing then
9711        we assume the segment base to be zero, as for the group relocations.
9712        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
9713        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
9714     case R_ARM_MOVW_BREL_NC:
9715     case R_ARM_MOVW_BREL:
9716     case R_ARM_MOVT_BREL:
9717       {
9718         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
9719
9720         if (globals->use_rel)
9721           {
9722             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
9723             signed_addend = (addend ^ 0x8000) - 0x8000;
9724           }
9725
9726         value += signed_addend;
9727
9728         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
9729           value -= (input_section->output_section->vma
9730                     + input_section->output_offset + rel->r_offset);
9731
9732         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
9733           return bfd_reloc_overflow;
9734
9735         if (branch_type == ST_BRANCH_TO_THUMB)
9736           value |= 1;
9737
9738         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
9739             || r_type == R_ARM_MOVT_BREL)
9740           value >>= 16;
9741
9742         insn &= 0xfff0f000;
9743         insn |= value & 0xfff;
9744         insn |= (value & 0xf000) << 4;
9745         bfd_put_32 (input_bfd, insn, hit_data);
9746       }
9747       return bfd_reloc_ok;
9748
9749     case R_ARM_THM_MOVW_ABS_NC:
9750     case R_ARM_THM_MOVT_ABS:
9751     case R_ARM_THM_MOVW_PREL_NC:
9752     case R_ARM_THM_MOVT_PREL:
9753     /* Until we properly support segment-base-relative addressing then
9754        we assume the segment base to be zero, as for the above relocations.
9755        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
9756        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
9757        as R_ARM_THM_MOVT_ABS.  */
9758     case R_ARM_THM_MOVW_BREL_NC:
9759     case R_ARM_THM_MOVW_BREL:
9760     case R_ARM_THM_MOVT_BREL:
9761       {
9762         bfd_vma insn;
9763
9764         insn = bfd_get_16 (input_bfd, hit_data) << 16;
9765         insn |= bfd_get_16 (input_bfd, hit_data + 2);
9766
9767         if (globals->use_rel)
9768           {
9769             addend = ((insn >> 4)  & 0xf000)
9770                    | ((insn >> 15) & 0x0800)
9771                    | ((insn >> 4)  & 0x0700)
9772                    | (insn         & 0x00ff);
9773             signed_addend = (addend ^ 0x8000) - 0x8000;
9774           }
9775
9776         value += signed_addend;
9777
9778         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
9779           value -= (input_section->output_section->vma
9780                     + input_section->output_offset + rel->r_offset);
9781
9782         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
9783           return bfd_reloc_overflow;
9784
9785         if (branch_type == ST_BRANCH_TO_THUMB)
9786           value |= 1;
9787
9788         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
9789             || r_type == R_ARM_THM_MOVT_BREL)
9790           value >>= 16;
9791
9792         insn &= 0xfbf08f00;
9793         insn |= (value & 0xf000) << 4;
9794         insn |= (value & 0x0800) << 15;
9795         insn |= (value & 0x0700) << 4;
9796         insn |= (value & 0x00ff);
9797
9798         bfd_put_16 (input_bfd, insn >> 16, hit_data);
9799         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
9800       }
9801       return bfd_reloc_ok;
9802
9803     case R_ARM_ALU_PC_G0_NC:
9804     case R_ARM_ALU_PC_G1_NC:
9805     case R_ARM_ALU_PC_G0:
9806     case R_ARM_ALU_PC_G1:
9807     case R_ARM_ALU_PC_G2:
9808     case R_ARM_ALU_SB_G0_NC:
9809     case R_ARM_ALU_SB_G1_NC:
9810     case R_ARM_ALU_SB_G0:
9811     case R_ARM_ALU_SB_G1:
9812     case R_ARM_ALU_SB_G2:
9813       {
9814         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
9815         bfd_vma pc = input_section->output_section->vma
9816                      + input_section->output_offset + rel->r_offset;
9817         /* sb should be the origin of the *segment* containing the symbol.
9818            It is not clear how to obtain this OS-dependent value, so we
9819            make an arbitrary choice of zero.  */
9820         bfd_vma sb = 0;
9821         bfd_vma residual;
9822         bfd_vma g_n;
9823         bfd_signed_vma signed_value;
9824         int group = 0;
9825
9826         /* Determine which group of bits to select.  */
9827         switch (r_type)
9828           {
9829           case R_ARM_ALU_PC_G0_NC:
9830           case R_ARM_ALU_PC_G0:
9831           case R_ARM_ALU_SB_G0_NC:
9832           case R_ARM_ALU_SB_G0:
9833             group = 0;
9834             break;
9835
9836           case R_ARM_ALU_PC_G1_NC:
9837           case R_ARM_ALU_PC_G1:
9838           case R_ARM_ALU_SB_G1_NC:
9839           case R_ARM_ALU_SB_G1:
9840             group = 1;
9841             break;
9842
9843           case R_ARM_ALU_PC_G2:
9844           case R_ARM_ALU_SB_G2:
9845             group = 2;
9846             break;
9847
9848           default:
9849             abort ();
9850           }
9851
9852         /* If REL, extract the addend from the insn.  If RELA, it will
9853            have already been fetched for us.  */
9854         if (globals->use_rel)
9855           {
9856             int negative;
9857             bfd_vma constant = insn & 0xff;
9858             bfd_vma rotation = (insn & 0xf00) >> 8;
9859
9860             if (rotation == 0)
9861               signed_addend = constant;
9862             else
9863               {
9864                 /* Compensate for the fact that in the instruction, the
9865                    rotation is stored in multiples of 2 bits.  */
9866                 rotation *= 2;
9867
9868                 /* Rotate "constant" right by "rotation" bits.  */
9869                 signed_addend = (constant >> rotation) |
9870                                 (constant << (8 * sizeof (bfd_vma) - rotation));
9871               }
9872
9873             /* Determine if the instruction is an ADD or a SUB.
9874                (For REL, this determines the sign of the addend.)  */
9875             negative = identify_add_or_sub (insn);
9876             if (negative == 0)
9877               {
9878                 (*_bfd_error_handler)
9879                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
9880                   input_bfd, input_section,
9881                   (long) rel->r_offset, howto->name);
9882                 return bfd_reloc_overflow;
9883               }
9884
9885             signed_addend *= negative;
9886           }
9887
9888         /* Compute the value (X) to go in the place.  */
9889         if (r_type == R_ARM_ALU_PC_G0_NC
9890             || r_type == R_ARM_ALU_PC_G1_NC
9891             || r_type == R_ARM_ALU_PC_G0
9892             || r_type == R_ARM_ALU_PC_G1
9893             || r_type == R_ARM_ALU_PC_G2)
9894           /* PC relative.  */
9895           signed_value = value - pc + signed_addend;
9896         else
9897           /* Section base relative.  */
9898           signed_value = value - sb + signed_addend;
9899
9900         /* If the target symbol is a Thumb function, then set the
9901            Thumb bit in the address.  */
9902         if (branch_type == ST_BRANCH_TO_THUMB)
9903           signed_value |= 1;
9904
9905         /* Calculate the value of the relevant G_n, in encoded
9906            constant-with-rotation format.  */
9907         g_n = calculate_group_reloc_mask (abs (signed_value), group,
9908                                           &residual);
9909
9910         /* Check for overflow if required.  */
9911         if ((r_type == R_ARM_ALU_PC_G0
9912              || r_type == R_ARM_ALU_PC_G1
9913              || r_type == R_ARM_ALU_PC_G2
9914              || r_type == R_ARM_ALU_SB_G0
9915              || r_type == R_ARM_ALU_SB_G1
9916              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
9917           {
9918             (*_bfd_error_handler)
9919               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
9920               input_bfd, input_section,
9921               (long) rel->r_offset, abs (signed_value), howto->name);
9922             return bfd_reloc_overflow;
9923           }
9924
9925         /* Mask out the value and the ADD/SUB part of the opcode; take care
9926            not to destroy the S bit.  */
9927         insn &= 0xff1ff000;
9928
9929         /* Set the opcode according to whether the value to go in the
9930            place is negative.  */
9931         if (signed_value < 0)
9932           insn |= 1 << 22;
9933         else
9934           insn |= 1 << 23;
9935
9936         /* Encode the offset.  */
9937         insn |= g_n;
9938
9939         bfd_put_32 (input_bfd, insn, hit_data);
9940       }
9941       return bfd_reloc_ok;
9942
9943     case R_ARM_LDR_PC_G0:
9944     case R_ARM_LDR_PC_G1:
9945     case R_ARM_LDR_PC_G2:
9946     case R_ARM_LDR_SB_G0:
9947     case R_ARM_LDR_SB_G1:
9948     case R_ARM_LDR_SB_G2:
9949       {
9950         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
9951         bfd_vma pc = input_section->output_section->vma
9952                      + input_section->output_offset + rel->r_offset;
9953         bfd_vma sb = 0; /* See note above.  */
9954         bfd_vma residual;
9955         bfd_signed_vma signed_value;
9956         int group = 0;
9957
9958         /* Determine which groups of bits to calculate.  */
9959         switch (r_type)
9960           {
9961           case R_ARM_LDR_PC_G0:
9962           case R_ARM_LDR_SB_G0:
9963             group = 0;
9964             break;
9965
9966           case R_ARM_LDR_PC_G1:
9967           case R_ARM_LDR_SB_G1:
9968             group = 1;
9969             break;
9970
9971           case R_ARM_LDR_PC_G2:
9972           case R_ARM_LDR_SB_G2:
9973             group = 2;
9974             break;
9975
9976           default:
9977             abort ();
9978           }
9979
9980         /* If REL, extract the addend from the insn.  If RELA, it will
9981            have already been fetched for us.  */
9982         if (globals->use_rel)
9983           {
9984             int negative = (insn & (1 << 23)) ? 1 : -1;
9985             signed_addend = negative * (insn & 0xfff);
9986           }
9987
9988         /* Compute the value (X) to go in the place.  */
9989         if (r_type == R_ARM_LDR_PC_G0
9990             || r_type == R_ARM_LDR_PC_G1
9991             || r_type == R_ARM_LDR_PC_G2)
9992           /* PC relative.  */
9993           signed_value = value - pc + signed_addend;
9994         else
9995           /* Section base relative.  */
9996           signed_value = value - sb + signed_addend;
9997
9998         /* Calculate the value of the relevant G_{n-1} to obtain
9999            the residual at that stage.  */
10000         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
10001
10002         /* Check for overflow.  */
10003         if (residual >= 0x1000)
10004           {
10005             (*_bfd_error_handler)
10006               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10007               input_bfd, input_section,
10008               (long) rel->r_offset, abs (signed_value), howto->name);
10009             return bfd_reloc_overflow;
10010           }
10011
10012         /* Mask out the value and U bit.  */
10013         insn &= 0xff7ff000;
10014
10015         /* Set the U bit if the value to go in the place is non-negative.  */
10016         if (signed_value >= 0)
10017           insn |= 1 << 23;
10018
10019         /* Encode the offset.  */
10020         insn |= residual;
10021
10022         bfd_put_32 (input_bfd, insn, hit_data);
10023       }
10024       return bfd_reloc_ok;
10025
10026     case R_ARM_LDRS_PC_G0:
10027     case R_ARM_LDRS_PC_G1:
10028     case R_ARM_LDRS_PC_G2:
10029     case R_ARM_LDRS_SB_G0:
10030     case R_ARM_LDRS_SB_G1:
10031     case R_ARM_LDRS_SB_G2:
10032       {
10033         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10034         bfd_vma pc = input_section->output_section->vma
10035                      + input_section->output_offset + rel->r_offset;
10036         bfd_vma sb = 0; /* See note above.  */
10037         bfd_vma residual;
10038         bfd_signed_vma signed_value;
10039         int group = 0;
10040
10041         /* Determine which groups of bits to calculate.  */
10042         switch (r_type)
10043           {
10044           case R_ARM_LDRS_PC_G0:
10045           case R_ARM_LDRS_SB_G0:
10046             group = 0;
10047             break;
10048
10049           case R_ARM_LDRS_PC_G1:
10050           case R_ARM_LDRS_SB_G1:
10051             group = 1;
10052             break;
10053
10054           case R_ARM_LDRS_PC_G2:
10055           case R_ARM_LDRS_SB_G2:
10056             group = 2;
10057             break;
10058
10059           default:
10060             abort ();
10061           }
10062
10063         /* If REL, extract the addend from the insn.  If RELA, it will
10064            have already been fetched for us.  */
10065         if (globals->use_rel)
10066           {
10067             int negative = (insn & (1 << 23)) ? 1 : -1;
10068             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
10069           }
10070
10071         /* Compute the value (X) to go in the place.  */
10072         if (r_type == R_ARM_LDRS_PC_G0
10073             || r_type == R_ARM_LDRS_PC_G1
10074             || r_type == R_ARM_LDRS_PC_G2)
10075           /* PC relative.  */
10076           signed_value = value - pc + signed_addend;
10077         else
10078           /* Section base relative.  */
10079           signed_value = value - sb + signed_addend;
10080
10081         /* Calculate the value of the relevant G_{n-1} to obtain
10082            the residual at that stage.  */
10083         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
10084
10085         /* Check for overflow.  */
10086         if (residual >= 0x100)
10087           {
10088             (*_bfd_error_handler)
10089               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10090               input_bfd, input_section,
10091               (long) rel->r_offset, abs (signed_value), howto->name);
10092             return bfd_reloc_overflow;
10093           }
10094
10095         /* Mask out the value and U bit.  */
10096         insn &= 0xff7ff0f0;
10097
10098         /* Set the U bit if the value to go in the place is non-negative.  */
10099         if (signed_value >= 0)
10100           insn |= 1 << 23;
10101
10102         /* Encode the offset.  */
10103         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
10104
10105         bfd_put_32 (input_bfd, insn, hit_data);
10106       }
10107       return bfd_reloc_ok;
10108
10109     case R_ARM_LDC_PC_G0:
10110     case R_ARM_LDC_PC_G1:
10111     case R_ARM_LDC_PC_G2:
10112     case R_ARM_LDC_SB_G0:
10113     case R_ARM_LDC_SB_G1:
10114     case R_ARM_LDC_SB_G2:
10115       {
10116         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
10117         bfd_vma pc = input_section->output_section->vma
10118                      + input_section->output_offset + rel->r_offset;
10119         bfd_vma sb = 0; /* See note above.  */
10120         bfd_vma residual;
10121         bfd_signed_vma signed_value;
10122         int group = 0;
10123
10124         /* Determine which groups of bits to calculate.  */
10125         switch (r_type)
10126           {
10127           case R_ARM_LDC_PC_G0:
10128           case R_ARM_LDC_SB_G0:
10129             group = 0;
10130             break;
10131
10132           case R_ARM_LDC_PC_G1:
10133           case R_ARM_LDC_SB_G1:
10134             group = 1;
10135             break;
10136
10137           case R_ARM_LDC_PC_G2:
10138           case R_ARM_LDC_SB_G2:
10139             group = 2;
10140             break;
10141
10142           default:
10143             abort ();
10144           }
10145
10146         /* If REL, extract the addend from the insn.  If RELA, it will
10147            have already been fetched for us.  */
10148         if (globals->use_rel)
10149           {
10150             int negative = (insn & (1 << 23)) ? 1 : -1;
10151             signed_addend = negative * ((insn & 0xff) << 2);
10152           }
10153
10154         /* Compute the value (X) to go in the place.  */
10155         if (r_type == R_ARM_LDC_PC_G0
10156             || r_type == R_ARM_LDC_PC_G1
10157             || r_type == R_ARM_LDC_PC_G2)
10158           /* PC relative.  */
10159           signed_value = value - pc + signed_addend;
10160         else
10161           /* Section base relative.  */
10162           signed_value = value - sb + signed_addend;
10163
10164         /* Calculate the value of the relevant G_{n-1} to obtain
10165            the residual at that stage.  */
10166         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
10167
10168         /* Check for overflow.  (The absolute value to go in the place must be
10169            divisible by four and, after having been divided by four, must
10170            fit in eight bits.)  */
10171         if ((residual & 0x3) != 0 || residual >= 0x400)
10172           {
10173             (*_bfd_error_handler)
10174               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
10175               input_bfd, input_section,
10176               (long) rel->r_offset, abs (signed_value), howto->name);
10177             return bfd_reloc_overflow;
10178           }
10179
10180         /* Mask out the value and U bit.  */
10181         insn &= 0xff7fff00;
10182
10183         /* Set the U bit if the value to go in the place is non-negative.  */
10184         if (signed_value >= 0)
10185           insn |= 1 << 23;
10186
10187         /* Encode the offset.  */
10188         insn |= residual >> 2;
10189
10190         bfd_put_32 (input_bfd, insn, hit_data);
10191       }
10192       return bfd_reloc_ok;
10193
10194     default:
10195       return bfd_reloc_notsupported;
10196     }
10197 }
10198
10199 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
10200 static void
10201 arm_add_to_rel (bfd *              abfd,
10202                 bfd_byte *         address,
10203                 reloc_howto_type * howto,
10204                 bfd_signed_vma     increment)
10205 {
10206   bfd_signed_vma addend;
10207
10208   if (howto->type == R_ARM_THM_CALL
10209       || howto->type == R_ARM_THM_JUMP24)
10210     {
10211       int upper_insn, lower_insn;
10212       int upper, lower;
10213
10214       upper_insn = bfd_get_16 (abfd, address);
10215       lower_insn = bfd_get_16 (abfd, address + 2);
10216       upper = upper_insn & 0x7ff;
10217       lower = lower_insn & 0x7ff;
10218
10219       addend = (upper << 12) | (lower << 1);
10220       addend += increment;
10221       addend >>= 1;
10222
10223       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
10224       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
10225
10226       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
10227       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
10228     }
10229   else
10230     {
10231       bfd_vma        contents;
10232
10233       contents = bfd_get_32 (abfd, address);
10234
10235       /* Get the (signed) value from the instruction.  */
10236       addend = contents & howto->src_mask;
10237       if (addend & ((howto->src_mask + 1) >> 1))
10238         {
10239           bfd_signed_vma mask;
10240
10241           mask = -1;
10242           mask &= ~ howto->src_mask;
10243           addend |= mask;
10244         }
10245
10246       /* Add in the increment, (which is a byte value).  */
10247       switch (howto->type)
10248         {
10249         default:
10250           addend += increment;
10251           break;
10252
10253         case R_ARM_PC24:
10254         case R_ARM_PLT32:
10255         case R_ARM_CALL:
10256         case R_ARM_JUMP24:
10257           addend <<= howto->size;
10258           addend += increment;
10259
10260           /* Should we check for overflow here ?  */
10261
10262           /* Drop any undesired bits.  */
10263           addend >>= howto->rightshift;
10264           break;
10265         }
10266
10267       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
10268
10269       bfd_put_32 (abfd, contents, address);
10270     }
10271 }
10272
10273 #define IS_ARM_TLS_RELOC(R_TYPE)        \
10274   ((R_TYPE) == R_ARM_TLS_GD32           \
10275    || (R_TYPE) == R_ARM_TLS_LDO32       \
10276    || (R_TYPE) == R_ARM_TLS_LDM32       \
10277    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
10278    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
10279    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
10280    || (R_TYPE) == R_ARM_TLS_LE32        \
10281    || (R_TYPE) == R_ARM_TLS_IE32        \
10282    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
10283
10284 /* Specific set of relocations for the gnu tls dialect.  */
10285 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)    \
10286   ((R_TYPE) == R_ARM_TLS_GOTDESC        \
10287    || (R_TYPE) == R_ARM_TLS_CALL        \
10288    || (R_TYPE) == R_ARM_THM_TLS_CALL    \
10289    || (R_TYPE) == R_ARM_TLS_DESCSEQ     \
10290    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
10291
10292 /* Relocate an ARM ELF section.  */
10293
10294 static bfd_boolean
10295 elf32_arm_relocate_section (bfd *                  output_bfd,
10296                             struct bfd_link_info * info,
10297                             bfd *                  input_bfd,
10298                             asection *             input_section,
10299                             bfd_byte *             contents,
10300                             Elf_Internal_Rela *    relocs,
10301                             Elf_Internal_Sym *     local_syms,
10302                             asection **            local_sections)
10303 {
10304   Elf_Internal_Shdr *symtab_hdr;
10305   struct elf_link_hash_entry **sym_hashes;
10306   Elf_Internal_Rela *rel;
10307   Elf_Internal_Rela *relend;
10308   const char *name;
10309   struct elf32_arm_link_hash_table * globals;
10310
10311   globals = elf32_arm_hash_table (info);
10312   if (globals == NULL)
10313     return FALSE;
10314
10315   symtab_hdr = & elf_symtab_hdr (input_bfd);
10316   sym_hashes = elf_sym_hashes (input_bfd);
10317
10318   rel = relocs;
10319   relend = relocs + input_section->reloc_count;
10320   for (; rel < relend; rel++)
10321     {
10322       int                          r_type;
10323       reloc_howto_type *           howto;
10324       unsigned long                r_symndx;
10325       Elf_Internal_Sym *           sym;
10326       asection *                   sec;
10327       struct elf_link_hash_entry * h;
10328       bfd_vma                      relocation;
10329       bfd_reloc_status_type        r;
10330       arelent                      bfd_reloc;
10331       char                         sym_type;
10332       bfd_boolean                  unresolved_reloc = FALSE;
10333       char *error_message = NULL;
10334
10335       r_symndx = ELF32_R_SYM (rel->r_info);
10336       r_type   = ELF32_R_TYPE (rel->r_info);
10337       r_type   = arm_real_reloc_type (globals, r_type);
10338
10339       if (   r_type == R_ARM_GNU_VTENTRY
10340           || r_type == R_ARM_GNU_VTINHERIT)
10341         continue;
10342
10343       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
10344       howto = bfd_reloc.howto;
10345
10346       h = NULL;
10347       sym = NULL;
10348       sec = NULL;
10349
10350       if (r_symndx < symtab_hdr->sh_info)
10351         {
10352           sym = local_syms + r_symndx;
10353           sym_type = ELF32_ST_TYPE (sym->st_info);
10354           sec = local_sections[r_symndx];
10355
10356           /* An object file might have a reference to a local
10357              undefined symbol.  This is a daft object file, but we
10358              should at least do something about it.  V4BX & NONE
10359              relocations do not use the symbol and are explicitly
10360              allowed to use the undefined symbol, so allow those.
10361              Likewise for relocations against STN_UNDEF.  */
10362           if (r_type != R_ARM_V4BX
10363               && r_type != R_ARM_NONE
10364               && r_symndx != STN_UNDEF
10365               && bfd_is_und_section (sec)
10366               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
10367             {
10368               if (!info->callbacks->undefined_symbol
10369                   (info, bfd_elf_string_from_elf_section
10370                    (input_bfd, symtab_hdr->sh_link, sym->st_name),
10371                    input_bfd, input_section,
10372                    rel->r_offset, TRUE))
10373                 return FALSE;
10374             }
10375
10376           if (globals->use_rel)
10377             {
10378               relocation = (sec->output_section->vma
10379                             + sec->output_offset
10380                             + sym->st_value);
10381               if (!info->relocatable
10382                   && (sec->flags & SEC_MERGE)
10383                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10384                 {
10385                   asection *msec;
10386                   bfd_vma addend, value;
10387
10388                   switch (r_type)
10389                     {
10390                     case R_ARM_MOVW_ABS_NC:
10391                     case R_ARM_MOVT_ABS:
10392                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
10393                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
10394                       addend = (addend ^ 0x8000) - 0x8000;
10395                       break;
10396
10397                     case R_ARM_THM_MOVW_ABS_NC:
10398                     case R_ARM_THM_MOVT_ABS:
10399                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
10400                               << 16;
10401                       value |= bfd_get_16 (input_bfd,
10402                                            contents + rel->r_offset + 2);
10403                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
10404                                | ((value & 0x04000000) >> 15);
10405                       addend = (addend ^ 0x8000) - 0x8000;
10406                       break;
10407
10408                     default:
10409                       if (howto->rightshift
10410                           || (howto->src_mask & (howto->src_mask + 1)))
10411                         {
10412                           (*_bfd_error_handler)
10413                             (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
10414                              input_bfd, input_section,
10415                              (long) rel->r_offset, howto->name);
10416                           return FALSE;
10417                         }
10418
10419                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
10420
10421                       /* Get the (signed) value from the instruction.  */
10422                       addend = value & howto->src_mask;
10423                       if (addend & ((howto->src_mask + 1) >> 1))
10424                         {
10425                           bfd_signed_vma mask;
10426
10427                           mask = -1;
10428                           mask &= ~ howto->src_mask;
10429                           addend |= mask;
10430                         }
10431                       break;
10432                     }
10433
10434                   msec = sec;
10435                   addend =
10436                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
10437                     - relocation;
10438                   addend += msec->output_section->vma + msec->output_offset;
10439
10440                   /* Cases here must match those in the preceding
10441                      switch statement.  */
10442                   switch (r_type)
10443                     {
10444                     case R_ARM_MOVW_ABS_NC:
10445                     case R_ARM_MOVT_ABS:
10446                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
10447                               | (addend & 0xfff);
10448                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
10449                       break;
10450
10451                     case R_ARM_THM_MOVW_ABS_NC:
10452                     case R_ARM_THM_MOVT_ABS:
10453                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
10454                               | (addend & 0xff) | ((addend & 0x0800) << 15);
10455                       bfd_put_16 (input_bfd, value >> 16,
10456                                   contents + rel->r_offset);
10457                       bfd_put_16 (input_bfd, value,
10458                                   contents + rel->r_offset + 2);
10459                       break;
10460
10461                     default:
10462                       value = (value & ~ howto->dst_mask)
10463                               | (addend & howto->dst_mask);
10464                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
10465                       break;
10466                     }
10467                 }
10468             }
10469           else
10470             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10471         }
10472       else
10473         {
10474           bfd_boolean warned;
10475
10476           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
10477                                    r_symndx, symtab_hdr, sym_hashes,
10478                                    h, sec, relocation,
10479                                    unresolved_reloc, warned);
10480
10481           sym_type = h->type;
10482         }
10483
10484       if (sec != NULL && elf_discarded_section (sec))
10485         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10486                                          rel, relend, howto, contents);
10487
10488       if (info->relocatable)
10489         {
10490           /* This is a relocatable link.  We don't have to change
10491              anything, unless the reloc is against a section symbol,
10492              in which case we have to adjust according to where the
10493              section symbol winds up in the output section.  */
10494           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10495             {
10496               if (globals->use_rel)
10497                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
10498                                 howto, (bfd_signed_vma) sec->output_offset);
10499               else
10500                 rel->r_addend += sec->output_offset;
10501             }
10502           continue;
10503         }
10504
10505       if (h != NULL)
10506         name = h->root.root.string;
10507       else
10508         {
10509           name = (bfd_elf_string_from_elf_section
10510                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
10511           if (name == NULL || *name == '\0')
10512             name = bfd_section_name (input_bfd, sec);
10513         }
10514
10515       if (r_symndx != STN_UNDEF
10516           && r_type != R_ARM_NONE
10517           && (h == NULL
10518               || h->root.type == bfd_link_hash_defined
10519               || h->root.type == bfd_link_hash_defweak)
10520           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
10521         {
10522           (*_bfd_error_handler)
10523             ((sym_type == STT_TLS
10524               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
10525               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
10526              input_bfd,
10527              input_section,
10528              (long) rel->r_offset,
10529              howto->name,
10530              name);
10531         }
10532
10533       /* We call elf32_arm_final_link_relocate unless we're completely
10534          done, i.e., the relaxation produced the final output we want,
10535          and we won't let anybody mess with it. Also, we have to do
10536          addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
10537          both in relaxed and non-relaxed cases */
10538      if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
10539          || (IS_ARM_TLS_GNU_RELOC (r_type)
10540              && !((h ? elf32_arm_hash_entry (h)->tls_type :
10541                    elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
10542                   & GOT_TLS_GDESC)))
10543        {
10544          r = elf32_arm_tls_relax (globals, input_bfd, input_section,
10545                                   contents, rel, h == NULL);
10546          /* This may have been marked unresolved because it came from
10547             a shared library.  But we've just dealt with that.  */
10548          unresolved_reloc = 0;
10549        }
10550      else
10551        r = bfd_reloc_continue;
10552
10553      if (r == bfd_reloc_continue)
10554        r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
10555                                           input_section, contents, rel,
10556                                           relocation, info, sec, name, sym_type,
10557                                           (h ? h->target_internal
10558                                            : ARM_SYM_BRANCH_TYPE (sym)), h,
10559                                           &unresolved_reloc, &error_message);
10560
10561       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
10562          because such sections are not SEC_ALLOC and thus ld.so will
10563          not process them.  */
10564       if (unresolved_reloc
10565           && !((input_section->flags & SEC_DEBUGGING) != 0
10566                && h->def_dynamic)
10567           && _bfd_elf_section_offset (output_bfd, info, input_section,
10568                                       rel->r_offset) != (bfd_vma) -1)
10569         {
10570           (*_bfd_error_handler)
10571             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
10572              input_bfd,
10573              input_section,
10574              (long) rel->r_offset,
10575              howto->name,
10576              h->root.root.string);
10577           return FALSE;
10578         }
10579
10580       if (r != bfd_reloc_ok)
10581         {
10582           switch (r)
10583             {
10584             case bfd_reloc_overflow:
10585               /* If the overflowing reloc was to an undefined symbol,
10586                  we have already printed one error message and there
10587                  is no point complaining again.  */
10588               if ((! h ||
10589                    h->root.type != bfd_link_hash_undefined)
10590                   && (!((*info->callbacks->reloc_overflow)
10591                         (info, (h ? &h->root : NULL), name, howto->name,
10592                          (bfd_vma) 0, input_bfd, input_section,
10593                          rel->r_offset))))
10594                   return FALSE;
10595               break;
10596
10597             case bfd_reloc_undefined:
10598               if (!((*info->callbacks->undefined_symbol)
10599                     (info, name, input_bfd, input_section,
10600                      rel->r_offset, TRUE)))
10601                 return FALSE;
10602               break;
10603
10604             case bfd_reloc_outofrange:
10605               error_message = _("out of range");
10606               goto common_error;
10607
10608             case bfd_reloc_notsupported:
10609               error_message = _("unsupported relocation");
10610               goto common_error;
10611
10612             case bfd_reloc_dangerous:
10613               /* error_message should already be set.  */
10614               goto common_error;
10615
10616             default:
10617               error_message = _("unknown error");
10618               /* Fall through.  */
10619
10620             common_error:
10621               BFD_ASSERT (error_message != NULL);
10622               if (!((*info->callbacks->reloc_dangerous)
10623                     (info, error_message, input_bfd, input_section,
10624                      rel->r_offset)))
10625                 return FALSE;
10626               break;
10627             }
10628         }
10629     }
10630
10631   return TRUE;
10632 }
10633
10634 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
10635    adds the edit to the start of the list.  (The list must be built in order of
10636    ascending TINDEX: the function's callers are primarily responsible for
10637    maintaining that condition).  */
10638
10639 static void
10640 add_unwind_table_edit (arm_unwind_table_edit **head,
10641                        arm_unwind_table_edit **tail,
10642                        arm_unwind_edit_type type,
10643                        asection *linked_section,
10644                        unsigned int tindex)
10645 {
10646   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
10647       xmalloc (sizeof (arm_unwind_table_edit));
10648
10649   new_edit->type = type;
10650   new_edit->linked_section = linked_section;
10651   new_edit->index = tindex;
10652
10653   if (tindex > 0)
10654     {
10655       new_edit->next = NULL;
10656
10657       if (*tail)
10658         (*tail)->next = new_edit;
10659
10660       (*tail) = new_edit;
10661
10662       if (!*head)
10663         (*head) = new_edit;
10664     }
10665   else
10666     {
10667       new_edit->next = *head;
10668
10669       if (!*tail)
10670         *tail = new_edit;
10671
10672       *head = new_edit;
10673     }
10674 }
10675
10676 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
10677
10678 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
10679 static void
10680 adjust_exidx_size(asection *exidx_sec, int adjust)
10681 {
10682   asection *out_sec;
10683
10684   if (!exidx_sec->rawsize)
10685     exidx_sec->rawsize = exidx_sec->size;
10686
10687   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
10688   out_sec = exidx_sec->output_section;
10689   /* Adjust size of output section.  */
10690   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
10691 }
10692
10693 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
10694 static void
10695 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
10696 {
10697   struct _arm_elf_section_data *exidx_arm_data;
10698
10699   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
10700   add_unwind_table_edit (
10701     &exidx_arm_data->u.exidx.unwind_edit_list,
10702     &exidx_arm_data->u.exidx.unwind_edit_tail,
10703     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
10704
10705   adjust_exidx_size(exidx_sec, 8);
10706 }
10707
10708 /* Scan .ARM.exidx tables, and create a list describing edits which should be
10709    made to those tables, such that:
10710
10711      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
10712      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
10713         codes which have been inlined into the index).
10714
10715    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
10716
10717    The edits are applied when the tables are written
10718    (in elf32_arm_write_section).  */
10719
10720 bfd_boolean
10721 elf32_arm_fix_exidx_coverage (asection **text_section_order,
10722                               unsigned int num_text_sections,
10723                               struct bfd_link_info *info,
10724                               bfd_boolean merge_exidx_entries)
10725 {
10726   bfd *inp;
10727   unsigned int last_second_word = 0, i;
10728   asection *last_exidx_sec = NULL;
10729   asection *last_text_sec = NULL;
10730   int last_unwind_type = -1;
10731
10732   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
10733      text sections.  */
10734   for (inp = info->input_bfds; inp != NULL; inp = inp->link_next)
10735     {
10736       asection *sec;
10737
10738       for (sec = inp->sections; sec != NULL; sec = sec->next)
10739         {
10740           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
10741           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
10742
10743           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
10744             continue;
10745
10746           if (elf_sec->linked_to)
10747             {
10748               Elf_Internal_Shdr *linked_hdr
10749                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
10750               struct _arm_elf_section_data *linked_sec_arm_data
10751                 = get_arm_elf_section_data (linked_hdr->bfd_section);
10752
10753               if (linked_sec_arm_data == NULL)
10754                 continue;
10755
10756               /* Link this .ARM.exidx section back from the text section it
10757                  describes.  */
10758               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
10759             }
10760         }
10761     }
10762
10763   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
10764      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
10765      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
10766
10767   for (i = 0; i < num_text_sections; i++)
10768     {
10769       asection *sec = text_section_order[i];
10770       asection *exidx_sec;
10771       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
10772       struct _arm_elf_section_data *exidx_arm_data;
10773       bfd_byte *contents = NULL;
10774       int deleted_exidx_bytes = 0;
10775       bfd_vma j;
10776       arm_unwind_table_edit *unwind_edit_head = NULL;
10777       arm_unwind_table_edit *unwind_edit_tail = NULL;
10778       Elf_Internal_Shdr *hdr;
10779       bfd *ibfd;
10780
10781       if (arm_data == NULL)
10782         continue;
10783
10784       exidx_sec = arm_data->u.text.arm_exidx_sec;
10785       if (exidx_sec == NULL)
10786         {
10787           /* Section has no unwind data.  */
10788           if (last_unwind_type == 0 || !last_exidx_sec)
10789             continue;
10790
10791           /* Ignore zero sized sections.  */
10792           if (sec->size == 0)
10793             continue;
10794
10795           insert_cantunwind_after(last_text_sec, last_exidx_sec);
10796           last_unwind_type = 0;
10797           continue;
10798         }
10799
10800       /* Skip /DISCARD/ sections.  */
10801       if (bfd_is_abs_section (exidx_sec->output_section))
10802         continue;
10803
10804       hdr = &elf_section_data (exidx_sec)->this_hdr;
10805       if (hdr->sh_type != SHT_ARM_EXIDX)
10806         continue;
10807
10808       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
10809       if (exidx_arm_data == NULL)
10810         continue;
10811
10812       ibfd = exidx_sec->owner;
10813
10814       if (hdr->contents != NULL)
10815         contents = hdr->contents;
10816       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
10817         /* An error?  */
10818         continue;
10819
10820       for (j = 0; j < hdr->sh_size; j += 8)
10821         {
10822           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
10823           int unwind_type;
10824           int elide = 0;
10825
10826           /* An EXIDX_CANTUNWIND entry.  */
10827           if (second_word == 1)
10828             {
10829               if (last_unwind_type == 0)
10830                 elide = 1;
10831               unwind_type = 0;
10832             }
10833           /* Inlined unwinding data.  Merge if equal to previous.  */
10834           else if ((second_word & 0x80000000) != 0)
10835             {
10836               if (merge_exidx_entries
10837                    && last_second_word == second_word && last_unwind_type == 1)
10838                 elide = 1;
10839               unwind_type = 1;
10840               last_second_word = second_word;
10841             }
10842           /* Normal table entry.  In theory we could merge these too,
10843              but duplicate entries are likely to be much less common.  */
10844           else
10845             unwind_type = 2;
10846
10847           if (elide)
10848             {
10849               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
10850                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
10851
10852               deleted_exidx_bytes += 8;
10853             }
10854
10855           last_unwind_type = unwind_type;
10856         }
10857
10858       /* Free contents if we allocated it ourselves.  */
10859       if (contents != hdr->contents)
10860         free (contents);
10861
10862       /* Record edits to be applied later (in elf32_arm_write_section).  */
10863       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
10864       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
10865
10866       if (deleted_exidx_bytes > 0)
10867         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
10868
10869       last_exidx_sec = exidx_sec;
10870       last_text_sec = sec;
10871     }
10872
10873   /* Add terminating CANTUNWIND entry.  */
10874   if (last_exidx_sec && last_unwind_type != 0)
10875     insert_cantunwind_after(last_text_sec, last_exidx_sec);
10876
10877   return TRUE;
10878 }
10879
10880 static bfd_boolean
10881 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
10882                                bfd *ibfd, const char *name)
10883 {
10884   asection *sec, *osec;
10885
10886   sec = bfd_get_section_by_name (ibfd, name);
10887   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
10888     return TRUE;
10889
10890   osec = sec->output_section;
10891   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
10892     return TRUE;
10893
10894   if (! bfd_set_section_contents (obfd, osec, sec->contents,
10895                                   sec->output_offset, sec->size))
10896     return FALSE;
10897
10898   return TRUE;
10899 }
10900
10901 static bfd_boolean
10902 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
10903 {
10904   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
10905   asection *sec, *osec;
10906
10907   if (globals == NULL)
10908     return FALSE;
10909
10910   /* Invoke the regular ELF backend linker to do all the work.  */
10911   if (!bfd_elf_final_link (abfd, info))
10912     return FALSE;
10913
10914   /* Process stub sections (eg BE8 encoding, ...).  */
10915   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
10916   int i;
10917   for (i=0; i<htab->top_id; i++)
10918     {
10919       sec = htab->stub_group[i].stub_sec;
10920       /* Only process it once, in its link_sec slot.  */
10921       if (sec && i == htab->stub_group[i].link_sec->id)
10922         {
10923           osec = sec->output_section;
10924           elf32_arm_write_section (abfd, info, sec, sec->contents);
10925           if (! bfd_set_section_contents (abfd, osec, sec->contents,
10926                                           sec->output_offset, sec->size))
10927             return FALSE;
10928         }
10929     }
10930
10931   /* Write out any glue sections now that we have created all the
10932      stubs.  */
10933   if (globals->bfd_of_glue_owner != NULL)
10934     {
10935       if (! elf32_arm_output_glue_section (info, abfd,
10936                                            globals->bfd_of_glue_owner,
10937                                            ARM2THUMB_GLUE_SECTION_NAME))
10938         return FALSE;
10939
10940       if (! elf32_arm_output_glue_section (info, abfd,
10941                                            globals->bfd_of_glue_owner,
10942                                            THUMB2ARM_GLUE_SECTION_NAME))
10943         return FALSE;
10944
10945       if (! elf32_arm_output_glue_section (info, abfd,
10946                                            globals->bfd_of_glue_owner,
10947                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
10948         return FALSE;
10949
10950       if (! elf32_arm_output_glue_section (info, abfd,
10951                                            globals->bfd_of_glue_owner,
10952                                            ARM_BX_GLUE_SECTION_NAME))
10953         return FALSE;
10954     }
10955
10956   return TRUE;
10957 }
10958
10959 /* Set the right machine number.  */
10960
10961 static bfd_boolean
10962 elf32_arm_object_p (bfd *abfd)
10963 {
10964   unsigned int mach;
10965
10966   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
10967
10968   if (mach != bfd_mach_arm_unknown)
10969     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
10970
10971   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
10972     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
10973
10974   else
10975     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
10976
10977   return TRUE;
10978 }
10979
10980 /* Function to keep ARM specific flags in the ELF header.  */
10981
10982 static bfd_boolean
10983 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
10984 {
10985   if (elf_flags_init (abfd)
10986       && elf_elfheader (abfd)->e_flags != flags)
10987     {
10988       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
10989         {
10990           if (flags & EF_ARM_INTERWORK)
10991             (*_bfd_error_handler)
10992               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
10993                abfd);
10994           else
10995             _bfd_error_handler
10996               (_("Warning: Clearing the interworking flag of %B due to outside request"),
10997                abfd);
10998         }
10999     }
11000   else
11001     {
11002       elf_elfheader (abfd)->e_flags = flags;
11003       elf_flags_init (abfd) = TRUE;
11004     }
11005
11006   return TRUE;
11007 }
11008
11009 /* Copy backend specific data from one object module to another.  */
11010
11011 static bfd_boolean
11012 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
11013 {
11014   flagword in_flags;
11015   flagword out_flags;
11016
11017   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
11018     return TRUE;
11019
11020   in_flags  = elf_elfheader (ibfd)->e_flags;
11021   out_flags = elf_elfheader (obfd)->e_flags;
11022
11023   if (elf_flags_init (obfd)
11024       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
11025       && in_flags != out_flags)
11026     {
11027       /* Cannot mix APCS26 and APCS32 code.  */
11028       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
11029         return FALSE;
11030
11031       /* Cannot mix float APCS and non-float APCS code.  */
11032       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
11033         return FALSE;
11034
11035       /* If the src and dest have different interworking flags
11036          then turn off the interworking bit.  */
11037       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
11038         {
11039           if (out_flags & EF_ARM_INTERWORK)
11040             _bfd_error_handler
11041               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
11042                obfd, ibfd);
11043
11044           in_flags &= ~EF_ARM_INTERWORK;
11045         }
11046
11047       /* Likewise for PIC, though don't warn for this case.  */
11048       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
11049         in_flags &= ~EF_ARM_PIC;
11050     }
11051
11052   elf_elfheader (obfd)->e_flags = in_flags;
11053   elf_flags_init (obfd) = TRUE;
11054
11055   /* Also copy the EI_OSABI field.  */
11056   elf_elfheader (obfd)->e_ident[EI_OSABI] =
11057     elf_elfheader (ibfd)->e_ident[EI_OSABI];
11058
11059   /* Copy object attributes.  */
11060   _bfd_elf_copy_obj_attributes (ibfd, obfd);
11061
11062   return TRUE;
11063 }
11064
11065 /* Values for Tag_ABI_PCS_R9_use.  */
11066 enum
11067 {
11068   AEABI_R9_V6,
11069   AEABI_R9_SB,
11070   AEABI_R9_TLS,
11071   AEABI_R9_unused
11072 };
11073
11074 /* Values for Tag_ABI_PCS_RW_data.  */
11075 enum
11076 {
11077   AEABI_PCS_RW_data_absolute,
11078   AEABI_PCS_RW_data_PCrel,
11079   AEABI_PCS_RW_data_SBrel,
11080   AEABI_PCS_RW_data_unused
11081 };
11082
11083 /* Values for Tag_ABI_enum_size.  */
11084 enum
11085 {
11086   AEABI_enum_unused,
11087   AEABI_enum_short,
11088   AEABI_enum_wide,
11089   AEABI_enum_forced_wide
11090 };
11091
11092 /* Determine whether an object attribute tag takes an integer, a
11093    string or both.  */
11094
11095 static int
11096 elf32_arm_obj_attrs_arg_type (int tag)
11097 {
11098   if (tag == Tag_compatibility)
11099     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
11100   else if (tag == Tag_nodefaults)
11101     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
11102   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
11103     return ATTR_TYPE_FLAG_STR_VAL;
11104   else if (tag < 32)
11105     return ATTR_TYPE_FLAG_INT_VAL;
11106   else
11107     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
11108 }
11109
11110 /* The ABI defines that Tag_conformance should be emitted first, and that
11111    Tag_nodefaults should be second (if either is defined).  This sets those
11112    two positions, and bumps up the position of all the remaining tags to
11113    compensate.  */
11114 static int
11115 elf32_arm_obj_attrs_order (int num)
11116 {
11117   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
11118     return Tag_conformance;
11119   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
11120     return Tag_nodefaults;
11121   if ((num - 2) < Tag_nodefaults)
11122     return num - 2;
11123   if ((num - 1) < Tag_conformance)
11124     return num - 1;
11125   return num;
11126 }
11127
11128 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
11129 static bfd_boolean
11130 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
11131 {
11132   if ((tag & 127) < 64)
11133     {
11134       _bfd_error_handler
11135         (_("%B: Unknown mandatory EABI object attribute %d"),
11136          abfd, tag);
11137       bfd_set_error (bfd_error_bad_value);
11138       return FALSE;
11139     }
11140   else
11141     {
11142       _bfd_error_handler
11143         (_("Warning: %B: Unknown EABI object attribute %d"),
11144          abfd, tag);
11145       return TRUE;
11146     }
11147 }
11148
11149 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
11150    Returns -1 if no architecture could be read.  */
11151
11152 static int
11153 get_secondary_compatible_arch (bfd *abfd)
11154 {
11155   obj_attribute *attr =
11156     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
11157
11158   /* Note: the tag and its argument below are uleb128 values, though
11159      currently-defined values fit in one byte for each.  */
11160   if (attr->s
11161       && attr->s[0] == Tag_CPU_arch
11162       && (attr->s[1] & 128) != 128
11163       && attr->s[2] == 0)
11164    return attr->s[1];
11165
11166   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
11167   return -1;
11168 }
11169
11170 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
11171    The tag is removed if ARCH is -1.  */
11172
11173 static void
11174 set_secondary_compatible_arch (bfd *abfd, int arch)
11175 {
11176   obj_attribute *attr =
11177     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
11178
11179   if (arch == -1)
11180     {
11181       attr->s = NULL;
11182       return;
11183     }
11184
11185   /* Note: the tag and its argument below are uleb128 values, though
11186      currently-defined values fit in one byte for each.  */
11187   if (!attr->s)
11188     attr->s = (char *) bfd_alloc (abfd, 3);
11189   attr->s[0] = Tag_CPU_arch;
11190   attr->s[1] = arch;
11191   attr->s[2] = '\0';
11192 }
11193
11194 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
11195    into account.  */
11196
11197 static int
11198 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
11199                       int newtag, int secondary_compat)
11200 {
11201 #define T(X) TAG_CPU_ARCH_##X
11202   int tagl, tagh, result;
11203   const int v6t2[] =
11204     {
11205       T(V6T2),   /* PRE_V4.  */
11206       T(V6T2),   /* V4.  */
11207       T(V6T2),   /* V4T.  */
11208       T(V6T2),   /* V5T.  */
11209       T(V6T2),   /* V5TE.  */
11210       T(V6T2),   /* V5TEJ.  */
11211       T(V6T2),   /* V6.  */
11212       T(V7),     /* V6KZ.  */
11213       T(V6T2)    /* V6T2.  */
11214     };
11215   const int v6k[] =
11216     {
11217       T(V6K),    /* PRE_V4.  */
11218       T(V6K),    /* V4.  */
11219       T(V6K),    /* V4T.  */
11220       T(V6K),    /* V5T.  */
11221       T(V6K),    /* V5TE.  */
11222       T(V6K),    /* V5TEJ.  */
11223       T(V6K),    /* V6.  */
11224       T(V6KZ),   /* V6KZ.  */
11225       T(V7),     /* V6T2.  */
11226       T(V6K)     /* V6K.  */
11227     };
11228   const int v7[] =
11229     {
11230       T(V7),     /* PRE_V4.  */
11231       T(V7),     /* V4.  */
11232       T(V7),     /* V4T.  */
11233       T(V7),     /* V5T.  */
11234       T(V7),     /* V5TE.  */
11235       T(V7),     /* V5TEJ.  */
11236       T(V7),     /* V6.  */
11237       T(V7),     /* V6KZ.  */
11238       T(V7),     /* V6T2.  */
11239       T(V7),     /* V6K.  */
11240       T(V7)      /* V7.  */
11241     };
11242   const int v6_m[] =
11243     {
11244       -1,        /* PRE_V4.  */
11245       -1,        /* V4.  */
11246       T(V6K),    /* V4T.  */
11247       T(V6K),    /* V5T.  */
11248       T(V6K),    /* V5TE.  */
11249       T(V6K),    /* V5TEJ.  */
11250       T(V6K),    /* V6.  */
11251       T(V6KZ),   /* V6KZ.  */
11252       T(V7),     /* V6T2.  */
11253       T(V6K),    /* V6K.  */
11254       T(V7),     /* V7.  */
11255       T(V6_M)    /* V6_M.  */
11256     };
11257   const int v6s_m[] =
11258     {
11259       -1,        /* PRE_V4.  */
11260       -1,        /* V4.  */
11261       T(V6K),    /* V4T.  */
11262       T(V6K),    /* V5T.  */
11263       T(V6K),    /* V5TE.  */
11264       T(V6K),    /* V5TEJ.  */
11265       T(V6K),    /* V6.  */
11266       T(V6KZ),   /* V6KZ.  */
11267       T(V7),     /* V6T2.  */
11268       T(V6K),    /* V6K.  */
11269       T(V7),     /* V7.  */
11270       T(V6S_M),  /* V6_M.  */
11271       T(V6S_M)   /* V6S_M.  */
11272     };
11273   const int v7e_m[] =
11274     {
11275       -1,        /* PRE_V4.  */
11276       -1,        /* V4.  */
11277       T(V7E_M),  /* V4T.  */
11278       T(V7E_M),  /* V5T.  */
11279       T(V7E_M),  /* V5TE.  */
11280       T(V7E_M),  /* V5TEJ.  */
11281       T(V7E_M),  /* V6.  */
11282       T(V7E_M),  /* V6KZ.  */
11283       T(V7E_M),  /* V6T2.  */
11284       T(V7E_M),  /* V6K.  */
11285       T(V7E_M),  /* V7.  */
11286       T(V7E_M),  /* V6_M.  */
11287       T(V7E_M),  /* V6S_M.  */
11288       T(V7E_M)   /* V7E_M.  */
11289     };
11290   const int v4t_plus_v6_m[] =
11291     {
11292       -1,               /* PRE_V4.  */
11293       -1,               /* V4.  */
11294       T(V4T),           /* V4T.  */
11295       T(V5T),           /* V5T.  */
11296       T(V5TE),          /* V5TE.  */
11297       T(V5TEJ),         /* V5TEJ.  */
11298       T(V6),            /* V6.  */
11299       T(V6KZ),          /* V6KZ.  */
11300       T(V6T2),          /* V6T2.  */
11301       T(V6K),           /* V6K.  */
11302       T(V7),            /* V7.  */
11303       T(V6_M),          /* V6_M.  */
11304       T(V6S_M),         /* V6S_M.  */
11305       T(V7E_M),         /* V7E_M.  */
11306       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
11307     };
11308   const int *comb[] =
11309     {
11310       v6t2,
11311       v6k,
11312       v7,
11313       v6_m,
11314       v6s_m,
11315       v7e_m,
11316       /* Pseudo-architecture.  */
11317       v4t_plus_v6_m
11318     };
11319
11320   /* Check we've not got a higher architecture than we know about.  */
11321
11322   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
11323     {
11324       _bfd_error_handler (_("error: %B: Unknown CPU architecture"), ibfd);
11325       return -1;
11326     }
11327
11328   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
11329
11330   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
11331       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
11332     oldtag = T(V4T_PLUS_V6_M);
11333
11334   /* And override the new tag if we have a Tag_also_compatible_with on the
11335      input.  */
11336
11337   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
11338       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
11339     newtag = T(V4T_PLUS_V6_M);
11340
11341   tagl = (oldtag < newtag) ? oldtag : newtag;
11342   result = tagh = (oldtag > newtag) ? oldtag : newtag;
11343
11344   /* Architectures before V6KZ add features monotonically.  */
11345   if (tagh <= TAG_CPU_ARCH_V6KZ)
11346     return result;
11347
11348   result = comb[tagh - T(V6T2)][tagl];
11349
11350   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
11351      as the canonical version.  */
11352   if (result == T(V4T_PLUS_V6_M))
11353     {
11354       result = T(V4T);
11355       *secondary_compat_out = T(V6_M);
11356     }
11357   else
11358     *secondary_compat_out = -1;
11359
11360   if (result == -1)
11361     {
11362       _bfd_error_handler (_("error: %B: Conflicting CPU architectures %d/%d"),
11363                           ibfd, oldtag, newtag);
11364       return -1;
11365     }
11366
11367   return result;
11368 #undef T
11369 }
11370
11371 /* Query attributes object to see if integer divide instructions may be
11372    present in an object.  */
11373 static bfd_boolean
11374 elf32_arm_attributes_accept_div (const obj_attribute *attr)
11375 {
11376   int arch = attr[Tag_CPU_arch].i;
11377   int profile = attr[Tag_CPU_arch_profile].i;
11378
11379   switch (attr[Tag_DIV_use].i)
11380     {
11381     case 0:
11382       /* Integer divide allowed if instruction contained in archetecture.  */
11383       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
11384         return TRUE;
11385       else if (arch >= TAG_CPU_ARCH_V7E_M)
11386         return TRUE;
11387       else
11388         return FALSE;
11389
11390     case 1:
11391       /* Integer divide explicitly prohibited.  */
11392       return FALSE;
11393
11394     default:
11395       /* Unrecognised case - treat as allowing divide everywhere.  */
11396     case 2:
11397       /* Integer divide allowed in ARM state.  */
11398       return TRUE;
11399     }
11400 }
11401
11402 /* Query attributes object to see if integer divide instructions are
11403    forbidden to be in the object.  This is not the inverse of
11404    elf32_arm_attributes_accept_div.  */
11405 static bfd_boolean
11406 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
11407 {
11408   return attr[Tag_DIV_use].i == 1;
11409 }
11410
11411 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
11412    are conflicting attributes.  */
11413
11414 static bfd_boolean
11415 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
11416 {
11417   obj_attribute *in_attr;
11418   obj_attribute *out_attr;
11419   /* Some tags have 0 = don't care, 1 = strong requirement,
11420      2 = weak requirement.  */
11421   static const int order_021[3] = {0, 2, 1};
11422   int i;
11423   bfd_boolean result = TRUE;
11424
11425   /* Skip the linker stubs file.  This preserves previous behavior
11426      of accepting unknown attributes in the first input file - but
11427      is that a bug?  */
11428   if (ibfd->flags & BFD_LINKER_CREATED)
11429     return TRUE;
11430
11431   if (!elf_known_obj_attributes_proc (obfd)[0].i)
11432     {
11433       /* This is the first object.  Copy the attributes.  */
11434       _bfd_elf_copy_obj_attributes (ibfd, obfd);
11435
11436       out_attr = elf_known_obj_attributes_proc (obfd);
11437
11438       /* Use the Tag_null value to indicate the attributes have been
11439          initialized.  */
11440       out_attr[0].i = 1;
11441
11442       /* We do not output objects with Tag_MPextension_use_legacy - we move
11443          the attribute's value to Tag_MPextension_use.  */
11444       if (out_attr[Tag_MPextension_use_legacy].i != 0)
11445         {
11446           if (out_attr[Tag_MPextension_use].i != 0
11447               && out_attr[Tag_MPextension_use_legacy].i
11448                 != out_attr[Tag_MPextension_use].i)
11449             {
11450               _bfd_error_handler
11451                 (_("Error: %B has both the current and legacy "
11452                    "Tag_MPextension_use attributes"), ibfd);
11453               result = FALSE;
11454             }
11455
11456           out_attr[Tag_MPextension_use] =
11457             out_attr[Tag_MPextension_use_legacy];
11458           out_attr[Tag_MPextension_use_legacy].type = 0;
11459           out_attr[Tag_MPextension_use_legacy].i = 0;
11460         }
11461
11462       return result;
11463     }
11464
11465   in_attr = elf_known_obj_attributes_proc (ibfd);
11466   out_attr = elf_known_obj_attributes_proc (obfd);
11467   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
11468   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
11469     {
11470       /* Ignore mismatches if the object doesn't use floating point.  */
11471       if (out_attr[Tag_ABI_FP_number_model].i == 0)
11472         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
11473       else if (in_attr[Tag_ABI_FP_number_model].i != 0)
11474         {
11475           _bfd_error_handler
11476             (_("error: %B uses VFP register arguments, %B does not"),
11477              in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
11478              in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
11479           result = FALSE;
11480         }
11481     }
11482
11483   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
11484     {
11485       /* Merge this attribute with existing attributes.  */
11486       switch (i)
11487         {
11488         case Tag_CPU_raw_name:
11489         case Tag_CPU_name:
11490           /* These are merged after Tag_CPU_arch. */
11491           break;
11492
11493         case Tag_ABI_optimization_goals:
11494         case Tag_ABI_FP_optimization_goals:
11495           /* Use the first value seen.  */
11496           break;
11497
11498         case Tag_CPU_arch:
11499           {
11500             int secondary_compat = -1, secondary_compat_out = -1;
11501             unsigned int saved_out_attr = out_attr[i].i;
11502             static const char *name_table[] = {
11503                 /* These aren't real CPU names, but we can't guess
11504                    that from the architecture version alone.  */
11505                 "Pre v4",
11506                 "ARM v4",
11507                 "ARM v4T",
11508                 "ARM v5T",
11509                 "ARM v5TE",
11510                 "ARM v5TEJ",
11511                 "ARM v6",
11512                 "ARM v6KZ",
11513                 "ARM v6T2",
11514                 "ARM v6K",
11515                 "ARM v7",
11516                 "ARM v6-M",
11517                 "ARM v6S-M"
11518             };
11519
11520             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
11521             secondary_compat = get_secondary_compatible_arch (ibfd);
11522             secondary_compat_out = get_secondary_compatible_arch (obfd);
11523             out_attr[i].i = tag_cpu_arch_combine (ibfd, out_attr[i].i,
11524                                                   &secondary_compat_out,
11525                                                   in_attr[i].i,
11526                                                   secondary_compat);
11527             set_secondary_compatible_arch (obfd, secondary_compat_out);
11528
11529             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
11530             if (out_attr[i].i == saved_out_attr)
11531               ; /* Leave the names alone.  */
11532             else if (out_attr[i].i == in_attr[i].i)
11533               {
11534                 /* The output architecture has been changed to match the
11535                    input architecture.  Use the input names.  */
11536                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
11537                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
11538                   : NULL;
11539                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
11540                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
11541                   : NULL;
11542               }
11543             else
11544               {
11545                 out_attr[Tag_CPU_name].s = NULL;
11546                 out_attr[Tag_CPU_raw_name].s = NULL;
11547               }
11548
11549             /* If we still don't have a value for Tag_CPU_name,
11550                make one up now.  Tag_CPU_raw_name remains blank.  */
11551             if (out_attr[Tag_CPU_name].s == NULL
11552                 && out_attr[i].i < ARRAY_SIZE (name_table))
11553               out_attr[Tag_CPU_name].s =
11554                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
11555           }
11556           break;
11557
11558         case Tag_ARM_ISA_use:
11559         case Tag_THUMB_ISA_use:
11560         case Tag_WMMX_arch:
11561         case Tag_Advanced_SIMD_arch:
11562           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
11563         case Tag_ABI_FP_rounding:
11564         case Tag_ABI_FP_exceptions:
11565         case Tag_ABI_FP_user_exceptions:
11566         case Tag_ABI_FP_number_model:
11567         case Tag_FP_HP_extension:
11568         case Tag_CPU_unaligned_access:
11569         case Tag_T2EE_use:
11570         case Tag_MPextension_use:
11571           /* Use the largest value specified.  */
11572           if (in_attr[i].i > out_attr[i].i)
11573             out_attr[i].i = in_attr[i].i;
11574           break;
11575
11576         case Tag_ABI_align_preserved:
11577         case Tag_ABI_PCS_RO_data:
11578           /* Use the smallest value specified.  */
11579           if (in_attr[i].i < out_attr[i].i)
11580             out_attr[i].i = in_attr[i].i;
11581           break;
11582
11583         case Tag_ABI_align_needed:
11584           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
11585               && (in_attr[Tag_ABI_align_preserved].i == 0
11586                   || out_attr[Tag_ABI_align_preserved].i == 0))
11587             {
11588               /* This error message should be enabled once all non-conformant
11589                  binaries in the toolchain have had the attributes set
11590                  properly.
11591               _bfd_error_handler
11592                 (_("error: %B: 8-byte data alignment conflicts with %B"),
11593                  obfd, ibfd);
11594               result = FALSE; */
11595             }
11596           /* Fall through.  */
11597         case Tag_ABI_FP_denormal:
11598         case Tag_ABI_PCS_GOT_use:
11599           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
11600              value if greater than 2 (for future-proofing).  */
11601           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
11602               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
11603                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
11604             out_attr[i].i = in_attr[i].i;
11605           break;
11606
11607         case Tag_Virtualization_use:
11608           /* The virtualization tag effectively stores two bits of
11609              information: the intended use of TrustZone (in bit 0), and the
11610              intended use of Virtualization (in bit 1).  */
11611           if (out_attr[i].i == 0)
11612             out_attr[i].i = in_attr[i].i;
11613           else if (in_attr[i].i != 0
11614                    && in_attr[i].i != out_attr[i].i)
11615             {
11616               if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
11617                 out_attr[i].i = 3;
11618               else
11619                 {
11620                   _bfd_error_handler
11621                     (_("error: %B: unable to merge virtualization attributes "
11622                        "with %B"),
11623                      obfd, ibfd);
11624                   result = FALSE;
11625                 }
11626             }
11627           break;
11628
11629         case Tag_CPU_arch_profile:
11630           if (out_attr[i].i != in_attr[i].i)
11631             {
11632               /* 0 will merge with anything.
11633                  'A' and 'S' merge to 'A'.
11634                  'R' and 'S' merge to 'R'.
11635                  'M' and 'A|R|S' is an error.  */
11636               if (out_attr[i].i == 0
11637                   || (out_attr[i].i == 'S'
11638                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
11639                 out_attr[i].i = in_attr[i].i;
11640               else if (in_attr[i].i == 0
11641                        || (in_attr[i].i == 'S'
11642                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
11643                 ; /* Do nothing. */
11644               else
11645                 {
11646                   _bfd_error_handler
11647                     (_("error: %B: Conflicting architecture profiles %c/%c"),
11648                      ibfd,
11649                      in_attr[i].i ? in_attr[i].i : '0',
11650                      out_attr[i].i ? out_attr[i].i : '0');
11651                   result = FALSE;
11652                 }
11653             }
11654           break;
11655         case Tag_FP_arch:
11656             {
11657               /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
11658                  the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
11659                  when it's 0.  It might mean absence of FP hardware if
11660                  Tag_FP_arch is zero, otherwise it is effectively SP + DP.  */
11661
11662               static const struct
11663               {
11664                   int ver;
11665                   int regs;
11666               } vfp_versions[7] =
11667                 {
11668                   {0, 0},
11669                   {1, 16},
11670                   {2, 16},
11671                   {3, 32},
11672                   {3, 16},
11673                   {4, 32},
11674                   {4, 16}
11675                 };
11676               int ver;
11677               int regs;
11678               int newval;
11679
11680               /* If the output has no requirement about FP hardware,
11681                  follow the requirement of the input.  */
11682               if (out_attr[i].i == 0)
11683                 {
11684                   BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
11685                   out_attr[i].i = in_attr[i].i;
11686                   out_attr[Tag_ABI_HardFP_use].i
11687                     = in_attr[Tag_ABI_HardFP_use].i;
11688                   break;
11689                 }
11690               /* If the input has no requirement about FP hardware, do
11691                  nothing.  */
11692               else if (in_attr[i].i == 0)
11693                 {
11694                   BFD_ASSERT (in_attr[Tag_ABI_HardFP_use].i == 0);
11695                   break;
11696                 }
11697
11698               /* Both the input and the output have nonzero Tag_FP_arch.
11699                  So Tag_ABI_HardFP_use is (SP & DP) when it's zero.  */
11700
11701               /* If both the input and the output have zero Tag_ABI_HardFP_use,
11702                  do nothing.  */
11703               if (in_attr[Tag_ABI_HardFP_use].i == 0
11704                   && out_attr[Tag_ABI_HardFP_use].i == 0)
11705                 ;
11706               /* If the input and the output have different Tag_ABI_HardFP_use,
11707                  the combination of them is 3 (SP & DP).  */
11708               else if (in_attr[Tag_ABI_HardFP_use].i
11709                        != out_attr[Tag_ABI_HardFP_use].i)
11710                 out_attr[Tag_ABI_HardFP_use].i = 3;
11711
11712               /* Now we can handle Tag_FP_arch.  */
11713
11714               /* Values greater than 6 aren't defined, so just pick the
11715                  biggest */
11716               if (in_attr[i].i > 6 && in_attr[i].i > out_attr[i].i)
11717                 {
11718                   out_attr[i] = in_attr[i];
11719                   break;
11720                 }
11721               /* The output uses the superset of input features
11722                  (ISA version) and registers.  */
11723               ver = vfp_versions[in_attr[i].i].ver;
11724               if (ver < vfp_versions[out_attr[i].i].ver)
11725                 ver = vfp_versions[out_attr[i].i].ver;
11726               regs = vfp_versions[in_attr[i].i].regs;
11727               if (regs < vfp_versions[out_attr[i].i].regs)
11728                 regs = vfp_versions[out_attr[i].i].regs;
11729               /* This assumes all possible supersets are also a valid
11730                  options.  */
11731               for (newval = 6; newval > 0; newval--)
11732                 {
11733                   if (regs == vfp_versions[newval].regs
11734                       && ver == vfp_versions[newval].ver)
11735                     break;
11736                 }
11737               out_attr[i].i = newval;
11738             }
11739           break;
11740         case Tag_PCS_config:
11741           if (out_attr[i].i == 0)
11742             out_attr[i].i = in_attr[i].i;
11743           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
11744             {
11745               /* It's sometimes ok to mix different configs, so this is only
11746                  a warning.  */
11747               _bfd_error_handler
11748                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
11749             }
11750           break;
11751         case Tag_ABI_PCS_R9_use:
11752           if (in_attr[i].i != out_attr[i].i
11753               && out_attr[i].i != AEABI_R9_unused
11754               && in_attr[i].i != AEABI_R9_unused)
11755             {
11756               _bfd_error_handler
11757                 (_("error: %B: Conflicting use of R9"), ibfd);
11758               result = FALSE;
11759             }
11760           if (out_attr[i].i == AEABI_R9_unused)
11761             out_attr[i].i = in_attr[i].i;
11762           break;
11763         case Tag_ABI_PCS_RW_data:
11764           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
11765               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
11766               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
11767             {
11768               _bfd_error_handler
11769                 (_("error: %B: SB relative addressing conflicts with use of R9"),
11770                  ibfd);
11771               result = FALSE;
11772             }
11773           /* Use the smallest value specified.  */
11774           if (in_attr[i].i < out_attr[i].i)
11775             out_attr[i].i = in_attr[i].i;
11776           break;
11777         case Tag_ABI_PCS_wchar_t:
11778           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
11779               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
11780             {
11781               _bfd_error_handler
11782                 (_("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"),
11783                  ibfd, in_attr[i].i, out_attr[i].i);
11784             }
11785           else if (in_attr[i].i && !out_attr[i].i)
11786             out_attr[i].i = in_attr[i].i;
11787           break;
11788         case Tag_ABI_enum_size:
11789           if (in_attr[i].i != AEABI_enum_unused)
11790             {
11791               if (out_attr[i].i == AEABI_enum_unused
11792                   || out_attr[i].i == AEABI_enum_forced_wide)
11793                 {
11794                   /* The existing object is compatible with anything.
11795                      Use whatever requirements the new object has.  */
11796                   out_attr[i].i = in_attr[i].i;
11797                 }
11798               else if (in_attr[i].i != AEABI_enum_forced_wide
11799                        && out_attr[i].i != in_attr[i].i
11800                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
11801                 {
11802                   static const char *aeabi_enum_names[] =
11803                     { "", "variable-size", "32-bit", "" };
11804                   const char *in_name =
11805                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
11806                     ? aeabi_enum_names[in_attr[i].i]
11807                     : "<unknown>";
11808                   const char *out_name =
11809                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
11810                     ? aeabi_enum_names[out_attr[i].i]
11811                     : "<unknown>";
11812                   _bfd_error_handler
11813                     (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
11814                      ibfd, in_name, out_name);
11815                 }
11816             }
11817           break;
11818         case Tag_ABI_VFP_args:
11819           /* Aready done.  */
11820           break;
11821         case Tag_ABI_WMMX_args:
11822           if (in_attr[i].i != out_attr[i].i)
11823             {
11824               _bfd_error_handler
11825                 (_("error: %B uses iWMMXt register arguments, %B does not"),
11826                  ibfd, obfd);
11827               result = FALSE;
11828             }
11829           break;
11830         case Tag_compatibility:
11831           /* Merged in target-independent code.  */
11832           break;
11833         case Tag_ABI_HardFP_use:
11834           /* This is handled along with Tag_FP_arch.  */
11835           break;
11836         case Tag_ABI_FP_16bit_format:
11837           if (in_attr[i].i != 0 && out_attr[i].i != 0)
11838             {
11839               if (in_attr[i].i != out_attr[i].i)
11840                 {
11841                   _bfd_error_handler
11842                     (_("error: fp16 format mismatch between %B and %B"),
11843                      ibfd, obfd);
11844                   result = FALSE;
11845                 }
11846             }
11847           if (in_attr[i].i != 0)
11848             out_attr[i].i = in_attr[i].i;
11849           break;
11850
11851         case Tag_DIV_use:
11852           /* A value of zero on input means that the divide instruction may
11853              be used if available in the base architecture as specified via
11854              Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
11855              the user did not want divide instructions.  A value of 2
11856              explicitly means that divide instructions were allowed in ARM
11857              and Thumb state.  */
11858           if (in_attr[i].i == out_attr[i].i)
11859             /* Do nothing.  */ ;
11860           else if (elf32_arm_attributes_forbid_div (in_attr)
11861                    && !elf32_arm_attributes_accept_div (out_attr))
11862             out_attr[i].i = 1;
11863           else if (elf32_arm_attributes_forbid_div (out_attr)
11864                    && elf32_arm_attributes_accept_div (in_attr))
11865             out_attr[i].i = in_attr[i].i;
11866           else if (in_attr[i].i == 2)
11867             out_attr[i].i = in_attr[i].i;
11868           break;
11869
11870         case Tag_MPextension_use_legacy:
11871           /* We don't output objects with Tag_MPextension_use_legacy - we
11872              move the value to Tag_MPextension_use.  */
11873           if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
11874             {
11875               if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
11876                 {
11877                   _bfd_error_handler
11878                     (_("%B has has both the current and legacy "
11879                        "Tag_MPextension_use attributes"),
11880                      ibfd);
11881                   result = FALSE;
11882                 }
11883             }
11884
11885           if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
11886             out_attr[Tag_MPextension_use] = in_attr[i];
11887
11888           break;
11889
11890         case Tag_nodefaults:
11891           /* This tag is set if it exists, but the value is unused (and is
11892              typically zero).  We don't actually need to do anything here -
11893              the merge happens automatically when the type flags are merged
11894              below.  */
11895           break;
11896         case Tag_also_compatible_with:
11897           /* Already done in Tag_CPU_arch.  */
11898           break;
11899         case Tag_conformance:
11900           /* Keep the attribute if it matches.  Throw it away otherwise.
11901              No attribute means no claim to conform.  */
11902           if (!in_attr[i].s || !out_attr[i].s
11903               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
11904             out_attr[i].s = NULL;
11905           break;
11906
11907         default:
11908           result
11909             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
11910         }
11911
11912       /* If out_attr was copied from in_attr then it won't have a type yet.  */
11913       if (in_attr[i].type && !out_attr[i].type)
11914         out_attr[i].type = in_attr[i].type;
11915     }
11916
11917   /* Merge Tag_compatibility attributes and any common GNU ones.  */
11918   if (!_bfd_elf_merge_object_attributes (ibfd, obfd))
11919     return FALSE;
11920
11921   /* Check for any attributes not known on ARM.  */
11922   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
11923
11924   return result;
11925 }
11926
11927
11928 /* Return TRUE if the two EABI versions are incompatible.  */
11929
11930 static bfd_boolean
11931 elf32_arm_versions_compatible (unsigned iver, unsigned over)
11932 {
11933   /* v4 and v5 are the same spec before and after it was released,
11934      so allow mixing them.  */
11935   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
11936       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
11937     return TRUE;
11938
11939   return (iver == over);
11940 }
11941
11942 /* Merge backend specific data from an object file to the output
11943    object file when linking.  */
11944
11945 static bfd_boolean
11946 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd);
11947
11948 /* Display the flags field.  */
11949
11950 static bfd_boolean
11951 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
11952 {
11953   FILE * file = (FILE *) ptr;
11954   unsigned long flags;
11955
11956   BFD_ASSERT (abfd != NULL && ptr != NULL);
11957
11958   /* Print normal ELF private data.  */
11959   _bfd_elf_print_private_bfd_data (abfd, ptr);
11960
11961   flags = elf_elfheader (abfd)->e_flags;
11962   /* Ignore init flag - it may not be set, despite the flags field
11963      containing valid data.  */
11964
11965   /* xgettext:c-format */
11966   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
11967
11968   switch (EF_ARM_EABI_VERSION (flags))
11969     {
11970     case EF_ARM_EABI_UNKNOWN:
11971       /* The following flag bits are GNU extensions and not part of the
11972          official ARM ELF extended ABI.  Hence they are only decoded if
11973          the EABI version is not set.  */
11974       if (flags & EF_ARM_INTERWORK)
11975         fprintf (file, _(" [interworking enabled]"));
11976
11977       if (flags & EF_ARM_APCS_26)
11978         fprintf (file, " [APCS-26]");
11979       else
11980         fprintf (file, " [APCS-32]");
11981
11982       if (flags & EF_ARM_VFP_FLOAT)
11983         fprintf (file, _(" [VFP float format]"));
11984       else if (flags & EF_ARM_MAVERICK_FLOAT)
11985         fprintf (file, _(" [Maverick float format]"));
11986       else
11987         fprintf (file, _(" [FPA float format]"));
11988
11989       if (flags & EF_ARM_APCS_FLOAT)
11990         fprintf (file, _(" [floats passed in float registers]"));
11991
11992       if (flags & EF_ARM_PIC)
11993         fprintf (file, _(" [position independent]"));
11994
11995       if (flags & EF_ARM_NEW_ABI)
11996         fprintf (file, _(" [new ABI]"));
11997
11998       if (flags & EF_ARM_OLD_ABI)
11999         fprintf (file, _(" [old ABI]"));
12000
12001       if (flags & EF_ARM_SOFT_FLOAT)
12002         fprintf (file, _(" [software FP]"));
12003
12004       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
12005                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
12006                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
12007                  | EF_ARM_MAVERICK_FLOAT);
12008       break;
12009
12010     case EF_ARM_EABI_VER1:
12011       fprintf (file, _(" [Version1 EABI]"));
12012
12013       if (flags & EF_ARM_SYMSARESORTED)
12014         fprintf (file, _(" [sorted symbol table]"));
12015       else
12016         fprintf (file, _(" [unsorted symbol table]"));
12017
12018       flags &= ~ EF_ARM_SYMSARESORTED;
12019       break;
12020
12021     case EF_ARM_EABI_VER2:
12022       fprintf (file, _(" [Version2 EABI]"));
12023
12024       if (flags & EF_ARM_SYMSARESORTED)
12025         fprintf (file, _(" [sorted symbol table]"));
12026       else
12027         fprintf (file, _(" [unsorted symbol table]"));
12028
12029       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
12030         fprintf (file, _(" [dynamic symbols use segment index]"));
12031
12032       if (flags & EF_ARM_MAPSYMSFIRST)
12033         fprintf (file, _(" [mapping symbols precede others]"));
12034
12035       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
12036                  | EF_ARM_MAPSYMSFIRST);
12037       break;
12038
12039     case EF_ARM_EABI_VER3:
12040       fprintf (file, _(" [Version3 EABI]"));
12041       break;
12042
12043     case EF_ARM_EABI_VER4:
12044       fprintf (file, _(" [Version4 EABI]"));
12045       goto eabi;
12046
12047     case EF_ARM_EABI_VER5:
12048       fprintf (file, _(" [Version5 EABI]"));
12049     eabi:
12050       if (flags & EF_ARM_BE8)
12051         fprintf (file, _(" [BE8]"));
12052
12053       if (flags & EF_ARM_LE8)
12054         fprintf (file, _(" [LE8]"));
12055
12056       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
12057       break;
12058
12059     default:
12060       fprintf (file, _(" <EABI version unrecognised>"));
12061       break;
12062     }
12063
12064   flags &= ~ EF_ARM_EABIMASK;
12065
12066   if (flags & EF_ARM_RELEXEC)
12067     fprintf (file, _(" [relocatable executable]"));
12068
12069   if (flags & EF_ARM_HASENTRY)
12070     fprintf (file, _(" [has entry point]"));
12071
12072   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
12073
12074   if (flags)
12075     fprintf (file, _("<Unrecognised flag bits set>"));
12076
12077   fputc ('\n', file);
12078
12079   return TRUE;
12080 }
12081
12082 static int
12083 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
12084 {
12085   switch (ELF_ST_TYPE (elf_sym->st_info))
12086     {
12087     case STT_ARM_TFUNC:
12088       return ELF_ST_TYPE (elf_sym->st_info);
12089
12090     case STT_ARM_16BIT:
12091       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
12092          This allows us to distinguish between data used by Thumb instructions
12093          and non-data (which is probably code) inside Thumb regions of an
12094          executable.  */
12095       if (type != STT_OBJECT && type != STT_TLS)
12096         return ELF_ST_TYPE (elf_sym->st_info);
12097       break;
12098
12099     default:
12100       break;
12101     }
12102
12103   return type;
12104 }
12105
12106 static asection *
12107 elf32_arm_gc_mark_hook (asection *sec,
12108                         struct bfd_link_info *info,
12109                         Elf_Internal_Rela *rel,
12110                         struct elf_link_hash_entry *h,
12111                         Elf_Internal_Sym *sym)
12112 {
12113   if (h != NULL)
12114     switch (ELF32_R_TYPE (rel->r_info))
12115       {
12116       case R_ARM_GNU_VTINHERIT:
12117       case R_ARM_GNU_VTENTRY:
12118         return NULL;
12119       }
12120
12121   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12122 }
12123
12124 /* Update the got entry reference counts for the section being removed.  */
12125
12126 static bfd_boolean
12127 elf32_arm_gc_sweep_hook (bfd *                     abfd,
12128                          struct bfd_link_info *    info,
12129                          asection *                sec,
12130                          const Elf_Internal_Rela * relocs)
12131 {
12132   Elf_Internal_Shdr *symtab_hdr;
12133   struct elf_link_hash_entry **sym_hashes;
12134   bfd_signed_vma *local_got_refcounts;
12135   const Elf_Internal_Rela *rel, *relend;
12136   struct elf32_arm_link_hash_table * globals;
12137
12138   if (info->relocatable)
12139     return TRUE;
12140
12141   globals = elf32_arm_hash_table (info);
12142   if (globals == NULL)
12143     return FALSE;
12144
12145   elf_section_data (sec)->local_dynrel = NULL;
12146
12147   symtab_hdr = & elf_symtab_hdr (abfd);
12148   sym_hashes = elf_sym_hashes (abfd);
12149   local_got_refcounts = elf_local_got_refcounts (abfd);
12150
12151   check_use_blx (globals);
12152
12153   relend = relocs + sec->reloc_count;
12154   for (rel = relocs; rel < relend; rel++)
12155     {
12156       unsigned long r_symndx;
12157       struct elf_link_hash_entry *h = NULL;
12158       struct elf32_arm_link_hash_entry *eh;
12159       int r_type;
12160       bfd_boolean call_reloc_p;
12161       bfd_boolean may_become_dynamic_p;
12162       bfd_boolean may_need_local_target_p;
12163       union gotplt_union *root_plt;
12164       struct arm_plt_info *arm_plt;
12165
12166       r_symndx = ELF32_R_SYM (rel->r_info);
12167       if (r_symndx >= symtab_hdr->sh_info)
12168         {
12169           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
12170           while (h->root.type == bfd_link_hash_indirect
12171                  || h->root.type == bfd_link_hash_warning)
12172             h = (struct elf_link_hash_entry *) h->root.u.i.link;
12173         }
12174       eh = (struct elf32_arm_link_hash_entry *) h;
12175
12176       call_reloc_p = FALSE;
12177       may_become_dynamic_p = FALSE;
12178       may_need_local_target_p = FALSE;
12179
12180       r_type = ELF32_R_TYPE (rel->r_info);
12181       r_type = arm_real_reloc_type (globals, r_type);
12182       switch (r_type)
12183         {
12184         case R_ARM_GOT32:
12185         case R_ARM_GOT_PREL:
12186         case R_ARM_TLS_GD32:
12187         case R_ARM_TLS_IE32:
12188           if (h != NULL)
12189             {
12190               if (h->got.refcount > 0)
12191                 h->got.refcount -= 1;
12192             }
12193           else if (local_got_refcounts != NULL)
12194             {
12195               if (local_got_refcounts[r_symndx] > 0)
12196                 local_got_refcounts[r_symndx] -= 1;
12197             }
12198           break;
12199
12200         case R_ARM_TLS_LDM32:
12201           globals->tls_ldm_got.refcount -= 1;
12202           break;
12203
12204         case R_ARM_PC24:
12205         case R_ARM_PLT32:
12206         case R_ARM_CALL:
12207         case R_ARM_JUMP24:
12208         case R_ARM_PREL31:
12209         case R_ARM_THM_CALL:
12210         case R_ARM_THM_JUMP24:
12211         case R_ARM_THM_JUMP19:
12212           call_reloc_p = TRUE;
12213           may_need_local_target_p = TRUE;
12214           break;
12215
12216         case R_ARM_ABS12:
12217           if (!globals->vxworks_p)
12218             {
12219               may_need_local_target_p = TRUE;
12220               break;
12221             }
12222           /* Fall through.  */
12223         case R_ARM_ABS32:
12224         case R_ARM_ABS32_NOI:
12225         case R_ARM_REL32:
12226         case R_ARM_REL32_NOI:
12227         case R_ARM_MOVW_ABS_NC:
12228         case R_ARM_MOVT_ABS:
12229         case R_ARM_MOVW_PREL_NC:
12230         case R_ARM_MOVT_PREL:
12231         case R_ARM_THM_MOVW_ABS_NC:
12232         case R_ARM_THM_MOVT_ABS:
12233         case R_ARM_THM_MOVW_PREL_NC:
12234         case R_ARM_THM_MOVT_PREL:
12235           /* Should the interworking branches be here also?  */
12236           if ((info->shared || globals->root.is_relocatable_executable)
12237               && (sec->flags & SEC_ALLOC) != 0)
12238             {
12239               if (h == NULL
12240                   && (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI))
12241                 {
12242                   call_reloc_p = TRUE;
12243                   may_need_local_target_p = TRUE;
12244                 }
12245               else
12246                 may_become_dynamic_p = TRUE;
12247             }
12248           else
12249             may_need_local_target_p = TRUE;
12250           break;
12251
12252         default:
12253           break;
12254         }
12255
12256       if (may_need_local_target_p
12257           && elf32_arm_get_plt_info (abfd, eh, r_symndx, &root_plt, &arm_plt))
12258         {
12259           BFD_ASSERT (root_plt->refcount > 0);
12260           root_plt->refcount -= 1;
12261
12262           if (!call_reloc_p)
12263             arm_plt->noncall_refcount--;
12264
12265           if (r_type == R_ARM_THM_CALL)
12266             arm_plt->maybe_thumb_refcount--;
12267
12268           if (r_type == R_ARM_THM_JUMP24
12269               || r_type == R_ARM_THM_JUMP19)
12270             arm_plt->thumb_refcount--;
12271         }
12272
12273       if (may_become_dynamic_p)
12274         {
12275           struct elf_dyn_relocs **pp;
12276           struct elf_dyn_relocs *p;
12277
12278           if (h != NULL)
12279             pp = &(eh->dyn_relocs);
12280           else
12281             {
12282               Elf_Internal_Sym *isym;
12283
12284               isym = bfd_sym_from_r_symndx (&globals->sym_cache,
12285                                             abfd, r_symndx);
12286               if (isym == NULL)
12287                 return FALSE;
12288               pp = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
12289               if (pp == NULL)
12290                 return FALSE;
12291             }
12292           for (; (p = *pp) != NULL; pp = &p->next)
12293             if (p->sec == sec)
12294               {
12295                 /* Everything must go for SEC.  */
12296                 *pp = p->next;
12297                 break;
12298               }
12299         }
12300     }
12301
12302   return TRUE;
12303 }
12304
12305 /* Look through the relocs for a section during the first phase.  */
12306
12307 static bfd_boolean
12308 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
12309                         asection *sec, const Elf_Internal_Rela *relocs)
12310 {
12311   Elf_Internal_Shdr *symtab_hdr;
12312   struct elf_link_hash_entry **sym_hashes;
12313   const Elf_Internal_Rela *rel;
12314   const Elf_Internal_Rela *rel_end;
12315   bfd *dynobj;
12316   asection *sreloc;
12317   struct elf32_arm_link_hash_table *htab;
12318   bfd_boolean call_reloc_p;
12319   bfd_boolean may_become_dynamic_p;
12320   bfd_boolean may_need_local_target_p;
12321   unsigned long nsyms;
12322
12323   if (info->relocatable)
12324     return TRUE;
12325
12326   BFD_ASSERT (is_arm_elf (abfd));
12327
12328   htab = elf32_arm_hash_table (info);
12329   if (htab == NULL)
12330     return FALSE;
12331
12332   sreloc = NULL;
12333
12334   /* Create dynamic sections for relocatable executables so that we can
12335      copy relocations.  */
12336   if (htab->root.is_relocatable_executable
12337       && ! htab->root.dynamic_sections_created)
12338     {
12339       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
12340         return FALSE;
12341     }
12342
12343   if (htab->root.dynobj == NULL)
12344     htab->root.dynobj = abfd;
12345   if (!create_ifunc_sections (info))
12346     return FALSE;
12347
12348   dynobj = htab->root.dynobj;
12349
12350   symtab_hdr = & elf_symtab_hdr (abfd);
12351   sym_hashes = elf_sym_hashes (abfd);
12352   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
12353
12354   rel_end = relocs + sec->reloc_count;
12355   for (rel = relocs; rel < rel_end; rel++)
12356     {
12357       Elf_Internal_Sym *isym;
12358       struct elf_link_hash_entry *h;
12359       struct elf32_arm_link_hash_entry *eh;
12360       unsigned long r_symndx;
12361       int r_type;
12362
12363       r_symndx = ELF32_R_SYM (rel->r_info);
12364       r_type = ELF32_R_TYPE (rel->r_info);
12365       r_type = arm_real_reloc_type (htab, r_type);
12366
12367       if (r_symndx >= nsyms
12368           /* PR 9934: It is possible to have relocations that do not
12369              refer to symbols, thus it is also possible to have an
12370              object file containing relocations but no symbol table.  */
12371           && (r_symndx > STN_UNDEF || nsyms > 0))
12372         {
12373           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
12374                                    r_symndx);
12375           return FALSE;
12376         }
12377
12378       h = NULL;
12379       isym = NULL;
12380       if (nsyms > 0)
12381         {
12382           if (r_symndx < symtab_hdr->sh_info)
12383             {
12384               /* A local symbol.  */
12385               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
12386                                             abfd, r_symndx);
12387               if (isym == NULL)
12388                 return FALSE;
12389             }
12390           else
12391             {
12392               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
12393               while (h->root.type == bfd_link_hash_indirect
12394                      || h->root.type == bfd_link_hash_warning)
12395                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12396             }
12397         }
12398
12399       eh = (struct elf32_arm_link_hash_entry *) h;
12400
12401       call_reloc_p = FALSE;
12402       may_become_dynamic_p = FALSE;
12403       may_need_local_target_p = FALSE;
12404
12405       /* Could be done earlier, if h were already available.  */
12406       r_type = elf32_arm_tls_transition (info, r_type, h);
12407       switch (r_type)
12408         {
12409           case R_ARM_GOT32:
12410           case R_ARM_GOT_PREL:
12411           case R_ARM_TLS_GD32:
12412           case R_ARM_TLS_IE32:
12413           case R_ARM_TLS_GOTDESC:
12414           case R_ARM_TLS_DESCSEQ:
12415           case R_ARM_THM_TLS_DESCSEQ:
12416           case R_ARM_TLS_CALL:
12417           case R_ARM_THM_TLS_CALL:
12418             /* This symbol requires a global offset table entry.  */
12419             {
12420               int tls_type, old_tls_type;
12421
12422               switch (r_type)
12423                 {
12424                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
12425
12426                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
12427
12428                 case R_ARM_TLS_GOTDESC:
12429                 case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
12430                 case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
12431                   tls_type = GOT_TLS_GDESC; break;
12432
12433                 default: tls_type = GOT_NORMAL; break;
12434                 }
12435
12436               if (h != NULL)
12437                 {
12438                   h->got.refcount++;
12439                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
12440                 }
12441               else
12442                 {
12443                   /* This is a global offset table entry for a local symbol.  */
12444                   if (!elf32_arm_allocate_local_sym_info (abfd))
12445                     return FALSE;
12446                   elf_local_got_refcounts (abfd)[r_symndx] += 1;
12447                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
12448                 }
12449
12450               /* If a variable is accessed with both tls methods, two
12451                  slots may be created.  */
12452               if (GOT_TLS_GD_ANY_P (old_tls_type)
12453                   && GOT_TLS_GD_ANY_P (tls_type))
12454                 tls_type |= old_tls_type;
12455
12456               /* We will already have issued an error message if there
12457                  is a TLS/non-TLS mismatch, based on the symbol
12458                  type.  So just combine any TLS types needed.  */
12459               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
12460                   && tls_type != GOT_NORMAL)
12461                 tls_type |= old_tls_type;
12462
12463               /* If the symbol is accessed in both IE and GDESC
12464                  method, we're able to relax. Turn off the GDESC flag,
12465                  without messing up with any other kind of tls types
12466                  that may be involved */
12467               if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
12468                 tls_type &= ~GOT_TLS_GDESC;
12469
12470               if (old_tls_type != tls_type)
12471                 {
12472                   if (h != NULL)
12473                     elf32_arm_hash_entry (h)->tls_type = tls_type;
12474                   else
12475                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
12476                 }
12477             }
12478             /* Fall through.  */
12479
12480           case R_ARM_TLS_LDM32:
12481             if (r_type == R_ARM_TLS_LDM32)
12482                 htab->tls_ldm_got.refcount++;
12483             /* Fall through.  */
12484
12485           case R_ARM_GOTOFF32:
12486           case R_ARM_GOTPC:
12487             if (htab->root.sgot == NULL
12488                 && !create_got_section (htab->root.dynobj, info))
12489               return FALSE;
12490             break;
12491
12492           case R_ARM_PC24:
12493           case R_ARM_PLT32:
12494           case R_ARM_CALL:
12495           case R_ARM_JUMP24:
12496           case R_ARM_PREL31:
12497           case R_ARM_THM_CALL:
12498           case R_ARM_THM_JUMP24:
12499           case R_ARM_THM_JUMP19:
12500             call_reloc_p = TRUE;
12501             may_need_local_target_p = TRUE;
12502             break;
12503
12504           case R_ARM_ABS12:
12505             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
12506                ldr __GOTT_INDEX__ offsets.  */
12507             if (!htab->vxworks_p)
12508               {
12509                 may_need_local_target_p = TRUE;
12510                 break;
12511               }
12512             /* Fall through.  */
12513
12514           case R_ARM_MOVW_ABS_NC:
12515           case R_ARM_MOVT_ABS:
12516           case R_ARM_THM_MOVW_ABS_NC:
12517           case R_ARM_THM_MOVT_ABS:
12518             if (info->shared)
12519               {
12520                 (*_bfd_error_handler)
12521                   (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
12522                    abfd, elf32_arm_howto_table_1[r_type].name,
12523                    (h) ? h->root.root.string : "a local symbol");
12524                 bfd_set_error (bfd_error_bad_value);
12525                 return FALSE;
12526               }
12527
12528             /* Fall through.  */
12529           case R_ARM_ABS32:
12530           case R_ARM_ABS32_NOI:
12531           case R_ARM_REL32:
12532           case R_ARM_REL32_NOI:
12533           case R_ARM_MOVW_PREL_NC:
12534           case R_ARM_MOVT_PREL:
12535           case R_ARM_THM_MOVW_PREL_NC:
12536           case R_ARM_THM_MOVT_PREL:
12537
12538             /* Should the interworking branches be listed here?  */
12539             if ((info->shared || htab->root.is_relocatable_executable)
12540                 && (sec->flags & SEC_ALLOC) != 0)
12541               {
12542                 if (h == NULL
12543                     && (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI))
12544                   {
12545                     /* In shared libraries and relocatable executables,
12546                        we treat local relative references as calls;
12547                        see the related SYMBOL_CALLS_LOCAL code in
12548                        allocate_dynrelocs.  */
12549                     call_reloc_p = TRUE;
12550                     may_need_local_target_p = TRUE;
12551                   }
12552                 else
12553                   /* We are creating a shared library or relocatable
12554                      executable, and this is a reloc against a global symbol,
12555                      or a non-PC-relative reloc against a local symbol.
12556                      We may need to copy the reloc into the output.  */
12557                   may_become_dynamic_p = TRUE;
12558               }
12559             else
12560               may_need_local_target_p = TRUE;
12561             break;
12562
12563         /* This relocation describes the C++ object vtable hierarchy.
12564            Reconstruct it for later use during GC.  */
12565         case R_ARM_GNU_VTINHERIT:
12566           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
12567             return FALSE;
12568           break;
12569
12570         /* This relocation describes which C++ vtable entries are actually
12571            used.  Record for later use during GC.  */
12572         case R_ARM_GNU_VTENTRY:
12573           BFD_ASSERT (h != NULL);
12574           if (h != NULL
12575               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
12576             return FALSE;
12577           break;
12578         }
12579
12580       if (h != NULL)
12581         {
12582           if (call_reloc_p)
12583             /* We may need a .plt entry if the function this reloc
12584                refers to is in a different object, regardless of the
12585                symbol's type.  We can't tell for sure yet, because
12586                something later might force the symbol local.  */
12587             h->needs_plt = 1;
12588           else if (may_need_local_target_p)
12589             /* If this reloc is in a read-only section, we might
12590                need a copy reloc.  We can't check reliably at this
12591                stage whether the section is read-only, as input
12592                sections have not yet been mapped to output sections.
12593                Tentatively set the flag for now, and correct in
12594                adjust_dynamic_symbol.  */
12595             h->non_got_ref = 1;
12596         }
12597
12598       if (may_need_local_target_p
12599           && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
12600         {
12601           union gotplt_union *root_plt;
12602           struct arm_plt_info *arm_plt;
12603           struct arm_local_iplt_info *local_iplt;
12604
12605           if (h != NULL)
12606             {
12607               root_plt = &h->plt;
12608               arm_plt = &eh->plt;
12609             }
12610           else
12611             {
12612               local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
12613               if (local_iplt == NULL)
12614                 return FALSE;
12615               root_plt = &local_iplt->root;
12616               arm_plt = &local_iplt->arm;
12617             }
12618
12619           /* If the symbol is a function that doesn't bind locally,
12620              this relocation will need a PLT entry.  */
12621           root_plt->refcount += 1;
12622
12623           if (!call_reloc_p)
12624             arm_plt->noncall_refcount++;
12625
12626           /* It's too early to use htab->use_blx here, so we have to
12627              record possible blx references separately from
12628              relocs that definitely need a thumb stub.  */
12629
12630           if (r_type == R_ARM_THM_CALL)
12631             arm_plt->maybe_thumb_refcount += 1;
12632
12633           if (r_type == R_ARM_THM_JUMP24
12634               || r_type == R_ARM_THM_JUMP19)
12635             arm_plt->thumb_refcount += 1;
12636         }
12637
12638       if (may_become_dynamic_p)
12639         {
12640           struct elf_dyn_relocs *p, **head;
12641
12642           /* Create a reloc section in dynobj.  */
12643           if (sreloc == NULL)
12644             {
12645               sreloc = _bfd_elf_make_dynamic_reloc_section
12646                 (sec, dynobj, 2, abfd, ! htab->use_rel);
12647
12648               if (sreloc == NULL)
12649                 return FALSE;
12650
12651               /* BPABI objects never have dynamic relocations mapped.  */
12652               if (htab->symbian_p)
12653                 {
12654                   flagword flags;
12655
12656                   flags = bfd_get_section_flags (dynobj, sreloc);
12657                   flags &= ~(SEC_LOAD | SEC_ALLOC);
12658                   bfd_set_section_flags (dynobj, sreloc, flags);
12659                 }
12660             }
12661
12662           /* If this is a global symbol, count the number of
12663              relocations we need for this symbol.  */
12664           if (h != NULL)
12665             head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
12666           else
12667             {
12668               head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
12669               if (head == NULL)
12670                 return FALSE;
12671             }
12672
12673           p = *head;
12674           if (p == NULL || p->sec != sec)
12675             {
12676               bfd_size_type amt = sizeof *p;
12677
12678               p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
12679               if (p == NULL)
12680                 return FALSE;
12681               p->next = *head;
12682               *head = p;
12683               p->sec = sec;
12684               p->count = 0;
12685               p->pc_count = 0;
12686             }
12687
12688           if (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
12689             p->pc_count += 1;
12690           p->count += 1;
12691         }
12692     }
12693
12694   return TRUE;
12695 }
12696
12697 /* Unwinding tables are not referenced directly.  This pass marks them as
12698    required if the corresponding code section is marked.  */
12699
12700 static bfd_boolean
12701 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
12702                                   elf_gc_mark_hook_fn gc_mark_hook)
12703 {
12704   bfd *sub;
12705   Elf_Internal_Shdr **elf_shdrp;
12706   bfd_boolean again;
12707
12708   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12709
12710   /* Marking EH data may cause additional code sections to be marked,
12711      requiring multiple passes.  */
12712   again = TRUE;
12713   while (again)
12714     {
12715       again = FALSE;
12716       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12717         {
12718           asection *o;
12719
12720           if (! is_arm_elf (sub))
12721             continue;
12722
12723           elf_shdrp = elf_elfsections (sub);
12724           for (o = sub->sections; o != NULL; o = o->next)
12725             {
12726               Elf_Internal_Shdr *hdr;
12727
12728               hdr = &elf_section_data (o)->this_hdr;
12729               if (hdr->sh_type == SHT_ARM_EXIDX
12730                   && hdr->sh_link
12731                   && hdr->sh_link < elf_numsections (sub)
12732                   && !o->gc_mark
12733                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
12734                 {
12735                   again = TRUE;
12736                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12737                     return FALSE;
12738                 }
12739             }
12740         }
12741     }
12742
12743   return TRUE;
12744 }
12745
12746 /* Treat mapping symbols as special target symbols.  */
12747
12748 static bfd_boolean
12749 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
12750 {
12751   return bfd_is_arm_special_symbol_name (sym->name,
12752                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
12753 }
12754
12755 /* This is a copy of elf_find_function() from elf.c except that
12756    ARM mapping symbols are ignored when looking for function names
12757    and STT_ARM_TFUNC is considered to a function type.  */
12758
12759 static bfd_boolean
12760 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
12761                        asection *    section,
12762                        asymbol **    symbols,
12763                        bfd_vma       offset,
12764                        const char ** filename_ptr,
12765                        const char ** functionname_ptr)
12766 {
12767   const char * filename = NULL;
12768   asymbol * func = NULL;
12769   bfd_vma low_func = 0;
12770   asymbol ** p;
12771
12772   for (p = symbols; *p != NULL; p++)
12773     {
12774       elf_symbol_type *q;
12775
12776       q = (elf_symbol_type *) *p;
12777
12778       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
12779         {
12780         default:
12781           break;
12782         case STT_FILE:
12783           filename = bfd_asymbol_name (&q->symbol);
12784           break;
12785         case STT_FUNC:
12786         case STT_ARM_TFUNC:
12787         case STT_NOTYPE:
12788           /* Skip mapping symbols.  */
12789           if ((q->symbol.flags & BSF_LOCAL)
12790               && bfd_is_arm_special_symbol_name (q->symbol.name,
12791                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
12792             continue;
12793           /* Fall through.  */
12794           if (bfd_get_section (&q->symbol) == section
12795               && q->symbol.value >= low_func
12796               && q->symbol.value <= offset)
12797             {
12798               func = (asymbol *) q;
12799               low_func = q->symbol.value;
12800             }
12801           break;
12802         }
12803     }
12804
12805   if (func == NULL)
12806     return FALSE;
12807
12808   if (filename_ptr)
12809     *filename_ptr = filename;
12810   if (functionname_ptr)
12811     *functionname_ptr = bfd_asymbol_name (func);
12812
12813   return TRUE;
12814 }
12815
12816
12817 /* Find the nearest line to a particular section and offset, for error
12818    reporting.   This code is a duplicate of the code in elf.c, except
12819    that it uses arm_elf_find_function.  */
12820
12821 static bfd_boolean
12822 elf32_arm_find_nearest_line (bfd *          abfd,
12823                              asection *     section,
12824                              asymbol **     symbols,
12825                              bfd_vma        offset,
12826                              const char **  filename_ptr,
12827                              const char **  functionname_ptr,
12828                              unsigned int * line_ptr)
12829 {
12830   bfd_boolean found = FALSE;
12831
12832   /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
12833
12834   if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12835                                      section, symbols, offset,
12836                                      filename_ptr, functionname_ptr,
12837                                      line_ptr, 0,
12838                                      & elf_tdata (abfd)->dwarf2_find_line_info))
12839     {
12840       if (!*functionname_ptr)
12841         arm_elf_find_function (abfd, section, symbols, offset,
12842                                *filename_ptr ? NULL : filename_ptr,
12843                                functionname_ptr);
12844
12845       return TRUE;
12846     }
12847
12848   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
12849                                              & found, filename_ptr,
12850                                              functionname_ptr, line_ptr,
12851                                              & elf_tdata (abfd)->line_info))
12852     return FALSE;
12853
12854   if (found && (*functionname_ptr || *line_ptr))
12855     return TRUE;
12856
12857   if (symbols == NULL)
12858     return FALSE;
12859
12860   if (! arm_elf_find_function (abfd, section, symbols, offset,
12861                                filename_ptr, functionname_ptr))
12862     return FALSE;
12863
12864   *line_ptr = 0;
12865   return TRUE;
12866 }
12867
12868 static bfd_boolean
12869 elf32_arm_find_inliner_info (bfd *          abfd,
12870                              const char **  filename_ptr,
12871                              const char **  functionname_ptr,
12872                              unsigned int * line_ptr)
12873 {
12874   bfd_boolean found;
12875   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12876                                          functionname_ptr, line_ptr,
12877                                          & elf_tdata (abfd)->dwarf2_find_line_info);
12878   return found;
12879 }
12880
12881 /* Adjust a symbol defined by a dynamic object and referenced by a
12882    regular object.  The current definition is in some section of the
12883    dynamic object, but we're not including those sections.  We have to
12884    change the definition to something the rest of the link can
12885    understand.  */
12886
12887 static bfd_boolean
12888 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
12889                                  struct elf_link_hash_entry * h)
12890 {
12891   bfd * dynobj;
12892   asection * s;
12893   struct elf32_arm_link_hash_entry * eh;
12894   struct elf32_arm_link_hash_table *globals;
12895
12896   globals = elf32_arm_hash_table (info);
12897   if (globals == NULL)
12898     return FALSE;
12899
12900   dynobj = elf_hash_table (info)->dynobj;
12901
12902   /* Make sure we know what is going on here.  */
12903   BFD_ASSERT (dynobj != NULL
12904               && (h->needs_plt
12905                   || h->type == STT_GNU_IFUNC
12906                   || h->u.weakdef != NULL
12907                   || (h->def_dynamic
12908                       && h->ref_regular
12909                       && !h->def_regular)));
12910
12911   eh = (struct elf32_arm_link_hash_entry *) h;
12912
12913   /* If this is a function, put it in the procedure linkage table.  We
12914      will fill in the contents of the procedure linkage table later,
12915      when we know the address of the .got section.  */
12916   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
12917     {
12918       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
12919          symbol binds locally.  */
12920       if (h->plt.refcount <= 0
12921           || (h->type != STT_GNU_IFUNC
12922               && (SYMBOL_CALLS_LOCAL (info, h)
12923                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
12924                       && h->root.type == bfd_link_hash_undefweak))))
12925         {
12926           /* This case can occur if we saw a PLT32 reloc in an input
12927              file, but the symbol was never referred to by a dynamic
12928              object, or if all references were garbage collected.  In
12929              such a case, we don't actually need to build a procedure
12930              linkage table, and we can just do a PC24 reloc instead.  */
12931           h->plt.offset = (bfd_vma) -1;
12932           eh->plt.thumb_refcount = 0;
12933           eh->plt.maybe_thumb_refcount = 0;
12934           eh->plt.noncall_refcount = 0;
12935           h->needs_plt = 0;
12936         }
12937
12938       return TRUE;
12939     }
12940   else
12941     {
12942       /* It's possible that we incorrectly decided a .plt reloc was
12943          needed for an R_ARM_PC24 or similar reloc to a non-function sym
12944          in check_relocs.  We can't decide accurately between function
12945          and non-function syms in check-relocs; Objects loaded later in
12946          the link may change h->type.  So fix it now.  */
12947       h->plt.offset = (bfd_vma) -1;
12948       eh->plt.thumb_refcount = 0;
12949       eh->plt.maybe_thumb_refcount = 0;
12950       eh->plt.noncall_refcount = 0;
12951     }
12952
12953   /* If this is a weak symbol, and there is a real definition, the
12954      processor independent code will have arranged for us to see the
12955      real definition first, and we can just use the same value.  */
12956   if (h->u.weakdef != NULL)
12957     {
12958       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
12959                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
12960       h->root.u.def.section = h->u.weakdef->root.u.def.section;
12961       h->root.u.def.value = h->u.weakdef->root.u.def.value;
12962       return TRUE;
12963     }
12964
12965   /* If there are no non-GOT references, we do not need a copy
12966      relocation.  */
12967   if (!h->non_got_ref)
12968     return TRUE;
12969
12970   /* This is a reference to a symbol defined by a dynamic object which
12971      is not a function.  */
12972
12973   /* If we are creating a shared library, we must presume that the
12974      only references to the symbol are via the global offset table.
12975      For such cases we need not do anything here; the relocations will
12976      be handled correctly by relocate_section.  Relocatable executables
12977      can reference data in shared objects directly, so we don't need to
12978      do anything here.  */
12979   if (info->shared || globals->root.is_relocatable_executable)
12980     return TRUE;
12981
12982   /* We must allocate the symbol in our .dynbss section, which will
12983      become part of the .bss section of the executable.  There will be
12984      an entry for this symbol in the .dynsym section.  The dynamic
12985      object will contain position independent code, so all references
12986      from the dynamic object to this symbol will go through the global
12987      offset table.  The dynamic linker will use the .dynsym entry to
12988      determine the address it must put in the global offset table, so
12989      both the dynamic object and the regular object will refer to the
12990      same memory location for the variable.  */
12991   s = bfd_get_section_by_name (dynobj, ".dynbss");
12992   BFD_ASSERT (s != NULL);
12993
12994   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
12995      copy the initial value out of the dynamic object and into the
12996      runtime process image.  We need to remember the offset into the
12997      .rel(a).bss section we are going to use.  */
12998   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
12999     {
13000       asection *srel;
13001
13002       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (globals, ".bss"));
13003       elf32_arm_allocate_dynrelocs (info, srel, 1);
13004       h->needs_copy = 1;
13005     }
13006
13007   return _bfd_elf_adjust_dynamic_copy (h, s);
13008 }
13009
13010 /* Allocate space in .plt, .got and associated reloc sections for
13011    dynamic relocs.  */
13012
13013 static bfd_boolean
13014 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
13015 {
13016   struct bfd_link_info *info;
13017   struct elf32_arm_link_hash_table *htab;
13018   struct elf32_arm_link_hash_entry *eh;
13019   struct elf_dyn_relocs *p;
13020
13021   if (h->root.type == bfd_link_hash_indirect)
13022     return TRUE;
13023
13024   eh = (struct elf32_arm_link_hash_entry *) h;
13025
13026   info = (struct bfd_link_info *) inf;
13027   htab = elf32_arm_hash_table (info);
13028   if (htab == NULL)
13029     return FALSE;
13030
13031   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
13032       && h->plt.refcount > 0)
13033     {
13034       /* Make sure this symbol is output as a dynamic symbol.
13035          Undefined weak syms won't yet be marked as dynamic.  */
13036       if (h->dynindx == -1
13037           && !h->forced_local)
13038         {
13039           if (! bfd_elf_link_record_dynamic_symbol (info, h))
13040             return FALSE;
13041         }
13042
13043       /* If the call in the PLT entry binds locally, the associated
13044          GOT entry should use an R_ARM_IRELATIVE relocation instead of
13045          the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
13046          than the .plt section.  */
13047       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
13048         {
13049           eh->is_iplt = 1;
13050           if (eh->plt.noncall_refcount == 0
13051               && SYMBOL_REFERENCES_LOCAL (info, h))
13052             /* All non-call references can be resolved directly.
13053                This means that they can (and in some cases, must)
13054                resolve directly to the run-time target, rather than
13055                to the PLT.  That in turns means that any .got entry
13056                would be equal to the .igot.plt entry, so there's
13057                no point having both.  */
13058             h->got.refcount = 0;
13059         }
13060
13061       if (info->shared
13062           || eh->is_iplt
13063           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
13064         {
13065           elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
13066
13067           /* If this symbol is not defined in a regular file, and we are
13068              not generating a shared library, then set the symbol to this
13069              location in the .plt.  This is required to make function
13070              pointers compare as equal between the normal executable and
13071              the shared library.  */
13072           if (! info->shared
13073               && !h->def_regular)
13074             {
13075               h->root.u.def.section = htab->root.splt;
13076               h->root.u.def.value = h->plt.offset;
13077
13078               /* Make sure the function is not marked as Thumb, in case
13079                  it is the target of an ABS32 relocation, which will
13080                  point to the PLT entry.  */
13081               h->target_internal = ST_BRANCH_TO_ARM;
13082             }
13083
13084           htab->next_tls_desc_index++;
13085
13086           /* VxWorks executables have a second set of relocations for
13087              each PLT entry.  They go in a separate relocation section,
13088              which is processed by the kernel loader.  */
13089           if (htab->vxworks_p && !info->shared)
13090             {
13091               /* There is a relocation for the initial PLT entry:
13092                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
13093               if (h->plt.offset == htab->plt_header_size)
13094                 elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
13095
13096               /* There are two extra relocations for each subsequent
13097                  PLT entry: an R_ARM_32 relocation for the GOT entry,
13098                  and an R_ARM_32 relocation for the PLT entry.  */
13099               elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
13100             }
13101         }
13102       else
13103         {
13104           h->plt.offset = (bfd_vma) -1;
13105           h->needs_plt = 0;
13106         }
13107     }
13108   else
13109     {
13110       h->plt.offset = (bfd_vma) -1;
13111       h->needs_plt = 0;
13112     }
13113
13114   eh = (struct elf32_arm_link_hash_entry *) h;
13115   eh->tlsdesc_got = (bfd_vma) -1;
13116
13117   if (h->got.refcount > 0)
13118     {
13119       asection *s;
13120       bfd_boolean dyn;
13121       int tls_type = elf32_arm_hash_entry (h)->tls_type;
13122       int indx;
13123
13124       /* Make sure this symbol is output as a dynamic symbol.
13125          Undefined weak syms won't yet be marked as dynamic.  */
13126       if (h->dynindx == -1
13127           && !h->forced_local)
13128         {
13129           if (! bfd_elf_link_record_dynamic_symbol (info, h))
13130             return FALSE;
13131         }
13132
13133       if (!htab->symbian_p)
13134         {
13135           s = htab->root.sgot;
13136           h->got.offset = s->size;
13137
13138           if (tls_type == GOT_UNKNOWN)
13139             abort ();
13140
13141           if (tls_type == GOT_NORMAL)
13142             /* Non-TLS symbols need one GOT slot.  */
13143             s->size += 4;
13144           else
13145             {
13146               if (tls_type & GOT_TLS_GDESC)
13147                 {
13148                   /* R_ARM_TLS_DESC needs 2 GOT slots.  */
13149                   eh->tlsdesc_got
13150                     = (htab->root.sgotplt->size
13151                        - elf32_arm_compute_jump_table_size (htab));
13152                   htab->root.sgotplt->size += 8;
13153                   h->got.offset = (bfd_vma) -2;
13154                   /* plt.got_offset needs to know there's a TLS_DESC
13155                      reloc in the middle of .got.plt.  */
13156                   htab->num_tls_desc++;
13157                 }
13158
13159               if (tls_type & GOT_TLS_GD)
13160                 {
13161                   /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  If
13162                      the symbol is both GD and GDESC, got.offset may
13163                      have been overwritten.  */
13164                   h->got.offset = s->size;
13165                   s->size += 8;
13166                 }
13167
13168               if (tls_type & GOT_TLS_IE)
13169                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
13170                 s->size += 4;
13171             }
13172
13173           dyn = htab->root.dynamic_sections_created;
13174
13175           indx = 0;
13176           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
13177               && (!info->shared
13178                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
13179             indx = h->dynindx;
13180
13181           if (tls_type != GOT_NORMAL
13182               && (info->shared || indx != 0)
13183               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
13184                   || h->root.type != bfd_link_hash_undefweak))
13185             {
13186               if (tls_type & GOT_TLS_IE)
13187                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13188
13189               if (tls_type & GOT_TLS_GD)
13190                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13191
13192               if (tls_type & GOT_TLS_GDESC)
13193                 {
13194                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
13195                   /* GDESC needs a trampoline to jump to.  */
13196                   htab->tls_trampoline = -1;
13197                 }
13198
13199               /* Only GD needs it.  GDESC just emits one relocation per
13200                  2 entries.  */
13201               if ((tls_type & GOT_TLS_GD) && indx != 0)
13202                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13203             }
13204           else if (!SYMBOL_REFERENCES_LOCAL (info, h))
13205             {
13206               if (htab->root.dynamic_sections_created)
13207                 /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
13208                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13209             }
13210           else if (h->type == STT_GNU_IFUNC
13211                    && eh->plt.noncall_refcount == 0)
13212             /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
13213                they all resolve dynamically instead.  Reserve room for the
13214                GOT entry's R_ARM_IRELATIVE relocation.  */
13215             elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
13216           else if (info->shared)
13217             /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
13218             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13219         }
13220     }
13221   else
13222     h->got.offset = (bfd_vma) -1;
13223
13224   /* Allocate stubs for exported Thumb functions on v4t.  */
13225   if (!htab->use_blx && h->dynindx != -1
13226       && h->def_regular
13227       && h->target_internal == ST_BRANCH_TO_THUMB
13228       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
13229     {
13230       struct elf_link_hash_entry * th;
13231       struct bfd_link_hash_entry * bh;
13232       struct elf_link_hash_entry * myh;
13233       char name[1024];
13234       asection *s;
13235       bh = NULL;
13236       /* Create a new symbol to regist the real location of the function.  */
13237       s = h->root.u.def.section;
13238       sprintf (name, "__real_%s", h->root.root.string);
13239       _bfd_generic_link_add_one_symbol (info, s->owner,
13240                                         name, BSF_GLOBAL, s,
13241                                         h->root.u.def.value,
13242                                         NULL, TRUE, FALSE, &bh);
13243
13244       myh = (struct elf_link_hash_entry *) bh;
13245       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
13246       myh->forced_local = 1;
13247       myh->target_internal = ST_BRANCH_TO_THUMB;
13248       eh->export_glue = myh;
13249       th = record_arm_to_thumb_glue (info, h);
13250       /* Point the symbol at the stub.  */
13251       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
13252       h->target_internal = ST_BRANCH_TO_ARM;
13253       h->root.u.def.section = th->root.u.def.section;
13254       h->root.u.def.value = th->root.u.def.value & ~1;
13255     }
13256
13257   if (eh->dyn_relocs == NULL)
13258     return TRUE;
13259
13260   /* In the shared -Bsymbolic case, discard space allocated for
13261      dynamic pc-relative relocs against symbols which turn out to be
13262      defined in regular objects.  For the normal shared case, discard
13263      space for pc-relative relocs that have become local due to symbol
13264      visibility changes.  */
13265
13266   if (info->shared || htab->root.is_relocatable_executable)
13267     {
13268       /* The only relocs that use pc_count are R_ARM_REL32 and
13269          R_ARM_REL32_NOI, which will appear on something like
13270          ".long foo - .".  We want calls to protected symbols to resolve
13271          directly to the function rather than going via the plt.  If people
13272          want function pointer comparisons to work as expected then they
13273          should avoid writing assembly like ".long foo - .".  */
13274       if (SYMBOL_CALLS_LOCAL (info, h))
13275         {
13276           struct elf_dyn_relocs **pp;
13277
13278           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
13279             {
13280               p->count -= p->pc_count;
13281               p->pc_count = 0;
13282               if (p->count == 0)
13283                 *pp = p->next;
13284               else
13285                 pp = &p->next;
13286             }
13287         }
13288
13289       if (htab->vxworks_p)
13290         {
13291           struct elf_dyn_relocs **pp;
13292
13293           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
13294             {
13295               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
13296                 *pp = p->next;
13297               else
13298                 pp = &p->next;
13299             }
13300         }
13301
13302       /* Also discard relocs on undefined weak syms with non-default
13303          visibility.  */
13304       if (eh->dyn_relocs != NULL
13305           && h->root.type == bfd_link_hash_undefweak)
13306         {
13307           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
13308             eh->dyn_relocs = NULL;
13309
13310           /* Make sure undefined weak symbols are output as a dynamic
13311              symbol in PIEs.  */
13312           else if (h->dynindx == -1
13313                    && !h->forced_local)
13314             {
13315               if (! bfd_elf_link_record_dynamic_symbol (info, h))
13316                 return FALSE;
13317             }
13318         }
13319
13320       else if (htab->root.is_relocatable_executable && h->dynindx == -1
13321                && h->root.type == bfd_link_hash_new)
13322         {
13323           /* Output absolute symbols so that we can create relocations
13324              against them.  For normal symbols we output a relocation
13325              against the section that contains them.  */
13326           if (! bfd_elf_link_record_dynamic_symbol (info, h))
13327             return FALSE;
13328         }
13329
13330     }
13331   else
13332     {
13333       /* For the non-shared case, discard space for relocs against
13334          symbols which turn out to need copy relocs or are not
13335          dynamic.  */
13336
13337       if (!h->non_got_ref
13338           && ((h->def_dynamic
13339                && !h->def_regular)
13340               || (htab->root.dynamic_sections_created
13341                   && (h->root.type == bfd_link_hash_undefweak
13342                       || h->root.type == bfd_link_hash_undefined))))
13343         {
13344           /* Make sure this symbol is output as a dynamic symbol.
13345              Undefined weak syms won't yet be marked as dynamic.  */
13346           if (h->dynindx == -1
13347               && !h->forced_local)
13348             {
13349               if (! bfd_elf_link_record_dynamic_symbol (info, h))
13350                 return FALSE;
13351             }
13352
13353           /* If that succeeded, we know we'll be keeping all the
13354              relocs.  */
13355           if (h->dynindx != -1)
13356             goto keep;
13357         }
13358
13359       eh->dyn_relocs = NULL;
13360
13361     keep: ;
13362     }
13363
13364   /* Finally, allocate space.  */
13365   for (p = eh->dyn_relocs; p != NULL; p = p->next)
13366     {
13367       asection *sreloc = elf_section_data (p->sec)->sreloc;
13368       if (h->type == STT_GNU_IFUNC
13369           && eh->plt.noncall_refcount == 0
13370           && SYMBOL_REFERENCES_LOCAL (info, h))
13371         elf32_arm_allocate_irelocs (info, sreloc, p->count);
13372       else
13373         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
13374     }
13375
13376   return TRUE;
13377 }
13378
13379 /* Find any dynamic relocs that apply to read-only sections.  */
13380
13381 static bfd_boolean
13382 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
13383 {
13384   struct elf32_arm_link_hash_entry * eh;
13385   struct elf_dyn_relocs * p;
13386
13387   eh = (struct elf32_arm_link_hash_entry *) h;
13388   for (p = eh->dyn_relocs; p != NULL; p = p->next)
13389     {
13390       asection *s = p->sec;
13391
13392       if (s != NULL && (s->flags & SEC_READONLY) != 0)
13393         {
13394           struct bfd_link_info *info = (struct bfd_link_info *) inf;
13395
13396           info->flags |= DF_TEXTREL;
13397
13398           /* Not an error, just cut short the traversal.  */
13399           return FALSE;
13400         }
13401     }
13402   return TRUE;
13403 }
13404
13405 void
13406 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
13407                                  int byteswap_code)
13408 {
13409   struct elf32_arm_link_hash_table *globals;
13410
13411   globals = elf32_arm_hash_table (info);
13412   if (globals == NULL)
13413     return;
13414
13415   globals->byteswap_code = byteswap_code;
13416 }
13417
13418 /* Set the sizes of the dynamic sections.  */
13419
13420 static bfd_boolean
13421 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
13422                                  struct bfd_link_info * info)
13423 {
13424   bfd * dynobj;
13425   asection * s;
13426   bfd_boolean plt;
13427   bfd_boolean relocs;
13428   bfd *ibfd;
13429   struct elf32_arm_link_hash_table *htab;
13430
13431   htab = elf32_arm_hash_table (info);
13432   if (htab == NULL)
13433     return FALSE;
13434
13435   dynobj = elf_hash_table (info)->dynobj;
13436   BFD_ASSERT (dynobj != NULL);
13437   check_use_blx (htab);
13438
13439   if (elf_hash_table (info)->dynamic_sections_created)
13440     {
13441       /* Set the contents of the .interp section to the interpreter.  */
13442       if (info->executable)
13443         {
13444           s = bfd_get_section_by_name (dynobj, ".interp");
13445           BFD_ASSERT (s != NULL);
13446           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
13447           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
13448         }
13449     }
13450
13451   /* Set up .got offsets for local syms, and space for local dynamic
13452      relocs.  */
13453   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
13454     {
13455       bfd_signed_vma *local_got;
13456       bfd_signed_vma *end_local_got;
13457       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
13458       char *local_tls_type;
13459       bfd_vma *local_tlsdesc_gotent;
13460       bfd_size_type locsymcount;
13461       Elf_Internal_Shdr *symtab_hdr;
13462       asection *srel;
13463       bfd_boolean is_vxworks = htab->vxworks_p;
13464       unsigned int symndx;
13465
13466       if (! is_arm_elf (ibfd))
13467         continue;
13468
13469       for (s = ibfd->sections; s != NULL; s = s->next)
13470         {
13471           struct elf_dyn_relocs *p;
13472
13473           for (p = (struct elf_dyn_relocs *)
13474                    elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
13475             {
13476               if (!bfd_is_abs_section (p->sec)
13477                   && bfd_is_abs_section (p->sec->output_section))
13478                 {
13479                   /* Input section has been discarded, either because
13480                      it is a copy of a linkonce section or due to
13481                      linker script /DISCARD/, so we'll be discarding
13482                      the relocs too.  */
13483                 }
13484               else if (is_vxworks
13485                        && strcmp (p->sec->output_section->name,
13486                                   ".tls_vars") == 0)
13487                 {
13488                   /* Relocations in vxworks .tls_vars sections are
13489                      handled specially by the loader.  */
13490                 }
13491               else if (p->count != 0)
13492                 {
13493                   srel = elf_section_data (p->sec)->sreloc;
13494                   elf32_arm_allocate_dynrelocs (info, srel, p->count);
13495                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
13496                     info->flags |= DF_TEXTREL;
13497                 }
13498             }
13499         }
13500
13501       local_got = elf_local_got_refcounts (ibfd);
13502       if (!local_got)
13503         continue;
13504
13505       symtab_hdr = & elf_symtab_hdr (ibfd);
13506       locsymcount = symtab_hdr->sh_info;
13507       end_local_got = local_got + locsymcount;
13508       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
13509       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
13510       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
13511       symndx = 0;
13512       s = htab->root.sgot;
13513       srel = htab->root.srelgot;
13514       for (; local_got < end_local_got;
13515            ++local_got, ++local_iplt_ptr, ++local_tls_type,
13516            ++local_tlsdesc_gotent, ++symndx)
13517         {
13518           *local_tlsdesc_gotent = (bfd_vma) -1;
13519           local_iplt = *local_iplt_ptr;
13520           if (local_iplt != NULL)
13521             {
13522               struct elf_dyn_relocs *p;
13523
13524               if (local_iplt->root.refcount > 0)
13525                 {
13526                   elf32_arm_allocate_plt_entry (info, TRUE,
13527                                                 &local_iplt->root,
13528                                                 &local_iplt->arm);
13529                   if (local_iplt->arm.noncall_refcount == 0)
13530                     /* All references to the PLT are calls, so all
13531                        non-call references can resolve directly to the
13532                        run-time target.  This means that the .got entry
13533                        would be the same as the .igot.plt entry, so there's
13534                        no point creating both.  */
13535                     *local_got = 0;
13536                 }
13537               else
13538                 {
13539                   BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
13540                   local_iplt->root.offset = (bfd_vma) -1;
13541                 }
13542
13543               for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
13544                 {
13545                   asection *psrel;
13546
13547                   psrel = elf_section_data (p->sec)->sreloc;
13548                   if (local_iplt->arm.noncall_refcount == 0)
13549                     elf32_arm_allocate_irelocs (info, psrel, p->count);
13550                   else
13551                     elf32_arm_allocate_dynrelocs (info, psrel, p->count);
13552                 }
13553             }
13554           if (*local_got > 0)
13555             {
13556               Elf_Internal_Sym *isym;
13557
13558               *local_got = s->size;
13559               if (*local_tls_type & GOT_TLS_GD)
13560                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
13561                 s->size += 8;
13562               if (*local_tls_type & GOT_TLS_GDESC)
13563                 {
13564                   *local_tlsdesc_gotent = htab->root.sgotplt->size
13565                     - elf32_arm_compute_jump_table_size (htab);
13566                   htab->root.sgotplt->size += 8;
13567                   *local_got = (bfd_vma) -2;
13568                   /* plt.got_offset needs to know there's a TLS_DESC
13569                      reloc in the middle of .got.plt.  */
13570                   htab->num_tls_desc++;
13571                 }
13572               if (*local_tls_type & GOT_TLS_IE)
13573                 s->size += 4;
13574
13575               if (*local_tls_type & GOT_NORMAL)
13576                 {
13577                   /* If the symbol is both GD and GDESC, *local_got
13578                      may have been overwritten.  */
13579                   *local_got = s->size;
13580                   s->size += 4;
13581                 }
13582
13583               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
13584               if (isym == NULL)
13585                 return FALSE;
13586
13587               /* If all references to an STT_GNU_IFUNC PLT are calls,
13588                  then all non-call references, including this GOT entry,
13589                  resolve directly to the run-time target.  */
13590               if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
13591                   && (local_iplt == NULL
13592                       || local_iplt->arm.noncall_refcount == 0))
13593                 elf32_arm_allocate_irelocs (info, srel, 1);
13594               else if ((info->shared && !(*local_tls_type & GOT_TLS_GDESC))
13595                        || *local_tls_type & GOT_TLS_GD)
13596                 elf32_arm_allocate_dynrelocs (info, srel, 1);
13597
13598               if (info->shared && *local_tls_type & GOT_TLS_GDESC)
13599                 {
13600                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
13601                   htab->tls_trampoline = -1;
13602                 }
13603             }
13604           else
13605             *local_got = (bfd_vma) -1;
13606         }
13607     }
13608
13609   if (htab->tls_ldm_got.refcount > 0)
13610     {
13611       /* Allocate two GOT entries and one dynamic relocation (if necessary)
13612          for R_ARM_TLS_LDM32 relocations.  */
13613       htab->tls_ldm_got.offset = htab->root.sgot->size;
13614       htab->root.sgot->size += 8;
13615       if (info->shared)
13616         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
13617     }
13618   else
13619     htab->tls_ldm_got.offset = -1;
13620
13621   /* Allocate global sym .plt and .got entries, and space for global
13622      sym dynamic relocs.  */
13623   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
13624
13625   /* Here we rummage through the found bfds to collect glue information.  */
13626   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
13627     {
13628       if (! is_arm_elf (ibfd))
13629         continue;
13630
13631       /* Initialise mapping tables for code/data.  */
13632       bfd_elf32_arm_init_maps (ibfd);
13633
13634       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
13635           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info))
13636         /* xgettext:c-format */
13637         _bfd_error_handler (_("Errors encountered processing file %s"),
13638                             ibfd->filename);
13639     }
13640
13641   /* Allocate space for the glue sections now that we've sized them.  */
13642   bfd_elf32_arm_allocate_interworking_sections (info);
13643
13644   /* For every jump slot reserved in the sgotplt, reloc_count is
13645      incremented.  However, when we reserve space for TLS descriptors,
13646      it's not incremented, so in order to compute the space reserved
13647      for them, it suffices to multiply the reloc count by the jump
13648      slot size.  */
13649   if (htab->root.srelplt)
13650     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
13651
13652   if (htab->tls_trampoline)
13653     {
13654       if (htab->root.splt->size == 0)
13655         htab->root.splt->size += htab->plt_header_size;
13656
13657       htab->tls_trampoline = htab->root.splt->size;
13658       htab->root.splt->size += htab->plt_entry_size;
13659
13660       /* If we're not using lazy TLS relocations, don't generate the
13661          PLT and GOT entries they require.  */
13662       if (!(info->flags & DF_BIND_NOW))
13663         {
13664           htab->dt_tlsdesc_got = htab->root.sgot->size;
13665           htab->root.sgot->size += 4;
13666
13667           htab->dt_tlsdesc_plt = htab->root.splt->size;
13668           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
13669         }
13670     }
13671
13672   /* The check_relocs and adjust_dynamic_symbol entry points have
13673      determined the sizes of the various dynamic sections.  Allocate
13674      memory for them.  */
13675   plt = FALSE;
13676   relocs = FALSE;
13677   for (s = dynobj->sections; s != NULL; s = s->next)
13678     {
13679       const char * name;
13680
13681       if ((s->flags & SEC_LINKER_CREATED) == 0)
13682         continue;
13683
13684       /* It's OK to base decisions on the section name, because none
13685          of the dynobj section names depend upon the input files.  */
13686       name = bfd_get_section_name (dynobj, s);
13687
13688       if (s == htab->root.splt)
13689         {
13690           /* Remember whether there is a PLT.  */
13691           plt = s->size != 0;
13692         }
13693       else if (CONST_STRNEQ (name, ".rel"))
13694         {
13695           if (s->size != 0)
13696             {
13697               /* Remember whether there are any reloc sections other
13698                  than .rel(a).plt and .rela.plt.unloaded.  */
13699               if (s != htab->root.srelplt && s != htab->srelplt2)
13700                 relocs = TRUE;
13701
13702               /* We use the reloc_count field as a counter if we need
13703                  to copy relocs into the output file.  */
13704               s->reloc_count = 0;
13705             }
13706         }
13707       else if (s != htab->root.sgot
13708                && s != htab->root.sgotplt
13709                && s != htab->root.iplt
13710                && s != htab->root.igotplt
13711                && s != htab->sdynbss)
13712         {
13713           /* It's not one of our sections, so don't allocate space.  */
13714           continue;
13715         }
13716
13717       if (s->size == 0)
13718         {
13719           /* If we don't need this section, strip it from the
13720              output file.  This is mostly to handle .rel(a).bss and
13721              .rel(a).plt.  We must create both sections in
13722              create_dynamic_sections, because they must be created
13723              before the linker maps input sections to output
13724              sections.  The linker does that before
13725              adjust_dynamic_symbol is called, and it is that
13726              function which decides whether anything needs to go
13727              into these sections.  */
13728           s->flags |= SEC_EXCLUDE;
13729           continue;
13730         }
13731
13732       if ((s->flags & SEC_HAS_CONTENTS) == 0)
13733         continue;
13734
13735       /* Allocate memory for the section contents.  */
13736       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
13737       if (s->contents == NULL)
13738         return FALSE;
13739     }
13740
13741   if (elf_hash_table (info)->dynamic_sections_created)
13742     {
13743       /* Add some entries to the .dynamic section.  We fill in the
13744          values later, in elf32_arm_finish_dynamic_sections, but we
13745          must add the entries now so that we get the correct size for
13746          the .dynamic section.  The DT_DEBUG entry is filled in by the
13747          dynamic linker and used by the debugger.  */
13748 #define add_dynamic_entry(TAG, VAL) \
13749   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
13750
13751      if (info->executable)
13752         {
13753           if (!add_dynamic_entry (DT_DEBUG, 0))
13754             return FALSE;
13755         }
13756
13757       if (plt)
13758         {
13759           if (   !add_dynamic_entry (DT_PLTGOT, 0)
13760               || !add_dynamic_entry (DT_PLTRELSZ, 0)
13761               || !add_dynamic_entry (DT_PLTREL,
13762                                      htab->use_rel ? DT_REL : DT_RELA)
13763               || !add_dynamic_entry (DT_JMPREL, 0))
13764             return FALSE;
13765
13766           if (htab->dt_tlsdesc_plt &&
13767                 (!add_dynamic_entry (DT_TLSDESC_PLT,0)
13768                  || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
13769             return FALSE;
13770         }
13771
13772       if (relocs)
13773         {
13774           if (htab->use_rel)
13775             {
13776               if (!add_dynamic_entry (DT_REL, 0)
13777                   || !add_dynamic_entry (DT_RELSZ, 0)
13778                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
13779                 return FALSE;
13780             }
13781           else
13782             {
13783               if (!add_dynamic_entry (DT_RELA, 0)
13784                   || !add_dynamic_entry (DT_RELASZ, 0)
13785                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
13786                 return FALSE;
13787             }
13788         }
13789
13790       /* If any dynamic relocs apply to a read-only section,
13791          then we need a DT_TEXTREL entry.  */
13792       if ((info->flags & DF_TEXTREL) == 0)
13793         elf_link_hash_traverse (& htab->root, elf32_arm_readonly_dynrelocs,
13794                                 info);
13795
13796       if ((info->flags & DF_TEXTREL) != 0)
13797         {
13798           if (!add_dynamic_entry (DT_TEXTREL, 0))
13799             return FALSE;
13800         }
13801       if (htab->vxworks_p
13802           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
13803         return FALSE;
13804     }
13805 #undef add_dynamic_entry
13806
13807   return TRUE;
13808 }
13809
13810 /* Size sections even though they're not dynamic.  We use it to setup
13811    _TLS_MODULE_BASE_, if needed.  */
13812
13813 static bfd_boolean
13814 elf32_arm_always_size_sections (bfd *output_bfd,
13815                                 struct bfd_link_info *info)
13816 {
13817   asection *tls_sec;
13818
13819   if (info->relocatable)
13820     return TRUE;
13821
13822   tls_sec = elf_hash_table (info)->tls_sec;
13823
13824   if (tls_sec)
13825     {
13826       struct elf_link_hash_entry *tlsbase;
13827
13828       tlsbase = elf_link_hash_lookup
13829         (elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
13830
13831       if (tlsbase)
13832         {
13833           struct bfd_link_hash_entry *bh = NULL;
13834           const struct elf_backend_data *bed
13835             = get_elf_backend_data (output_bfd);
13836
13837           if (!(_bfd_generic_link_add_one_symbol
13838                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
13839                  tls_sec, 0, NULL, FALSE,
13840                  bed->collect, &bh)))
13841             return FALSE;
13842
13843           tlsbase->type = STT_TLS;
13844           tlsbase = (struct elf_link_hash_entry *)bh;
13845           tlsbase->def_regular = 1;
13846           tlsbase->other = STV_HIDDEN;
13847           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
13848         }
13849     }
13850   return TRUE;
13851 }
13852
13853 /* Finish up dynamic symbol handling.  We set the contents of various
13854    dynamic sections here.  */
13855
13856 static bfd_boolean
13857 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
13858                                  struct bfd_link_info * info,
13859                                  struct elf_link_hash_entry * h,
13860                                  Elf_Internal_Sym * sym)
13861 {
13862   struct elf32_arm_link_hash_table *htab;
13863   struct elf32_arm_link_hash_entry *eh;
13864
13865   htab = elf32_arm_hash_table (info);
13866   if (htab == NULL)
13867     return FALSE;
13868
13869   eh = (struct elf32_arm_link_hash_entry *) h;
13870
13871   if (h->plt.offset != (bfd_vma) -1)
13872     {
13873       if (!eh->is_iplt)
13874         {
13875           BFD_ASSERT (h->dynindx != -1);
13876           elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
13877                                         h->dynindx, 0);
13878         }
13879
13880       if (!h->def_regular)
13881         {
13882           /* Mark the symbol as undefined, rather than as defined in
13883              the .plt section.  Leave the value alone.  */
13884           sym->st_shndx = SHN_UNDEF;
13885           /* If the symbol is weak, we do need to clear the value.
13886              Otherwise, the PLT entry would provide a definition for
13887              the symbol even if the symbol wasn't defined anywhere,
13888              and so the symbol would never be NULL.  */
13889           if (!h->ref_regular_nonweak)
13890             sym->st_value = 0;
13891         }
13892       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
13893         {
13894           /* At least one non-call relocation references this .iplt entry,
13895              so the .iplt entry is the function's canonical address.  */
13896           sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
13897           sym->st_target_internal = ST_BRANCH_TO_ARM;
13898           sym->st_shndx = (_bfd_elf_section_from_bfd_section
13899                            (output_bfd, htab->root.iplt->output_section));
13900           sym->st_value = (h->plt.offset
13901                            + htab->root.iplt->output_section->vma
13902                            + htab->root.iplt->output_offset);
13903         }
13904     }
13905
13906   if (h->needs_copy)
13907     {
13908       asection * s;
13909       Elf_Internal_Rela rel;
13910
13911       /* This symbol needs a copy reloc.  Set it up.  */
13912       BFD_ASSERT (h->dynindx != -1
13913                   && (h->root.type == bfd_link_hash_defined
13914                       || h->root.type == bfd_link_hash_defweak));
13915
13916       s = htab->srelbss;
13917       BFD_ASSERT (s != NULL);
13918
13919       rel.r_addend = 0;
13920       rel.r_offset = (h->root.u.def.value
13921                       + h->root.u.def.section->output_section->vma
13922                       + h->root.u.def.section->output_offset);
13923       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
13924       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
13925     }
13926
13927   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
13928      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
13929      to the ".got" section.  */
13930   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
13931       || (!htab->vxworks_p && h == htab->root.hgot))
13932     sym->st_shndx = SHN_ABS;
13933
13934   return TRUE;
13935 }
13936
13937 static void
13938 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
13939                     void *contents,
13940                     const unsigned long *template, unsigned count)
13941 {
13942   unsigned ix;
13943
13944   for (ix = 0; ix != count; ix++)
13945     {
13946       unsigned long insn = template[ix];
13947
13948       /* Emit mov pc,rx if bx is not permitted.  */
13949       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
13950         insn = (insn & 0xf000000f) | 0x01a0f000;
13951       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
13952     }
13953 }
13954
13955 /* Finish up the dynamic sections.  */
13956
13957 static bfd_boolean
13958 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
13959 {
13960   bfd * dynobj;
13961   asection * sgot;
13962   asection * sdyn;
13963   struct elf32_arm_link_hash_table *htab;
13964
13965   htab = elf32_arm_hash_table (info);
13966   if (htab == NULL)
13967     return FALSE;
13968
13969   dynobj = elf_hash_table (info)->dynobj;
13970
13971   sgot = htab->root.sgotplt;
13972   /* A broken linker script might have discarded the dynamic sections.
13973      Catch this here so that we do not seg-fault later on.  */
13974   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
13975     return FALSE;
13976   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13977
13978   if (elf_hash_table (info)->dynamic_sections_created)
13979     {
13980       asection *splt;
13981       Elf32_External_Dyn *dyncon, *dynconend;
13982
13983       splt = htab->root.splt;
13984       BFD_ASSERT (splt != NULL && sdyn != NULL);
13985       BFD_ASSERT (htab->symbian_p || sgot != NULL);
13986
13987       dyncon = (Elf32_External_Dyn *) sdyn->contents;
13988       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
13989
13990       for (; dyncon < dynconend; dyncon++)
13991         {
13992           Elf_Internal_Dyn dyn;
13993           const char * name;
13994           asection * s;
13995
13996           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
13997
13998           switch (dyn.d_tag)
13999             {
14000               unsigned int type;
14001
14002             default:
14003               if (htab->vxworks_p
14004                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
14005                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14006               break;
14007
14008             case DT_HASH:
14009               name = ".hash";
14010               goto get_vma_if_bpabi;
14011             case DT_STRTAB:
14012               name = ".dynstr";
14013               goto get_vma_if_bpabi;
14014             case DT_SYMTAB:
14015               name = ".dynsym";
14016               goto get_vma_if_bpabi;
14017             case DT_VERSYM:
14018               name = ".gnu.version";
14019               goto get_vma_if_bpabi;
14020             case DT_VERDEF:
14021               name = ".gnu.version_d";
14022               goto get_vma_if_bpabi;
14023             case DT_VERNEED:
14024               name = ".gnu.version_r";
14025               goto get_vma_if_bpabi;
14026
14027             case DT_PLTGOT:
14028               name = ".got";
14029               goto get_vma;
14030             case DT_JMPREL:
14031               name = RELOC_SECTION (htab, ".plt");
14032             get_vma:
14033               s = bfd_get_section_by_name (output_bfd, name);
14034               BFD_ASSERT (s != NULL);
14035               if (!htab->symbian_p)
14036                 dyn.d_un.d_ptr = s->vma;
14037               else
14038                 /* In the BPABI, tags in the PT_DYNAMIC section point
14039                    at the file offset, not the memory address, for the
14040                    convenience of the post linker.  */
14041                 dyn.d_un.d_ptr = s->filepos;
14042               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14043               break;
14044
14045             get_vma_if_bpabi:
14046               if (htab->symbian_p)
14047                 goto get_vma;
14048               break;
14049
14050             case DT_PLTRELSZ:
14051               s = htab->root.srelplt;
14052               BFD_ASSERT (s != NULL);
14053               dyn.d_un.d_val = s->size;
14054               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14055               break;
14056
14057             case DT_RELSZ:
14058             case DT_RELASZ:
14059               if (!htab->symbian_p)
14060                 {
14061                   /* My reading of the SVR4 ABI indicates that the
14062                      procedure linkage table relocs (DT_JMPREL) should be
14063                      included in the overall relocs (DT_REL).  This is
14064                      what Solaris does.  However, UnixWare can not handle
14065                      that case.  Therefore, we override the DT_RELSZ entry
14066                      here to make it not include the JMPREL relocs.  Since
14067                      the linker script arranges for .rel(a).plt to follow all
14068                      other relocation sections, we don't have to worry
14069                      about changing the DT_REL entry.  */
14070                   s = htab->root.srelplt;
14071                   if (s != NULL)
14072                     dyn.d_un.d_val -= s->size;
14073                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14074                   break;
14075                 }
14076               /* Fall through.  */
14077
14078             case DT_REL:
14079             case DT_RELA:
14080               /* In the BPABI, the DT_REL tag must point at the file
14081                  offset, not the VMA, of the first relocation
14082                  section.  So, we use code similar to that in
14083                  elflink.c, but do not check for SHF_ALLOC on the
14084                  relcoation section, since relocations sections are
14085                  never allocated under the BPABI.  The comments above
14086                  about Unixware notwithstanding, we include all of the
14087                  relocations here.  */
14088               if (htab->symbian_p)
14089                 {
14090                   unsigned int i;
14091                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
14092                           ? SHT_REL : SHT_RELA);
14093                   dyn.d_un.d_val = 0;
14094                   for (i = 1; i < elf_numsections (output_bfd); i++)
14095                     {
14096                       Elf_Internal_Shdr *hdr
14097                         = elf_elfsections (output_bfd)[i];
14098                       if (hdr->sh_type == type)
14099                         {
14100                           if (dyn.d_tag == DT_RELSZ
14101                               || dyn.d_tag == DT_RELASZ)
14102                             dyn.d_un.d_val += hdr->sh_size;
14103                           else if ((ufile_ptr) hdr->sh_offset
14104                                    <= dyn.d_un.d_val - 1)
14105                             dyn.d_un.d_val = hdr->sh_offset;
14106                         }
14107                     }
14108                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14109                 }
14110               break;
14111
14112             case DT_TLSDESC_PLT:
14113               s = htab->root.splt;
14114               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
14115                                 + htab->dt_tlsdesc_plt);
14116               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14117               break;
14118
14119             case DT_TLSDESC_GOT:
14120               s = htab->root.sgot;
14121               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
14122                                 + htab->dt_tlsdesc_got);
14123               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14124               break;
14125
14126               /* Set the bottom bit of DT_INIT/FINI if the
14127                  corresponding function is Thumb.  */
14128             case DT_INIT:
14129               name = info->init_function;
14130               goto get_sym;
14131             case DT_FINI:
14132               name = info->fini_function;
14133             get_sym:
14134               /* If it wasn't set by elf_bfd_final_link
14135                  then there is nothing to adjust.  */
14136               if (dyn.d_un.d_val != 0)
14137                 {
14138                   struct elf_link_hash_entry * eh;
14139
14140                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
14141                                              FALSE, FALSE, TRUE);
14142                   if (eh != NULL && eh->target_internal == ST_BRANCH_TO_THUMB)
14143                     {
14144                       dyn.d_un.d_val |= 1;
14145                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
14146                     }
14147                 }
14148               break;
14149             }
14150         }
14151
14152       /* Fill in the first entry in the procedure linkage table.  */
14153       if (splt->size > 0 && htab->plt_header_size)
14154         {
14155           const bfd_vma *plt0_entry;
14156           bfd_vma got_address, plt_address, got_displacement;
14157
14158           /* Calculate the addresses of the GOT and PLT.  */
14159           got_address = sgot->output_section->vma + sgot->output_offset;
14160           plt_address = splt->output_section->vma + splt->output_offset;
14161
14162           if (htab->vxworks_p)
14163             {
14164               /* The VxWorks GOT is relocated by the dynamic linker.
14165                  Therefore, we must emit relocations rather than simply
14166                  computing the values now.  */
14167               Elf_Internal_Rela rel;
14168
14169               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
14170               put_arm_insn (htab, output_bfd, plt0_entry[0],
14171                             splt->contents + 0);
14172               put_arm_insn (htab, output_bfd, plt0_entry[1],
14173                             splt->contents + 4);
14174               put_arm_insn (htab, output_bfd, plt0_entry[2],
14175                             splt->contents + 8);
14176               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
14177
14178               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
14179               rel.r_offset = plt_address + 12;
14180               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
14181               rel.r_addend = 0;
14182               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
14183                                      htab->srelplt2->contents);
14184             }
14185           else if (htab->nacl_p)
14186             {
14187               unsigned int i;
14188
14189               got_displacement = got_address + 8 - (plt_address + 16);
14190
14191               put_arm_insn (htab, output_bfd,
14192                             elf32_arm_nacl_plt0_entry[0]
14193                             | arm_movw_immediate (got_displacement),
14194                             splt->contents + 0);
14195               put_arm_insn (htab, output_bfd,
14196                             elf32_arm_nacl_plt0_entry[1]
14197                             | arm_movt_immediate (got_displacement),
14198                             splt->contents + 4);
14199               for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
14200                 put_arm_insn (htab, output_bfd,
14201                               elf32_arm_nacl_plt0_entry[i],
14202                               splt->contents + (i * 4));
14203             }
14204           else
14205             {
14206               got_displacement = got_address - (plt_address + 16);
14207
14208               plt0_entry = elf32_arm_plt0_entry;
14209               put_arm_insn (htab, output_bfd, plt0_entry[0],
14210                             splt->contents + 0);
14211               put_arm_insn (htab, output_bfd, plt0_entry[1],
14212                             splt->contents + 4);
14213               put_arm_insn (htab, output_bfd, plt0_entry[2],
14214                             splt->contents + 8);
14215               put_arm_insn (htab, output_bfd, plt0_entry[3],
14216                             splt->contents + 12);
14217
14218 #ifdef FOUR_WORD_PLT
14219               /* The displacement value goes in the otherwise-unused
14220                  last word of the second entry.  */
14221               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
14222 #else
14223               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
14224 #endif
14225             }
14226         }
14227
14228       /* UnixWare sets the entsize of .plt to 4, although that doesn't
14229          really seem like the right value.  */
14230       if (splt->output_section->owner == output_bfd)
14231         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
14232
14233       if (htab->dt_tlsdesc_plt)
14234         {
14235           bfd_vma got_address
14236             = sgot->output_section->vma + sgot->output_offset;
14237           bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
14238                                     + htab->root.sgot->output_offset);
14239           bfd_vma plt_address
14240             = splt->output_section->vma + splt->output_offset;
14241
14242           arm_put_trampoline (htab, output_bfd,
14243                               splt->contents + htab->dt_tlsdesc_plt,
14244                               dl_tlsdesc_lazy_trampoline, 6);
14245
14246           bfd_put_32 (output_bfd,
14247                       gotplt_address + htab->dt_tlsdesc_got
14248                       - (plt_address + htab->dt_tlsdesc_plt)
14249                       - dl_tlsdesc_lazy_trampoline[6],
14250                       splt->contents + htab->dt_tlsdesc_plt + 24);
14251           bfd_put_32 (output_bfd,
14252                       got_address - (plt_address + htab->dt_tlsdesc_plt)
14253                       - dl_tlsdesc_lazy_trampoline[7],
14254                       splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
14255         }
14256
14257       if (htab->tls_trampoline)
14258         {
14259           arm_put_trampoline (htab, output_bfd,
14260                               splt->contents + htab->tls_trampoline,
14261                               tls_trampoline, 3);
14262 #ifdef FOUR_WORD_PLT
14263           bfd_put_32 (output_bfd, 0x00000000,
14264                       splt->contents + htab->tls_trampoline + 12);
14265 #endif
14266         }
14267
14268       if (htab->vxworks_p && !info->shared && htab->root.splt->size > 0)
14269         {
14270           /* Correct the .rel(a).plt.unloaded relocations.  They will have
14271              incorrect symbol indexes.  */
14272           int num_plts;
14273           unsigned char *p;
14274
14275           num_plts = ((htab->root.splt->size - htab->plt_header_size)
14276                       / htab->plt_entry_size);
14277           p = htab->srelplt2->contents + RELOC_SIZE (htab);
14278
14279           for (; num_plts; num_plts--)
14280             {
14281               Elf_Internal_Rela rel;
14282
14283               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
14284               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
14285               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
14286               p += RELOC_SIZE (htab);
14287
14288               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
14289               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
14290               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
14291               p += RELOC_SIZE (htab);
14292             }
14293         }
14294     }
14295
14296   /* Fill in the first three entries in the global offset table.  */
14297   if (sgot)
14298     {
14299       if (sgot->size > 0)
14300         {
14301           if (sdyn == NULL)
14302             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
14303           else
14304             bfd_put_32 (output_bfd,
14305                         sdyn->output_section->vma + sdyn->output_offset,
14306                         sgot->contents);
14307           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
14308           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
14309         }
14310
14311       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
14312     }
14313
14314   return TRUE;
14315 }
14316
14317 static void
14318 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
14319 {
14320   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
14321   struct elf32_arm_link_hash_table *globals;
14322
14323   i_ehdrp = elf_elfheader (abfd);
14324
14325   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
14326     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
14327   else
14328     i_ehdrp->e_ident[EI_OSABI] = 0;
14329   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
14330
14331   if (link_info)
14332     {
14333       globals = elf32_arm_hash_table (link_info);
14334       if (globals != NULL && globals->byteswap_code)
14335         i_ehdrp->e_flags |= EF_ARM_BE8;
14336     }
14337 }
14338
14339 static enum elf_reloc_type_class
14340 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
14341 {
14342   switch ((int) ELF32_R_TYPE (rela->r_info))
14343     {
14344     case R_ARM_RELATIVE:
14345       return reloc_class_relative;
14346     case R_ARM_JUMP_SLOT:
14347       return reloc_class_plt;
14348     case R_ARM_COPY:
14349       return reloc_class_copy;
14350     default:
14351       return reloc_class_normal;
14352     }
14353 }
14354
14355 static void
14356 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
14357 {
14358   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
14359 }
14360
14361 /* Return TRUE if this is an unwinding table entry.  */
14362
14363 static bfd_boolean
14364 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
14365 {
14366   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
14367           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
14368 }
14369
14370
14371 /* Set the type and flags for an ARM section.  We do this by
14372    the section name, which is a hack, but ought to work.  */
14373
14374 static bfd_boolean
14375 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
14376 {
14377   const char * name;
14378
14379   name = bfd_get_section_name (abfd, sec);
14380
14381   if (is_arm_elf_unwind_section_name (abfd, name))
14382     {
14383       hdr->sh_type = SHT_ARM_EXIDX;
14384       hdr->sh_flags |= SHF_LINK_ORDER;
14385     }
14386   return TRUE;
14387 }
14388
14389 /* Handle an ARM specific section when reading an object file.  This is
14390    called when bfd_section_from_shdr finds a section with an unknown
14391    type.  */
14392
14393 static bfd_boolean
14394 elf32_arm_section_from_shdr (bfd *abfd,
14395                              Elf_Internal_Shdr * hdr,
14396                              const char *name,
14397                              int shindex)
14398 {
14399   /* There ought to be a place to keep ELF backend specific flags, but
14400      at the moment there isn't one.  We just keep track of the
14401      sections by their name, instead.  Fortunately, the ABI gives
14402      names for all the ARM specific sections, so we will probably get
14403      away with this.  */
14404   switch (hdr->sh_type)
14405     {
14406     case SHT_ARM_EXIDX:
14407     case SHT_ARM_PREEMPTMAP:
14408     case SHT_ARM_ATTRIBUTES:
14409       break;
14410
14411     default:
14412       return FALSE;
14413     }
14414
14415   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
14416     return FALSE;
14417
14418   return TRUE;
14419 }
14420
14421 static _arm_elf_section_data *
14422 get_arm_elf_section_data (asection * sec)
14423 {
14424   if (sec && sec->owner && is_arm_elf (sec->owner))
14425     return elf32_arm_section_data (sec);
14426   else
14427     return NULL;
14428 }
14429
14430 typedef struct
14431 {
14432   void *finfo;
14433   struct bfd_link_info *info;
14434   asection *sec;
14435   int sec_shndx;
14436   int (*func) (void *, const char *, Elf_Internal_Sym *,
14437                asection *, struct elf_link_hash_entry *);
14438 } output_arch_syminfo;
14439
14440 enum map_symbol_type
14441 {
14442   ARM_MAP_ARM,
14443   ARM_MAP_THUMB,
14444   ARM_MAP_DATA
14445 };
14446
14447
14448 /* Output a single mapping symbol.  */
14449
14450 static bfd_boolean
14451 elf32_arm_output_map_sym (output_arch_syminfo *osi,
14452                           enum map_symbol_type type,
14453                           bfd_vma offset)
14454 {
14455   static const char *names[3] = {"$a", "$t", "$d"};
14456   Elf_Internal_Sym sym;
14457
14458   sym.st_value = osi->sec->output_section->vma
14459                  + osi->sec->output_offset
14460                  + offset;
14461   sym.st_size = 0;
14462   sym.st_other = 0;
14463   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
14464   sym.st_shndx = osi->sec_shndx;
14465   sym.st_target_internal = 0;
14466   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
14467   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
14468 }
14469
14470 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
14471    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
14472
14473 static bfd_boolean
14474 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
14475                             bfd_boolean is_iplt_entry_p,
14476                             union gotplt_union *root_plt,
14477                             struct arm_plt_info *arm_plt)
14478 {
14479   struct elf32_arm_link_hash_table *htab;
14480   bfd_vma addr, plt_header_size;
14481
14482   if (root_plt->offset == (bfd_vma) -1)
14483     return TRUE;
14484
14485   htab = elf32_arm_hash_table (osi->info);
14486   if (htab == NULL)
14487     return FALSE;
14488
14489   if (is_iplt_entry_p)
14490     {
14491       osi->sec = htab->root.iplt;
14492       plt_header_size = 0;
14493     }
14494   else
14495     {
14496       osi->sec = htab->root.splt;
14497       plt_header_size = htab->plt_header_size;
14498     }
14499   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
14500                     (osi->info->output_bfd, osi->sec->output_section));
14501
14502   addr = root_plt->offset & -2;
14503   if (htab->symbian_p)
14504     {
14505       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
14506         return FALSE;
14507       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
14508         return FALSE;
14509     }
14510   else if (htab->vxworks_p)
14511     {
14512       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
14513         return FALSE;
14514       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
14515         return FALSE;
14516       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
14517         return FALSE;
14518       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
14519         return FALSE;
14520     }
14521   else if (htab->nacl_p)
14522     {
14523       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
14524         return FALSE;
14525     }
14526   else
14527     {
14528       bfd_boolean thumb_stub_p;
14529
14530       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
14531       if (thumb_stub_p)
14532         {
14533           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
14534             return FALSE;
14535         }
14536 #ifdef FOUR_WORD_PLT
14537       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
14538         return FALSE;
14539       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
14540         return FALSE;
14541 #else
14542       /* A three-word PLT with no Thumb thunk contains only Arm code,
14543          so only need to output a mapping symbol for the first PLT entry and
14544          entries with thumb thunks.  */
14545       if (thumb_stub_p || addr == plt_header_size)
14546         {
14547           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
14548             return FALSE;
14549         }
14550 #endif
14551     }
14552
14553   return TRUE;
14554 }
14555
14556 /* Output mapping symbols for PLT entries associated with H.  */
14557
14558 static bfd_boolean
14559 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
14560 {
14561   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
14562   struct elf32_arm_link_hash_entry *eh;
14563
14564   if (h->root.type == bfd_link_hash_indirect)
14565     return TRUE;
14566
14567   if (h->root.type == bfd_link_hash_warning)
14568     /* When warning symbols are created, they **replace** the "real"
14569        entry in the hash table, thus we never get to see the real
14570        symbol in a hash traversal.  So look at it now.  */
14571     h = (struct elf_link_hash_entry *) h->root.u.i.link;
14572
14573   eh = (struct elf32_arm_link_hash_entry *) h;
14574   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
14575                                      &h->plt, &eh->plt);
14576 }
14577
14578 /* Output a single local symbol for a generated stub.  */
14579
14580 static bfd_boolean
14581 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
14582                            bfd_vma offset, bfd_vma size)
14583 {
14584   Elf_Internal_Sym sym;
14585
14586   sym.st_value = osi->sec->output_section->vma
14587                  + osi->sec->output_offset
14588                  + offset;
14589   sym.st_size = size;
14590   sym.st_other = 0;
14591   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
14592   sym.st_shndx = osi->sec_shndx;
14593   sym.st_target_internal = 0;
14594   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
14595 }
14596
14597 static bfd_boolean
14598 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
14599                   void * in_arg)
14600 {
14601   struct elf32_arm_stub_hash_entry *stub_entry;
14602   asection *stub_sec;
14603   bfd_vma addr;
14604   char *stub_name;
14605   output_arch_syminfo *osi;
14606   const insn_sequence *template_sequence;
14607   enum stub_insn_type prev_type;
14608   int size;
14609   int i;
14610   enum map_symbol_type sym_type;
14611
14612   /* Massage our args to the form they really have.  */
14613   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
14614   osi = (output_arch_syminfo *) in_arg;
14615
14616   stub_sec = stub_entry->stub_sec;
14617
14618   /* Ensure this stub is attached to the current section being
14619      processed.  */
14620   if (stub_sec != osi->sec)
14621     return TRUE;
14622
14623   addr = (bfd_vma) stub_entry->stub_offset;
14624   stub_name = stub_entry->output_name;
14625
14626   template_sequence = stub_entry->stub_template;
14627   switch (template_sequence[0].type)
14628     {
14629     case ARM_TYPE:
14630       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, stub_entry->stub_size))
14631         return FALSE;
14632       break;
14633     case THUMB16_TYPE:
14634     case THUMB32_TYPE:
14635       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
14636                                       stub_entry->stub_size))
14637         return FALSE;
14638       break;
14639     default:
14640       BFD_FAIL ();
14641       return 0;
14642     }
14643
14644   prev_type = DATA_TYPE;
14645   size = 0;
14646   for (i = 0; i < stub_entry->stub_template_size; i++)
14647     {
14648       switch (template_sequence[i].type)
14649         {
14650         case ARM_TYPE:
14651           sym_type = ARM_MAP_ARM;
14652           break;
14653
14654         case THUMB16_TYPE:
14655         case THUMB32_TYPE:
14656           sym_type = ARM_MAP_THUMB;
14657           break;
14658
14659         case DATA_TYPE:
14660           sym_type = ARM_MAP_DATA;
14661           break;
14662
14663         default:
14664           BFD_FAIL ();
14665           return FALSE;
14666         }
14667
14668       if (template_sequence[i].type != prev_type)
14669         {
14670           prev_type = template_sequence[i].type;
14671           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
14672             return FALSE;
14673         }
14674
14675       switch (template_sequence[i].type)
14676         {
14677         case ARM_TYPE:
14678         case THUMB32_TYPE:
14679           size += 4;
14680           break;
14681
14682         case THUMB16_TYPE:
14683           size += 2;
14684           break;
14685
14686         case DATA_TYPE:
14687           size += 4;
14688           break;
14689
14690         default:
14691           BFD_FAIL ();
14692           return FALSE;
14693         }
14694     }
14695
14696   return TRUE;
14697 }
14698
14699 /* Output mapping symbols for linker generated sections,
14700    and for those data-only sections that do not have a
14701    $d.  */
14702
14703 static bfd_boolean
14704 elf32_arm_output_arch_local_syms (bfd *output_bfd,
14705                                   struct bfd_link_info *info,
14706                                   void *finfo,
14707                                   int (*func) (void *, const char *,
14708                                                Elf_Internal_Sym *,
14709                                                asection *,
14710                                                struct elf_link_hash_entry *))
14711 {
14712   output_arch_syminfo osi;
14713   struct elf32_arm_link_hash_table *htab;
14714   bfd_vma offset;
14715   bfd_size_type size;
14716   bfd *input_bfd;
14717
14718   htab = elf32_arm_hash_table (info);
14719   if (htab == NULL)
14720     return FALSE;
14721
14722   check_use_blx (htab);
14723
14724   osi.finfo = finfo;
14725   osi.info = info;
14726   osi.func = func;
14727
14728   /* Add a $d mapping symbol to data-only sections that
14729      don't have any mapping symbol.  This may result in (harmless) redundant
14730      mapping symbols.  */
14731   for (input_bfd = info->input_bfds;
14732        input_bfd != NULL;
14733        input_bfd = input_bfd->link_next)
14734     {
14735       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
14736         for (osi.sec = input_bfd->sections;
14737              osi.sec != NULL;
14738              osi.sec = osi.sec->next)
14739           {
14740             if (osi.sec->output_section != NULL
14741                 && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
14742                     != 0)
14743                 && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
14744                    == SEC_HAS_CONTENTS
14745                 && get_arm_elf_section_data (osi.sec) != NULL
14746                 && get_arm_elf_section_data (osi.sec)->mapcount == 0
14747                 && osi.sec->size > 0
14748                 && (osi.sec->flags & SEC_EXCLUDE) == 0)
14749               {
14750                 osi.sec_shndx = _bfd_elf_section_from_bfd_section
14751                   (output_bfd, osi.sec->output_section);
14752                 if (osi.sec_shndx != (int)SHN_BAD)
14753                   elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
14754               }
14755           }
14756     }
14757
14758   /* ARM->Thumb glue.  */
14759   if (htab->arm_glue_size > 0)
14760     {
14761       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
14762                                          ARM2THUMB_GLUE_SECTION_NAME);
14763
14764       osi.sec_shndx = _bfd_elf_section_from_bfd_section
14765           (output_bfd, osi.sec->output_section);
14766       if (info->shared || htab->root.is_relocatable_executable
14767           || htab->pic_veneer)
14768         size = ARM2THUMB_PIC_GLUE_SIZE;
14769       else if (htab->use_blx)
14770         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
14771       else
14772         size = ARM2THUMB_STATIC_GLUE_SIZE;
14773
14774       for (offset = 0; offset < htab->arm_glue_size; offset += size)
14775         {
14776           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
14777           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
14778         }
14779     }
14780
14781   /* Thumb->ARM glue.  */
14782   if (htab->thumb_glue_size > 0)
14783     {
14784       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
14785                                          THUMB2ARM_GLUE_SECTION_NAME);
14786
14787       osi.sec_shndx = _bfd_elf_section_from_bfd_section
14788           (output_bfd, osi.sec->output_section);
14789       size = THUMB2ARM_GLUE_SIZE;
14790
14791       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
14792         {
14793           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
14794           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
14795         }
14796     }
14797
14798   /* ARMv4 BX veneers.  */
14799   if (htab->bx_glue_size > 0)
14800     {
14801       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
14802                                          ARM_BX_GLUE_SECTION_NAME);
14803
14804       osi.sec_shndx = _bfd_elf_section_from_bfd_section
14805           (output_bfd, osi.sec->output_section);
14806
14807       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
14808     }
14809
14810   /* Long calls stubs.  */
14811   if (htab->stub_bfd && htab->stub_bfd->sections)
14812     {
14813       asection* stub_sec;
14814
14815       for (stub_sec = htab->stub_bfd->sections;
14816            stub_sec != NULL;
14817            stub_sec = stub_sec->next)
14818         {
14819           /* Ignore non-stub sections.  */
14820           if (!strstr (stub_sec->name, STUB_SUFFIX))
14821             continue;
14822
14823           osi.sec = stub_sec;
14824
14825           osi.sec_shndx = _bfd_elf_section_from_bfd_section
14826             (output_bfd, osi.sec->output_section);
14827
14828           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
14829         }
14830     }
14831
14832   /* Finally, output mapping symbols for the PLT.  */
14833   if (htab->root.splt && htab->root.splt->size > 0)
14834     {
14835       osi.sec = htab->root.splt;
14836       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
14837                        (output_bfd, osi.sec->output_section));
14838
14839       /* Output mapping symbols for the plt header.  SymbianOS does not have a
14840          plt header.  */
14841       if (htab->vxworks_p)
14842         {
14843           /* VxWorks shared libraries have no PLT header.  */
14844           if (!info->shared)
14845             {
14846               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
14847                 return FALSE;
14848               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
14849                 return FALSE;
14850             }
14851         }
14852       else if (htab->nacl_p)
14853         {
14854           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
14855             return FALSE;
14856         }
14857       else if (!htab->symbian_p)
14858         {
14859           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
14860             return FALSE;
14861 #ifndef FOUR_WORD_PLT
14862           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
14863             return FALSE;
14864 #endif
14865         }
14866     }
14867   if ((htab->root.splt && htab->root.splt->size > 0)
14868       || (htab->root.iplt && htab->root.iplt->size > 0))
14869     {
14870       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
14871       for (input_bfd = info->input_bfds;
14872            input_bfd != NULL;
14873            input_bfd = input_bfd->link_next)
14874         {
14875           struct arm_local_iplt_info **local_iplt;
14876           unsigned int i, num_syms;
14877
14878           local_iplt = elf32_arm_local_iplt (input_bfd);
14879           if (local_iplt != NULL)
14880             {
14881               num_syms = elf_symtab_hdr (input_bfd).sh_info;
14882               for (i = 0; i < num_syms; i++)
14883                 if (local_iplt[i] != NULL
14884                     && !elf32_arm_output_plt_map_1 (&osi, TRUE,
14885                                                     &local_iplt[i]->root,
14886                                                     &local_iplt[i]->arm))
14887                   return FALSE;
14888             }
14889         }
14890     }
14891   if (htab->dt_tlsdesc_plt != 0)
14892     {
14893       /* Mapping symbols for the lazy tls trampoline.  */
14894       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
14895         return FALSE;
14896
14897       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
14898                                      htab->dt_tlsdesc_plt + 24))
14899         return FALSE;
14900     }
14901   if (htab->tls_trampoline != 0)
14902     {
14903       /* Mapping symbols for the tls trampoline.  */
14904       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
14905         return FALSE;
14906 #ifdef FOUR_WORD_PLT
14907       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
14908                                      htab->tls_trampoline + 12))
14909         return FALSE;
14910 #endif
14911     }
14912
14913   return TRUE;
14914 }
14915
14916 /* Allocate target specific section data.  */
14917
14918 static bfd_boolean
14919 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
14920 {
14921   if (!sec->used_by_bfd)
14922     {
14923       _arm_elf_section_data *sdata;
14924       bfd_size_type amt = sizeof (*sdata);
14925
14926       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
14927       if (sdata == NULL)
14928         return FALSE;
14929       sec->used_by_bfd = sdata;
14930     }
14931
14932   return _bfd_elf_new_section_hook (abfd, sec);
14933 }
14934
14935
14936 /* Used to order a list of mapping symbols by address.  */
14937
14938 static int
14939 elf32_arm_compare_mapping (const void * a, const void * b)
14940 {
14941   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
14942   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
14943
14944   if (amap->vma > bmap->vma)
14945     return 1;
14946   else if (amap->vma < bmap->vma)
14947     return -1;
14948   else if (amap->type > bmap->type)
14949     /* Ensure results do not depend on the host qsort for objects with
14950        multiple mapping symbols at the same address by sorting on type
14951        after vma.  */
14952     return 1;
14953   else if (amap->type < bmap->type)
14954     return -1;
14955   else
14956     return 0;
14957 }
14958
14959 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
14960
14961 static unsigned long
14962 offset_prel31 (unsigned long addr, bfd_vma offset)
14963 {
14964   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
14965 }
14966
14967 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
14968    relocations.  */
14969
14970 static void
14971 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
14972 {
14973   unsigned long first_word = bfd_get_32 (output_bfd, from);
14974   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
14975
14976   /* High bit of first word is supposed to be zero.  */
14977   if ((first_word & 0x80000000ul) == 0)
14978     first_word = offset_prel31 (first_word, offset);
14979
14980   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
14981      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
14982   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
14983     second_word = offset_prel31 (second_word, offset);
14984
14985   bfd_put_32 (output_bfd, first_word, to);
14986   bfd_put_32 (output_bfd, second_word, to + 4);
14987 }
14988
14989 /* Data for make_branch_to_a8_stub().  */
14990
14991 struct a8_branch_to_stub_data
14992 {
14993   asection *writing_section;
14994   bfd_byte *contents;
14995 };
14996
14997
14998 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
14999    places for a particular section.  */
15000
15001 static bfd_boolean
15002 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
15003                        void *in_arg)
15004 {
15005   struct elf32_arm_stub_hash_entry *stub_entry;
15006   struct a8_branch_to_stub_data *data;
15007   bfd_byte *contents;
15008   unsigned long branch_insn;
15009   bfd_vma veneered_insn_loc, veneer_entry_loc;
15010   bfd_signed_vma branch_offset;
15011   bfd *abfd;
15012   unsigned int target;
15013
15014   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
15015   data = (struct a8_branch_to_stub_data *) in_arg;
15016
15017   if (stub_entry->target_section != data->writing_section
15018       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
15019     return TRUE;
15020
15021   contents = data->contents;
15022
15023   veneered_insn_loc = stub_entry->target_section->output_section->vma
15024                       + stub_entry->target_section->output_offset
15025                       + stub_entry->target_value;
15026
15027   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
15028                      + stub_entry->stub_sec->output_offset
15029                      + stub_entry->stub_offset;
15030
15031   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
15032     veneered_insn_loc &= ~3u;
15033
15034   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
15035
15036   abfd = stub_entry->target_section->owner;
15037   target = stub_entry->target_value;
15038
15039   /* We attempt to avoid this condition by setting stubs_always_after_branch
15040      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
15041      This check is just to be on the safe side...  */
15042   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
15043     {
15044       (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub is "
15045                                "allocated in unsafe location"), abfd);
15046       return FALSE;
15047     }
15048
15049   switch (stub_entry->stub_type)
15050     {
15051     case arm_stub_a8_veneer_b:
15052     case arm_stub_a8_veneer_b_cond:
15053       branch_insn = 0xf0009000;
15054       goto jump24;
15055
15056     case arm_stub_a8_veneer_blx:
15057       branch_insn = 0xf000e800;
15058       goto jump24;
15059
15060     case arm_stub_a8_veneer_bl:
15061       {
15062         unsigned int i1, j1, i2, j2, s;
15063
15064         branch_insn = 0xf000d000;
15065
15066       jump24:
15067         if (branch_offset < -16777216 || branch_offset > 16777214)
15068           {
15069             /* There's not much we can do apart from complain if this
15070                happens.  */
15071             (*_bfd_error_handler) (_("%B: error: Cortex-A8 erratum stub out "
15072                                      "of range (input file too large)"), abfd);
15073             return FALSE;
15074           }
15075
15076         /* i1 = not(j1 eor s), so:
15077            not i1 = j1 eor s
15078            j1 = (not i1) eor s.  */
15079
15080         branch_insn |= (branch_offset >> 1) & 0x7ff;
15081         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
15082         i2 = (branch_offset >> 22) & 1;
15083         i1 = (branch_offset >> 23) & 1;
15084         s = (branch_offset >> 24) & 1;
15085         j1 = (!i1) ^ s;
15086         j2 = (!i2) ^ s;
15087         branch_insn |= j2 << 11;
15088         branch_insn |= j1 << 13;
15089         branch_insn |= s << 26;
15090       }
15091       break;
15092
15093     default:
15094       BFD_FAIL ();
15095       return FALSE;
15096     }
15097
15098   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[target]);
15099   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[target + 2]);
15100
15101   return TRUE;
15102 }
15103
15104 /* Do code byteswapping.  Return FALSE afterwards so that the section is
15105    written out as normal.  */
15106
15107 static bfd_boolean
15108 elf32_arm_write_section (bfd *output_bfd,
15109                          struct bfd_link_info *link_info,
15110                          asection *sec,
15111                          bfd_byte *contents)
15112 {
15113   unsigned int mapcount, errcount;
15114   _arm_elf_section_data *arm_data;
15115   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
15116   elf32_arm_section_map *map;
15117   elf32_vfp11_erratum_list *errnode;
15118   bfd_vma ptr;
15119   bfd_vma end;
15120   bfd_vma offset = sec->output_section->vma + sec->output_offset;
15121   bfd_byte tmp;
15122   unsigned int i;
15123
15124   if (globals == NULL)
15125     return FALSE;
15126
15127   /* If this section has not been allocated an _arm_elf_section_data
15128      structure then we cannot record anything.  */
15129   arm_data = get_arm_elf_section_data (sec);
15130   if (arm_data == NULL)
15131     return FALSE;
15132
15133   mapcount = arm_data->mapcount;
15134   map = arm_data->map;
15135   errcount = arm_data->erratumcount;
15136
15137   if (errcount != 0)
15138     {
15139       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
15140
15141       for (errnode = arm_data->erratumlist; errnode != 0;
15142            errnode = errnode->next)
15143         {
15144           bfd_vma target = errnode->vma - offset;
15145
15146           switch (errnode->type)
15147             {
15148             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
15149               {
15150                 bfd_vma branch_to_veneer;
15151                 /* Original condition code of instruction, plus bit mask for
15152                    ARM B instruction.  */
15153                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
15154                                   | 0x0a000000;
15155
15156                 /* The instruction is before the label.  */
15157                 target -= 4;
15158
15159                 /* Above offset included in -4 below.  */
15160                 branch_to_veneer = errnode->u.b.veneer->vma
15161                                    - errnode->vma - 4;
15162
15163                 if ((signed) branch_to_veneer < -(1 << 25)
15164                     || (signed) branch_to_veneer >= (1 << 25))
15165                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
15166                                            "range"), output_bfd);
15167
15168                 insn |= (branch_to_veneer >> 2) & 0xffffff;
15169                 contents[endianflip ^ target] = insn & 0xff;
15170                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
15171                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
15172                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
15173               }
15174               break;
15175
15176             case VFP11_ERRATUM_ARM_VENEER:
15177               {
15178                 bfd_vma branch_from_veneer;
15179                 unsigned int insn;
15180
15181                 /* Take size of veneer into account.  */
15182                 branch_from_veneer = errnode->u.v.branch->vma
15183                                      - errnode->vma - 12;
15184
15185                 if ((signed) branch_from_veneer < -(1 << 25)
15186                     || (signed) branch_from_veneer >= (1 << 25))
15187                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
15188                                            "range"), output_bfd);
15189
15190                 /* Original instruction.  */
15191                 insn = errnode->u.v.branch->u.b.vfp_insn;
15192                 contents[endianflip ^ target] = insn & 0xff;
15193                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
15194                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
15195                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
15196
15197                 /* Branch back to insn after original insn.  */
15198                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
15199                 contents[endianflip ^ (target + 4)] = insn & 0xff;
15200                 contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
15201                 contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
15202                 contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
15203               }
15204               break;
15205
15206             default:
15207               abort ();
15208             }
15209         }
15210     }
15211
15212   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
15213     {
15214       arm_unwind_table_edit *edit_node
15215         = arm_data->u.exidx.unwind_edit_list;
15216       /* Now, sec->size is the size of the section we will write.  The original
15217          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
15218          markers) was sec->rawsize.  (This isn't the case if we perform no
15219          edits, then rawsize will be zero and we should use size).  */
15220       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
15221       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
15222       unsigned int in_index, out_index;
15223       bfd_vma add_to_offsets = 0;
15224
15225       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
15226         {
15227           if (edit_node)
15228             {
15229               unsigned int edit_index = edit_node->index;
15230
15231               if (in_index < edit_index && in_index * 8 < input_size)
15232                 {
15233                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
15234                                     contents + in_index * 8, add_to_offsets);
15235                   out_index++;
15236                   in_index++;
15237                 }
15238               else if (in_index == edit_index
15239                        || (in_index * 8 >= input_size
15240                            && edit_index == UINT_MAX))
15241                 {
15242                   switch (edit_node->type)
15243                     {
15244                     case DELETE_EXIDX_ENTRY:
15245                       in_index++;
15246                       add_to_offsets += 8;
15247                       break;
15248
15249                     case INSERT_EXIDX_CANTUNWIND_AT_END:
15250                       {
15251                         asection *text_sec = edit_node->linked_section;
15252                         bfd_vma text_offset = text_sec->output_section->vma
15253                                               + text_sec->output_offset
15254                                               + text_sec->size;
15255                         bfd_vma exidx_offset = offset + out_index * 8;
15256                         unsigned long prel31_offset;
15257
15258                         /* Note: this is meant to be equivalent to an
15259                            R_ARM_PREL31 relocation.  These synthetic
15260                            EXIDX_CANTUNWIND markers are not relocated by the
15261                            usual BFD method.  */
15262                         prel31_offset = (text_offset - exidx_offset)
15263                                         & 0x7ffffffful;
15264
15265                         /* First address we can't unwind.  */
15266                         bfd_put_32 (output_bfd, prel31_offset,
15267                                     &edited_contents[out_index * 8]);
15268
15269                         /* Code for EXIDX_CANTUNWIND.  */
15270                         bfd_put_32 (output_bfd, 0x1,
15271                                     &edited_contents[out_index * 8 + 4]);
15272
15273                         out_index++;
15274                         add_to_offsets -= 8;
15275                       }
15276                       break;
15277                     }
15278
15279                   edit_node = edit_node->next;
15280                 }
15281             }
15282           else
15283             {
15284               /* No more edits, copy remaining entries verbatim.  */
15285               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
15286                                 contents + in_index * 8, add_to_offsets);
15287               out_index++;
15288               in_index++;
15289             }
15290         }
15291
15292       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
15293         bfd_set_section_contents (output_bfd, sec->output_section,
15294                                   edited_contents,
15295                                   (file_ptr) sec->output_offset, sec->size);
15296
15297       return TRUE;
15298     }
15299
15300   /* Fix code to point to Cortex-A8 erratum stubs.  */
15301   if (globals->fix_cortex_a8)
15302     {
15303       struct a8_branch_to_stub_data data;
15304
15305       data.writing_section = sec;
15306       data.contents = contents;
15307
15308       bfd_hash_traverse (&globals->stub_hash_table, make_branch_to_a8_stub,
15309                          &data);
15310     }
15311
15312   if (mapcount == 0)
15313     return FALSE;
15314
15315   if (globals->byteswap_code)
15316     {
15317       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
15318
15319       ptr = map[0].vma;
15320       for (i = 0; i < mapcount; i++)
15321         {
15322           if (i == mapcount - 1)
15323             end = sec->size;
15324           else
15325             end = map[i + 1].vma;
15326
15327           switch (map[i].type)
15328             {
15329             case 'a':
15330               /* Byte swap code words.  */
15331               while (ptr + 3 < end)
15332                 {
15333                   tmp = contents[ptr];
15334                   contents[ptr] = contents[ptr + 3];
15335                   contents[ptr + 3] = tmp;
15336                   tmp = contents[ptr + 1];
15337                   contents[ptr + 1] = contents[ptr + 2];
15338                   contents[ptr + 2] = tmp;
15339                   ptr += 4;
15340                 }
15341               break;
15342
15343             case 't':
15344               /* Byte swap code halfwords.  */
15345               while (ptr + 1 < end)
15346                 {
15347                   tmp = contents[ptr];
15348                   contents[ptr] = contents[ptr + 1];
15349                   contents[ptr + 1] = tmp;
15350                   ptr += 2;
15351                 }
15352               break;
15353
15354             case 'd':
15355               /* Leave data alone.  */
15356               break;
15357             }
15358           ptr = end;
15359         }
15360     }
15361
15362   free (map);
15363   arm_data->mapcount = -1;
15364   arm_data->mapsize = 0;
15365   arm_data->map = NULL;
15366
15367   return FALSE;
15368 }
15369
15370 /* Mangle thumb function symbols as we read them in.  */
15371
15372 static bfd_boolean
15373 elf32_arm_swap_symbol_in (bfd * abfd,
15374                           const void *psrc,
15375                           const void *pshn,
15376                           Elf_Internal_Sym *dst)
15377 {
15378   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
15379     return FALSE;
15380
15381   /* New EABI objects mark thumb function symbols by setting the low bit of
15382      the address.  */
15383   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
15384       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
15385     {
15386       if (dst->st_value & 1)
15387         {
15388           dst->st_value &= ~(bfd_vma) 1;
15389           dst->st_target_internal = ST_BRANCH_TO_THUMB;
15390         }
15391       else
15392         dst->st_target_internal = ST_BRANCH_TO_ARM;
15393     }
15394   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
15395     {
15396       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
15397       dst->st_target_internal = ST_BRANCH_TO_THUMB;
15398     }
15399   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
15400     dst->st_target_internal = ST_BRANCH_LONG;
15401   else
15402     dst->st_target_internal = ST_BRANCH_UNKNOWN;
15403
15404   return TRUE;
15405 }
15406
15407
15408 /* Mangle thumb function symbols as we write them out.  */
15409
15410 static void
15411 elf32_arm_swap_symbol_out (bfd *abfd,
15412                            const Elf_Internal_Sym *src,
15413                            void *cdst,
15414                            void *shndx)
15415 {
15416   Elf_Internal_Sym newsym;
15417
15418   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
15419      of the address set, as per the new EABI.  We do this unconditionally
15420      because objcopy does not set the elf header flags until after
15421      it writes out the symbol table.  */
15422   if (src->st_target_internal == ST_BRANCH_TO_THUMB)
15423     {
15424       newsym = *src;
15425       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
15426         newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
15427       if (newsym.st_shndx != SHN_UNDEF)
15428         {
15429           /* Do this only for defined symbols. At link type, the static
15430              linker will simulate the work of dynamic linker of resolving
15431              symbols and will carry over the thumbness of found symbols to
15432              the output symbol table. It's not clear how it happens, but
15433              the thumbness of undefined symbols can well be different at
15434              runtime, and writing '1' for them will be confusing for users
15435              and possibly for dynamic linker itself.
15436           */
15437           newsym.st_value |= 1;
15438         }
15439
15440       src = &newsym;
15441     }
15442   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
15443 }
15444
15445 /* Add the PT_ARM_EXIDX program header.  */
15446
15447 static bfd_boolean
15448 elf32_arm_modify_segment_map (bfd *abfd,
15449                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
15450 {
15451   struct elf_segment_map *m;
15452   asection *sec;
15453
15454   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
15455   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
15456     {
15457       /* If there is already a PT_ARM_EXIDX header, then we do not
15458          want to add another one.  This situation arises when running
15459          "strip"; the input binary already has the header.  */
15460       m = elf_tdata (abfd)->segment_map;
15461       while (m && m->p_type != PT_ARM_EXIDX)
15462         m = m->next;
15463       if (!m)
15464         {
15465           m = (struct elf_segment_map *)
15466               bfd_zalloc (abfd, sizeof (struct elf_segment_map));
15467           if (m == NULL)
15468             return FALSE;
15469           m->p_type = PT_ARM_EXIDX;
15470           m->count = 1;
15471           m->sections[0] = sec;
15472
15473           m->next = elf_tdata (abfd)->segment_map;
15474           elf_tdata (abfd)->segment_map = m;
15475         }
15476     }
15477
15478   return TRUE;
15479 }
15480
15481 /* We may add a PT_ARM_EXIDX program header.  */
15482
15483 static int
15484 elf32_arm_additional_program_headers (bfd *abfd,
15485                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
15486 {
15487   asection *sec;
15488
15489   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
15490   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
15491     return 1;
15492   else
15493     return 0;
15494 }
15495
15496 /* Hook called by the linker routine which adds symbols from an object
15497    file.  */
15498
15499 static bfd_boolean
15500 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
15501                            Elf_Internal_Sym *sym, const char **namep,
15502                            flagword *flagsp, asection **secp, bfd_vma *valp)
15503 {
15504   if ((abfd->flags & DYNAMIC) == 0
15505       && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
15506           || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
15507     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
15508
15509   if (elf32_arm_hash_table (info)->vxworks_p
15510       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
15511                                        flagsp, secp, valp))
15512     return FALSE;
15513
15514   return TRUE;
15515 }
15516
15517 /* We use this to override swap_symbol_in and swap_symbol_out.  */
15518 const struct elf_size_info elf32_arm_size_info =
15519 {
15520   sizeof (Elf32_External_Ehdr),
15521   sizeof (Elf32_External_Phdr),
15522   sizeof (Elf32_External_Shdr),
15523   sizeof (Elf32_External_Rel),
15524   sizeof (Elf32_External_Rela),
15525   sizeof (Elf32_External_Sym),
15526   sizeof (Elf32_External_Dyn),
15527   sizeof (Elf_External_Note),
15528   4,
15529   1,
15530   32, 2,
15531   ELFCLASS32, EV_CURRENT,
15532   bfd_elf32_write_out_phdrs,
15533   bfd_elf32_write_shdrs_and_ehdr,
15534   bfd_elf32_checksum_contents,
15535   bfd_elf32_write_relocs,
15536   elf32_arm_swap_symbol_in,
15537   elf32_arm_swap_symbol_out,
15538   bfd_elf32_slurp_reloc_table,
15539   bfd_elf32_slurp_symbol_table,
15540   bfd_elf32_swap_dyn_in,
15541   bfd_elf32_swap_dyn_out,
15542   bfd_elf32_swap_reloc_in,
15543   bfd_elf32_swap_reloc_out,
15544   bfd_elf32_swap_reloca_in,
15545   bfd_elf32_swap_reloca_out
15546 };
15547
15548 #define ELF_ARCH                        bfd_arch_arm
15549 #define ELF_TARGET_ID                   ARM_ELF_DATA
15550 #define ELF_MACHINE_CODE                EM_ARM
15551 #ifdef __QNXTARGET__
15552 #define ELF_MAXPAGESIZE                 0x1000
15553 #else
15554 #define ELF_MAXPAGESIZE                 0x8000
15555 #endif
15556 #define ELF_MINPAGESIZE                 0x1000
15557 #define ELF_COMMONPAGESIZE              0x1000
15558
15559 #define bfd_elf32_mkobject                      elf32_arm_mkobject
15560
15561 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
15562 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
15563 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
15564 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
15565 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
15566 #define bfd_elf32_bfd_link_hash_table_free      elf32_arm_hash_table_free
15567 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
15568 #define bfd_elf32_bfd_reloc_name_lookup         elf32_arm_reloc_name_lookup
15569 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
15570 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
15571 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
15572 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
15573 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
15574
15575 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
15576 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
15577 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
15578 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
15579 #define elf_backend_check_relocs                elf32_arm_check_relocs
15580 #define elf_backend_relocate_section            elf32_arm_relocate_section
15581 #define elf_backend_write_section               elf32_arm_write_section
15582 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
15583 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
15584 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
15585 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
15586 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
15587 #define elf_backend_always_size_sections        elf32_arm_always_size_sections
15588 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
15589 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
15590 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
15591 #define elf_backend_object_p                    elf32_arm_object_p
15592 #define elf_backend_fake_sections               elf32_arm_fake_sections
15593 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
15594 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
15595 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
15596 #define elf_backend_size_info                   elf32_arm_size_info
15597 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
15598 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
15599 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
15600 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
15601 #define elf_backend_add_symbol_hook             elf32_arm_add_symbol_hook
15602
15603 #define elf_backend_can_refcount       1
15604 #define elf_backend_can_gc_sections    1
15605 #define elf_backend_plt_readonly       1
15606 #define elf_backend_want_got_plt       1
15607 #define elf_backend_want_plt_sym       0
15608 #define elf_backend_may_use_rel_p      1
15609 #define elf_backend_may_use_rela_p     0
15610 #define elf_backend_default_use_rela_p 0
15611
15612 #define elf_backend_got_header_size     12
15613
15614 #undef  elf_backend_obj_attrs_vendor
15615 #define elf_backend_obj_attrs_vendor            "aeabi"
15616 #undef  elf_backend_obj_attrs_section
15617 #define elf_backend_obj_attrs_section           ".ARM.attributes"
15618 #undef  elf_backend_obj_attrs_arg_type
15619 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
15620 #undef  elf_backend_obj_attrs_section_type
15621 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
15622 #define elf_backend_obj_attrs_order             elf32_arm_obj_attrs_order
15623 #define elf_backend_obj_attrs_handle_unknown    elf32_arm_obj_attrs_handle_unknown
15624
15625 #include "elf32-target.h"
15626
15627 /* Native Client targets.  */
15628
15629 #undef  TARGET_LITTLE_SYM
15630 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_nacl_vec
15631 #undef  TARGET_LITTLE_NAME
15632 #define TARGET_LITTLE_NAME              "elf32-littlearm-nacl"
15633 #undef  TARGET_BIG_SYM
15634 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_nacl_vec
15635 #undef  TARGET_BIG_NAME
15636 #define TARGET_BIG_NAME                 "elf32-bigarm-nacl"
15637
15638 /* Like elf32_arm_link_hash_table_create -- but overrides
15639    appropriately for NaCl.  */
15640
15641 static struct bfd_link_hash_table *
15642 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
15643 {
15644   struct bfd_link_hash_table *ret;
15645
15646   ret = elf32_arm_link_hash_table_create (abfd);
15647   if (ret)
15648     {
15649       struct elf32_arm_link_hash_table *htab
15650         = (struct elf32_arm_link_hash_table *) ret;
15651
15652       htab->nacl_p = 1;
15653
15654       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
15655       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
15656     }
15657   return ret;
15658 }
15659
15660 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
15661    really need to use elf32_arm_modify_segment_map.  But we do it
15662    anyway just to reduce gratuitous differences with the stock ARM backend.  */
15663
15664 static bfd_boolean
15665 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
15666 {
15667   return (elf32_arm_modify_segment_map (abfd, info)
15668           && nacl_modify_segment_map (abfd, info));
15669 }
15670
15671 #undef  elf32_bed
15672 #define elf32_bed                       elf32_arm_nacl_bed
15673 #undef  bfd_elf32_bfd_link_hash_table_create
15674 #define bfd_elf32_bfd_link_hash_table_create    \
15675   elf32_arm_nacl_link_hash_table_create
15676 #undef  elf_backend_plt_alignment
15677 #define elf_backend_plt_alignment       4
15678 #undef  elf_backend_modify_segment_map
15679 #define elf_backend_modify_segment_map          elf32_arm_nacl_modify_segment_map
15680 #undef  elf_backend_modify_program_headers
15681 #define elf_backend_modify_program_headers      nacl_modify_program_headers
15682
15683 #undef  ELF_MAXPAGESIZE
15684 #define ELF_MAXPAGESIZE                 0x10000
15685
15686 #include "elf32-target.h"
15687
15688 /* Reset to defaults.  */
15689 #undef  elf_backend_plt_alignment
15690 #undef  elf_backend_modify_segment_map
15691 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
15692 #undef  elf_backend_modify_program_headers
15693
15694 /* VxWorks Targets.  */
15695
15696 #undef  TARGET_LITTLE_SYM
15697 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
15698 #undef  TARGET_LITTLE_NAME
15699 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
15700 #undef  TARGET_BIG_SYM
15701 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
15702 #undef  TARGET_BIG_NAME
15703 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
15704
15705 /* Like elf32_arm_link_hash_table_create -- but overrides
15706    appropriately for VxWorks.  */
15707
15708 static struct bfd_link_hash_table *
15709 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
15710 {
15711   struct bfd_link_hash_table *ret;
15712
15713   ret = elf32_arm_link_hash_table_create (abfd);
15714   if (ret)
15715     {
15716       struct elf32_arm_link_hash_table *htab
15717         = (struct elf32_arm_link_hash_table *) ret;
15718       htab->use_rel = 0;
15719       htab->vxworks_p = 1;
15720     }
15721   return ret;
15722 }
15723
15724 static void
15725 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
15726 {
15727   elf32_arm_final_write_processing (abfd, linker);
15728   elf_vxworks_final_write_processing (abfd, linker);
15729 }
15730
15731 #undef  elf32_bed
15732 #define elf32_bed elf32_arm_vxworks_bed
15733
15734 #undef  bfd_elf32_bfd_link_hash_table_create
15735 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
15736 #undef  elf_backend_final_write_processing
15737 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
15738 #undef  elf_backend_emit_relocs
15739 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
15740
15741 #undef  elf_backend_may_use_rel_p
15742 #define elf_backend_may_use_rel_p       0
15743 #undef  elf_backend_may_use_rela_p
15744 #define elf_backend_may_use_rela_p      1
15745 #undef  elf_backend_default_use_rela_p
15746 #define elf_backend_default_use_rela_p  1
15747 #undef  elf_backend_want_plt_sym
15748 #define elf_backend_want_plt_sym        1
15749 #undef  ELF_MAXPAGESIZE
15750 #define ELF_MAXPAGESIZE                 0x1000
15751
15752 #include "elf32-target.h"
15753
15754
15755 /* Merge backend specific data from an object file to the output
15756    object file when linking.  */
15757
15758 static bfd_boolean
15759 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
15760 {
15761   flagword out_flags;
15762   flagword in_flags;
15763   bfd_boolean flags_compatible = TRUE;
15764   asection *sec;
15765
15766   /* Check if we have the same endianness.  */
15767   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
15768     return FALSE;
15769
15770   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
15771     return TRUE;
15772
15773   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
15774     return FALSE;
15775
15776   /* The input BFD must have had its flags initialised.  */
15777   /* The following seems bogus to me -- The flags are initialized in
15778      the assembler but I don't think an elf_flags_init field is
15779      written into the object.  */
15780   /* BFD_ASSERT (elf_flags_init (ibfd)); */
15781
15782   in_flags  = elf_elfheader (ibfd)->e_flags;
15783   out_flags = elf_elfheader (obfd)->e_flags;
15784
15785   /* In theory there is no reason why we couldn't handle this.  However
15786      in practice it isn't even close to working and there is no real
15787      reason to want it.  */
15788   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
15789       && !(ibfd->flags & DYNAMIC)
15790       && (in_flags & EF_ARM_BE8))
15791     {
15792       _bfd_error_handler (_("error: %B is already in final BE8 format"),
15793                           ibfd);
15794       return FALSE;
15795     }
15796
15797   if (!elf_flags_init (obfd))
15798     {
15799       /* If the input is the default architecture and had the default
15800          flags then do not bother setting the flags for the output
15801          architecture, instead allow future merges to do this.  If no
15802          future merges ever set these flags then they will retain their
15803          uninitialised values, which surprise surprise, correspond
15804          to the default values.  */
15805       if (bfd_get_arch_info (ibfd)->the_default
15806           && elf_elfheader (ibfd)->e_flags == 0)
15807         return TRUE;
15808
15809       elf_flags_init (obfd) = TRUE;
15810       elf_elfheader (obfd)->e_flags = in_flags;
15811
15812       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
15813           && bfd_get_arch_info (obfd)->the_default)
15814         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
15815
15816       return TRUE;
15817     }
15818
15819   /* Determine what should happen if the input ARM architecture
15820      does not match the output ARM architecture.  */
15821   if (! bfd_arm_merge_machines (ibfd, obfd))
15822     return FALSE;
15823
15824   /* Identical flags must be compatible.  */
15825   if (in_flags == out_flags)
15826     return TRUE;
15827
15828   /* Check to see if the input BFD actually contains any sections.  If
15829      not, its flags may not have been initialised either, but it
15830      cannot actually cause any incompatiblity.  Do not short-circuit
15831      dynamic objects; their section list may be emptied by
15832     elf_link_add_object_symbols.
15833
15834     Also check to see if there are no code sections in the input.
15835     In this case there is no need to check for code specific flags.
15836     XXX - do we need to worry about floating-point format compatability
15837     in data sections ?  */
15838   if (!(ibfd->flags & DYNAMIC))
15839     {
15840       bfd_boolean null_input_bfd = TRUE;
15841       bfd_boolean only_data_sections = TRUE;
15842
15843       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15844         {
15845           /* Ignore synthetic glue sections.  */
15846           if (strcmp (sec->name, ".glue_7")
15847               && strcmp (sec->name, ".glue_7t"))
15848             {
15849               if ((bfd_get_section_flags (ibfd, sec)
15850                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
15851                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
15852                 only_data_sections = FALSE;
15853
15854               null_input_bfd = FALSE;
15855               break;
15856             }
15857         }
15858
15859       if (null_input_bfd || only_data_sections)
15860         return TRUE;
15861     }
15862
15863   /* Complain about various flag mismatches.  */
15864   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
15865                                       EF_ARM_EABI_VERSION (out_flags)))
15866     {
15867       _bfd_error_handler
15868         (_("error: Source object %B has EABI version %d, but target %B has EABI version %d"),
15869          ibfd, obfd,
15870          (in_flags & EF_ARM_EABIMASK) >> 24,
15871          (out_flags & EF_ARM_EABIMASK) >> 24);
15872       return FALSE;
15873     }
15874
15875   /* Not sure what needs to be checked for EABI versions >= 1.  */
15876   /* VxWorks libraries do not use these flags.  */
15877   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
15878       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
15879       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
15880     {
15881       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
15882         {
15883           _bfd_error_handler
15884             (_("error: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
15885              ibfd, obfd,
15886              in_flags & EF_ARM_APCS_26 ? 26 : 32,
15887              out_flags & EF_ARM_APCS_26 ? 26 : 32);
15888           flags_compatible = FALSE;
15889         }
15890
15891       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
15892         {
15893           if (in_flags & EF_ARM_APCS_FLOAT)
15894             _bfd_error_handler
15895               (_("error: %B passes floats in float registers, whereas %B passes them in integer registers"),
15896                ibfd, obfd);
15897           else
15898             _bfd_error_handler
15899               (_("error: %B passes floats in integer registers, whereas %B passes them in float registers"),
15900                ibfd, obfd);
15901
15902           flags_compatible = FALSE;
15903         }
15904
15905       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
15906         {
15907           if (in_flags & EF_ARM_VFP_FLOAT)
15908             _bfd_error_handler
15909               (_("error: %B uses VFP instructions, whereas %B does not"),
15910                ibfd, obfd);
15911           else
15912             _bfd_error_handler
15913               (_("error: %B uses FPA instructions, whereas %B does not"),
15914                ibfd, obfd);
15915
15916           flags_compatible = FALSE;
15917         }
15918
15919       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
15920         {
15921           if (in_flags & EF_ARM_MAVERICK_FLOAT)
15922             _bfd_error_handler
15923               (_("error: %B uses Maverick instructions, whereas %B does not"),
15924                ibfd, obfd);
15925           else
15926             _bfd_error_handler
15927               (_("error: %B does not use Maverick instructions, whereas %B does"),
15928                ibfd, obfd);
15929
15930           flags_compatible = FALSE;
15931         }
15932
15933 #ifdef EF_ARM_SOFT_FLOAT
15934       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
15935         {
15936           /* We can allow interworking between code that is VFP format
15937              layout, and uses either soft float or integer regs for
15938              passing floating point arguments and results.  We already
15939              know that the APCS_FLOAT flags match; similarly for VFP
15940              flags.  */
15941           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
15942               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
15943             {
15944               if (in_flags & EF_ARM_SOFT_FLOAT)
15945                 _bfd_error_handler
15946                   (_("error: %B uses software FP, whereas %B uses hardware FP"),
15947                    ibfd, obfd);
15948               else
15949                 _bfd_error_handler
15950                   (_("error: %B uses hardware FP, whereas %B uses software FP"),
15951                    ibfd, obfd);
15952
15953               flags_compatible = FALSE;
15954             }
15955         }
15956 #endif
15957
15958       /* Interworking mismatch is only a warning.  */
15959       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
15960         {
15961           if (in_flags & EF_ARM_INTERWORK)
15962             {
15963               _bfd_error_handler
15964                 (_("Warning: %B supports interworking, whereas %B does not"),
15965                  ibfd, obfd);
15966             }
15967           else
15968             {
15969               _bfd_error_handler
15970                 (_("Warning: %B does not support interworking, whereas %B does"),
15971                  ibfd, obfd);
15972             }
15973         }
15974     }
15975
15976   return flags_compatible;
15977 }
15978
15979
15980 /* Symbian OS Targets.  */
15981
15982 #undef  TARGET_LITTLE_SYM
15983 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
15984 #undef  TARGET_LITTLE_NAME
15985 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
15986 #undef  TARGET_BIG_SYM
15987 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
15988 #undef  TARGET_BIG_NAME
15989 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
15990
15991 /* Like elf32_arm_link_hash_table_create -- but overrides
15992    appropriately for Symbian OS.  */
15993
15994 static struct bfd_link_hash_table *
15995 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
15996 {
15997   struct bfd_link_hash_table *ret;
15998
15999   ret = elf32_arm_link_hash_table_create (abfd);
16000   if (ret)
16001     {
16002       struct elf32_arm_link_hash_table *htab
16003         = (struct elf32_arm_link_hash_table *)ret;
16004       /* There is no PLT header for Symbian OS.  */
16005       htab->plt_header_size = 0;
16006       /* The PLT entries are each one instruction and one word.  */
16007       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
16008       htab->symbian_p = 1;
16009       /* Symbian uses armv5t or above, so use_blx is always true.  */
16010       htab->use_blx = 1;
16011       htab->root.is_relocatable_executable = 1;
16012     }
16013   return ret;
16014 }
16015
16016 static const struct bfd_elf_special_section
16017 elf32_arm_symbian_special_sections[] =
16018 {
16019   /* In a BPABI executable, the dynamic linking sections do not go in
16020      the loadable read-only segment.  The post-linker may wish to
16021      refer to these sections, but they are not part of the final
16022      program image.  */
16023   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
16024   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
16025   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
16026   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
16027   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
16028   /* These sections do not need to be writable as the SymbianOS
16029      postlinker will arrange things so that no dynamic relocation is
16030      required.  */
16031   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
16032   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
16033   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
16034   { NULL,                             0, 0, 0,                 0 }
16035 };
16036
16037 static void
16038 elf32_arm_symbian_begin_write_processing (bfd *abfd,
16039                                           struct bfd_link_info *link_info)
16040 {
16041   /* BPABI objects are never loaded directly by an OS kernel; they are
16042      processed by a postlinker first, into an OS-specific format.  If
16043      the D_PAGED bit is set on the file, BFD will align segments on
16044      page boundaries, so that an OS can directly map the file.  With
16045      BPABI objects, that just results in wasted space.  In addition,
16046      because we clear the D_PAGED bit, map_sections_to_segments will
16047      recognize that the program headers should not be mapped into any
16048      loadable segment.  */
16049   abfd->flags &= ~D_PAGED;
16050   elf32_arm_begin_write_processing (abfd, link_info);
16051 }
16052
16053 static bfd_boolean
16054 elf32_arm_symbian_modify_segment_map (bfd *abfd,
16055                                       struct bfd_link_info *info)
16056 {
16057   struct elf_segment_map *m;
16058   asection *dynsec;
16059
16060   /* BPABI shared libraries and executables should have a PT_DYNAMIC
16061      segment.  However, because the .dynamic section is not marked
16062      with SEC_LOAD, the generic ELF code will not create such a
16063      segment.  */
16064   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
16065   if (dynsec)
16066     {
16067       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
16068         if (m->p_type == PT_DYNAMIC)
16069           break;
16070
16071       if (m == NULL)
16072         {
16073           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
16074           m->next = elf_tdata (abfd)->segment_map;
16075           elf_tdata (abfd)->segment_map = m;
16076         }
16077     }
16078
16079   /* Also call the generic arm routine.  */
16080   return elf32_arm_modify_segment_map (abfd, info);
16081 }
16082
16083 /* Return address for Ith PLT stub in section PLT, for relocation REL
16084    or (bfd_vma) -1 if it should not be included.  */
16085
16086 static bfd_vma
16087 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
16088                                const arelent *rel ATTRIBUTE_UNUSED)
16089 {
16090   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
16091 }
16092
16093
16094 #undef  elf32_bed
16095 #define elf32_bed elf32_arm_symbian_bed
16096
16097 /* The dynamic sections are not allocated on SymbianOS; the postlinker
16098    will process them and then discard them.  */
16099 #undef  ELF_DYNAMIC_SEC_FLAGS
16100 #define ELF_DYNAMIC_SEC_FLAGS \
16101   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
16102
16103 #undef elf_backend_emit_relocs
16104
16105 #undef  bfd_elf32_bfd_link_hash_table_create
16106 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
16107 #undef  elf_backend_special_sections
16108 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
16109 #undef  elf_backend_begin_write_processing
16110 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
16111 #undef  elf_backend_final_write_processing
16112 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
16113
16114 #undef  elf_backend_modify_segment_map
16115 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
16116
16117 /* There is no .got section for BPABI objects, and hence no header.  */
16118 #undef  elf_backend_got_header_size
16119 #define elf_backend_got_header_size 0
16120
16121 /* Similarly, there is no .got.plt section.  */
16122 #undef  elf_backend_want_got_plt
16123 #define elf_backend_want_got_plt 0
16124
16125 #undef  elf_backend_plt_sym_val
16126 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
16127
16128 #undef  elf_backend_may_use_rel_p
16129 #define elf_backend_may_use_rel_p       1
16130 #undef  elf_backend_may_use_rela_p
16131 #define elf_backend_may_use_rela_p      0
16132 #undef  elf_backend_default_use_rela_p
16133 #define elf_backend_default_use_rela_p  0
16134 #undef  elf_backend_want_plt_sym
16135 #define elf_backend_want_plt_sym        0
16136 #undef  ELF_MAXPAGESIZE
16137 #define ELF_MAXPAGESIZE                 0x8000
16138
16139 #include "elf32-target.h"