* elf32-arm.c: Tidy up the code.
[platform/upstream/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 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 "bfd.h"
24 #include "libiberty.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 #include "elf/arm.h"
29
30 /* Return the relocation section associated with NAME.  HTAB is the
31    bfd's elf32_arm_link_hash_entry.  */
32 #define RELOC_SECTION(HTAB, NAME) \
33   ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
34
35 /* Return size of a relocation entry.  HTAB is the bfd's
36    elf32_arm_link_hash_entry.  */
37 #define RELOC_SIZE(HTAB) \
38   ((HTAB)->use_rel \
39    ? sizeof (Elf32_External_Rel) \
40    : sizeof (Elf32_External_Rela))
41
42 /* Return function to swap relocations in.  HTAB is the bfd's
43    elf32_arm_link_hash_entry.  */
44 #define SWAP_RELOC_IN(HTAB) \
45   ((HTAB)->use_rel \
46    ? bfd_elf32_swap_reloc_in \
47    : bfd_elf32_swap_reloca_in)
48
49 /* Return function to swap relocations out.  HTAB is the bfd's
50    elf32_arm_link_hash_entry.  */
51 #define SWAP_RELOC_OUT(HTAB) \
52   ((HTAB)->use_rel \
53    ? bfd_elf32_swap_reloc_out \
54    : bfd_elf32_swap_reloca_out)
55
56 #define elf_info_to_howto               0
57 #define elf_info_to_howto_rel           elf32_arm_info_to_howto
58
59 #define ARM_ELF_ABI_VERSION             0
60 #define ARM_ELF_OS_ABI_VERSION          ELFOSABI_ARM
61
62 static struct elf_backend_data elf32_arm_vxworks_bed;
63
64 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
65    R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
66    in that slot.  */
67
68 static reloc_howto_type elf32_arm_howto_table_1[] =
69 {
70   /* No relocation.  */
71   HOWTO (R_ARM_NONE,            /* type */
72          0,                     /* rightshift */
73          0,                     /* size (0 = byte, 1 = short, 2 = long) */
74          0,                     /* bitsize */
75          FALSE,                 /* pc_relative */
76          0,                     /* bitpos */
77          complain_overflow_dont,/* complain_on_overflow */
78          bfd_elf_generic_reloc, /* special_function */
79          "R_ARM_NONE",          /* name */
80          FALSE,                 /* partial_inplace */
81          0,                     /* src_mask */
82          0,                     /* dst_mask */
83          FALSE),                /* pcrel_offset */
84
85   HOWTO (R_ARM_PC24,            /* type */
86          2,                     /* rightshift */
87          2,                     /* size (0 = byte, 1 = short, 2 = long) */
88          24,                    /* bitsize */
89          TRUE,                  /* pc_relative */
90          0,                     /* bitpos */
91          complain_overflow_signed,/* complain_on_overflow */
92          bfd_elf_generic_reloc, /* special_function */
93          "R_ARM_PC24",          /* name */
94          FALSE,                 /* partial_inplace */
95          0x00ffffff,            /* src_mask */
96          0x00ffffff,            /* dst_mask */
97          TRUE),                 /* pcrel_offset */
98
99   /* 32 bit absolute */
100   HOWTO (R_ARM_ABS32,           /* type */
101          0,                     /* rightshift */
102          2,                     /* size (0 = byte, 1 = short, 2 = long) */
103          32,                    /* bitsize */
104          FALSE,                 /* pc_relative */
105          0,                     /* bitpos */
106          complain_overflow_bitfield,/* complain_on_overflow */
107          bfd_elf_generic_reloc, /* special_function */
108          "R_ARM_ABS32",         /* name */
109          FALSE,                 /* partial_inplace */
110          0xffffffff,            /* src_mask */
111          0xffffffff,            /* dst_mask */
112          FALSE),                /* pcrel_offset */
113
114   /* standard 32bit pc-relative reloc */
115   HOWTO (R_ARM_REL32,           /* type */
116          0,                     /* rightshift */
117          2,                     /* size (0 = byte, 1 = short, 2 = long) */
118          32,                    /* bitsize */
119          TRUE,                  /* pc_relative */
120          0,                     /* bitpos */
121          complain_overflow_bitfield,/* complain_on_overflow */
122          bfd_elf_generic_reloc, /* special_function */
123          "R_ARM_REL32",         /* name */
124          FALSE,                 /* partial_inplace */
125          0xffffffff,            /* src_mask */
126          0xffffffff,            /* dst_mask */
127          TRUE),                 /* pcrel_offset */
128
129   /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
130   HOWTO (R_ARM_LDR_PC_G0,       /* type */
131          0,                     /* rightshift */
132          0,                     /* size (0 = byte, 1 = short, 2 = long) */
133          32,                    /* bitsize */
134          TRUE,                  /* pc_relative */
135          0,                     /* bitpos */
136          complain_overflow_dont,/* complain_on_overflow */
137          bfd_elf_generic_reloc, /* special_function */
138          "R_ARM_LDR_PC_G0",     /* name */
139          FALSE,                 /* partial_inplace */
140          0xffffffff,            /* src_mask */
141          0xffffffff,            /* dst_mask */
142          TRUE),                 /* pcrel_offset */
143
144    /* 16 bit absolute */
145   HOWTO (R_ARM_ABS16,           /* type */
146          0,                     /* rightshift */
147          1,                     /* size (0 = byte, 1 = short, 2 = long) */
148          16,                    /* bitsize */
149          FALSE,                 /* pc_relative */
150          0,                     /* bitpos */
151          complain_overflow_bitfield,/* complain_on_overflow */
152          bfd_elf_generic_reloc, /* special_function */
153          "R_ARM_ABS16",         /* name */
154          FALSE,                 /* partial_inplace */
155          0x0000ffff,            /* src_mask */
156          0x0000ffff,            /* dst_mask */
157          FALSE),                /* pcrel_offset */
158
159   /* 12 bit absolute */
160   HOWTO (R_ARM_ABS12,           /* type */
161          0,                     /* rightshift */
162          2,                     /* size (0 = byte, 1 = short, 2 = long) */
163          12,                    /* bitsize */
164          FALSE,                 /* pc_relative */
165          0,                     /* bitpos */
166          complain_overflow_bitfield,/* complain_on_overflow */
167          bfd_elf_generic_reloc, /* special_function */
168          "R_ARM_ABS12",         /* name */
169          FALSE,                 /* partial_inplace */
170          0x00000fff,            /* src_mask */
171          0x00000fff,            /* dst_mask */
172          FALSE),                /* pcrel_offset */
173
174   HOWTO (R_ARM_THM_ABS5,        /* type */
175          6,                     /* rightshift */
176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
177          5,                     /* bitsize */
178          FALSE,                 /* pc_relative */
179          0,                     /* bitpos */
180          complain_overflow_bitfield,/* complain_on_overflow */
181          bfd_elf_generic_reloc, /* special_function */
182          "R_ARM_THM_ABS5",      /* name */
183          FALSE,                 /* partial_inplace */
184          0x000007e0,            /* src_mask */
185          0x000007e0,            /* dst_mask */
186          FALSE),                /* pcrel_offset */
187
188   /* 8 bit absolute */
189   HOWTO (R_ARM_ABS8,            /* type */
190          0,                     /* rightshift */
191          0,                     /* size (0 = byte, 1 = short, 2 = long) */
192          8,                     /* bitsize */
193          FALSE,                 /* pc_relative */
194          0,                     /* bitpos */
195          complain_overflow_bitfield,/* complain_on_overflow */
196          bfd_elf_generic_reloc, /* special_function */
197          "R_ARM_ABS8",          /* name */
198          FALSE,                 /* partial_inplace */
199          0x000000ff,            /* src_mask */
200          0x000000ff,            /* dst_mask */
201          FALSE),                /* pcrel_offset */
202
203   HOWTO (R_ARM_SBREL32,         /* type */
204          0,                     /* rightshift */
205          2,                     /* size (0 = byte, 1 = short, 2 = long) */
206          32,                    /* bitsize */
207          FALSE,                 /* pc_relative */
208          0,                     /* bitpos */
209          complain_overflow_dont,/* complain_on_overflow */
210          bfd_elf_generic_reloc, /* special_function */
211          "R_ARM_SBREL32",       /* name */
212          FALSE,                 /* partial_inplace */
213          0xffffffff,            /* src_mask */
214          0xffffffff,            /* dst_mask */
215          FALSE),                /* pcrel_offset */
216
217   HOWTO (R_ARM_THM_CALL,        /* type */
218          1,                     /* rightshift */
219          2,                     /* size (0 = byte, 1 = short, 2 = long) */
220          25,                    /* bitsize */
221          TRUE,                  /* pc_relative */
222          0,                     /* bitpos */
223          complain_overflow_signed,/* complain_on_overflow */
224          bfd_elf_generic_reloc, /* special_function */
225          "R_ARM_THM_CALL",      /* name */
226          FALSE,                 /* partial_inplace */
227          0x07ff07ff,            /* src_mask */
228          0x07ff07ff,            /* dst_mask */
229          TRUE),                 /* pcrel_offset */
230
231   HOWTO (R_ARM_THM_PC8,         /* type */
232          1,                     /* rightshift */
233          1,                     /* size (0 = byte, 1 = short, 2 = long) */
234          8,                     /* bitsize */
235          TRUE,                  /* pc_relative */
236          0,                     /* bitpos */
237          complain_overflow_signed,/* complain_on_overflow */
238          bfd_elf_generic_reloc, /* special_function */
239          "R_ARM_THM_PC8",       /* name */
240          FALSE,                 /* partial_inplace */
241          0x000000ff,            /* src_mask */
242          0x000000ff,            /* dst_mask */
243          TRUE),                 /* pcrel_offset */
244
245   HOWTO (R_ARM_BREL_ADJ,        /* type */
246          1,                     /* rightshift */
247          1,                     /* size (0 = byte, 1 = short, 2 = long) */
248          32,                    /* bitsize */
249          FALSE,                 /* pc_relative */
250          0,                     /* bitpos */
251          complain_overflow_signed,/* complain_on_overflow */
252          bfd_elf_generic_reloc, /* special_function */
253          "R_ARM_BREL_ADJ",      /* name */
254          FALSE,                 /* partial_inplace */
255          0xffffffff,            /* src_mask */
256          0xffffffff,            /* dst_mask */
257          FALSE),                /* pcrel_offset */
258
259   HOWTO (R_ARM_SWI24,           /* type */
260          0,                     /* rightshift */
261          0,                     /* size (0 = byte, 1 = short, 2 = long) */
262          0,                     /* bitsize */
263          FALSE,                 /* pc_relative */
264          0,                     /* bitpos */
265          complain_overflow_signed,/* complain_on_overflow */
266          bfd_elf_generic_reloc, /* special_function */
267          "R_ARM_SWI24",         /* name */
268          FALSE,                 /* partial_inplace */
269          0x00000000,            /* src_mask */
270          0x00000000,            /* dst_mask */
271          FALSE),                /* pcrel_offset */
272
273   HOWTO (R_ARM_THM_SWI8,        /* type */
274          0,                     /* rightshift */
275          0,                     /* size (0 = byte, 1 = short, 2 = long) */
276          0,                     /* bitsize */
277          FALSE,                 /* pc_relative */
278          0,                     /* bitpos */
279          complain_overflow_signed,/* complain_on_overflow */
280          bfd_elf_generic_reloc, /* special_function */
281          "R_ARM_SWI8",          /* name */
282          FALSE,                 /* partial_inplace */
283          0x00000000,            /* src_mask */
284          0x00000000,            /* dst_mask */
285          FALSE),                /* pcrel_offset */
286
287   /* BLX instruction for the ARM.  */
288   HOWTO (R_ARM_XPC25,           /* type */
289          2,                     /* rightshift */
290          2,                     /* size (0 = byte, 1 = short, 2 = long) */
291          25,                    /* bitsize */
292          TRUE,                  /* pc_relative */
293          0,                     /* bitpos */
294          complain_overflow_signed,/* complain_on_overflow */
295          bfd_elf_generic_reloc, /* special_function */
296          "R_ARM_XPC25",         /* name */
297          FALSE,                 /* partial_inplace */
298          0x00ffffff,            /* src_mask */
299          0x00ffffff,            /* dst_mask */
300          TRUE),                 /* pcrel_offset */
301
302   /* BLX instruction for the Thumb.  */
303   HOWTO (R_ARM_THM_XPC22,       /* type */
304          2,                     /* rightshift */
305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
306          22,                    /* bitsize */
307          TRUE,                  /* pc_relative */
308          0,                     /* bitpos */
309          complain_overflow_signed,/* complain_on_overflow */
310          bfd_elf_generic_reloc, /* special_function */
311          "R_ARM_THM_XPC22",     /* name */
312          FALSE,                 /* partial_inplace */
313          0x07ff07ff,            /* src_mask */
314          0x07ff07ff,            /* dst_mask */
315          TRUE),                 /* pcrel_offset */
316
317   /* Dynamic TLS relocations.  */
318
319   HOWTO (R_ARM_TLS_DTPMOD32,    /* type */
320          0,                     /* rightshift */
321          2,                     /* size (0 = byte, 1 = short, 2 = long) */
322          32,                    /* bitsize */
323          FALSE,                 /* pc_relative */
324          0,                     /* bitpos */
325          complain_overflow_bitfield,/* complain_on_overflow */
326          bfd_elf_generic_reloc, /* special_function */
327          "R_ARM_TLS_DTPMOD32",  /* name */
328          TRUE,                  /* partial_inplace */
329          0xffffffff,            /* src_mask */
330          0xffffffff,            /* dst_mask */
331          FALSE),                /* pcrel_offset */
332
333   HOWTO (R_ARM_TLS_DTPOFF32,    /* type */
334          0,                     /* rightshift */
335          2,                     /* size (0 = byte, 1 = short, 2 = long) */
336          32,                    /* bitsize */
337          FALSE,                 /* pc_relative */
338          0,                     /* bitpos */
339          complain_overflow_bitfield,/* complain_on_overflow */
340          bfd_elf_generic_reloc, /* special_function */
341          "R_ARM_TLS_DTPOFF32",  /* name */
342          TRUE,                  /* partial_inplace */
343          0xffffffff,            /* src_mask */
344          0xffffffff,            /* dst_mask */
345          FALSE),                /* pcrel_offset */
346
347   HOWTO (R_ARM_TLS_TPOFF32,     /* type */
348          0,                     /* rightshift */
349          2,                     /* size (0 = byte, 1 = short, 2 = long) */
350          32,                    /* bitsize */
351          FALSE,                 /* pc_relative */
352          0,                     /* bitpos */
353          complain_overflow_bitfield,/* complain_on_overflow */
354          bfd_elf_generic_reloc, /* special_function */
355          "R_ARM_TLS_TPOFF32",   /* name */
356          TRUE,                  /* partial_inplace */
357          0xffffffff,            /* src_mask */
358          0xffffffff,            /* dst_mask */
359          FALSE),                /* pcrel_offset */
360
361   /* Relocs used in ARM Linux */
362
363   HOWTO (R_ARM_COPY,            /* type */
364          0,                     /* rightshift */
365          2,                     /* size (0 = byte, 1 = short, 2 = long) */
366          32,                    /* bitsize */
367          FALSE,                 /* pc_relative */
368          0,                     /* bitpos */
369          complain_overflow_bitfield,/* complain_on_overflow */
370          bfd_elf_generic_reloc, /* special_function */
371          "R_ARM_COPY",          /* name */
372          TRUE,                  /* partial_inplace */
373          0xffffffff,            /* src_mask */
374          0xffffffff,            /* dst_mask */
375          FALSE),                /* pcrel_offset */
376
377   HOWTO (R_ARM_GLOB_DAT,        /* type */
378          0,                     /* rightshift */
379          2,                     /* size (0 = byte, 1 = short, 2 = long) */
380          32,                    /* bitsize */
381          FALSE,                 /* pc_relative */
382          0,                     /* bitpos */
383          complain_overflow_bitfield,/* complain_on_overflow */
384          bfd_elf_generic_reloc, /* special_function */
385          "R_ARM_GLOB_DAT",      /* name */
386          TRUE,                  /* partial_inplace */
387          0xffffffff,            /* src_mask */
388          0xffffffff,            /* dst_mask */
389          FALSE),                /* pcrel_offset */
390
391   HOWTO (R_ARM_JUMP_SLOT,       /* type */
392          0,                     /* rightshift */
393          2,                     /* size (0 = byte, 1 = short, 2 = long) */
394          32,                    /* bitsize */
395          FALSE,                 /* pc_relative */
396          0,                     /* bitpos */
397          complain_overflow_bitfield,/* complain_on_overflow */
398          bfd_elf_generic_reloc, /* special_function */
399          "R_ARM_JUMP_SLOT",     /* name */
400          TRUE,                  /* partial_inplace */
401          0xffffffff,            /* src_mask */
402          0xffffffff,            /* dst_mask */
403          FALSE),                /* pcrel_offset */
404
405   HOWTO (R_ARM_RELATIVE,        /* type */
406          0,                     /* rightshift */
407          2,                     /* size (0 = byte, 1 = short, 2 = long) */
408          32,                    /* bitsize */
409          FALSE,                 /* pc_relative */
410          0,                     /* bitpos */
411          complain_overflow_bitfield,/* complain_on_overflow */
412          bfd_elf_generic_reloc, /* special_function */
413          "R_ARM_RELATIVE",      /* name */
414          TRUE,                  /* partial_inplace */
415          0xffffffff,            /* src_mask */
416          0xffffffff,            /* dst_mask */
417          FALSE),                /* pcrel_offset */
418
419   HOWTO (R_ARM_GOTOFF32,        /* type */
420          0,                     /* rightshift */
421          2,                     /* size (0 = byte, 1 = short, 2 = long) */
422          32,                    /* bitsize */
423          FALSE,                 /* pc_relative */
424          0,                     /* bitpos */
425          complain_overflow_bitfield,/* complain_on_overflow */
426          bfd_elf_generic_reloc, /* special_function */
427          "R_ARM_GOTOFF32",      /* name */
428          TRUE,                  /* partial_inplace */
429          0xffffffff,            /* src_mask */
430          0xffffffff,            /* dst_mask */
431          FALSE),                /* pcrel_offset */
432
433   HOWTO (R_ARM_GOTPC,           /* type */
434          0,                     /* rightshift */
435          2,                     /* size (0 = byte, 1 = short, 2 = long) */
436          32,                    /* bitsize */
437          TRUE,                  /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_bitfield,/* complain_on_overflow */
440          bfd_elf_generic_reloc, /* special_function */
441          "R_ARM_GOTPC",         /* name */
442          TRUE,                  /* partial_inplace */
443          0xffffffff,            /* src_mask */
444          0xffffffff,            /* dst_mask */
445          TRUE),                 /* pcrel_offset */
446
447   HOWTO (R_ARM_GOT32,           /* type */
448          0,                     /* rightshift */
449          2,                     /* size (0 = byte, 1 = short, 2 = long) */
450          32,                    /* bitsize */
451          FALSE,                 /* pc_relative */
452          0,                     /* bitpos */
453          complain_overflow_bitfield,/* complain_on_overflow */
454          bfd_elf_generic_reloc, /* special_function */
455          "R_ARM_GOT32",         /* name */
456          TRUE,                  /* partial_inplace */
457          0xffffffff,            /* src_mask */
458          0xffffffff,            /* dst_mask */
459          FALSE),                /* pcrel_offset */
460
461   HOWTO (R_ARM_PLT32,           /* type */
462          2,                     /* rightshift */
463          2,                     /* size (0 = byte, 1 = short, 2 = long) */
464          24,                    /* bitsize */
465          TRUE,                  /* pc_relative */
466          0,                     /* bitpos */
467          complain_overflow_bitfield,/* complain_on_overflow */
468          bfd_elf_generic_reloc, /* special_function */
469          "R_ARM_PLT32",         /* name */
470          FALSE,                 /* partial_inplace */
471          0x00ffffff,            /* src_mask */
472          0x00ffffff,            /* dst_mask */
473          TRUE),                 /* pcrel_offset */
474
475   HOWTO (R_ARM_CALL,            /* type */
476          2,                     /* rightshift */
477          2,                     /* size (0 = byte, 1 = short, 2 = long) */
478          24,                    /* bitsize */
479          TRUE,                  /* pc_relative */
480          0,                     /* bitpos */
481          complain_overflow_signed,/* complain_on_overflow */
482          bfd_elf_generic_reloc, /* special_function */
483          "R_ARM_CALL",          /* name */
484          FALSE,                 /* partial_inplace */
485          0x00ffffff,            /* src_mask */
486          0x00ffffff,            /* dst_mask */
487          TRUE),                 /* pcrel_offset */
488
489   HOWTO (R_ARM_JUMP24,          /* type */
490          2,                     /* rightshift */
491          2,                     /* size (0 = byte, 1 = short, 2 = long) */
492          24,                    /* bitsize */
493          TRUE,                  /* pc_relative */
494          0,                     /* bitpos */
495          complain_overflow_signed,/* complain_on_overflow */
496          bfd_elf_generic_reloc, /* special_function */
497          "R_ARM_JUMP24",        /* name */
498          FALSE,                 /* partial_inplace */
499          0x00ffffff,            /* src_mask */
500          0x00ffffff,            /* dst_mask */
501          TRUE),                 /* pcrel_offset */
502
503   HOWTO (R_ARM_THM_JUMP24,      /* type */
504          1,                     /* rightshift */
505          2,                     /* size (0 = byte, 1 = short, 2 = long) */
506          24,                    /* bitsize */
507          TRUE,                  /* pc_relative */
508          0,                     /* bitpos */
509          complain_overflow_signed,/* complain_on_overflow */
510          bfd_elf_generic_reloc, /* special_function */
511          "R_ARM_THM_JUMP24",    /* name */
512          FALSE,                 /* partial_inplace */
513          0x07ff2fff,            /* src_mask */
514          0x07ff2fff,            /* dst_mask */
515          TRUE),                 /* pcrel_offset */
516
517   HOWTO (R_ARM_BASE_ABS,        /* type */
518          0,                     /* rightshift */
519          2,                     /* size (0 = byte, 1 = short, 2 = long) */
520          32,                    /* bitsize */
521          FALSE,                 /* pc_relative */
522          0,                     /* bitpos */
523          complain_overflow_dont,/* complain_on_overflow */
524          bfd_elf_generic_reloc, /* special_function */
525          "R_ARM_BASE_ABS",      /* name */
526          FALSE,                 /* partial_inplace */
527          0xffffffff,            /* src_mask */
528          0xffffffff,            /* dst_mask */
529          FALSE),                /* pcrel_offset */
530
531   HOWTO (R_ARM_ALU_PCREL7_0,    /* type */
532          0,                     /* rightshift */
533          2,                     /* size (0 = byte, 1 = short, 2 = long) */
534          12,                    /* bitsize */
535          TRUE,                  /* pc_relative */
536          0,                     /* bitpos */
537          complain_overflow_dont,/* complain_on_overflow */
538          bfd_elf_generic_reloc, /* special_function */
539          "R_ARM_ALU_PCREL_7_0", /* name */
540          FALSE,                 /* partial_inplace */
541          0x00000fff,            /* src_mask */
542          0x00000fff,            /* dst_mask */
543          TRUE),                 /* pcrel_offset */
544
545   HOWTO (R_ARM_ALU_PCREL15_8,   /* type */
546          0,                     /* rightshift */
547          2,                     /* size (0 = byte, 1 = short, 2 = long) */
548          12,                    /* bitsize */
549          TRUE,                  /* pc_relative */
550          8,                     /* bitpos */
551          complain_overflow_dont,/* complain_on_overflow */
552          bfd_elf_generic_reloc, /* special_function */
553          "R_ARM_ALU_PCREL_15_8",/* name */
554          FALSE,                 /* partial_inplace */
555          0x00000fff,            /* src_mask */
556          0x00000fff,            /* dst_mask */
557          TRUE),                 /* pcrel_offset */
558
559   HOWTO (R_ARM_ALU_PCREL23_15,  /* type */
560          0,                     /* rightshift */
561          2,                     /* size (0 = byte, 1 = short, 2 = long) */
562          12,                    /* bitsize */
563          TRUE,                  /* pc_relative */
564          16,                    /* bitpos */
565          complain_overflow_dont,/* complain_on_overflow */
566          bfd_elf_generic_reloc, /* special_function */
567          "R_ARM_ALU_PCREL_23_15",/* name */
568          FALSE,                 /* partial_inplace */
569          0x00000fff,            /* src_mask */
570          0x00000fff,            /* dst_mask */
571          TRUE),                 /* pcrel_offset */
572
573   HOWTO (R_ARM_LDR_SBREL_11_0,  /* type */
574          0,                     /* rightshift */
575          2,                     /* size (0 = byte, 1 = short, 2 = long) */
576          12,                    /* bitsize */
577          FALSE,                 /* pc_relative */
578          0,                     /* bitpos */
579          complain_overflow_dont,/* complain_on_overflow */
580          bfd_elf_generic_reloc, /* special_function */
581          "R_ARM_LDR_SBREL_11_0",/* name */
582          FALSE,                 /* partial_inplace */
583          0x00000fff,            /* src_mask */
584          0x00000fff,            /* dst_mask */
585          FALSE),                /* pcrel_offset */
586
587   HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
588          0,                     /* rightshift */
589          2,                     /* size (0 = byte, 1 = short, 2 = long) */
590          8,                     /* bitsize */
591          FALSE,                 /* pc_relative */
592          12,                    /* bitpos */
593          complain_overflow_dont,/* complain_on_overflow */
594          bfd_elf_generic_reloc, /* special_function */
595          "R_ARM_ALU_SBREL_19_12",/* name */
596          FALSE,                 /* partial_inplace */
597          0x000ff000,            /* src_mask */
598          0x000ff000,            /* dst_mask */
599          FALSE),                /* pcrel_offset */
600
601   HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
602          0,                     /* rightshift */
603          2,                     /* size (0 = byte, 1 = short, 2 = long) */
604          8,                     /* bitsize */
605          FALSE,                 /* pc_relative */
606          20,                    /* bitpos */
607          complain_overflow_dont,/* complain_on_overflow */
608          bfd_elf_generic_reloc, /* special_function */
609          "R_ARM_ALU_SBREL_27_20",/* name */
610          FALSE,                 /* partial_inplace */
611          0x0ff00000,            /* src_mask */
612          0x0ff00000,            /* dst_mask */
613          FALSE),                /* pcrel_offset */
614
615   HOWTO (R_ARM_TARGET1,         /* type */
616          0,                     /* rightshift */
617          2,                     /* size (0 = byte, 1 = short, 2 = long) */
618          32,                    /* bitsize */
619          FALSE,                 /* pc_relative */
620          0,                     /* bitpos */
621          complain_overflow_dont,/* complain_on_overflow */
622          bfd_elf_generic_reloc, /* special_function */
623          "R_ARM_TARGET1",       /* name */
624          FALSE,                 /* partial_inplace */
625          0xffffffff,            /* src_mask */
626          0xffffffff,            /* dst_mask */
627          FALSE),                /* pcrel_offset */
628
629   HOWTO (R_ARM_ROSEGREL32,      /* type */
630          0,                     /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          32,                    /* bitsize */
633          FALSE,                 /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_dont,/* complain_on_overflow */
636          bfd_elf_generic_reloc, /* special_function */
637          "R_ARM_ROSEGREL32",    /* name */
638          FALSE,                 /* partial_inplace */
639          0xffffffff,            /* src_mask */
640          0xffffffff,            /* dst_mask */
641          FALSE),                /* pcrel_offset */
642
643   HOWTO (R_ARM_V4BX,            /* type */
644          0,                     /* rightshift */
645          2,                     /* size (0 = byte, 1 = short, 2 = long) */
646          32,                    /* bitsize */
647          FALSE,                 /* pc_relative */
648          0,                     /* bitpos */
649          complain_overflow_dont,/* complain_on_overflow */
650          bfd_elf_generic_reloc, /* special_function */
651          "R_ARM_V4BX",          /* name */
652          FALSE,                 /* partial_inplace */
653          0xffffffff,            /* src_mask */
654          0xffffffff,            /* dst_mask */
655          FALSE),                /* pcrel_offset */
656
657   HOWTO (R_ARM_TARGET2,         /* type */
658          0,                     /* rightshift */
659          2,                     /* size (0 = byte, 1 = short, 2 = long) */
660          32,                    /* bitsize */
661          FALSE,                 /* pc_relative */
662          0,                     /* bitpos */
663          complain_overflow_signed,/* complain_on_overflow */
664          bfd_elf_generic_reloc, /* special_function */
665          "R_ARM_TARGET2",       /* name */
666          FALSE,                 /* partial_inplace */
667          0xffffffff,            /* src_mask */
668          0xffffffff,            /* dst_mask */
669          TRUE),                 /* pcrel_offset */
670
671   HOWTO (R_ARM_PREL31,          /* type */
672          0,                     /* rightshift */
673          2,                     /* size (0 = byte, 1 = short, 2 = long) */
674          31,                    /* bitsize */
675          TRUE,                  /* pc_relative */
676          0,                     /* bitpos */
677          complain_overflow_signed,/* complain_on_overflow */
678          bfd_elf_generic_reloc, /* special_function */
679          "R_ARM_PREL31",        /* name */
680          FALSE,                 /* partial_inplace */
681          0x7fffffff,            /* src_mask */
682          0x7fffffff,            /* dst_mask */
683          TRUE),                 /* pcrel_offset */
684
685   HOWTO (R_ARM_MOVW_ABS_NC,     /* type */
686          0,                     /* rightshift */
687          2,                     /* size (0 = byte, 1 = short, 2 = long) */
688          16,                    /* bitsize */
689          FALSE,                 /* pc_relative */
690          0,                     /* bitpos */
691          complain_overflow_dont,/* complain_on_overflow */
692          bfd_elf_generic_reloc, /* special_function */
693          "R_ARM_MOVW_ABS_NC",   /* name */
694          FALSE,                 /* partial_inplace */
695          0x000f0fff,            /* src_mask */
696          0x000f0fff,            /* dst_mask */
697          FALSE),                /* pcrel_offset */
698
699   HOWTO (R_ARM_MOVT_ABS,        /* type */
700          0,                     /* rightshift */
701          2,                     /* size (0 = byte, 1 = short, 2 = long) */
702          16,                    /* bitsize */
703          FALSE,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_bitfield,/* complain_on_overflow */
706          bfd_elf_generic_reloc, /* special_function */
707          "R_ARM_MOVT_ABS",      /* name */
708          FALSE,                 /* partial_inplace */
709          0x000f0fff,            /* src_mask */
710          0x000f0fff,            /* dst_mask */
711          FALSE),                /* pcrel_offset */
712
713   HOWTO (R_ARM_MOVW_PREL_NC,    /* type */
714          0,                     /* rightshift */
715          2,                     /* size (0 = byte, 1 = short, 2 = long) */
716          16,                    /* bitsize */
717          TRUE,                  /* pc_relative */
718          0,                     /* bitpos */
719          complain_overflow_dont,/* complain_on_overflow */
720          bfd_elf_generic_reloc, /* special_function */
721          "R_ARM_MOVW_PREL_NC",  /* name */
722          FALSE,                 /* partial_inplace */
723          0x000f0fff,            /* src_mask */
724          0x000f0fff,            /* dst_mask */
725          TRUE),                 /* pcrel_offset */
726
727   HOWTO (R_ARM_MOVT_PREL,       /* type */
728          0,                     /* rightshift */
729          2,                     /* size (0 = byte, 1 = short, 2 = long) */
730          16,                    /* bitsize */
731          TRUE,                  /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_bitfield,/* complain_on_overflow */
734          bfd_elf_generic_reloc, /* special_function */
735          "R_ARM_MOVT_PREL",     /* name */
736          FALSE,                 /* partial_inplace */
737          0x000f0fff,            /* src_mask */
738          0x000f0fff,            /* dst_mask */
739          TRUE),                 /* pcrel_offset */
740
741   HOWTO (R_ARM_THM_MOVW_ABS_NC, /* type */
742          0,                     /* rightshift */
743          2,                     /* size (0 = byte, 1 = short, 2 = long) */
744          16,                    /* bitsize */
745          FALSE,                 /* pc_relative */
746          0,                     /* bitpos */
747          complain_overflow_dont,/* complain_on_overflow */
748          bfd_elf_generic_reloc, /* special_function */
749          "R_ARM_THM_MOVW_ABS_NC",/* name */
750          FALSE,                 /* partial_inplace */
751          0x040f70ff,            /* src_mask */
752          0x040f70ff,            /* dst_mask */
753          FALSE),                /* pcrel_offset */
754
755   HOWTO (R_ARM_THM_MOVT_ABS,    /* type */
756          0,                     /* rightshift */
757          2,                     /* size (0 = byte, 1 = short, 2 = long) */
758          16,                    /* bitsize */
759          FALSE,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_bitfield,/* complain_on_overflow */
762          bfd_elf_generic_reloc, /* special_function */
763          "R_ARM_THM_MOVT_ABS",  /* name */
764          FALSE,                 /* partial_inplace */
765          0x040f70ff,            /* src_mask */
766          0x040f70ff,            /* dst_mask */
767          FALSE),                /* pcrel_offset */
768
769   HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
770          0,                     /* rightshift */
771          2,                     /* size (0 = byte, 1 = short, 2 = long) */
772          16,                    /* bitsize */
773          TRUE,                  /* pc_relative */
774          0,                     /* bitpos */
775          complain_overflow_dont,/* complain_on_overflow */
776          bfd_elf_generic_reloc, /* special_function */
777          "R_ARM_THM_MOVW_PREL_NC",/* name */
778          FALSE,                 /* partial_inplace */
779          0x040f70ff,            /* src_mask */
780          0x040f70ff,            /* dst_mask */
781          TRUE),                 /* pcrel_offset */
782
783   HOWTO (R_ARM_THM_MOVT_PREL,   /* type */
784          0,                     /* rightshift */
785          2,                     /* size (0 = byte, 1 = short, 2 = long) */
786          16,                    /* bitsize */
787          TRUE,                  /* pc_relative */
788          0,                     /* bitpos */
789          complain_overflow_bitfield,/* complain_on_overflow */
790          bfd_elf_generic_reloc, /* special_function */
791          "R_ARM_THM_MOVT_PREL", /* name */
792          FALSE,                 /* partial_inplace */
793          0x040f70ff,            /* src_mask */
794          0x040f70ff,            /* dst_mask */
795          TRUE),                 /* pcrel_offset */
796
797   HOWTO (R_ARM_THM_JUMP19,      /* type */
798          1,                     /* rightshift */
799          2,                     /* size (0 = byte, 1 = short, 2 = long) */
800          19,                    /* bitsize */
801          TRUE,                  /* pc_relative */
802          0,                     /* bitpos */
803          complain_overflow_signed,/* complain_on_overflow */
804          bfd_elf_generic_reloc, /* special_function */
805          "R_ARM_THM_JUMP19",    /* name */
806          FALSE,                 /* partial_inplace */
807          0x043f2fff,            /* src_mask */
808          0x043f2fff,            /* dst_mask */
809          TRUE),                 /* pcrel_offset */
810
811   HOWTO (R_ARM_THM_JUMP6,       /* type */
812          1,                     /* rightshift */
813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
814          6,                     /* bitsize */
815          TRUE,                  /* pc_relative */
816          0,                     /* bitpos */
817          complain_overflow_unsigned,/* complain_on_overflow */
818          bfd_elf_generic_reloc, /* special_function */
819          "R_ARM_THM_JUMP6",     /* name */
820          FALSE,                 /* partial_inplace */
821          0x02f8,                /* src_mask */
822          0x02f8,                /* dst_mask */
823          TRUE),                 /* pcrel_offset */
824
825   /* These are declared as 13-bit signed relocations because we can
826      address -4095 .. 4095(base) by altering ADDW to SUBW or vice
827      versa.  */
828   HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
829          0,                     /* rightshift */
830          2,                     /* size (0 = byte, 1 = short, 2 = long) */
831          13,                    /* bitsize */
832          TRUE,                  /* pc_relative */
833          0,                     /* bitpos */
834          complain_overflow_dont,/* complain_on_overflow */
835          bfd_elf_generic_reloc, /* special_function */
836          "R_ARM_THM_ALU_PREL_11_0",/* name */
837          FALSE,                 /* partial_inplace */
838          0xffffffff,            /* src_mask */
839          0xffffffff,            /* dst_mask */
840          TRUE),                 /* pcrel_offset */
841
842   HOWTO (R_ARM_THM_PC12,        /* type */
843          0,                     /* rightshift */
844          2,                     /* size (0 = byte, 1 = short, 2 = long) */
845          13,                    /* bitsize */
846          TRUE,                  /* pc_relative */
847          0,                     /* bitpos */
848          complain_overflow_dont,/* complain_on_overflow */
849          bfd_elf_generic_reloc, /* special_function */
850          "R_ARM_THM_PC12",      /* name */
851          FALSE,                 /* partial_inplace */
852          0xffffffff,            /* src_mask */
853          0xffffffff,            /* dst_mask */
854          TRUE),                 /* pcrel_offset */
855
856   HOWTO (R_ARM_ABS32_NOI,       /* type */
857          0,                     /* rightshift */
858          2,                     /* size (0 = byte, 1 = short, 2 = long) */
859          32,                    /* bitsize */
860          FALSE,                 /* pc_relative */
861          0,                     /* bitpos */
862          complain_overflow_dont,/* complain_on_overflow */
863          bfd_elf_generic_reloc, /* special_function */
864          "R_ARM_ABS32_NOI",     /* name */
865          FALSE,                 /* partial_inplace */
866          0xffffffff,            /* src_mask */
867          0xffffffff,            /* dst_mask */
868          FALSE),                /* pcrel_offset */
869
870   HOWTO (R_ARM_REL32_NOI,       /* type */
871          0,                     /* rightshift */
872          2,                     /* size (0 = byte, 1 = short, 2 = long) */
873          32,                    /* bitsize */
874          TRUE,                  /* pc_relative */
875          0,                     /* bitpos */
876          complain_overflow_dont,/* complain_on_overflow */
877          bfd_elf_generic_reloc, /* special_function */
878          "R_ARM_REL32_NOI",     /* name */
879          FALSE,                 /* partial_inplace */
880          0xffffffff,            /* src_mask */
881          0xffffffff,            /* dst_mask */
882          FALSE),                /* pcrel_offset */
883
884   /* Group relocations.  */
885
886   HOWTO (R_ARM_ALU_PC_G0_NC,    /* type */
887          0,                     /* rightshift */
888          2,                     /* size (0 = byte, 1 = short, 2 = long) */
889          32,                    /* bitsize */
890          TRUE,                  /* pc_relative */
891          0,                     /* bitpos */
892          complain_overflow_dont,/* complain_on_overflow */
893          bfd_elf_generic_reloc, /* special_function */
894          "R_ARM_ALU_PC_G0_NC",  /* name */
895          FALSE,                 /* partial_inplace */
896          0xffffffff,            /* src_mask */
897          0xffffffff,            /* dst_mask */
898          TRUE),                 /* pcrel_offset */
899
900   HOWTO (R_ARM_ALU_PC_G0,       /* type */
901          0,                     /* rightshift */
902          2,                     /* size (0 = byte, 1 = short, 2 = long) */
903          32,                    /* bitsize */
904          TRUE,                  /* pc_relative */
905          0,                     /* bitpos */
906          complain_overflow_dont,/* complain_on_overflow */
907          bfd_elf_generic_reloc, /* special_function */
908          "R_ARM_ALU_PC_G0",     /* name */
909          FALSE,                 /* partial_inplace */
910          0xffffffff,            /* src_mask */
911          0xffffffff,            /* dst_mask */
912          TRUE),                 /* pcrel_offset */
913
914   HOWTO (R_ARM_ALU_PC_G1_NC,    /* type */
915          0,                     /* rightshift */
916          2,                     /* size (0 = byte, 1 = short, 2 = long) */
917          32,                    /* bitsize */
918          TRUE,                  /* pc_relative */
919          0,                     /* bitpos */
920          complain_overflow_dont,/* complain_on_overflow */
921          bfd_elf_generic_reloc, /* special_function */
922          "R_ARM_ALU_PC_G1_NC",  /* name */
923          FALSE,                 /* partial_inplace */
924          0xffffffff,            /* src_mask */
925          0xffffffff,            /* dst_mask */
926          TRUE),                 /* pcrel_offset */
927
928   HOWTO (R_ARM_ALU_PC_G1,       /* type */
929          0,                     /* rightshift */
930          2,                     /* size (0 = byte, 1 = short, 2 = long) */
931          32,                    /* bitsize */
932          TRUE,                  /* pc_relative */
933          0,                     /* bitpos */
934          complain_overflow_dont,/* complain_on_overflow */
935          bfd_elf_generic_reloc, /* special_function */
936          "R_ARM_ALU_PC_G1",     /* name */
937          FALSE,                 /* partial_inplace */
938          0xffffffff,            /* src_mask */
939          0xffffffff,            /* dst_mask */
940          TRUE),                 /* pcrel_offset */
941
942   HOWTO (R_ARM_ALU_PC_G2,       /* type */
943          0,                     /* rightshift */
944          2,                     /* size (0 = byte, 1 = short, 2 = long) */
945          32,                    /* bitsize */
946          TRUE,                  /* pc_relative */
947          0,                     /* bitpos */
948          complain_overflow_dont,/* complain_on_overflow */
949          bfd_elf_generic_reloc, /* special_function */
950          "R_ARM_ALU_PC_G2",     /* name */
951          FALSE,                 /* partial_inplace */
952          0xffffffff,            /* src_mask */
953          0xffffffff,            /* dst_mask */
954          TRUE),                 /* pcrel_offset */
955
956   HOWTO (R_ARM_LDR_PC_G1,       /* type */
957          0,                     /* rightshift */
958          2,                     /* size (0 = byte, 1 = short, 2 = long) */
959          32,                    /* bitsize */
960          TRUE,                  /* pc_relative */
961          0,                     /* bitpos */
962          complain_overflow_dont,/* complain_on_overflow */
963          bfd_elf_generic_reloc, /* special_function */
964          "R_ARM_LDR_PC_G1",     /* name */
965          FALSE,                 /* partial_inplace */
966          0xffffffff,            /* src_mask */
967          0xffffffff,            /* dst_mask */
968          TRUE),                 /* pcrel_offset */
969
970   HOWTO (R_ARM_LDR_PC_G2,       /* type */
971          0,                     /* rightshift */
972          2,                     /* size (0 = byte, 1 = short, 2 = long) */
973          32,                    /* bitsize */
974          TRUE,                  /* pc_relative */
975          0,                     /* bitpos */
976          complain_overflow_dont,/* complain_on_overflow */
977          bfd_elf_generic_reloc, /* special_function */
978          "R_ARM_LDR_PC_G2",     /* name */
979          FALSE,                 /* partial_inplace */
980          0xffffffff,            /* src_mask */
981          0xffffffff,            /* dst_mask */
982          TRUE),                 /* pcrel_offset */
983
984   HOWTO (R_ARM_LDRS_PC_G0,      /* type */
985          0,                     /* rightshift */
986          2,                     /* size (0 = byte, 1 = short, 2 = long) */
987          32,                    /* bitsize */
988          TRUE,                  /* pc_relative */
989          0,                     /* bitpos */
990          complain_overflow_dont,/* complain_on_overflow */
991          bfd_elf_generic_reloc, /* special_function */
992          "R_ARM_LDRS_PC_G0",    /* name */
993          FALSE,                 /* partial_inplace */
994          0xffffffff,            /* src_mask */
995          0xffffffff,            /* dst_mask */
996          TRUE),                 /* pcrel_offset */
997
998   HOWTO (R_ARM_LDRS_PC_G1,      /* type */
999          0,                     /* rightshift */
1000          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1001          32,                    /* bitsize */
1002          TRUE,                  /* pc_relative */
1003          0,                     /* bitpos */
1004          complain_overflow_dont,/* complain_on_overflow */
1005          bfd_elf_generic_reloc, /* special_function */
1006          "R_ARM_LDRS_PC_G1",    /* name */
1007          FALSE,                 /* partial_inplace */
1008          0xffffffff,            /* src_mask */
1009          0xffffffff,            /* dst_mask */
1010          TRUE),                 /* pcrel_offset */
1011
1012   HOWTO (R_ARM_LDRS_PC_G2,      /* type */
1013          0,                     /* rightshift */
1014          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1015          32,                    /* bitsize */
1016          TRUE,                  /* pc_relative */
1017          0,                     /* bitpos */
1018          complain_overflow_dont,/* complain_on_overflow */
1019          bfd_elf_generic_reloc, /* special_function */
1020          "R_ARM_LDRS_PC_G2",    /* name */
1021          FALSE,                 /* partial_inplace */
1022          0xffffffff,            /* src_mask */
1023          0xffffffff,            /* dst_mask */
1024          TRUE),                 /* pcrel_offset */
1025
1026   HOWTO (R_ARM_LDC_PC_G0,       /* type */
1027          0,                     /* rightshift */
1028          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1029          32,                    /* bitsize */
1030          TRUE,                  /* pc_relative */
1031          0,                     /* bitpos */
1032          complain_overflow_dont,/* complain_on_overflow */
1033          bfd_elf_generic_reloc, /* special_function */
1034          "R_ARM_LDC_PC_G0",     /* name */
1035          FALSE,                 /* partial_inplace */
1036          0xffffffff,            /* src_mask */
1037          0xffffffff,            /* dst_mask */
1038          TRUE),                 /* pcrel_offset */
1039
1040   HOWTO (R_ARM_LDC_PC_G1,       /* type */
1041          0,                     /* rightshift */
1042          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1043          32,                    /* bitsize */
1044          TRUE,                  /* pc_relative */
1045          0,                     /* bitpos */
1046          complain_overflow_dont,/* complain_on_overflow */
1047          bfd_elf_generic_reloc, /* special_function */
1048          "R_ARM_LDC_PC_G1",     /* name */
1049          FALSE,                 /* partial_inplace */
1050          0xffffffff,            /* src_mask */
1051          0xffffffff,            /* dst_mask */
1052          TRUE),                 /* pcrel_offset */
1053
1054   HOWTO (R_ARM_LDC_PC_G2,       /* type */
1055          0,                     /* rightshift */
1056          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1057          32,                    /* bitsize */
1058          TRUE,                  /* pc_relative */
1059          0,                     /* bitpos */
1060          complain_overflow_dont,/* complain_on_overflow */
1061          bfd_elf_generic_reloc, /* special_function */
1062          "R_ARM_LDC_PC_G2",     /* name */
1063          FALSE,                 /* partial_inplace */
1064          0xffffffff,            /* src_mask */
1065          0xffffffff,            /* dst_mask */
1066          TRUE),                 /* pcrel_offset */
1067
1068   HOWTO (R_ARM_ALU_SB_G0_NC,    /* type */
1069          0,                     /* rightshift */
1070          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1071          32,                    /* bitsize */
1072          TRUE,                  /* pc_relative */
1073          0,                     /* bitpos */
1074          complain_overflow_dont,/* complain_on_overflow */
1075          bfd_elf_generic_reloc, /* special_function */
1076          "R_ARM_ALU_SB_G0_NC",  /* name */
1077          FALSE,                 /* partial_inplace */
1078          0xffffffff,            /* src_mask */
1079          0xffffffff,            /* dst_mask */
1080          TRUE),                 /* pcrel_offset */
1081
1082   HOWTO (R_ARM_ALU_SB_G0,       /* type */
1083          0,                     /* rightshift */
1084          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1085          32,                    /* bitsize */
1086          TRUE,                  /* pc_relative */
1087          0,                     /* bitpos */
1088          complain_overflow_dont,/* complain_on_overflow */
1089          bfd_elf_generic_reloc, /* special_function */
1090          "R_ARM_ALU_SB_G0",     /* name */
1091          FALSE,                 /* partial_inplace */
1092          0xffffffff,            /* src_mask */
1093          0xffffffff,            /* dst_mask */
1094          TRUE),                 /* pcrel_offset */
1095
1096   HOWTO (R_ARM_ALU_SB_G1_NC,    /* type */
1097          0,                     /* rightshift */
1098          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1099          32,                    /* bitsize */
1100          TRUE,                  /* pc_relative */
1101          0,                     /* bitpos */
1102          complain_overflow_dont,/* complain_on_overflow */
1103          bfd_elf_generic_reloc, /* special_function */
1104          "R_ARM_ALU_SB_G1_NC",  /* name */
1105          FALSE,                 /* partial_inplace */
1106          0xffffffff,            /* src_mask */
1107          0xffffffff,            /* dst_mask */
1108          TRUE),                 /* pcrel_offset */
1109
1110   HOWTO (R_ARM_ALU_SB_G1,       /* type */
1111          0,                     /* rightshift */
1112          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1113          32,                    /* bitsize */
1114          TRUE,                  /* pc_relative */
1115          0,                     /* bitpos */
1116          complain_overflow_dont,/* complain_on_overflow */
1117          bfd_elf_generic_reloc, /* special_function */
1118          "R_ARM_ALU_SB_G1",     /* name */
1119          FALSE,                 /* partial_inplace */
1120          0xffffffff,            /* src_mask */
1121          0xffffffff,            /* dst_mask */
1122          TRUE),                 /* pcrel_offset */
1123
1124   HOWTO (R_ARM_ALU_SB_G2,       /* type */
1125          0,                     /* rightshift */
1126          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1127          32,                    /* bitsize */
1128          TRUE,                  /* pc_relative */
1129          0,                     /* bitpos */
1130          complain_overflow_dont,/* complain_on_overflow */
1131          bfd_elf_generic_reloc, /* special_function */
1132          "R_ARM_ALU_SB_G2",     /* name */
1133          FALSE,                 /* partial_inplace */
1134          0xffffffff,            /* src_mask */
1135          0xffffffff,            /* dst_mask */
1136          TRUE),                 /* pcrel_offset */
1137
1138   HOWTO (R_ARM_LDR_SB_G0,       /* type */
1139          0,                     /* rightshift */
1140          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1141          32,                    /* bitsize */
1142          TRUE,                  /* pc_relative */
1143          0,                     /* bitpos */
1144          complain_overflow_dont,/* complain_on_overflow */
1145          bfd_elf_generic_reloc, /* special_function */
1146          "R_ARM_LDR_SB_G0",     /* name */
1147          FALSE,                 /* partial_inplace */
1148          0xffffffff,            /* src_mask */
1149          0xffffffff,            /* dst_mask */
1150          TRUE),                 /* pcrel_offset */
1151
1152   HOWTO (R_ARM_LDR_SB_G1,       /* type */
1153          0,                     /* rightshift */
1154          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1155          32,                    /* bitsize */
1156          TRUE,                  /* pc_relative */
1157          0,                     /* bitpos */
1158          complain_overflow_dont,/* complain_on_overflow */
1159          bfd_elf_generic_reloc, /* special_function */
1160          "R_ARM_LDR_SB_G1",     /* name */
1161          FALSE,                 /* partial_inplace */
1162          0xffffffff,            /* src_mask */
1163          0xffffffff,            /* dst_mask */
1164          TRUE),                 /* pcrel_offset */
1165
1166   HOWTO (R_ARM_LDR_SB_G2,       /* type */
1167          0,                     /* rightshift */
1168          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1169          32,                    /* bitsize */
1170          TRUE,                  /* pc_relative */
1171          0,                     /* bitpos */
1172          complain_overflow_dont,/* complain_on_overflow */
1173          bfd_elf_generic_reloc, /* special_function */
1174          "R_ARM_LDR_SB_G2",     /* name */
1175          FALSE,                 /* partial_inplace */
1176          0xffffffff,            /* src_mask */
1177          0xffffffff,            /* dst_mask */
1178          TRUE),                 /* pcrel_offset */
1179
1180   HOWTO (R_ARM_LDRS_SB_G0,      /* type */
1181          0,                     /* rightshift */
1182          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1183          32,                    /* bitsize */
1184          TRUE,                  /* pc_relative */
1185          0,                     /* bitpos */
1186          complain_overflow_dont,/* complain_on_overflow */
1187          bfd_elf_generic_reloc, /* special_function */
1188          "R_ARM_LDRS_SB_G0",    /* name */
1189          FALSE,                 /* partial_inplace */
1190          0xffffffff,            /* src_mask */
1191          0xffffffff,            /* dst_mask */
1192          TRUE),                 /* pcrel_offset */
1193
1194   HOWTO (R_ARM_LDRS_SB_G1,      /* type */
1195          0,                     /* rightshift */
1196          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1197          32,                    /* bitsize */
1198          TRUE,                  /* pc_relative */
1199          0,                     /* bitpos */
1200          complain_overflow_dont,/* complain_on_overflow */
1201          bfd_elf_generic_reloc, /* special_function */
1202          "R_ARM_LDRS_SB_G1",    /* name */
1203          FALSE,                 /* partial_inplace */
1204          0xffffffff,            /* src_mask */
1205          0xffffffff,            /* dst_mask */
1206          TRUE),                 /* pcrel_offset */
1207
1208   HOWTO (R_ARM_LDRS_SB_G2,      /* type */
1209          0,                     /* rightshift */
1210          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1211          32,                    /* bitsize */
1212          TRUE,                  /* pc_relative */
1213          0,                     /* bitpos */
1214          complain_overflow_dont,/* complain_on_overflow */
1215          bfd_elf_generic_reloc, /* special_function */
1216          "R_ARM_LDRS_SB_G2",    /* name */
1217          FALSE,                 /* partial_inplace */
1218          0xffffffff,            /* src_mask */
1219          0xffffffff,            /* dst_mask */
1220          TRUE),                 /* pcrel_offset */
1221
1222   HOWTO (R_ARM_LDC_SB_G0,       /* type */
1223          0,                     /* rightshift */
1224          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1225          32,                    /* bitsize */
1226          TRUE,                  /* pc_relative */
1227          0,                     /* bitpos */
1228          complain_overflow_dont,/* complain_on_overflow */
1229          bfd_elf_generic_reloc, /* special_function */
1230          "R_ARM_LDC_SB_G0",     /* name */
1231          FALSE,                 /* partial_inplace */
1232          0xffffffff,            /* src_mask */
1233          0xffffffff,            /* dst_mask */
1234          TRUE),                 /* pcrel_offset */
1235
1236   HOWTO (R_ARM_LDC_SB_G1,       /* type */
1237          0,                     /* rightshift */
1238          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1239          32,                    /* bitsize */
1240          TRUE,                  /* pc_relative */
1241          0,                     /* bitpos */
1242          complain_overflow_dont,/* complain_on_overflow */
1243          bfd_elf_generic_reloc, /* special_function */
1244          "R_ARM_LDC_SB_G1",     /* name */
1245          FALSE,                 /* partial_inplace */
1246          0xffffffff,            /* src_mask */
1247          0xffffffff,            /* dst_mask */
1248          TRUE),                 /* pcrel_offset */
1249
1250   HOWTO (R_ARM_LDC_SB_G2,       /* type */
1251          0,                     /* rightshift */
1252          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          32,                    /* bitsize */
1254          TRUE,                  /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_dont,/* complain_on_overflow */
1257          bfd_elf_generic_reloc, /* special_function */
1258          "R_ARM_LDC_SB_G2",     /* name */
1259          FALSE,                 /* partial_inplace */
1260          0xffffffff,            /* src_mask */
1261          0xffffffff,            /* dst_mask */
1262          TRUE),                 /* pcrel_offset */
1263
1264   /* End of group relocations.  */
1265
1266   HOWTO (R_ARM_MOVW_BREL_NC,    /* type */
1267          0,                     /* rightshift */
1268          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1269          16,                    /* bitsize */
1270          FALSE,                 /* pc_relative */
1271          0,                     /* bitpos */
1272          complain_overflow_dont,/* complain_on_overflow */
1273          bfd_elf_generic_reloc, /* special_function */
1274          "R_ARM_MOVW_BREL_NC",  /* name */
1275          FALSE,                 /* partial_inplace */
1276          0x0000ffff,            /* src_mask */
1277          0x0000ffff,            /* dst_mask */
1278          FALSE),                /* pcrel_offset */
1279
1280   HOWTO (R_ARM_MOVT_BREL,       /* type */
1281          0,                     /* rightshift */
1282          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1283          16,                    /* bitsize */
1284          FALSE,                 /* pc_relative */
1285          0,                     /* bitpos */
1286          complain_overflow_bitfield,/* complain_on_overflow */
1287          bfd_elf_generic_reloc, /* special_function */
1288          "R_ARM_MOVT_BREL",     /* name */
1289          FALSE,                 /* partial_inplace */
1290          0x0000ffff,            /* src_mask */
1291          0x0000ffff,            /* dst_mask */
1292          FALSE),                /* pcrel_offset */
1293
1294   HOWTO (R_ARM_MOVW_BREL,       /* type */
1295          0,                     /* rightshift */
1296          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1297          16,                    /* bitsize */
1298          FALSE,                 /* pc_relative */
1299          0,                     /* bitpos */
1300          complain_overflow_dont,/* complain_on_overflow */
1301          bfd_elf_generic_reloc, /* special_function */
1302          "R_ARM_MOVW_BREL",     /* name */
1303          FALSE,                 /* partial_inplace */
1304          0x0000ffff,            /* src_mask */
1305          0x0000ffff,            /* dst_mask */
1306          FALSE),                /* pcrel_offset */
1307
1308   HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
1309          0,                     /* rightshift */
1310          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1311          16,                    /* bitsize */
1312          FALSE,                 /* pc_relative */
1313          0,                     /* bitpos */
1314          complain_overflow_dont,/* complain_on_overflow */
1315          bfd_elf_generic_reloc, /* special_function */
1316          "R_ARM_THM_MOVW_BREL_NC",/* name */
1317          FALSE,                 /* partial_inplace */
1318          0x040f70ff,            /* src_mask */
1319          0x040f70ff,            /* dst_mask */
1320          FALSE),                /* pcrel_offset */
1321
1322   HOWTO (R_ARM_THM_MOVT_BREL,   /* type */
1323          0,                     /* rightshift */
1324          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1325          16,                    /* bitsize */
1326          FALSE,                 /* pc_relative */
1327          0,                     /* bitpos */
1328          complain_overflow_bitfield,/* complain_on_overflow */
1329          bfd_elf_generic_reloc, /* special_function */
1330          "R_ARM_THM_MOVT_BREL", /* name */
1331          FALSE,                 /* partial_inplace */
1332          0x040f70ff,            /* src_mask */
1333          0x040f70ff,            /* dst_mask */
1334          FALSE),                /* pcrel_offset */
1335
1336   HOWTO (R_ARM_THM_MOVW_BREL,   /* type */
1337          0,                     /* rightshift */
1338          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1339          16,                    /* bitsize */
1340          FALSE,                 /* pc_relative */
1341          0,                     /* bitpos */
1342          complain_overflow_dont,/* complain_on_overflow */
1343          bfd_elf_generic_reloc, /* special_function */
1344          "R_ARM_THM_MOVW_BREL", /* name */
1345          FALSE,                 /* partial_inplace */
1346          0x040f70ff,            /* src_mask */
1347          0x040f70ff,            /* dst_mask */
1348          FALSE),                /* pcrel_offset */
1349
1350   EMPTY_HOWTO (90),   /* Unallocated.  */
1351   EMPTY_HOWTO (91),
1352   EMPTY_HOWTO (92),
1353   EMPTY_HOWTO (93),
1354
1355   HOWTO (R_ARM_PLT32_ABS,       /* type */
1356          0,                     /* rightshift */
1357          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1358          32,                    /* bitsize */
1359          FALSE,                 /* pc_relative */
1360          0,                     /* bitpos */
1361          complain_overflow_dont,/* complain_on_overflow */
1362          bfd_elf_generic_reloc, /* special_function */
1363          "R_ARM_PLT32_ABS",     /* name */
1364          FALSE,                 /* partial_inplace */
1365          0xffffffff,            /* src_mask */
1366          0xffffffff,            /* dst_mask */
1367          FALSE),                /* pcrel_offset */
1368
1369   HOWTO (R_ARM_GOT_ABS,         /* type */
1370          0,                     /* rightshift */
1371          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1372          32,                    /* bitsize */
1373          FALSE,                 /* pc_relative */
1374          0,                     /* bitpos */
1375          complain_overflow_dont,/* complain_on_overflow */
1376          bfd_elf_generic_reloc, /* special_function */
1377          "R_ARM_GOT_ABS",       /* name */
1378          FALSE,                 /* partial_inplace */
1379          0xffffffff,            /* src_mask */
1380          0xffffffff,            /* dst_mask */
1381          FALSE),                        /* pcrel_offset */
1382
1383   HOWTO (R_ARM_GOT_PREL,        /* type */
1384          0,                     /* rightshift */
1385          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1386          32,                    /* bitsize */
1387          TRUE,                  /* pc_relative */
1388          0,                     /* bitpos */
1389          complain_overflow_dont,        /* complain_on_overflow */
1390          bfd_elf_generic_reloc, /* special_function */
1391          "R_ARM_GOT_PREL",      /* name */
1392          FALSE,                 /* partial_inplace */
1393          0xffffffff,            /* src_mask */
1394          0xffffffff,            /* dst_mask */
1395          TRUE),                 /* pcrel_offset */
1396
1397   HOWTO (R_ARM_GOT_BREL12,      /* type */
1398          0,                     /* rightshift */
1399          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1400          12,                    /* bitsize */
1401          FALSE,                 /* pc_relative */
1402          0,                     /* bitpos */
1403          complain_overflow_bitfield,/* complain_on_overflow */
1404          bfd_elf_generic_reloc, /* special_function */
1405          "R_ARM_GOT_BREL12",    /* name */
1406          FALSE,                 /* partial_inplace */
1407          0x00000fff,            /* src_mask */
1408          0x00000fff,            /* dst_mask */
1409          FALSE),                /* pcrel_offset */
1410
1411   HOWTO (R_ARM_GOTOFF12,        /* type */
1412          0,                     /* rightshift */
1413          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1414          12,                    /* bitsize */
1415          FALSE,                 /* pc_relative */
1416          0,                     /* bitpos */
1417          complain_overflow_bitfield,/* complain_on_overflow */
1418          bfd_elf_generic_reloc, /* special_function */
1419          "R_ARM_GOTOFF12",      /* name */
1420          FALSE,                 /* partial_inplace */
1421          0x00000fff,            /* src_mask */
1422          0x00000fff,            /* dst_mask */
1423          FALSE),                /* pcrel_offset */
1424
1425   EMPTY_HOWTO (R_ARM_GOTRELAX),  /* reserved for future GOT-load optimizations */
1426
1427   /* GNU extension to record C++ vtable member usage */
1428   HOWTO (R_ARM_GNU_VTENTRY,     /* type */
1429          0,                     /* rightshift */
1430          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1431          0,                     /* bitsize */
1432          FALSE,                 /* pc_relative */
1433          0,                     /* bitpos */
1434          complain_overflow_dont, /* complain_on_overflow */
1435          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
1436          "R_ARM_GNU_VTENTRY",   /* name */
1437          FALSE,                 /* partial_inplace */
1438          0,                     /* src_mask */
1439          0,                     /* dst_mask */
1440          FALSE),                /* pcrel_offset */
1441
1442   /* GNU extension to record C++ vtable hierarchy */
1443   HOWTO (R_ARM_GNU_VTINHERIT, /* type */
1444          0,                     /* rightshift */
1445          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1446          0,                     /* bitsize */
1447          FALSE,                 /* pc_relative */
1448          0,                     /* bitpos */
1449          complain_overflow_dont, /* complain_on_overflow */
1450          NULL,                  /* special_function */
1451          "R_ARM_GNU_VTINHERIT", /* name */
1452          FALSE,                 /* partial_inplace */
1453          0,                     /* src_mask */
1454          0,                     /* dst_mask */
1455          FALSE),                /* pcrel_offset */
1456
1457   HOWTO (R_ARM_THM_JUMP11,      /* type */
1458          1,                     /* rightshift */
1459          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1460          11,                    /* bitsize */
1461          TRUE,                  /* pc_relative */
1462          0,                     /* bitpos */
1463          complain_overflow_signed,      /* complain_on_overflow */
1464          bfd_elf_generic_reloc, /* special_function */
1465          "R_ARM_THM_JUMP11",    /* name */
1466          FALSE,                 /* partial_inplace */
1467          0x000007ff,            /* src_mask */
1468          0x000007ff,            /* dst_mask */
1469          TRUE),                 /* pcrel_offset */
1470
1471   HOWTO (R_ARM_THM_JUMP8,       /* type */
1472          1,                     /* rightshift */
1473          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1474          8,                     /* bitsize */
1475          TRUE,                  /* pc_relative */
1476          0,                     /* bitpos */
1477          complain_overflow_signed,      /* complain_on_overflow */
1478          bfd_elf_generic_reloc, /* special_function */
1479          "R_ARM_THM_JUMP8",     /* name */
1480          FALSE,                 /* partial_inplace */
1481          0x000000ff,            /* src_mask */
1482          0x000000ff,            /* dst_mask */
1483          TRUE),                 /* pcrel_offset */
1484
1485   /* TLS relocations */
1486   HOWTO (R_ARM_TLS_GD32,        /* type */
1487          0,                     /* rightshift */
1488          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1489          32,                    /* bitsize */
1490          FALSE,                 /* pc_relative */
1491          0,                     /* bitpos */
1492          complain_overflow_bitfield,/* complain_on_overflow */
1493          NULL,                  /* special_function */
1494          "R_ARM_TLS_GD32",      /* name */
1495          TRUE,                  /* partial_inplace */
1496          0xffffffff,            /* src_mask */
1497          0xffffffff,            /* dst_mask */
1498          FALSE),                /* pcrel_offset */
1499
1500   HOWTO (R_ARM_TLS_LDM32,       /* type */
1501          0,                     /* rightshift */
1502          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1503          32,                    /* bitsize */
1504          FALSE,                 /* pc_relative */
1505          0,                     /* bitpos */
1506          complain_overflow_bitfield,/* complain_on_overflow */
1507          bfd_elf_generic_reloc, /* special_function */
1508          "R_ARM_TLS_LDM32",     /* name */
1509          TRUE,                  /* partial_inplace */
1510          0xffffffff,            /* src_mask */
1511          0xffffffff,            /* dst_mask */
1512          FALSE),                /* pcrel_offset */
1513
1514   HOWTO (R_ARM_TLS_LDO32,       /* type */
1515          0,                     /* rightshift */
1516          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1517          32,                    /* bitsize */
1518          FALSE,                 /* pc_relative */
1519          0,                     /* bitpos */
1520          complain_overflow_bitfield,/* complain_on_overflow */
1521          bfd_elf_generic_reloc, /* special_function */
1522          "R_ARM_TLS_LDO32",     /* name */
1523          TRUE,                  /* partial_inplace */
1524          0xffffffff,            /* src_mask */
1525          0xffffffff,            /* dst_mask */
1526          FALSE),                /* pcrel_offset */
1527
1528   HOWTO (R_ARM_TLS_IE32,        /* type */
1529          0,                     /* rightshift */
1530          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1531          32,                    /* bitsize */
1532          FALSE,                  /* pc_relative */
1533          0,                     /* bitpos */
1534          complain_overflow_bitfield,/* complain_on_overflow */
1535          NULL,                  /* special_function */
1536          "R_ARM_TLS_IE32",      /* name */
1537          TRUE,                  /* partial_inplace */
1538          0xffffffff,            /* src_mask */
1539          0xffffffff,            /* dst_mask */
1540          FALSE),                /* pcrel_offset */
1541
1542   HOWTO (R_ARM_TLS_LE32,        /* type */
1543          0,                     /* rightshift */
1544          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1545          32,                    /* bitsize */
1546          FALSE,                 /* pc_relative */
1547          0,                     /* bitpos */
1548          complain_overflow_bitfield,/* complain_on_overflow */
1549          bfd_elf_generic_reloc, /* special_function */
1550          "R_ARM_TLS_LE32",      /* name */
1551          TRUE,                  /* partial_inplace */
1552          0xffffffff,            /* src_mask */
1553          0xffffffff,            /* dst_mask */
1554          FALSE),                /* pcrel_offset */
1555
1556   HOWTO (R_ARM_TLS_LDO12,       /* type */
1557          0,                     /* rightshift */
1558          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1559          12,                    /* bitsize */
1560          FALSE,                 /* pc_relative */
1561          0,                     /* bitpos */
1562          complain_overflow_bitfield,/* complain_on_overflow */
1563          bfd_elf_generic_reloc, /* special_function */
1564          "R_ARM_TLS_LDO12",     /* name */
1565          FALSE,                 /* partial_inplace */
1566          0x00000fff,            /* src_mask */
1567          0x00000fff,            /* dst_mask */
1568          FALSE),                /* pcrel_offset */
1569
1570   HOWTO (R_ARM_TLS_LE12,        /* type */
1571          0,                     /* rightshift */
1572          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1573          12,                    /* bitsize */
1574          FALSE,                 /* pc_relative */
1575          0,                     /* bitpos */
1576          complain_overflow_bitfield,/* complain_on_overflow */
1577          bfd_elf_generic_reloc, /* special_function */
1578          "R_ARM_TLS_LE12",      /* name */
1579          FALSE,                 /* partial_inplace */
1580          0x00000fff,            /* src_mask */
1581          0x00000fff,            /* dst_mask */
1582          FALSE),                /* pcrel_offset */
1583
1584   HOWTO (R_ARM_TLS_IE12GP,      /* type */
1585          0,                     /* rightshift */
1586          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1587          12,                    /* bitsize */
1588          FALSE,                 /* pc_relative */
1589          0,                     /* bitpos */
1590          complain_overflow_bitfield,/* complain_on_overflow */
1591          bfd_elf_generic_reloc, /* special_function */
1592          "R_ARM_TLS_IE12GP",    /* name */
1593          FALSE,                 /* partial_inplace */
1594          0x00000fff,            /* src_mask */
1595          0x00000fff,            /* dst_mask */
1596          FALSE),                /* pcrel_offset */
1597 };
1598
1599 /* 112-127 private relocations
1600    128 R_ARM_ME_TOO, obsolete
1601    129-255 unallocated in AAELF.
1602
1603    249-255 extended, currently unused, relocations:  */
1604
1605 static reloc_howto_type elf32_arm_howto_table_2[4] =
1606 {
1607   HOWTO (R_ARM_RREL32,          /* type */
1608          0,                     /* rightshift */
1609          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1610          0,                     /* bitsize */
1611          FALSE,                 /* pc_relative */
1612          0,                     /* bitpos */
1613          complain_overflow_dont,/* complain_on_overflow */
1614          bfd_elf_generic_reloc, /* special_function */
1615          "R_ARM_RREL32",        /* name */
1616          FALSE,                 /* partial_inplace */
1617          0,                     /* src_mask */
1618          0,                     /* dst_mask */
1619          FALSE),                /* pcrel_offset */
1620
1621   HOWTO (R_ARM_RABS32,          /* type */
1622          0,                     /* rightshift */
1623          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1624          0,                     /* bitsize */
1625          FALSE,                 /* pc_relative */
1626          0,                     /* bitpos */
1627          complain_overflow_dont,/* complain_on_overflow */
1628          bfd_elf_generic_reloc, /* special_function */
1629          "R_ARM_RABS32",        /* name */
1630          FALSE,                 /* partial_inplace */
1631          0,                     /* src_mask */
1632          0,                     /* dst_mask */
1633          FALSE),                /* pcrel_offset */
1634
1635   HOWTO (R_ARM_RPC24,           /* type */
1636          0,                     /* rightshift */
1637          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1638          0,                     /* bitsize */
1639          FALSE,                 /* pc_relative */
1640          0,                     /* bitpos */
1641          complain_overflow_dont,/* complain_on_overflow */
1642          bfd_elf_generic_reloc, /* special_function */
1643          "R_ARM_RPC24",         /* name */
1644          FALSE,                 /* partial_inplace */
1645          0,                     /* src_mask */
1646          0,                     /* dst_mask */
1647          FALSE),                /* pcrel_offset */
1648
1649   HOWTO (R_ARM_RBASE,           /* type */
1650          0,                     /* rightshift */
1651          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1652          0,                     /* bitsize */
1653          FALSE,                 /* pc_relative */
1654          0,                     /* bitpos */
1655          complain_overflow_dont,/* complain_on_overflow */
1656          bfd_elf_generic_reloc, /* special_function */
1657          "R_ARM_RBASE",         /* name */
1658          FALSE,                 /* partial_inplace */
1659          0,                     /* src_mask */
1660          0,                     /* dst_mask */
1661          FALSE)                 /* pcrel_offset */
1662 };
1663
1664 static reloc_howto_type *
1665 elf32_arm_howto_from_type (unsigned int r_type)
1666 {
1667   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1668     return &elf32_arm_howto_table_1[r_type];
1669
1670   if (r_type >= R_ARM_RREL32
1671       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_2))
1672     return &elf32_arm_howto_table_2[r_type - R_ARM_RREL32];
1673
1674   return NULL;
1675 }
1676
1677 static void
1678 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
1679                          Elf_Internal_Rela * elf_reloc)
1680 {
1681   unsigned int r_type;
1682
1683   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1684   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
1685 }
1686
1687 struct elf32_arm_reloc_map
1688   {
1689     bfd_reloc_code_real_type  bfd_reloc_val;
1690     unsigned char             elf_reloc_val;
1691   };
1692
1693 /* All entries in this list must also be present in elf32_arm_howto_table.  */
1694 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1695   {
1696     {BFD_RELOC_NONE,                 R_ARM_NONE},
1697     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1698     {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
1699     {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
1700     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1701     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1702     {BFD_RELOC_32,                   R_ARM_ABS32},
1703     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1704     {BFD_RELOC_8,                    R_ARM_ABS8},
1705     {BFD_RELOC_16,                   R_ARM_ABS16},
1706     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1707     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1708     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1709     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1710     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1711     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1712     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1713     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1714     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1715     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1716     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1717     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1718     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1719     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1720     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1721     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
1722     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
1723     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
1724     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
1725     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
1726     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1727     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
1728     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
1729     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
1730     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
1731     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
1732     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
1733     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
1734     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
1735     {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
1736     {BFD_RELOC_VTABLE_ENTRY,         R_ARM_GNU_VTENTRY},
1737     {BFD_RELOC_ARM_MOVW,             R_ARM_MOVW_ABS_NC},
1738     {BFD_RELOC_ARM_MOVT,             R_ARM_MOVT_ABS},
1739     {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
1740     {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
1741     {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
1742     {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
1743     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
1744     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
1745     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
1746     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
1747     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
1748     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
1749     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
1750     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
1751     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
1752     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
1753     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
1754     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
1755     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
1756     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
1757     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
1758     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
1759     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
1760     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
1761     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
1762     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
1763     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
1764     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
1765     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
1766     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
1767     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
1768     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
1769     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
1770     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
1771     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
1772     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
1773     {BFD_RELOC_ARM_V4BX,             R_ARM_V4BX}
1774   };
1775
1776 static reloc_howto_type *
1777 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1778                              bfd_reloc_code_real_type code)
1779 {
1780   unsigned int i;
1781
1782   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
1783     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1784       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
1785
1786   return NULL;
1787 }
1788
1789 static reloc_howto_type *
1790 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1791                              const char *r_name)
1792 {
1793   unsigned int i;
1794
1795   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
1796     if (elf32_arm_howto_table_1[i].name != NULL
1797         && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
1798       return &elf32_arm_howto_table_1[i];
1799
1800   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
1801     if (elf32_arm_howto_table_2[i].name != NULL
1802         && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
1803       return &elf32_arm_howto_table_2[i];
1804
1805   return NULL;
1806 }
1807
1808 /* Support for core dump NOTE sections.  */
1809
1810 static bfd_boolean
1811 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1812 {
1813   int offset;
1814   size_t size;
1815
1816   switch (note->descsz)
1817     {
1818       default:
1819         return FALSE;
1820
1821       case 148:         /* Linux/ARM 32-bit.  */
1822         /* pr_cursig */
1823         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1824
1825         /* pr_pid */
1826         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1827
1828         /* pr_reg */
1829         offset = 72;
1830         size = 72;
1831
1832         break;
1833     }
1834
1835   /* Make a ".reg/999" section.  */
1836   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1837                                           size, note->descpos + offset);
1838 }
1839
1840 static bfd_boolean
1841 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1842 {
1843   switch (note->descsz)
1844     {
1845       default:
1846         return FALSE;
1847
1848       case 124:         /* Linux/ARM elf_prpsinfo.  */
1849         elf_tdata (abfd)->core_program
1850          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1851         elf_tdata (abfd)->core_command
1852          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1853     }
1854
1855   /* Note that for some reason, a spurious space is tacked
1856      onto the end of the args in some (at least one anyway)
1857      implementations, so strip it off if it exists.  */
1858   {
1859     char *command = elf_tdata (abfd)->core_command;
1860     int n = strlen (command);
1861
1862     if (0 < n && command[n - 1] == ' ')
1863       command[n - 1] = '\0';
1864   }
1865
1866   return TRUE;
1867 }
1868
1869 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
1870 #define TARGET_LITTLE_NAME              "elf32-littlearm"
1871 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
1872 #define TARGET_BIG_NAME                 "elf32-bigarm"
1873
1874 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
1875 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
1876
1877 typedef unsigned long int insn32;
1878 typedef unsigned short int insn16;
1879
1880 /* In lieu of proper flags, assume all EABIv4 or later objects are
1881    interworkable.  */
1882 #define INTERWORK_FLAG(abfd)  \
1883   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
1884   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
1885
1886 /* The linker script knows the section names for placement.
1887    The entry_names are used to do simple name mangling on the stubs.
1888    Given a function name, and its type, the stub can be found. The
1889    name can be changed. The only requirement is the %s be present.  */
1890 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1891 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
1892
1893 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1894 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
1895
1896 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
1897 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
1898
1899 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
1900 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
1901
1902 #define STUB_ENTRY_NAME   "__%s_veneer"
1903
1904 /* The name of the dynamic interpreter.  This is put in the .interp
1905    section.  */
1906 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1907
1908 #ifdef FOUR_WORD_PLT
1909
1910 /* The first entry in a procedure linkage table looks like
1911    this.  It is set up so that any shared library function that is
1912    called before the relocation has been set up calls the dynamic
1913    linker first.  */
1914 static const bfd_vma elf32_arm_plt0_entry [] =
1915   {
1916     0xe52de004,         /* str   lr, [sp, #-4]! */
1917     0xe59fe010,         /* ldr   lr, [pc, #16]  */
1918     0xe08fe00e,         /* add   lr, pc, lr     */
1919     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1920   };
1921
1922 /* Subsequent entries in a procedure linkage table look like
1923    this.  */
1924 static const bfd_vma elf32_arm_plt_entry [] =
1925   {
1926     0xe28fc600,         /* add   ip, pc, #NN    */
1927     0xe28cca00,         /* add   ip, ip, #NN    */
1928     0xe5bcf000,         /* ldr   pc, [ip, #NN]! */
1929     0x00000000,         /* unused               */
1930   };
1931
1932 #else
1933
1934 /* The first entry in a procedure linkage table looks like
1935    this.  It is set up so that any shared library function that is
1936    called before the relocation has been set up calls the dynamic
1937    linker first.  */
1938 static const bfd_vma elf32_arm_plt0_entry [] =
1939   {
1940     0xe52de004,         /* str   lr, [sp, #-4]! */
1941     0xe59fe004,         /* ldr   lr, [pc, #4]   */
1942     0xe08fe00e,         /* add   lr, pc, lr     */
1943     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1944     0x00000000,         /* &GOT[0] - .          */
1945   };
1946
1947 /* Subsequent entries in a procedure linkage table look like
1948    this.  */
1949 static const bfd_vma elf32_arm_plt_entry [] =
1950   {
1951     0xe28fc600,         /* add   ip, pc, #0xNN00000 */
1952     0xe28cca00,         /* add   ip, ip, #0xNN000   */
1953     0xe5bcf000,         /* ldr   pc, [ip, #0xNNN]!  */
1954   };
1955
1956 #endif
1957
1958 /* The format of the first entry in the procedure linkage table
1959    for a VxWorks executable.  */
1960 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
1961   {
1962     0xe52dc008,         /* str    ip,[sp,#-8]!                  */
1963     0xe59fc000,         /* ldr    ip,[pc]                       */
1964     0xe59cf008,         /* ldr    pc,[ip,#8]                    */
1965     0x00000000,         /* .long  _GLOBAL_OFFSET_TABLE_         */
1966   };
1967
1968 /* The format of subsequent entries in a VxWorks executable.  */
1969 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
1970   {
1971     0xe59fc000,         /* ldr    ip,[pc]                       */
1972     0xe59cf000,         /* ldr    pc,[ip]                       */
1973     0x00000000,         /* .long  @got                          */
1974     0xe59fc000,         /* ldr    ip,[pc]                       */
1975     0xea000000,         /* b      _PLT                          */
1976     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1977   };
1978
1979 /* The format of entries in a VxWorks shared library.  */
1980 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
1981   {
1982     0xe59fc000,         /* ldr    ip,[pc]                       */
1983     0xe79cf009,         /* ldr    pc,[ip,r9]                    */
1984     0x00000000,         /* .long  @got                          */
1985     0xe59fc000,         /* ldr    ip,[pc]                       */
1986     0xe599f008,         /* ldr    pc,[r9,#8]                    */
1987     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1988   };
1989
1990 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
1991 #define PLT_THUMB_STUB_SIZE 4
1992 static const bfd_vma elf32_arm_plt_thumb_stub [] =
1993   {
1994     0x4778,             /* bx pc */
1995     0x46c0              /* nop   */
1996   };
1997
1998 /* The entries in a PLT when using a DLL-based target with multiple
1999    address spaces.  */
2000 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2001   {
2002     0xe51ff004,         /* ldr   pc, [pc, #-4] */
2003     0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2004   };
2005
2006 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2007 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2008 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2009 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2010 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2011 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2012
2013 static const bfd_vma arm_long_branch_stub[] =
2014   {
2015     0xe51ff004,         /* ldr   pc, [pc, #-4] */
2016     0x00000000,         /* dcd   R_ARM_ABS32(X) */
2017   };
2018
2019 static const bfd_vma arm_thumb_v4t_long_branch_stub[] =
2020   {
2021     0xe59fc000,         /* ldr   ip, [pc, #0] */
2022     0xe12fff1c,         /* bx    ip */
2023     0x00000000,         /* dcd   R_ARM_ABS32(X) */
2024   };
2025
2026 static const bfd_vma arm_thumb_thumb_long_branch_stub[] =
2027   {
2028     0x4e02b540,         /* push {r6, lr} */
2029                         /* ldr  r6, [pc, #8] */
2030     0x473046fe,         /* mov  lr, pc */
2031                         /* bx   r6 */
2032     0xbf00bd40,         /* pop  {r6, pc} */
2033                         /* nop */
2034     0x00000000,         /* dcd  R_ARM_ABS32(X) */
2035   };
2036
2037 static const bfd_vma arm_thumb_arm_v4t_long_branch_stub[] =
2038   {
2039     0x4e03b540,         /* push {r6, lr} */
2040                         /* ldr  r6, [pc, #12] */
2041     0x473046fe,         /* mov  lr, pc */
2042                         /* bx   r6 */
2043     0xe8bd4040,         /* pop  {r6, pc} */
2044     0xe12fff1e,         /* bx   lr */
2045     0x00000000,         /* dcd  R_ARM_ABS32(X) */
2046   };
2047
2048 static const bfd_vma arm_pic_long_branch_stub[] =
2049   {
2050     0xe59fc000,         /* ldr   r12, [pc] */
2051     0xe08ff00c,         /* add   pc, pc, ip */
2052     0x00000000,         /* dcd   R_ARM_REL32(X) */
2053   };
2054
2055 /* Section name for stubs is the associated section name plus this
2056    string.  */
2057 #define STUB_SUFFIX ".stub"
2058
2059 enum elf32_arm_stub_type
2060 {
2061   arm_stub_none,
2062   arm_stub_long_branch,
2063   arm_thumb_v4t_stub_long_branch,
2064   arm_thumb_thumb_stub_long_branch,
2065   arm_thumb_arm_v4t_stub_long_branch,
2066   arm_stub_pic_long_branch,
2067 };
2068
2069 struct elf32_arm_stub_hash_entry
2070 {
2071   /* Base hash table entry structure.  */
2072   struct bfd_hash_entry root;
2073
2074   /* The stub section.  */
2075   asection *stub_sec;
2076
2077   /* Offset within stub_sec of the beginning of this stub.  */
2078   bfd_vma stub_offset;
2079
2080   /* Given the symbol's value and its section we can determine its final
2081      value when building the stubs (so the stub knows where to jump).  */
2082   bfd_vma target_value;
2083   asection *target_section;
2084
2085   enum elf32_arm_stub_type stub_type;
2086
2087   /* The symbol table entry, if any, that this was derived from.  */
2088   struct elf32_arm_link_hash_entry *h;
2089
2090   /* Destination symbol type (STT_ARM_TFUNC, ...) */
2091   unsigned char st_type;
2092
2093   /* Where this stub is being called from, or, in the case of combined
2094      stub sections, the first input section in the group.  */
2095   asection *id_sec;
2096
2097   /* The name for the local symbol at the start of this stub.  The
2098      stub name in the hash table has to be unique; this does not, so
2099      it can be friendlier.  */
2100   char *output_name;
2101 };
2102
2103 /* Used to build a map of a section.  This is required for mixed-endian
2104    code/data.  */
2105
2106 typedef struct elf32_elf_section_map
2107 {
2108   bfd_vma vma;
2109   char type;
2110 }
2111 elf32_arm_section_map;
2112
2113 /* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2114
2115 typedef enum
2116 {
2117   VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2118   VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2119   VFP11_ERRATUM_ARM_VENEER,
2120   VFP11_ERRATUM_THUMB_VENEER
2121 }
2122 elf32_vfp11_erratum_type;
2123
2124 typedef struct elf32_vfp11_erratum_list
2125 {
2126   struct elf32_vfp11_erratum_list *next;
2127   bfd_vma vma;
2128   union
2129   {
2130     struct
2131     {
2132       struct elf32_vfp11_erratum_list *veneer;
2133       unsigned int vfp_insn;
2134     } b;
2135     struct
2136     {
2137       struct elf32_vfp11_erratum_list *branch;
2138       unsigned int id;
2139     } v;
2140   } u;
2141   elf32_vfp11_erratum_type type;
2142 }
2143 elf32_vfp11_erratum_list;
2144
2145 typedef struct _arm_elf_section_data
2146 {
2147   struct bfd_elf_section_data elf;
2148   unsigned int mapcount;
2149   unsigned int mapsize;
2150   elf32_arm_section_map *map;
2151   unsigned int erratumcount;
2152   elf32_vfp11_erratum_list *erratumlist;
2153 }
2154 _arm_elf_section_data;
2155
2156 #define elf32_arm_section_data(sec) \
2157   ((_arm_elf_section_data *) elf_section_data (sec))
2158
2159 /* The size of the thread control block.  */
2160 #define TCB_SIZE        8
2161
2162 struct elf_arm_obj_tdata
2163 {
2164   struct elf_obj_tdata root;
2165
2166   /* tls_type for each local got entry.  */
2167   char *local_got_tls_type;
2168
2169   /* Zero to warn when linking objects with incompatible enum sizes.  */
2170   int no_enum_size_warning;
2171
2172   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
2173   int no_wchar_size_warning;
2174 };
2175
2176 #define elf_arm_tdata(bfd) \
2177   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
2178
2179 #define elf32_arm_local_got_tls_type(bfd) \
2180   (elf_arm_tdata (bfd)->local_got_tls_type)
2181
2182 #define is_arm_elf(bfd) \
2183   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2184    && elf_tdata (bfd) != NULL \
2185    && elf_object_id (bfd) == ARM_ELF_TDATA)
2186
2187 static bfd_boolean
2188 elf32_arm_mkobject (bfd *abfd)
2189 {
2190   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
2191                                   ARM_ELF_TDATA);
2192 }
2193
2194 /* The ARM linker needs to keep track of the number of relocs that it
2195    decides to copy in check_relocs for each symbol.  This is so that
2196    it can discard PC relative relocs if it doesn't need them when
2197    linking with -Bsymbolic.  We store the information in a field
2198    extending the regular ELF linker hash table.  */
2199
2200 /* This structure keeps track of the number of relocs we have copied
2201    for a given symbol.  */
2202 struct elf32_arm_relocs_copied
2203   {
2204     /* Next section.  */
2205     struct elf32_arm_relocs_copied * next;
2206     /* A section in dynobj.  */
2207     asection * section;
2208     /* Number of relocs copied in this section.  */
2209     bfd_size_type count;
2210     /* Number of PC-relative relocs copied in this section.  */
2211     bfd_size_type pc_count;
2212   };
2213
2214 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2215
2216 /* Arm ELF linker hash entry.  */
2217 struct elf32_arm_link_hash_entry
2218   {
2219     struct elf_link_hash_entry root;
2220
2221     /* Number of PC relative relocs copied for this symbol.  */
2222     struct elf32_arm_relocs_copied * relocs_copied;
2223
2224     /* We reference count Thumb references to a PLT entry separately,
2225        so that we can emit the Thumb trampoline only if needed.  */
2226     bfd_signed_vma plt_thumb_refcount;
2227
2228     /* Some references from Thumb code may be eliminated by BL->BLX
2229        conversion, so record them separately.  */
2230     bfd_signed_vma plt_maybe_thumb_refcount;
2231
2232     /* Since PLT entries have variable size if the Thumb prologue is
2233        used, we need to record the index into .got.plt instead of
2234        recomputing it from the PLT offset.  */
2235     bfd_signed_vma plt_got_offset;
2236
2237 #define GOT_UNKNOWN     0
2238 #define GOT_NORMAL      1
2239 #define GOT_TLS_GD      2
2240 #define GOT_TLS_IE      4
2241     unsigned char tls_type;
2242
2243     /* The symbol marking the real symbol location for exported thumb
2244        symbols with Arm stubs.  */
2245     struct elf_link_hash_entry *export_glue;
2246
2247    /* A pointer to the most recently used stub hash entry against this
2248      symbol.  */
2249     struct elf32_arm_stub_hash_entry *stub_cache;
2250   };
2251
2252 /* Traverse an arm ELF linker hash table.  */
2253 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2254   (elf_link_hash_traverse                                               \
2255    (&(table)->root,                                                     \
2256     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2257     (info)))
2258
2259 /* Get the ARM elf linker hash table from a link_info structure.  */
2260 #define elf32_arm_hash_table(info) \
2261   ((struct elf32_arm_link_hash_table *) ((info)->hash))
2262
2263 #define arm_stub_hash_lookup(table, string, create, copy) \
2264   ((struct elf32_arm_stub_hash_entry *) \
2265    bfd_hash_lookup ((table), (string), (create), (copy)))
2266
2267 /* ARM ELF linker hash table.  */
2268 struct elf32_arm_link_hash_table
2269 {
2270   /* The main hash table.  */
2271   struct elf_link_hash_table root;
2272
2273   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
2274   bfd_size_type thumb_glue_size;
2275
2276   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
2277   bfd_size_type arm_glue_size;
2278
2279   /* The size in bytes of section containing the ARMv4 BX veneers.  */
2280   bfd_size_type bx_glue_size;
2281
2282   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
2283      veneer has been populated.  */
2284   bfd_vma bx_glue_offset[15];
2285
2286   /* The size in bytes of the section containing glue for VFP11 erratum
2287      veneers.  */
2288   bfd_size_type vfp11_erratum_glue_size;
2289
2290   /* An arbitrary input BFD chosen to hold the glue sections.  */
2291   bfd * bfd_of_glue_owner;
2292
2293   /* Nonzero to output a BE8 image.  */
2294   int byteswap_code;
2295
2296   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
2297      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
2298   int target1_is_rel;
2299
2300   /* The relocation to use for R_ARM_TARGET2 relocations.  */
2301   int target2_reloc;
2302
2303   /* 0 = Ignore R_ARM_V4BX.
2304      1 = Convert BX to MOV PC.
2305      2 = Generate v4 interworing stubs.  */
2306   int fix_v4bx;
2307
2308   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
2309   int use_blx;
2310
2311   /* What sort of code sequences we should look for which may trigger the
2312      VFP11 denorm erratum.  */
2313   bfd_arm_vfp11_fix vfp11_fix;
2314
2315   /* Global counter for the number of fixes we have emitted.  */
2316   int num_vfp11_fixes;
2317
2318   /* Nonzero to force PIC branch veneers.  */
2319   int pic_veneer;
2320
2321   /* The number of bytes in the initial entry in the PLT.  */
2322   bfd_size_type plt_header_size;
2323
2324   /* The number of bytes in the subsequent PLT etries.  */
2325   bfd_size_type plt_entry_size;
2326
2327   /* True if the target system is VxWorks.  */
2328   int vxworks_p;
2329
2330   /* True if the target system is Symbian OS.  */
2331   int symbian_p;
2332
2333   /* True if the target uses REL relocations.  */
2334   int use_rel;
2335
2336   /* Short-cuts to get to dynamic linker sections.  */
2337   asection *sgot;
2338   asection *sgotplt;
2339   asection *srelgot;
2340   asection *splt;
2341   asection *srelplt;
2342   asection *sdynbss;
2343   asection *srelbss;
2344
2345   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2346   asection *srelplt2;
2347
2348   /* Data for R_ARM_TLS_LDM32 relocations.  */
2349   union
2350   {
2351     bfd_signed_vma refcount;
2352     bfd_vma offset;
2353   } tls_ldm_got;
2354
2355   /* Small local sym to section mapping cache.  */
2356   struct sym_sec_cache sym_sec;
2357
2358   /* For convenience in allocate_dynrelocs.  */
2359   bfd * obfd;
2360
2361   /* The stub hash table.  */
2362   struct bfd_hash_table stub_hash_table;
2363
2364   /* Linker stub bfd.  */
2365   bfd *stub_bfd;
2366
2367   /* Linker call-backs.  */
2368   asection * (*add_stub_section) (const char *, asection *);
2369   void (*layout_sections_again) (void);
2370
2371   /* Array to keep track of which stub sections have been created, and
2372      information on stub grouping.  */
2373   struct map_stub
2374   {
2375     /* This is the section to which stubs in the group will be
2376        attached.  */
2377     asection *link_sec;
2378     /* The stub section.  */
2379     asection *stub_sec;
2380   } *stub_group;
2381
2382   /* Assorted information used by elf32_arm_size_stubs.  */
2383   unsigned int bfd_count;
2384   int top_index;
2385   asection **input_list;
2386 };
2387
2388 /* Create an entry in an ARM ELF linker hash table.  */
2389
2390 static struct bfd_hash_entry *
2391 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
2392                              struct bfd_hash_table * table,
2393                              const char * string)
2394 {
2395   struct elf32_arm_link_hash_entry * ret =
2396     (struct elf32_arm_link_hash_entry *) entry;
2397
2398   /* Allocate the structure if it has not already been allocated by a
2399      subclass.  */
2400   if (ret == NULL)
2401     ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
2402   if (ret == NULL)
2403     return (struct bfd_hash_entry *) ret;
2404
2405   /* Call the allocation method of the superclass.  */
2406   ret = ((struct elf32_arm_link_hash_entry *)
2407          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2408                                      table, string));
2409   if (ret != NULL)
2410     {
2411       ret->relocs_copied = NULL;
2412       ret->tls_type = GOT_UNKNOWN;
2413       ret->plt_thumb_refcount = 0;
2414       ret->plt_maybe_thumb_refcount = 0;
2415       ret->plt_got_offset = -1;
2416       ret->export_glue = NULL;
2417
2418       ret->stub_cache = NULL;
2419     }
2420
2421   return (struct bfd_hash_entry *) ret;
2422 }
2423
2424 /* Initialize an entry in the stub hash table.  */
2425
2426 static struct bfd_hash_entry *
2427 stub_hash_newfunc (struct bfd_hash_entry *entry,
2428                    struct bfd_hash_table *table,
2429                    const char *string)
2430 {
2431   /* Allocate the structure if it has not already been allocated by a
2432      subclass.  */
2433   if (entry == NULL)
2434     {
2435       entry = bfd_hash_allocate (table,
2436                                  sizeof (struct elf32_arm_stub_hash_entry));
2437       if (entry == NULL)
2438         return entry;
2439     }
2440
2441   /* Call the allocation method of the superclass.  */
2442   entry = bfd_hash_newfunc (entry, table, string);
2443   if (entry != NULL)
2444     {
2445       struct elf32_arm_stub_hash_entry *eh;
2446
2447       /* Initialize the local fields.  */
2448       eh = (struct elf32_arm_stub_hash_entry *) entry;
2449       eh->stub_sec = NULL;
2450       eh->stub_offset = 0;
2451       eh->target_value = 0;
2452       eh->target_section = NULL;
2453       eh->stub_type = arm_stub_none;
2454       eh->h = NULL;
2455       eh->id_sec = NULL;
2456     }
2457
2458   return entry;
2459 }
2460
2461 /* Return true if NAME is the name of the relocation section associated
2462    with S.  */
2463
2464 static bfd_boolean
2465 reloc_section_p (struct elf32_arm_link_hash_table *htab,
2466                  const char *name, asection *s)
2467 {
2468   if (htab->use_rel)
2469     return CONST_STRNEQ (name, ".rel") && strcmp (s->name, name + 4) == 0;
2470   else
2471     return CONST_STRNEQ (name, ".rela") && strcmp (s->name, name + 5) == 0;
2472 }
2473
2474 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
2475    shortcuts to them in our hash table.  */
2476
2477 static bfd_boolean
2478 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2479 {
2480   struct elf32_arm_link_hash_table *htab;
2481
2482   htab = elf32_arm_hash_table (info);
2483   /* BPABI objects never have a GOT, or associated sections.  */
2484   if (htab->symbian_p)
2485     return TRUE;
2486
2487   if (! _bfd_elf_create_got_section (dynobj, info))
2488     return FALSE;
2489
2490   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2491   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2492   if (!htab->sgot || !htab->sgotplt)
2493     abort ();
2494
2495   htab->srelgot = bfd_make_section_with_flags (dynobj,
2496                                                RELOC_SECTION (htab, ".got"),
2497                                                (SEC_ALLOC | SEC_LOAD
2498                                                 | SEC_HAS_CONTENTS
2499                                                 | SEC_IN_MEMORY
2500                                                 | SEC_LINKER_CREATED
2501                                                 | SEC_READONLY));
2502   if (htab->srelgot == NULL
2503       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
2504     return FALSE;
2505   return TRUE;
2506 }
2507
2508 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
2509    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
2510    hash table.  */
2511
2512 static bfd_boolean
2513 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2514 {
2515   struct elf32_arm_link_hash_table *htab;
2516
2517   htab = elf32_arm_hash_table (info);
2518   if (!htab->sgot && !create_got_section (dynobj, info))
2519     return FALSE;
2520
2521   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2522     return FALSE;
2523
2524   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2525   htab->srelplt = bfd_get_section_by_name (dynobj,
2526                                            RELOC_SECTION (htab, ".plt"));
2527   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2528   if (!info->shared)
2529     htab->srelbss = bfd_get_section_by_name (dynobj,
2530                                              RELOC_SECTION (htab, ".bss"));
2531
2532   if (htab->vxworks_p)
2533     {
2534       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
2535         return FALSE;
2536
2537       if (info->shared)
2538         {
2539           htab->plt_header_size = 0;
2540           htab->plt_entry_size
2541             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
2542         }
2543       else
2544         {
2545           htab->plt_header_size
2546             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
2547           htab->plt_entry_size
2548             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
2549         }
2550     }
2551
2552   if (!htab->splt
2553       || !htab->srelplt
2554       || !htab->sdynbss
2555       || (!info->shared && !htab->srelbss))
2556     abort ();
2557
2558   return TRUE;
2559 }
2560
2561 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2562
2563 static void
2564 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
2565                                 struct elf_link_hash_entry *dir,
2566                                 struct elf_link_hash_entry *ind)
2567 {
2568   struct elf32_arm_link_hash_entry *edir, *eind;
2569
2570   edir = (struct elf32_arm_link_hash_entry *) dir;
2571   eind = (struct elf32_arm_link_hash_entry *) ind;
2572
2573   if (eind->relocs_copied != NULL)
2574     {
2575       if (edir->relocs_copied != NULL)
2576         {
2577           struct elf32_arm_relocs_copied **pp;
2578           struct elf32_arm_relocs_copied *p;
2579
2580           /* Add reloc counts against the indirect sym to the direct sym
2581              list.  Merge any entries against the same section.  */
2582           for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
2583             {
2584               struct elf32_arm_relocs_copied *q;
2585
2586               for (q = edir->relocs_copied; q != NULL; q = q->next)
2587                 if (q->section == p->section)
2588                   {
2589                     q->pc_count += p->pc_count;
2590                     q->count += p->count;
2591                     *pp = p->next;
2592                     break;
2593                   }
2594               if (q == NULL)
2595                 pp = &p->next;
2596             }
2597           *pp = edir->relocs_copied;
2598         }
2599
2600       edir->relocs_copied = eind->relocs_copied;
2601       eind->relocs_copied = NULL;
2602     }
2603
2604   if (ind->root.type == bfd_link_hash_indirect)
2605     {
2606       /* Copy over PLT info.  */
2607       edir->plt_thumb_refcount += eind->plt_thumb_refcount;
2608       eind->plt_thumb_refcount = 0;
2609       edir->plt_maybe_thumb_refcount += eind->plt_maybe_thumb_refcount;
2610       eind->plt_maybe_thumb_refcount = 0;
2611
2612       if (dir->got.refcount <= 0)
2613         {
2614           edir->tls_type = eind->tls_type;
2615           eind->tls_type = GOT_UNKNOWN;
2616         }
2617     }
2618
2619   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2620 }
2621
2622 /* Create an ARM elf linker hash table.  */
2623
2624 static struct bfd_link_hash_table *
2625 elf32_arm_link_hash_table_create (bfd *abfd)
2626 {
2627   struct elf32_arm_link_hash_table *ret;
2628   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
2629
2630   ret = bfd_malloc (amt);
2631   if (ret == NULL)
2632     return NULL;
2633
2634   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
2635                                       elf32_arm_link_hash_newfunc,
2636                                       sizeof (struct elf32_arm_link_hash_entry)))
2637     {
2638       free (ret);
2639       return NULL;
2640     }
2641
2642   ret->sgot = NULL;
2643   ret->sgotplt = NULL;
2644   ret->srelgot = NULL;
2645   ret->splt = NULL;
2646   ret->srelplt = NULL;
2647   ret->sdynbss = NULL;
2648   ret->srelbss = NULL;
2649   ret->srelplt2 = NULL;
2650   ret->thumb_glue_size = 0;
2651   ret->arm_glue_size = 0;
2652   ret->bx_glue_size = 0;
2653   memset (ret->bx_glue_offset, 0, sizeof (ret->bx_glue_offset));
2654   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
2655   ret->vfp11_erratum_glue_size = 0;
2656   ret->num_vfp11_fixes = 0;
2657   ret->bfd_of_glue_owner = NULL;
2658   ret->byteswap_code = 0;
2659   ret->target1_is_rel = 0;
2660   ret->target2_reloc = R_ARM_NONE;
2661 #ifdef FOUR_WORD_PLT
2662   ret->plt_header_size = 16;
2663   ret->plt_entry_size = 16;
2664 #else
2665   ret->plt_header_size = 20;
2666   ret->plt_entry_size = 12;
2667 #endif
2668   ret->fix_v4bx = 0;
2669   ret->use_blx = 0;
2670   ret->vxworks_p = 0;
2671   ret->symbian_p = 0;
2672   ret->use_rel = 1;
2673   ret->sym_sec.abfd = NULL;
2674   ret->obfd = abfd;
2675   ret->tls_ldm_got.refcount = 0;
2676   ret->stub_bfd = NULL;
2677   ret->add_stub_section = NULL;
2678   ret->layout_sections_again = NULL;
2679   ret->stub_group = NULL;
2680   ret->bfd_count = 0;
2681   ret->top_index = 0;
2682   ret->input_list = NULL;
2683
2684   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2685                             sizeof (struct elf32_arm_stub_hash_entry)))
2686     {
2687       free (ret);
2688       return NULL;
2689     }
2690
2691   return &ret->root.root;
2692 }
2693
2694 /* Free the derived linker hash table.  */
2695
2696 static void
2697 elf32_arm_hash_table_free (struct bfd_link_hash_table *hash)
2698 {
2699   struct elf32_arm_link_hash_table *ret
2700     = (struct elf32_arm_link_hash_table *) hash;
2701
2702   bfd_hash_table_free (&ret->stub_hash_table);
2703   _bfd_generic_link_hash_table_free (hash);
2704 }
2705
2706 /* Determine if we're dealing with a Thumb only architecture.  */
2707
2708 static bfd_boolean
2709 using_thumb_only (struct elf32_arm_link_hash_table *globals)
2710 {
2711   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2712                                        Tag_CPU_arch);
2713   int profile;
2714
2715   if (arch != TAG_CPU_ARCH_V7)
2716     return FALSE;
2717
2718   profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2719                                       Tag_CPU_arch_profile);
2720
2721   return profile == 'M';
2722 }
2723
2724 /* Determine if we're dealing with a Thumb-2 object.  */
2725
2726 static bfd_boolean
2727 using_thumb2 (struct elf32_arm_link_hash_table *globals)
2728 {
2729   int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
2730                                        Tag_CPU_arch);
2731   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
2732 }
2733
2734 static bfd_boolean
2735 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
2736 {
2737   switch (stub_type)
2738     {
2739     case arm_thumb_thumb_stub_long_branch:
2740     case arm_thumb_arm_v4t_stub_long_branch:
2741       return TRUE;
2742     case arm_stub_none:
2743       BFD_FAIL ();
2744       return FALSE;
2745       break;
2746     default:
2747       return FALSE;
2748     }
2749 }
2750
2751 /* Determine the type of stub needed, if any, for a call.  */
2752
2753 static enum elf32_arm_stub_type
2754 arm_type_of_stub (struct bfd_link_info *info,
2755                   asection *input_sec,
2756                   const Elf_Internal_Rela *rel,
2757                   unsigned char st_type,
2758                   struct elf32_arm_link_hash_entry *hash,
2759                   bfd_vma destination)
2760 {
2761   bfd_vma location;
2762   bfd_signed_vma branch_offset;
2763   unsigned int r_type;
2764   struct elf32_arm_link_hash_table * globals;
2765   int thumb2;
2766   int thumb_only;
2767   enum elf32_arm_stub_type stub_type = arm_stub_none;
2768
2769   /* We don't know the actual type of destination in case it is of
2770      type STT_SECTION: give up.  */
2771   if (st_type == STT_SECTION)
2772     return stub_type;
2773
2774   globals = elf32_arm_hash_table (info);
2775
2776   thumb_only = using_thumb_only (globals);
2777
2778   thumb2 = using_thumb2 (globals);
2779
2780   /* Determine where the call point is.  */
2781   location = (input_sec->output_offset
2782               + input_sec->output_section->vma
2783               + rel->r_offset);
2784
2785   branch_offset = (bfd_signed_vma)(destination - location);
2786
2787   r_type = ELF32_R_TYPE (rel->r_info);
2788
2789   /* If the call will go through a PLT entry then we do not need
2790      glue.  */
2791   if (globals->splt != NULL && hash != NULL && hash->root.plt.offset != (bfd_vma) -1)
2792     return stub_type;
2793
2794   if (r_type == R_ARM_THM_CALL)
2795     {
2796       if ((!thumb2
2797             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2798                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2799           || (thumb2
2800               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2801                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2802           || ((st_type != STT_ARM_TFUNC) && !globals->use_blx))
2803         {
2804           if (st_type == STT_ARM_TFUNC)
2805             {
2806               /* Thumb to thumb.  */
2807               if (!thumb_only)
2808                 {
2809                   stub_type = (info->shared | globals->pic_veneer)
2810                     ? ((globals->use_blx)
2811                        ? arm_stub_pic_long_branch
2812                        : arm_stub_none)
2813                     : (globals->use_blx)
2814                     ? arm_stub_long_branch
2815                     : arm_stub_none;
2816                 }
2817               else
2818                 {
2819                   stub_type = (info->shared | globals->pic_veneer)
2820                     ? arm_stub_none
2821                     : (globals->use_blx)
2822                     ? arm_thumb_thumb_stub_long_branch
2823                     : arm_stub_none;
2824                 }
2825             }
2826           else
2827             {
2828               /* Thumb to arm.  */
2829               stub_type = (info->shared | globals->pic_veneer)
2830                 ? ((globals->use_blx)
2831                    ? arm_stub_pic_long_branch
2832                    : arm_stub_none)
2833                 : (globals->use_blx)
2834                 ? arm_stub_long_branch
2835                 : arm_thumb_arm_v4t_stub_long_branch;
2836             }
2837         }
2838     }
2839   else if (r_type == R_ARM_CALL)
2840     {
2841       if (st_type == STT_ARM_TFUNC)
2842         {
2843           /* Arm to thumb.  */
2844           /* We have an extra 2-bytes reach because of the mode change
2845              (bit 24 (H) of BLX encoding).  */
2846           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2847               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2848               || !globals->use_blx)
2849             {
2850               stub_type = (info->shared | globals->pic_veneer)
2851                 ? arm_stub_pic_long_branch
2852                 : (globals->use_blx)
2853                 ? arm_stub_long_branch
2854                 : arm_thumb_v4t_stub_long_branch;
2855             }
2856         }
2857       else
2858         {
2859           /* Arm to arm.  */
2860           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2861               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2862             {
2863               stub_type = (info->shared | globals->pic_veneer)
2864                 ? arm_stub_pic_long_branch
2865                 : arm_stub_long_branch;
2866             }
2867         }
2868     }
2869
2870   return stub_type;
2871 }
2872
2873 /* Build a name for an entry in the stub hash table.  */
2874
2875 static char *
2876 elf32_arm_stub_name (const asection *input_section,
2877                      const asection *sym_sec,
2878                      const struct elf32_arm_link_hash_entry *hash,
2879                      const Elf_Internal_Rela *rel)
2880 {
2881   char *stub_name;
2882   bfd_size_type len;
2883
2884   if (hash)
2885     {
2886       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1;
2887       stub_name = bfd_malloc (len);
2888       if (stub_name != NULL)
2889         sprintf (stub_name, "%08x_%s+%x",
2890                  input_section->id & 0xffffffff,
2891                  hash->root.root.root.string,
2892                  (int) rel->r_addend & 0xffffffff);
2893     }
2894   else
2895     {
2896       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
2897       stub_name = bfd_malloc (len);
2898       if (stub_name != NULL)
2899         sprintf (stub_name, "%08x_%x:%x+%x",
2900                  input_section->id & 0xffffffff,
2901                  sym_sec->id & 0xffffffff,
2902                  (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2903                  (int) rel->r_addend & 0xffffffff);
2904     }
2905
2906   return stub_name;
2907 }
2908
2909 /* Look up an entry in the stub hash.  Stub entries are cached because
2910    creating the stub name takes a bit of time.  */
2911
2912 static struct elf32_arm_stub_hash_entry *
2913 elf32_arm_get_stub_entry (const asection *input_section,
2914                           const asection *sym_sec,
2915                           struct elf_link_hash_entry *hash,
2916                           const Elf_Internal_Rela *rel,
2917                           struct elf32_arm_link_hash_table *htab)
2918 {
2919   struct elf32_arm_stub_hash_entry *stub_entry;
2920   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
2921   const asection *id_sec;
2922
2923   if ((input_section->flags & SEC_CODE) == 0)
2924     return NULL;
2925
2926   /* If this input section is part of a group of sections sharing one
2927      stub section, then use the id of the first section in the group.
2928      Stub names need to include a section id, as there may well be
2929      more than one stub used to reach say, printf, and we need to
2930      distinguish between them.  */
2931   id_sec = htab->stub_group[input_section->id].link_sec;
2932
2933   if (h != NULL && h->stub_cache != NULL
2934       && h->stub_cache->h == h
2935       && h->stub_cache->id_sec == id_sec)
2936     {
2937       stub_entry = h->stub_cache;
2938     }
2939   else
2940     {
2941       char *stub_name;
2942
2943       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel);
2944       if (stub_name == NULL)
2945         return NULL;
2946
2947       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
2948                                         stub_name, FALSE, FALSE);
2949       if (h != NULL)
2950         h->stub_cache = stub_entry;
2951
2952       free (stub_name);
2953     }
2954
2955   return stub_entry;
2956 }
2957
2958 /* Add a new stub entry to the stub hash.  Not all fields of the new
2959    stub entry are initialised.  */
2960
2961 static struct elf32_arm_stub_hash_entry *
2962 elf32_arm_add_stub (const char *stub_name,
2963                     asection *section,
2964                     struct elf32_arm_link_hash_table *htab)
2965 {
2966   asection *link_sec;
2967   asection *stub_sec;
2968   struct elf32_arm_stub_hash_entry *stub_entry;
2969
2970   link_sec = htab->stub_group[section->id].link_sec;
2971   stub_sec = htab->stub_group[section->id].stub_sec;
2972   if (stub_sec == NULL)
2973     {
2974       stub_sec = htab->stub_group[link_sec->id].stub_sec;
2975       if (stub_sec == NULL)
2976         {
2977           size_t namelen;
2978           bfd_size_type len;
2979           char *s_name;
2980
2981           namelen = strlen (link_sec->name);
2982           len = namelen + sizeof (STUB_SUFFIX);
2983           s_name = bfd_alloc (htab->stub_bfd, len);
2984           if (s_name == NULL)
2985             return NULL;
2986
2987           memcpy (s_name, link_sec->name, namelen);
2988           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2989           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2990           if (stub_sec == NULL)
2991             return NULL;
2992           htab->stub_group[link_sec->id].stub_sec = stub_sec;
2993         }
2994       htab->stub_group[section->id].stub_sec = stub_sec;
2995     }
2996
2997   /* Enter this entry into the linker stub hash table.  */
2998   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2999                                      TRUE, FALSE);
3000   if (stub_entry == NULL)
3001     {
3002       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3003                              section->owner,
3004                              stub_name);
3005       return NULL;
3006     }
3007
3008   stub_entry->stub_sec = stub_sec;
3009   stub_entry->stub_offset = 0;
3010   stub_entry->id_sec = link_sec;
3011
3012   return stub_entry;
3013 }
3014
3015 /* Store an Arm insn into an output section not processed by
3016    elf32_arm_write_section.  */
3017
3018 static void
3019 put_arm_insn (struct elf32_arm_link_hash_table * htab,
3020               bfd * output_bfd, bfd_vma val, void * ptr)
3021 {
3022   if (htab->byteswap_code != bfd_little_endian (output_bfd))
3023     bfd_putl32 (val, ptr);
3024   else
3025     bfd_putb32 (val, ptr);
3026 }
3027
3028 /* Store a 16-bit Thumb insn into an output section not processed by
3029    elf32_arm_write_section.  */
3030
3031 static void
3032 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
3033                 bfd * output_bfd, bfd_vma val, void * ptr)
3034 {
3035   if (htab->byteswap_code != bfd_little_endian (output_bfd))
3036     bfd_putl16 (val, ptr);
3037   else
3038     bfd_putb16 (val, ptr);
3039 }
3040
3041 static bfd_boolean
3042 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
3043                     void * in_arg)
3044 {
3045   struct elf32_arm_stub_hash_entry *stub_entry;
3046   struct bfd_link_info *info;
3047   struct elf32_arm_link_hash_table *htab;
3048   asection *stub_sec;
3049   bfd *stub_bfd;
3050   bfd_vma stub_addr;
3051   bfd_byte *loc;
3052   bfd_vma sym_value;
3053   int template_size;
3054   int size;
3055   const bfd_vma *template;
3056   int i;
3057   struct elf32_arm_link_hash_table * globals;
3058
3059   /* Massage our args to the form they really have.  */
3060   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
3061   info = (struct bfd_link_info *) in_arg;
3062
3063   globals = elf32_arm_hash_table (info);
3064
3065   htab = elf32_arm_hash_table (info);
3066   stub_sec = stub_entry->stub_sec;
3067
3068   /* Make a note of the offset within the stubs for this entry.  */
3069   stub_entry->stub_offset = stub_sec->size;
3070   loc = stub_sec->contents + stub_entry->stub_offset;
3071
3072   stub_bfd = stub_sec->owner;
3073
3074   /* This is the address of the start of the stub.  */
3075   stub_addr = stub_sec->output_section->vma + stub_sec->output_offset
3076     + stub_entry->stub_offset;
3077
3078   /* This is the address of the stub destination.  */
3079   sym_value = (stub_entry->target_value
3080                + stub_entry->target_section->output_offset
3081                + stub_entry->target_section->output_section->vma);
3082
3083   switch (stub_entry->stub_type)
3084     {
3085     case arm_stub_long_branch:
3086       template = arm_long_branch_stub;
3087       template_size = (sizeof (arm_long_branch_stub) / sizeof (bfd_vma)) * 4;
3088       break;
3089     case arm_thumb_v4t_stub_long_branch:
3090       template =  arm_thumb_v4t_long_branch_stub;
3091       template_size = (sizeof (arm_thumb_v4t_long_branch_stub) / sizeof (bfd_vma)) * 4;
3092       break;
3093     case arm_thumb_thumb_stub_long_branch:
3094       template =  arm_thumb_thumb_long_branch_stub;
3095       template_size = (sizeof (arm_thumb_thumb_long_branch_stub) / sizeof (bfd_vma)) * 4;
3096       break;
3097     case arm_thumb_arm_v4t_stub_long_branch:
3098       template =  arm_thumb_arm_v4t_long_branch_stub;
3099       template_size = (sizeof (arm_thumb_arm_v4t_long_branch_stub) / sizeof (bfd_vma)) * 4;
3100       break;
3101     case arm_stub_pic_long_branch:
3102       template = arm_pic_long_branch_stub;
3103       template_size = (sizeof (arm_pic_long_branch_stub) / sizeof (bfd_vma)) * 4;
3104       break;
3105     default:
3106       BFD_FAIL ();
3107       return FALSE;
3108     }
3109
3110   size = 0;
3111   for (i = 0; i < (template_size / 4); i++)
3112     {
3113       /* A 0 pattern is a placeholder, every other pattern is an
3114          instruction.  */
3115       if (template[i] != 0)
3116         put_arm_insn (globals, stub_bfd, template[i], loc + size);
3117       else
3118         bfd_put_32 (stub_bfd, template[i], loc + size);
3119
3120       size += 4;
3121     }
3122   stub_sec->size += size;
3123
3124   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
3125   if (stub_entry->st_type == STT_ARM_TFUNC)
3126     sym_value |= 1;
3127
3128   switch (stub_entry->stub_type)
3129     {
3130     case arm_stub_long_branch:
3131       _bfd_final_link_relocate (elf32_arm_howto_from_type (R_ARM_ABS32),
3132                                 stub_bfd, stub_sec, stub_sec->contents,
3133                                 stub_entry->stub_offset + 4, sym_value, 0);
3134       break;
3135     case arm_thumb_v4t_stub_long_branch:
3136       _bfd_final_link_relocate (elf32_arm_howto_from_type (R_ARM_ABS32),
3137                                 stub_bfd, stub_sec, stub_sec->contents,
3138                                 stub_entry->stub_offset + 8, sym_value, 0);
3139       break;
3140     case arm_thumb_thumb_stub_long_branch:
3141       _bfd_final_link_relocate (elf32_arm_howto_from_type (R_ARM_ABS32),
3142                                 stub_bfd, stub_sec, stub_sec->contents,
3143                                 stub_entry->stub_offset + 12, sym_value, 0);
3144       break;
3145     case arm_thumb_arm_v4t_stub_long_branch:
3146       _bfd_final_link_relocate (elf32_arm_howto_from_type (R_ARM_ABS32),
3147                                 stub_bfd, stub_sec, stub_sec->contents,
3148                                 stub_entry->stub_offset + 16, sym_value, 0);
3149       break;
3150     case arm_stub_pic_long_branch:
3151       /* We want the value relative to the address 8 bytes from the
3152          start of the stub.  */
3153       _bfd_final_link_relocate (elf32_arm_howto_from_type (R_ARM_REL32),
3154                                 stub_bfd, stub_sec, stub_sec->contents,
3155                                 stub_entry->stub_offset + 8, sym_value, 0);
3156       break;
3157     default:
3158       break;
3159     }
3160
3161   return TRUE;
3162 }
3163
3164 /* As above, but don't actually build the stub.  Just bump offset so
3165    we know stub section sizes.  */
3166
3167 static bfd_boolean
3168 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
3169                    void * in_arg)
3170 {
3171   struct elf32_arm_stub_hash_entry *stub_entry;
3172   struct elf32_arm_link_hash_table *htab;
3173   const bfd_vma *template;
3174   int template_size;
3175   int size;
3176   int i;
3177
3178   /* Massage our args to the form they really have.  */
3179   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
3180   htab = (struct elf32_arm_link_hash_table *) in_arg;
3181
3182   switch (stub_entry->stub_type)
3183     {
3184     case arm_stub_long_branch:
3185       template =  arm_long_branch_stub;
3186       template_size = (sizeof (arm_long_branch_stub) / sizeof (bfd_vma)) * 4;
3187       break;
3188     case arm_thumb_v4t_stub_long_branch:
3189       template =  arm_thumb_v4t_long_branch_stub;
3190       template_size = (sizeof (arm_thumb_v4t_long_branch_stub) / sizeof (bfd_vma)) * 4;
3191       break;
3192     case arm_thumb_thumb_stub_long_branch:
3193       template =  arm_thumb_thumb_long_branch_stub;
3194       template_size = (sizeof (arm_thumb_thumb_long_branch_stub) / sizeof (bfd_vma)) * 4;
3195       break;
3196     case arm_thumb_arm_v4t_stub_long_branch:
3197       template =  arm_thumb_arm_v4t_long_branch_stub;
3198       template_size = (sizeof (arm_thumb_arm_v4t_long_branch_stub) / sizeof (bfd_vma)) * 4;
3199       break;
3200     case arm_stub_pic_long_branch:
3201       template = arm_pic_long_branch_stub;
3202       template_size = (sizeof (arm_pic_long_branch_stub) / sizeof (bfd_vma)) * 4;
3203       break;
3204     default:
3205       BFD_FAIL ();
3206       return FALSE;
3207       break;
3208     }
3209
3210   size = 0;
3211   for (i = 0; i < (template_size / 4); i++)
3212       size += 4;
3213   size = (size + 7) & ~7;
3214   stub_entry->stub_sec->size += size;
3215   return TRUE;
3216 }
3217
3218 /* External entry points for sizing and building linker stubs.  */
3219
3220 /* Set up various things so that we can make a list of input sections
3221    for each output section included in the link.  Returns -1 on error,
3222    0 when no stubs will be needed, and 1 on success.  */
3223
3224 int
3225 elf32_arm_setup_section_lists (bfd *output_bfd,
3226                                struct bfd_link_info *info)
3227 {
3228   bfd *input_bfd;
3229   unsigned int bfd_count;
3230   int top_id, top_index;
3231   asection *section;
3232   asection **input_list, **list;
3233   bfd_size_type amt;
3234   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3235
3236   if (! is_elf_hash_table (htab))
3237     return 0;
3238
3239   /* Count the number of input BFDs and find the top input section id.  */
3240   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3241        input_bfd != NULL;
3242        input_bfd = input_bfd->link_next)
3243     {
3244       bfd_count += 1;
3245       for (section = input_bfd->sections;
3246            section != NULL;
3247            section = section->next)
3248         {
3249           if (top_id < section->id)
3250             top_id = section->id;
3251         }
3252     }
3253   htab->bfd_count = bfd_count;
3254
3255   amt = sizeof (struct map_stub) * (top_id + 1);
3256   htab->stub_group = bfd_zmalloc (amt);
3257   if (htab->stub_group == NULL)
3258     return -1;
3259
3260   /* We can't use output_bfd->section_count here to find the top output
3261      section index as some sections may have been removed, and
3262      _bfd_strip_section_from_output doesn't renumber the indices.  */
3263   for (section = output_bfd->sections, top_index = 0;
3264        section != NULL;
3265        section = section->next)
3266     {
3267       if (top_index < section->index)
3268         top_index = section->index;
3269     }
3270
3271   htab->top_index = top_index;
3272   amt = sizeof (asection *) * (top_index + 1);
3273   input_list = bfd_malloc (amt);
3274   htab->input_list = input_list;
3275   if (input_list == NULL)
3276     return -1;
3277
3278   /* For sections we aren't interested in, mark their entries with a
3279      value we can check later.  */
3280   list = input_list + top_index;
3281   do
3282     *list = bfd_abs_section_ptr;
3283   while (list-- != input_list);
3284
3285   for (section = output_bfd->sections;
3286        section != NULL;
3287        section = section->next)
3288     {
3289       if ((section->flags & SEC_CODE) != 0)
3290         input_list[section->index] = NULL;
3291     }
3292
3293   return 1;
3294 }
3295
3296 /* The linker repeatedly calls this function for each input section,
3297    in the order that input sections are linked into output sections.
3298    Build lists of input sections to determine groupings between which
3299    we may insert linker stubs.  */
3300
3301 void
3302 elf32_arm_next_input_section (struct bfd_link_info *info,
3303                               asection *isec)
3304 {
3305   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3306
3307   if (isec->output_section->index <= htab->top_index)
3308     {
3309       asection **list = htab->input_list + isec->output_section->index;
3310
3311       if (*list != bfd_abs_section_ptr)
3312         {
3313           /* Steal the link_sec pointer for our list.  */
3314 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3315           /* This happens to make the list in reverse order,
3316              which is what we want.  */
3317           PREV_SEC (isec) = *list;
3318           *list = isec;
3319         }
3320     }
3321 }
3322
3323 /* See whether we can group stub sections together.  Grouping stub
3324    sections may result in fewer stubs.  More importantly, we need to
3325    put all .init* and .fini* stubs at the beginning of the .init or
3326    .fini output sections respectively, because glibc splits the
3327    _init and _fini functions into multiple parts.  Putting a stub in
3328    the middle of a function is not a good idea.  */
3329
3330 static void
3331 group_sections (struct elf32_arm_link_hash_table *htab,
3332                 bfd_size_type stub_group_size,
3333                 bfd_boolean stubs_always_before_branch)
3334 {
3335   asection **list = htab->input_list + htab->top_index;
3336
3337   do
3338     {
3339       asection *tail = *list;
3340
3341       if (tail == bfd_abs_section_ptr)
3342         continue;
3343
3344       while (tail != NULL)
3345         {
3346           asection *curr;
3347           asection *prev;
3348           bfd_size_type total;
3349
3350           curr = tail;
3351           total = tail->size;
3352           while ((prev = PREV_SEC (curr)) != NULL
3353                  && ((total += curr->output_offset - prev->output_offset)
3354                      < stub_group_size))
3355             curr = prev;
3356
3357           /* OK, the size from the start of CURR to the end is less
3358              than stub_group_size and thus can be handled by one stub
3359              section.  (Or the tail section is itself larger than
3360              stub_group_size, in which case we may be toast.)
3361              We should really be keeping track of the total size of
3362              stubs added here, as stubs contribute to the final output
3363              section size.  */
3364           do
3365             {
3366               prev = PREV_SEC (tail);
3367               /* Set up this stub group.  */
3368               htab->stub_group[tail->id].link_sec = curr;
3369             }
3370           while (tail != curr && (tail = prev) != NULL);
3371
3372           /* But wait, there's more!  Input sections up to stub_group_size
3373              bytes before the stub section can be handled by it too.  */
3374           if (!stubs_always_before_branch)
3375             {
3376               total = 0;
3377               while (prev != NULL
3378                      && ((total += tail->output_offset - prev->output_offset)
3379                          < stub_group_size))
3380                 {
3381                   tail = prev;
3382                   prev = PREV_SEC (tail);
3383                   htab->stub_group[tail->id].link_sec = curr;
3384                 }
3385             }
3386           tail = prev;
3387         }
3388     }
3389   while (list-- != htab->input_list);
3390
3391   free (htab->input_list);
3392 #undef PREV_SEC
3393 }
3394
3395 /* Determine and set the size of the stub section for a final link.
3396
3397    The basic idea here is to examine all the relocations looking for
3398    PC-relative calls to a target that is unreachable with a "bl"
3399    instruction.  */
3400
3401 bfd_boolean
3402 elf32_arm_size_stubs (bfd *output_bfd,
3403                       bfd *stub_bfd,
3404                       struct bfd_link_info *info,
3405                       bfd_signed_vma group_size,
3406                       asection * (*add_stub_section) (const char *, asection *),
3407                       void (*layout_sections_again) (void))
3408 {
3409   bfd_size_type stub_group_size;
3410   bfd_boolean stubs_always_before_branch;
3411   bfd_boolean stub_changed = 0;
3412   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3413
3414   /* Propagate mach to stub bfd, because it may not have been
3415      finalized when we created stub_bfd.  */
3416   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3417                      bfd_get_mach (output_bfd));
3418
3419   /* Stash our params away.  */
3420   htab->stub_bfd = stub_bfd;
3421   htab->add_stub_section = add_stub_section;
3422   htab->layout_sections_again = layout_sections_again;
3423   stubs_always_before_branch = group_size < 0;
3424   if (group_size < 0)
3425     stub_group_size = -group_size;
3426   else
3427     stub_group_size = group_size;
3428
3429   if (stub_group_size == 1)
3430     {
3431       /* Default values.  */
3432       /* Thumb branch range is +-4MB has to be used as the default
3433          maximum size (a given section can contain both ARM and Thumb
3434          code, so the worst case has to be taken into account).
3435
3436          This value is 24K less than that, which allows for 2025
3437          12-byte stubs.  If we exceed that, then we will fail to link.
3438          The user will have to relink with an explicit group size
3439          option.  */
3440       stub_group_size = 4170000;
3441     }
3442
3443   group_sections (htab, stub_group_size, stubs_always_before_branch);
3444
3445   while (1)
3446     {
3447       bfd *input_bfd;
3448       unsigned int bfd_indx;
3449       asection *stub_sec;
3450
3451       for (input_bfd = info->input_bfds, bfd_indx = 0;
3452            input_bfd != NULL;
3453            input_bfd = input_bfd->link_next, bfd_indx++)
3454         {
3455           Elf_Internal_Shdr *symtab_hdr;
3456           asection *section;
3457           Elf_Internal_Sym *local_syms = NULL;
3458
3459           /* We'll need the symbol table in a second.  */
3460           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3461           if (symtab_hdr->sh_info == 0)
3462             continue;
3463
3464           /* Walk over each section attached to the input bfd.  */
3465           for (section = input_bfd->sections;
3466                section != NULL;
3467                section = section->next)
3468             {
3469               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3470
3471               /* If there aren't any relocs, then there's nothing more
3472                  to do.  */
3473               if ((section->flags & SEC_RELOC) == 0
3474                   || section->reloc_count == 0
3475                   || (section->flags & SEC_CODE) == 0)
3476                 continue;
3477
3478               /* If this section is a link-once section that will be
3479                  discarded, then don't create any stubs.  */
3480               if (section->output_section == NULL
3481                   || section->output_section->owner != output_bfd)
3482                 continue;
3483
3484               /* Get the relocs.  */
3485               internal_relocs
3486                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
3487                                              NULL, info->keep_memory);
3488               if (internal_relocs == NULL)
3489                 goto error_ret_free_local;
3490
3491               /* Now examine each relocation.  */
3492               irela = internal_relocs;
3493               irelaend = irela + section->reloc_count;
3494               for (; irela < irelaend; irela++)
3495                 {
3496                   unsigned int r_type, r_indx;
3497                   enum elf32_arm_stub_type stub_type;
3498                   struct elf32_arm_stub_hash_entry *stub_entry;
3499                   asection *sym_sec;
3500                   bfd_vma sym_value;
3501                   bfd_vma destination;
3502                   struct elf32_arm_link_hash_entry *hash;
3503                   const char *sym_name;
3504                   char *stub_name;
3505                   const asection *id_sec;
3506                   unsigned char st_type;
3507
3508                   r_type = ELF32_R_TYPE (irela->r_info);
3509                   r_indx = ELF32_R_SYM (irela->r_info);
3510
3511                   if (r_type >= (unsigned int) R_ARM_max)
3512                     {
3513                       bfd_set_error (bfd_error_bad_value);
3514                     error_ret_free_internal:
3515                       if (elf_section_data (section)->relocs == NULL)
3516                         free (internal_relocs);
3517                       goto error_ret_free_local;
3518                     }
3519
3520                   /* Only look for stubs on call instructions.  */
3521                   if ((r_type != (unsigned int) R_ARM_CALL)
3522                       && (r_type != (unsigned int) R_ARM_THM_CALL))
3523                     continue;
3524
3525                   /* Now determine the call target, its name, value,
3526                      section.  */
3527                   sym_sec = NULL;
3528                   sym_value = 0;
3529                   destination = 0;
3530                   hash = NULL;
3531                   sym_name = NULL;
3532                   if (r_indx < symtab_hdr->sh_info)
3533                     {
3534                       /* It's a local symbol.  */
3535                       Elf_Internal_Sym *sym;
3536                       Elf_Internal_Shdr *hdr;
3537
3538                       if (local_syms == NULL)
3539                         {
3540                           local_syms
3541                             = (Elf_Internal_Sym *) symtab_hdr->contents;
3542                           if (local_syms == NULL)
3543                             local_syms
3544                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3545                                                       symtab_hdr->sh_info, 0,
3546                                                       NULL, NULL, NULL);
3547                           if (local_syms == NULL)
3548                             goto error_ret_free_internal;
3549                         }
3550
3551                       sym = local_syms + r_indx;
3552                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3553                       sym_sec = hdr->bfd_section;
3554                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3555                         sym_value = sym->st_value;
3556                       destination = (sym_value + irela->r_addend
3557                                      + sym_sec->output_offset
3558                                      + sym_sec->output_section->vma);
3559                       st_type = ELF_ST_TYPE (sym->st_info);
3560                       sym_name
3561                         = bfd_elf_string_from_elf_section (input_bfd,
3562                                                            symtab_hdr->sh_link,
3563                                                            sym->st_name);
3564                     }
3565                   else
3566                     {
3567                       /* It's an external symbol.  */
3568                       int e_indx;
3569
3570                       e_indx = r_indx - symtab_hdr->sh_info;
3571                       hash = ((struct elf32_arm_link_hash_entry *)
3572                               elf_sym_hashes (input_bfd)[e_indx]);
3573
3574                       while (hash->root.root.type == bfd_link_hash_indirect
3575                              || hash->root.root.type == bfd_link_hash_warning)
3576                         hash = ((struct elf32_arm_link_hash_entry *)
3577                                 hash->root.root.u.i.link);
3578
3579                       if (hash->root.root.type == bfd_link_hash_defined
3580                           || hash->root.root.type == bfd_link_hash_defweak)
3581                         {
3582                           sym_sec = hash->root.root.u.def.section;
3583                           sym_value = hash->root.root.u.def.value;
3584                           if (sym_sec->output_section != NULL)
3585                             destination = (sym_value + irela->r_addend
3586                                            + sym_sec->output_offset
3587                                            + sym_sec->output_section->vma);
3588                         }
3589                       else if (hash->root.root.type == bfd_link_hash_undefweak
3590                                || hash->root.root.type == bfd_link_hash_undefined)
3591                         /* For a shared library, these will need a PLT stub,
3592                            which is treated separately.
3593                            For absolute code, they cannot be handled.  */
3594                         continue;
3595                       else
3596                         {
3597                           bfd_set_error (bfd_error_bad_value);
3598                           goto error_ret_free_internal;
3599                         }
3600                       st_type = ELF_ST_TYPE (hash->root.type);
3601                       sym_name = hash->root.root.root.string;
3602                     }
3603
3604                   /* Determine what (if any) linker stub is needed.  */
3605                   stub_type = arm_type_of_stub (info, section, irela, st_type,
3606                                                 hash, destination);
3607                   if (stub_type == arm_stub_none)
3608                     continue;
3609
3610                   /* Support for grouping stub sections.  */
3611                   id_sec = htab->stub_group[section->id].link_sec;
3612
3613                   /* Get the name of this stub.  */
3614                   stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela);
3615                   if (!stub_name)
3616                     goto error_ret_free_internal;
3617
3618                   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
3619                                                     stub_name,
3620                                                     FALSE, FALSE);
3621                   if (stub_entry != NULL)
3622                     {
3623                       /* The proper stub has already been created.  */
3624                       free (stub_name);
3625                       continue;
3626                     }
3627
3628                   stub_entry = elf32_arm_add_stub (stub_name, section, htab);
3629                   if (stub_entry == NULL)
3630                     {
3631                       free (stub_name);
3632                       goto error_ret_free_internal;
3633                     }
3634
3635                   stub_entry->target_value = sym_value;
3636                   stub_entry->target_section = sym_sec;
3637                   stub_entry->stub_type = stub_type;
3638                   stub_entry->h = hash;
3639                   stub_entry->st_type = st_type;
3640
3641                   if (sym_name == NULL)
3642                     sym_name = "unnamed";
3643                   stub_entry->output_name
3644                     = bfd_alloc (htab->stub_bfd,
3645                                  sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
3646                                  + strlen (sym_name));
3647                   if (stub_entry->output_name == NULL)
3648                     {
3649                       free (stub_name);
3650                       goto error_ret_free_internal;
3651                     }
3652
3653                   /* For historical reasons, use the existing names for
3654                      ARM-to-Thumb and Thumb-to-ARM stubs.  */
3655                   if (r_type == (unsigned int) R_ARM_THM_CALL
3656                       && st_type != STT_ARM_TFUNC)
3657                     sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME,
3658                              sym_name);
3659                   else if (r_type == (unsigned int) R_ARM_CALL
3660                            && st_type == STT_ARM_TFUNC)
3661                     sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME,
3662                              sym_name);
3663                   else
3664                     sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3665                              sym_name);
3666
3667                   stub_changed = TRUE;
3668                 }
3669
3670               /* We're done with the internal relocs, free them.  */
3671               if (elf_section_data (section)->relocs == NULL)
3672                 free (internal_relocs);
3673             }
3674         }
3675
3676       if (!stub_changed)
3677         break;
3678
3679       /* OK, we've added some stubs.  Find out the new size of the
3680          stub sections.  */
3681       for (stub_sec = htab->stub_bfd->sections;
3682            stub_sec != NULL;
3683            stub_sec = stub_sec->next)
3684         stub_sec->size = 0;
3685
3686       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
3687
3688       /* Ask the linker to do its stuff.  */
3689       (*htab->layout_sections_again) ();
3690       stub_changed = FALSE;
3691     }
3692
3693   return TRUE;
3694
3695  error_ret_free_local:
3696   return FALSE;
3697 }
3698
3699 /* Build all the stubs associated with the current output file.  The
3700    stubs are kept in a hash table attached to the main linker hash
3701    table.  We also set up the .plt entries for statically linked PIC
3702    functions here.  This function is called via arm_elf_finish in the
3703    linker.  */
3704
3705 bfd_boolean
3706 elf32_arm_build_stubs (struct bfd_link_info *info)
3707 {
3708   asection *stub_sec;
3709   struct bfd_hash_table *table;
3710   struct elf32_arm_link_hash_table *htab;
3711
3712   htab = elf32_arm_hash_table (info);
3713
3714   for (stub_sec = htab->stub_bfd->sections;
3715        stub_sec != NULL;
3716        stub_sec = stub_sec->next)
3717     {
3718       bfd_size_type size;
3719
3720       /* Ignore non-stub sections.  */
3721       if (!strstr (stub_sec->name, STUB_SUFFIX))
3722         continue;
3723
3724       /* Allocate memory to hold the linker stubs.  */
3725       size = stub_sec->size;
3726       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3727       if (stub_sec->contents == NULL && size != 0)
3728         return FALSE;
3729       stub_sec->size = 0;
3730     }
3731
3732   /* Build the stubs as directed by the stub hash table.  */
3733   table = &htab->stub_hash_table;
3734   bfd_hash_traverse (table, arm_build_one_stub, info);
3735
3736   return TRUE;
3737 }
3738
3739 /* Locate the Thumb encoded calling stub for NAME.  */
3740
3741 static struct elf_link_hash_entry *
3742 find_thumb_glue (struct bfd_link_info *link_info,
3743                  const char *name,
3744                  char **error_message)
3745 {
3746   char *tmp_name;
3747   struct elf_link_hash_entry *hash;
3748   struct elf32_arm_link_hash_table *hash_table;
3749
3750   /* We need a pointer to the armelf specific hash table.  */
3751   hash_table = elf32_arm_hash_table (link_info);
3752
3753   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
3754                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
3755
3756   BFD_ASSERT (tmp_name);
3757
3758   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
3759
3760   hash = elf_link_hash_lookup
3761     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
3762
3763   if (hash == NULL
3764       && asprintf (error_message, _("unable to find THUMB glue '%s' for '%s'"),
3765                    tmp_name, name) == -1)
3766     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
3767
3768   free (tmp_name);
3769
3770   return hash;
3771 }
3772
3773 /* Locate the ARM encoded calling stub for NAME.  */
3774
3775 static struct elf_link_hash_entry *
3776 find_arm_glue (struct bfd_link_info *link_info,
3777                const char *name,
3778                char **error_message)
3779 {
3780   char *tmp_name;
3781   struct elf_link_hash_entry *myh;
3782   struct elf32_arm_link_hash_table *hash_table;
3783
3784   /* We need a pointer to the elfarm specific hash table.  */
3785   hash_table = elf32_arm_hash_table (link_info);
3786
3787   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
3788                          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
3789
3790   BFD_ASSERT (tmp_name);
3791
3792   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
3793
3794   myh = elf_link_hash_lookup
3795     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
3796
3797   if (myh == NULL
3798       && asprintf (error_message, _("unable to find ARM glue '%s' for '%s'"),
3799                    tmp_name, name) == -1)
3800     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
3801
3802   free (tmp_name);
3803
3804   return myh;
3805 }
3806
3807 /* ARM->Thumb glue (static images):
3808
3809    .arm
3810    __func_from_arm:
3811    ldr r12, __func_addr
3812    bx  r12
3813    __func_addr:
3814    .word func    @ behave as if you saw a ARM_32 reloc.
3815
3816    (v5t static images)
3817    .arm
3818    __func_from_arm:
3819    ldr pc, __func_addr
3820    __func_addr:
3821    .word func    @ behave as if you saw a ARM_32 reloc.
3822
3823    (relocatable images)
3824    .arm
3825    __func_from_arm:
3826    ldr r12, __func_offset
3827    add r12, r12, pc
3828    bx  r12
3829    __func_offset:
3830    .word func - .   */
3831
3832 #define ARM2THUMB_STATIC_GLUE_SIZE 12
3833 static const insn32 a2t1_ldr_insn = 0xe59fc000;
3834 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
3835 static const insn32 a2t3_func_addr_insn = 0x00000001;
3836
3837 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
3838 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
3839 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
3840
3841 #define ARM2THUMB_PIC_GLUE_SIZE 16
3842 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
3843 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
3844 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
3845
3846 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
3847
3848      .thumb                             .thumb
3849      .align 2                           .align 2
3850  __func_from_thumb:                 __func_from_thumb:
3851      bx pc                              push {r6, lr}
3852      nop                                ldr  r6, __func_addr
3853      .arm                               mov  lr, pc
3854      b func                             bx   r6
3855  ;; back_to_thumb                       .arm
3856      ldmia r13! {r6, lr}                ldmia r13! {r6, lr}
3857      bx    lr                           bx    lr
3858                                     __func_addr:
3859                                         .word        func  */
3860
3861 #define THUMB2ARM_GLUE_SIZE 8
3862 static const insn16 t2a1_bx_pc_insn = 0x4778;
3863 static const insn16 t2a2_noop_insn = 0x46c0;
3864 static const insn32 t2a3_b_insn = 0xea000000;
3865
3866 #define VFP11_ERRATUM_VENEER_SIZE 8
3867
3868 #define ARM_BX_VENEER_SIZE 12
3869 static const insn32 armbx1_tst_insn = 0xe3100001;
3870 static const insn32 armbx2_moveq_insn = 0x01a0f000;
3871 static const insn32 armbx3_bx_insn = 0xe12fff10;
3872
3873 #ifndef ELFARM_NABI_C_INCLUDED
3874 static void
3875 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
3876 {
3877   asection * s;
3878   bfd_byte * contents;
3879
3880   if (size == 0)
3881     return;
3882
3883   BFD_ASSERT (abfd != NULL);
3884
3885   s = bfd_get_section_by_name (abfd, name);
3886   BFD_ASSERT (s != NULL);
3887
3888   contents = bfd_alloc (abfd, size);
3889
3890   BFD_ASSERT (s->size == size);
3891   s->contents = contents;
3892 }
3893
3894 bfd_boolean
3895 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
3896 {
3897   struct elf32_arm_link_hash_table * globals;
3898
3899   globals = elf32_arm_hash_table (info);
3900   BFD_ASSERT (globals != NULL);
3901
3902   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
3903                                    globals->arm_glue_size,
3904                                    ARM2THUMB_GLUE_SECTION_NAME);
3905
3906   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
3907                                    globals->thumb_glue_size,
3908                                    THUMB2ARM_GLUE_SECTION_NAME);
3909
3910   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
3911                                    globals->vfp11_erratum_glue_size,
3912                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
3913
3914   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
3915                                    globals->bx_glue_size,
3916                                    ARM_BX_GLUE_SECTION_NAME);
3917
3918   return TRUE;
3919 }
3920
3921 /* Allocate space and symbols for calling a Thumb function from Arm mode.
3922    returns the symbol identifying the stub.  */
3923
3924 static struct elf_link_hash_entry *
3925 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
3926                           struct elf_link_hash_entry * h)
3927 {
3928   const char * name = h->root.root.string;
3929   asection * s;
3930   char * tmp_name;
3931   struct elf_link_hash_entry * myh;
3932   struct bfd_link_hash_entry * bh;
3933   struct elf32_arm_link_hash_table * globals;
3934   bfd_vma val;
3935   bfd_size_type size;
3936
3937   globals = elf32_arm_hash_table (link_info);
3938
3939   BFD_ASSERT (globals != NULL);
3940   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
3941
3942   s = bfd_get_section_by_name
3943     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
3944
3945   BFD_ASSERT (s != NULL);
3946
3947   tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
3948
3949   BFD_ASSERT (tmp_name);
3950
3951   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
3952
3953   myh = elf_link_hash_lookup
3954     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
3955
3956   if (myh != NULL)
3957     {
3958       /* We've already seen this guy.  */
3959       free (tmp_name);
3960       return myh;
3961     }
3962
3963   /* The only trick here is using hash_table->arm_glue_size as the value.
3964      Even though the section isn't allocated yet, this is where we will be
3965      putting it.  The +1 on the value marks that the stub has not been
3966      output yet - not that it is a Thumb function.  */
3967   bh = NULL;
3968   val = globals->arm_glue_size + 1;
3969   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
3970                                     tmp_name, BSF_GLOBAL, s, val,
3971                                     NULL, TRUE, FALSE, &bh);
3972
3973   myh = (struct elf_link_hash_entry *) bh;
3974   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
3975   myh->forced_local = 1;
3976
3977   free (tmp_name);
3978
3979   if (link_info->shared || globals->root.is_relocatable_executable
3980       || globals->pic_veneer)
3981     size = ARM2THUMB_PIC_GLUE_SIZE;
3982   else if (globals->use_blx)
3983     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
3984   else
3985     size = ARM2THUMB_STATIC_GLUE_SIZE;
3986
3987   s->size += size;
3988   globals->arm_glue_size += size;
3989
3990   return myh;
3991 }
3992
3993 static void
3994 record_thumb_to_arm_glue (struct bfd_link_info *link_info,
3995                           struct elf_link_hash_entry *h)
3996 {
3997   const char *name = h->root.root.string;
3998   asection *s;
3999   char *tmp_name;
4000   struct elf_link_hash_entry *myh;
4001   struct bfd_link_hash_entry *bh;
4002   struct elf32_arm_link_hash_table *hash_table;
4003   bfd_vma val;
4004
4005   hash_table = elf32_arm_hash_table (link_info);
4006
4007   BFD_ASSERT (hash_table != NULL);
4008   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
4009
4010   s = bfd_get_section_by_name
4011     (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
4012
4013   BFD_ASSERT (s != NULL);
4014
4015   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
4016                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
4017
4018   BFD_ASSERT (tmp_name);
4019
4020   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
4021
4022   myh = elf_link_hash_lookup
4023     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
4024
4025   if (myh != NULL)
4026     {
4027       /* We've already seen this guy.  */
4028       free (tmp_name);
4029       return;
4030     }
4031
4032   /* The only trick here is using hash_table->thumb_glue_size as the value.
4033      Even though the section isn't allocated yet, this is where we will be
4034      putting it.  The +1 on the value marks that the stub has not been
4035      output yet - not that it is a Thumb function.  */
4036   bh = NULL;
4037   val = hash_table->thumb_glue_size + 1;
4038   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
4039                                     tmp_name, BSF_GLOBAL, s, val,
4040                                     NULL, TRUE, FALSE, &bh);
4041
4042   /* If we mark it 'Thumb', the disassembler will do a better job.  */
4043   myh = (struct elf_link_hash_entry *) bh;
4044   myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
4045   myh->forced_local = 1;
4046
4047   free (tmp_name);
4048
4049 #define CHANGE_TO_ARM "__%s_change_to_arm"
4050 #define BACK_FROM_ARM "__%s_back_from_arm"
4051
4052   /* Allocate another symbol to mark where we switch to Arm mode.  */
4053   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
4054                          + strlen (CHANGE_TO_ARM) + 1);
4055
4056   BFD_ASSERT (tmp_name);
4057
4058   sprintf (tmp_name, CHANGE_TO_ARM, name);
4059
4060   bh = NULL;
4061   val = hash_table->thumb_glue_size + 4,
4062   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
4063                                     tmp_name, BSF_LOCAL, s, val,
4064                                     NULL, TRUE, FALSE, &bh);
4065
4066   free (tmp_name);
4067
4068   s->size += THUMB2ARM_GLUE_SIZE;
4069   hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
4070 }
4071
4072
4073 /* Allocate space for ARMv4 BX veneers.  */
4074
4075 static void
4076 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
4077 {
4078   asection * s;
4079   struct elf32_arm_link_hash_table *globals;
4080   char *tmp_name;
4081   struct elf_link_hash_entry *myh;
4082   struct bfd_link_hash_entry *bh;
4083   bfd_vma val;
4084
4085   /* BX PC does not need a veneer.  */
4086   if (reg == 15)
4087     return;
4088
4089   globals = elf32_arm_hash_table (link_info);
4090
4091   BFD_ASSERT (globals != NULL);
4092   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
4093
4094   /* Check if this veneer has already been allocated.  */
4095   if (globals->bx_glue_offset[reg])
4096     return;
4097
4098   s = bfd_get_section_by_name
4099     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
4100
4101   BFD_ASSERT (s != NULL);
4102
4103   /* Add symbol for veneer.  */
4104   tmp_name = bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
4105
4106   BFD_ASSERT (tmp_name);
4107
4108   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
4109
4110   myh = elf_link_hash_lookup
4111     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
4112
4113   BFD_ASSERT (myh == NULL);
4114
4115   bh = NULL;
4116   val = globals->bx_glue_size;
4117   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
4118                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
4119                                     NULL, TRUE, FALSE, &bh);
4120
4121   myh = (struct elf_link_hash_entry *) bh;
4122   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
4123   myh->forced_local = 1;
4124
4125   s->size += ARM_BX_VENEER_SIZE;
4126   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
4127   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
4128 }
4129
4130
4131 /* Add an entry to the code/data map for section SEC.  */
4132
4133 static void
4134 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
4135 {
4136   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
4137   unsigned int newidx;
4138
4139   if (sec_data->map == NULL)
4140     {
4141       sec_data->map = bfd_malloc (sizeof (elf32_arm_section_map));
4142       sec_data->mapcount = 0;
4143       sec_data->mapsize = 1;
4144     }
4145
4146   newidx = sec_data->mapcount++;
4147
4148   if (sec_data->mapcount > sec_data->mapsize)
4149     {
4150       sec_data->mapsize *= 2;
4151       sec_data->map = bfd_realloc_or_free (sec_data->map, sec_data->mapsize
4152                                            * sizeof (elf32_arm_section_map));
4153     }
4154
4155   if (sec_data->map)
4156     {
4157       sec_data->map[newidx].vma = vma;
4158       sec_data->map[newidx].type = type;
4159     }
4160 }
4161
4162
4163 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
4164    veneers are handled for now.  */
4165
4166 static bfd_vma
4167 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
4168                              elf32_vfp11_erratum_list *branch,
4169                              bfd *branch_bfd,
4170                              asection *branch_sec,
4171                              unsigned int offset)
4172 {
4173   asection *s;
4174   struct elf32_arm_link_hash_table *hash_table;
4175   char *tmp_name;
4176   struct elf_link_hash_entry *myh;
4177   struct bfd_link_hash_entry *bh;
4178   bfd_vma val;
4179   struct _arm_elf_section_data *sec_data;
4180   int errcount;
4181   elf32_vfp11_erratum_list *newerr;
4182
4183   hash_table = elf32_arm_hash_table (link_info);
4184
4185   BFD_ASSERT (hash_table != NULL);
4186   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
4187
4188   s = bfd_get_section_by_name
4189     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
4190
4191   sec_data = elf32_arm_section_data (s);
4192
4193   BFD_ASSERT (s != NULL);
4194
4195   tmp_name = bfd_malloc ((bfd_size_type) strlen
4196                          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
4197
4198   BFD_ASSERT (tmp_name);
4199
4200   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
4201            hash_table->num_vfp11_fixes);
4202
4203   myh = elf_link_hash_lookup
4204     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
4205
4206   BFD_ASSERT (myh == NULL);
4207
4208   bh = NULL;
4209   val = hash_table->vfp11_erratum_glue_size;
4210   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
4211                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
4212                                     NULL, TRUE, FALSE, &bh);
4213
4214   myh = (struct elf_link_hash_entry *) bh;
4215   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
4216   myh->forced_local = 1;
4217
4218   /* Link veneer back to calling location.  */
4219   errcount = ++(sec_data->erratumcount);
4220   newerr = bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
4221
4222   newerr->type = VFP11_ERRATUM_ARM_VENEER;
4223   newerr->vma = -1;
4224   newerr->u.v.branch = branch;
4225   newerr->u.v.id = hash_table->num_vfp11_fixes;
4226   branch->u.b.veneer = newerr;
4227
4228   newerr->next = sec_data->erratumlist;
4229   sec_data->erratumlist = newerr;
4230
4231   /* A symbol for the return from the veneer.  */
4232   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
4233            hash_table->num_vfp11_fixes);
4234
4235   myh = elf_link_hash_lookup
4236     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
4237
4238   if (myh != NULL)
4239     abort ();
4240
4241   bh = NULL;
4242   val = offset + 4;
4243   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
4244                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
4245
4246   myh = (struct elf_link_hash_entry *) bh;
4247   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
4248   myh->forced_local = 1;
4249
4250   free (tmp_name);
4251
4252   /* Generate a mapping symbol for the veneer section, and explicitly add an
4253      entry for that symbol to the code/data map for the section.  */
4254   if (hash_table->vfp11_erratum_glue_size == 0)
4255     {
4256       bh = NULL;
4257       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
4258          ever requires this erratum fix.  */
4259       _bfd_generic_link_add_one_symbol (link_info,
4260                                         hash_table->bfd_of_glue_owner, "$a",
4261                                         BSF_LOCAL, s, 0, NULL,
4262                                         TRUE, FALSE, &bh);
4263
4264       myh = (struct elf_link_hash_entry *) bh;
4265       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
4266       myh->forced_local = 1;
4267
4268       /* The elf32_arm_init_maps function only cares about symbols from input
4269          BFDs.  We must make a note of this generated mapping symbol
4270          ourselves so that code byteswapping works properly in
4271          elf32_arm_write_section.  */
4272       elf32_arm_section_map_add (s, 'a', 0);
4273     }
4274
4275   s->size += VFP11_ERRATUM_VENEER_SIZE;
4276   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
4277   hash_table->num_vfp11_fixes++;
4278
4279   /* The offset of the veneer.  */
4280   return val;
4281 }
4282
4283 /* Note: we do not include the flag SEC_LINKER_CREATED, as that
4284    would prevent elf_link_input_bfd() from processing the contents
4285    of the section.  */
4286 #define ARM_GLUE_SECTION_FLAGS \
4287   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY)
4288
4289 /* Create a fake section for use by the ARM backend of the linker.  */
4290
4291 static bfd_boolean
4292 arm_make_glue_section (bfd * abfd, const char * name)
4293 {
4294   asection * sec;
4295
4296   sec = bfd_get_section_by_name (abfd, name);
4297   if (sec != NULL)
4298     /* Already made.  */
4299     return TRUE;
4300
4301   sec = bfd_make_section_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
4302
4303   if (sec == NULL
4304       || !bfd_set_section_alignment (abfd, sec, 2))
4305     return FALSE;
4306
4307   /* Set the gc mark to prevent the section from being removed by garbage
4308      collection, despite the fact that no relocs refer to this section.  */
4309   sec->gc_mark = 1;
4310
4311   return TRUE;
4312 }
4313
4314 /* Add the glue sections to ABFD.  This function is called from the
4315    linker scripts in ld/emultempl/{armelf}.em.  */
4316
4317 bfd_boolean
4318 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
4319                                         struct bfd_link_info *info)
4320 {
4321   /* If we are only performing a partial
4322      link do not bother adding the glue.  */
4323   if (info->relocatable)
4324     return TRUE;
4325
4326   /* Linker stubs don't need glue.  */
4327   if (!strcmp (abfd->filename, "linker stubs"))
4328     return TRUE;
4329
4330   return arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
4331     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
4332     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
4333     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
4334 }
4335
4336 /* Select a BFD to be used to hold the sections used by the glue code.
4337    This function is called from the linker scripts in ld/emultempl/
4338    {armelf/pe}.em.  */
4339
4340 bfd_boolean
4341 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
4342 {
4343   struct elf32_arm_link_hash_table *globals;
4344
4345   /* If we are only performing a partial link
4346      do not bother getting a bfd to hold the glue.  */
4347   if (info->relocatable)
4348     return TRUE;
4349
4350   /* Make sure we don't attach the glue sections to a dynamic object.  */
4351   BFD_ASSERT (!(abfd->flags & DYNAMIC));
4352
4353   globals = elf32_arm_hash_table (info);
4354
4355   BFD_ASSERT (globals != NULL);
4356
4357   if (globals->bfd_of_glue_owner != NULL)
4358     return TRUE;
4359
4360   /* Save the bfd for later use.  */
4361   globals->bfd_of_glue_owner = abfd;
4362
4363   return TRUE;
4364 }
4365
4366 static void
4367 check_use_blx (struct elf32_arm_link_hash_table *globals)
4368 {
4369   if (bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
4370                                 Tag_CPU_arch) > 2)
4371     globals->use_blx = 1;
4372 }
4373
4374 bfd_boolean
4375 bfd_elf32_arm_process_before_allocation (bfd *abfd,
4376                                          struct bfd_link_info *link_info)
4377 {
4378   Elf_Internal_Shdr *symtab_hdr;
4379   Elf_Internal_Rela *internal_relocs = NULL;
4380   Elf_Internal_Rela *irel, *irelend;
4381   bfd_byte *contents = NULL;
4382
4383   asection *sec;
4384   struct elf32_arm_link_hash_table *globals;
4385
4386   /* If we are only performing a partial link do not bother
4387      to construct any glue.  */
4388   if (link_info->relocatable)
4389     return TRUE;
4390
4391   /* Here we have a bfd that is to be included on the link.  We have a
4392      hook to do reloc rummaging, before section sizes are nailed down.  */
4393   globals = elf32_arm_hash_table (link_info);
4394
4395   BFD_ASSERT (globals != NULL);
4396
4397   check_use_blx (globals);
4398
4399   if (globals->byteswap_code && !bfd_big_endian (abfd))
4400     {
4401       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
4402                           abfd);
4403       return FALSE;
4404     }
4405
4406   /* PR 5398: If we have not decided to include any loadable sections in
4407      the output then we will not have a glue owner bfd.  This is OK, it
4408      just means that there is nothing else for us to do here.  */
4409   if (globals->bfd_of_glue_owner == NULL)
4410     return TRUE;
4411
4412   /* Rummage around all the relocs and map the glue vectors.  */
4413   sec = abfd->sections;
4414
4415   if (sec == NULL)
4416     return TRUE;
4417
4418   for (; sec != NULL; sec = sec->next)
4419     {
4420       if (sec->reloc_count == 0)
4421         continue;
4422
4423       if ((sec->flags & SEC_EXCLUDE) != 0)
4424         continue;
4425
4426       symtab_hdr = & elf_symtab_hdr (abfd);
4427
4428       /* Load the relocs.  */
4429       internal_relocs
4430         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
4431
4432       if (internal_relocs == NULL)
4433         goto error_return;
4434
4435       irelend = internal_relocs + sec->reloc_count;
4436       for (irel = internal_relocs; irel < irelend; irel++)
4437         {
4438           long r_type;
4439           unsigned long r_index;
4440
4441           struct elf_link_hash_entry *h;
4442
4443           r_type = ELF32_R_TYPE (irel->r_info);
4444           r_index = ELF32_R_SYM (irel->r_info);
4445
4446           /* These are the only relocation types we care about.  */
4447           if (   r_type != R_ARM_PC24
4448               && r_type != R_ARM_PLT32
4449               && r_type != R_ARM_JUMP24
4450               && r_type != R_ARM_THM_JUMP24
4451               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
4452             continue;
4453
4454           /* Get the section contents if we haven't done so already.  */
4455           if (contents == NULL)
4456             {
4457               /* Get cached copy if it exists.  */
4458               if (elf_section_data (sec)->this_hdr.contents != NULL)
4459                 contents = elf_section_data (sec)->this_hdr.contents;
4460               else
4461                 {
4462                   /* Go get them off disk.  */
4463                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
4464                     goto error_return;
4465                 }
4466             }
4467
4468           if (r_type == R_ARM_V4BX)
4469             {
4470               int reg;
4471
4472               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
4473               record_arm_bx_glue (link_info, reg);
4474               continue;
4475             }
4476
4477           /* If the relocation is not against a symbol it cannot concern us.  */
4478           h = NULL;
4479
4480           /* We don't care about local symbols.  */
4481           if (r_index < symtab_hdr->sh_info)
4482             continue;
4483
4484           /* This is an external symbol.  */
4485           r_index -= symtab_hdr->sh_info;
4486           h = (struct elf_link_hash_entry *)
4487             elf_sym_hashes (abfd)[r_index];
4488
4489           /* If the relocation is against a static symbol it must be within
4490              the current section and so cannot be a cross ARM/Thumb relocation.  */
4491           if (h == NULL)
4492             continue;
4493
4494           /* If the call will go through a PLT entry then we do not need
4495              glue.  */
4496           if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
4497             continue;
4498
4499           switch (r_type)
4500             {
4501             case R_ARM_PC24:
4502             case R_ARM_PLT32:
4503             case R_ARM_JUMP24:
4504               /* This one is a call from arm code.  We need to look up
4505                  the target of the call.  If it is a thumb target, we
4506                  insert glue.  */
4507               if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC
4508                   && !(r_type == R_ARM_CALL && globals->use_blx))
4509                 record_arm_to_thumb_glue (link_info, h);
4510               break;
4511
4512             case R_ARM_THM_JUMP24:
4513               /* This one is a call from thumb code.  We look
4514                  up the target of the call.  If it is not a thumb
4515                  target, we insert glue.  */
4516               if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC
4517                   && !(globals->use_blx && r_type == R_ARM_THM_CALL)
4518                   && h->root.type != bfd_link_hash_undefweak)
4519                 record_thumb_to_arm_glue (link_info, h);
4520               break;
4521
4522             default:
4523               abort ();
4524             }
4525         }
4526
4527       if (contents != NULL
4528           && elf_section_data (sec)->this_hdr.contents != contents)
4529         free (contents);
4530       contents = NULL;
4531
4532       if (internal_relocs != NULL
4533           && elf_section_data (sec)->relocs != internal_relocs)
4534         free (internal_relocs);
4535       internal_relocs = NULL;
4536     }
4537
4538   return TRUE;
4539
4540 error_return:
4541   if (contents != NULL
4542       && elf_section_data (sec)->this_hdr.contents != contents)
4543     free (contents);
4544   if (internal_relocs != NULL
4545       && elf_section_data (sec)->relocs != internal_relocs)
4546     free (internal_relocs);
4547
4548   return FALSE;
4549 }
4550 #endif
4551
4552
4553 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
4554
4555 void
4556 bfd_elf32_arm_init_maps (bfd *abfd)
4557 {
4558   Elf_Internal_Sym *isymbuf;
4559   Elf_Internal_Shdr *hdr;
4560   unsigned int i, localsyms;
4561
4562   if ((abfd->flags & DYNAMIC) != 0)
4563     return;
4564
4565   hdr = & elf_symtab_hdr (abfd);
4566   localsyms = hdr->sh_info;
4567
4568   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
4569      should contain the number of local symbols, which should come before any
4570      global symbols.  Mapping symbols are always local.  */
4571   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
4572                                   NULL);
4573
4574   /* No internal symbols read?  Skip this BFD.  */
4575   if (isymbuf == NULL)
4576     return;
4577
4578   for (i = 0; i < localsyms; i++)
4579     {
4580       Elf_Internal_Sym *isym = &isymbuf[i];
4581       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4582       const char *name;
4583
4584       if (sec != NULL
4585           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
4586         {
4587           name = bfd_elf_string_from_elf_section (abfd,
4588             hdr->sh_link, isym->st_name);
4589
4590           if (bfd_is_arm_special_symbol_name (name,
4591                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
4592             elf32_arm_section_map_add (sec, name[1], isym->st_value);
4593         }
4594     }
4595 }
4596
4597
4598 void
4599 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
4600 {
4601   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
4602   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
4603
4604   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
4605   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
4606     {
4607       switch (globals->vfp11_fix)
4608         {
4609         case BFD_ARM_VFP11_FIX_DEFAULT:
4610         case BFD_ARM_VFP11_FIX_NONE:
4611           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4612           break;
4613
4614         default:
4615           /* Give a warning, but do as the user requests anyway.  */
4616           (*_bfd_error_handler) (_("%B: warning: selected VFP11 erratum "
4617             "workaround is not necessary for target architecture"), obfd);
4618         }
4619     }
4620   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
4621     /* For earlier architectures, we might need the workaround, but do not
4622        enable it by default.  If users is running with broken hardware, they
4623        must enable the erratum fix explicitly.  */
4624     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4625 }
4626
4627
4628 enum bfd_arm_vfp11_pipe
4629 {
4630   VFP11_FMAC,
4631   VFP11_LS,
4632   VFP11_DS,
4633   VFP11_BAD
4634 };
4635
4636 /* Return a VFP register number.  This is encoded as RX:X for single-precision
4637    registers, or X:RX for double-precision registers, where RX is the group of
4638    four bits in the instruction encoding and X is the single extension bit.
4639    RX and X fields are specified using their lowest (starting) bit.  The return
4640    value is:
4641
4642      0...31: single-precision registers s0...s31
4643      32...63: double-precision registers d0...d31.
4644
4645    Although X should be zero for VFP11 (encoding d0...d15 only), we might
4646    encounter VFP3 instructions, so we allow the full range for DP registers.  */
4647
4648 static unsigned int
4649 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
4650                      unsigned int x)
4651 {
4652   if (is_double)
4653     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
4654   else
4655     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
4656 }
4657
4658 /* Set bits in *WMASK according to a register number REG as encoded by
4659    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
4660
4661 static void
4662 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
4663 {
4664   if (reg < 32)
4665     *wmask |= 1 << reg;
4666   else if (reg < 48)
4667     *wmask |= 3 << ((reg - 32) * 2);
4668 }
4669
4670 /* Return TRUE if WMASK overwrites anything in REGS.  */
4671
4672 static bfd_boolean
4673 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
4674 {
4675   int i;
4676
4677   for (i = 0; i < numregs; i++)
4678     {
4679       unsigned int reg = regs[i];
4680
4681       if (reg < 32 && (wmask & (1 << reg)) != 0)
4682         return TRUE;
4683
4684       reg -= 32;
4685
4686       if (reg >= 16)
4687         continue;
4688
4689       if ((wmask & (3 << (reg * 2))) != 0)
4690         return TRUE;
4691     }
4692
4693   return FALSE;
4694 }
4695
4696 /* In this function, we're interested in two things: finding input registers
4697    for VFP data-processing instructions, and finding the set of registers which
4698    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
4699    hold the written set, so FLDM etc. are easy to deal with (we're only
4700    interested in 32 SP registers or 16 dp registers, due to the VFP version
4701    implemented by the chip in question).  DP registers are marked by setting
4702    both SP registers in the write mask).  */
4703
4704 static enum bfd_arm_vfp11_pipe
4705 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
4706                            int *numregs)
4707 {
4708   enum bfd_arm_vfp11_pipe pipe = VFP11_BAD;
4709   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
4710
4711   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
4712     {
4713       unsigned int pqrs;
4714       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
4715       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
4716
4717       pqrs = ((insn & 0x00800000) >> 20)
4718            | ((insn & 0x00300000) >> 19)
4719            | ((insn & 0x00000040) >> 6);
4720
4721       switch (pqrs)
4722         {
4723         case 0: /* fmac[sd].  */
4724         case 1: /* fnmac[sd].  */
4725         case 2: /* fmsc[sd].  */
4726         case 3: /* fnmsc[sd].  */
4727           pipe = VFP11_FMAC;
4728           bfd_arm_vfp11_write_mask (destmask, fd);
4729           regs[0] = fd;
4730           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
4731           regs[2] = fm;
4732           *numregs = 3;
4733           break;
4734
4735         case 4: /* fmul[sd].  */
4736         case 5: /* fnmul[sd].  */
4737         case 6: /* fadd[sd].  */
4738         case 7: /* fsub[sd].  */
4739           pipe = VFP11_FMAC;
4740           goto vfp_binop;
4741
4742         case 8: /* fdiv[sd].  */
4743           pipe = VFP11_DS;
4744           vfp_binop:
4745           bfd_arm_vfp11_write_mask (destmask, fd);
4746           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
4747           regs[1] = fm;
4748           *numregs = 2;
4749           break;
4750
4751         case 15: /* extended opcode.  */
4752           {
4753             unsigned int extn = ((insn >> 15) & 0x1e)
4754                               | ((insn >> 7) & 1);
4755
4756             switch (extn)
4757               {
4758               case 0: /* fcpy[sd].  */
4759               case 1: /* fabs[sd].  */
4760               case 2: /* fneg[sd].  */
4761               case 8: /* fcmp[sd].  */
4762               case 9: /* fcmpe[sd].  */
4763               case 10: /* fcmpz[sd].  */
4764               case 11: /* fcmpez[sd].  */
4765               case 16: /* fuito[sd].  */
4766               case 17: /* fsito[sd].  */
4767               case 24: /* ftoui[sd].  */
4768               case 25: /* ftouiz[sd].  */
4769               case 26: /* ftosi[sd].  */
4770               case 27: /* ftosiz[sd].  */
4771                 /* These instructions will not bounce due to underflow.  */
4772                 *numregs = 0;
4773                 pipe = VFP11_FMAC;
4774                 break;
4775
4776               case 3: /* fsqrt[sd].  */
4777                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
4778                    registers to cause the erratum in previous instructions.  */
4779                 bfd_arm_vfp11_write_mask (destmask, fd);
4780                 pipe = VFP11_DS;
4781                 break;
4782
4783               case 15: /* fcvt{ds,sd}.  */
4784                 {
4785                   int rnum = 0;
4786
4787                   bfd_arm_vfp11_write_mask (destmask, fd);
4788
4789                   /* Only FCVTSD can underflow.  */
4790                   if ((insn & 0x100) != 0)
4791                     regs[rnum++] = fm;
4792
4793                   *numregs = rnum;
4794
4795                   pipe = VFP11_FMAC;
4796                 }
4797                 break;
4798
4799               default:
4800                 return VFP11_BAD;
4801               }
4802           }
4803           break;
4804
4805         default:
4806           return VFP11_BAD;
4807         }
4808     }
4809   /* Two-register transfer.  */
4810   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
4811     {
4812       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
4813
4814       if ((insn & 0x100000) == 0)
4815         {
4816           if (is_double)
4817             bfd_arm_vfp11_write_mask (destmask, fm);
4818           else
4819             {
4820               bfd_arm_vfp11_write_mask (destmask, fm);
4821               bfd_arm_vfp11_write_mask (destmask, fm + 1);
4822             }
4823         }
4824
4825       pipe = VFP11_LS;
4826     }
4827   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
4828     {
4829       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
4830       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
4831
4832       switch (puw)
4833         {
4834         case 0: /* Two-reg transfer.  We should catch these above.  */
4835           abort ();
4836
4837         case 2: /* fldm[sdx].  */
4838         case 3:
4839         case 5:
4840           {
4841             unsigned int i, offset = insn & 0xff;
4842
4843             if (is_double)
4844               offset >>= 1;
4845
4846             for (i = fd; i < fd + offset; i++)
4847               bfd_arm_vfp11_write_mask (destmask, i);
4848           }
4849           break;
4850
4851         case 4: /* fld[sd].  */
4852         case 6:
4853           bfd_arm_vfp11_write_mask (destmask, fd);
4854           break;
4855
4856         default:
4857           return VFP11_BAD;
4858         }
4859
4860       pipe = VFP11_LS;
4861     }
4862   /* Single-register transfer. Note L==0.  */
4863   else if ((insn & 0x0f100e10) == 0x0e000a10)
4864     {
4865       unsigned int opcode = (insn >> 21) & 7;
4866       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
4867
4868       switch (opcode)
4869         {
4870         case 0: /* fmsr/fmdlr.  */
4871         case 1: /* fmdhr.  */
4872           /* Mark fmdhr and fmdlr as writing to the whole of the DP
4873              destination register.  I don't know if this is exactly right,
4874              but it is the conservative choice.  */
4875           bfd_arm_vfp11_write_mask (destmask, fn);
4876           break;
4877
4878         case 7: /* fmxr.  */
4879           break;
4880         }
4881
4882       pipe = VFP11_LS;
4883     }
4884
4885   return pipe;
4886 }
4887
4888
4889 static int elf32_arm_compare_mapping (const void * a, const void * b);
4890
4891
4892 /* Look for potentially-troublesome code sequences which might trigger the
4893    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
4894    (available from ARM) for details of the erratum.  A short version is
4895    described in ld.texinfo.  */
4896
4897 bfd_boolean
4898 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
4899 {
4900   asection *sec;
4901   bfd_byte *contents = NULL;
4902   int state = 0;
4903   int regs[3], numregs = 0;
4904   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
4905   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
4906
4907   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
4908      The states transition as follows:
4909
4910        0 -> 1 (vector) or 0 -> 2 (scalar)
4911            A VFP FMAC-pipeline instruction has been seen. Fill
4912            regs[0]..regs[numregs-1] with its input operands. Remember this
4913            instruction in 'first_fmac'.
4914
4915        1 -> 2
4916            Any instruction, except for a VFP instruction which overwrites
4917            regs[*].
4918
4919        1 -> 3 [ -> 0 ]  or
4920        2 -> 3 [ -> 0 ]
4921            A VFP instruction has been seen which overwrites any of regs[*].
4922            We must make a veneer!  Reset state to 0 before examining next
4923            instruction.
4924
4925        2 -> 0
4926            If we fail to match anything in state 2, reset to state 0 and reset
4927            the instruction pointer to the instruction after 'first_fmac'.
4928
4929      If the VFP11 vector mode is in use, there must be at least two unrelated
4930      instructions between anti-dependent VFP11 instructions to properly avoid
4931      triggering the erratum, hence the use of the extra state 1.  */
4932
4933   /* If we are only performing a partial link do not bother
4934      to construct any glue.  */
4935   if (link_info->relocatable)
4936     return TRUE;
4937
4938   /* Skip if this bfd does not correspond to an ELF image.  */
4939   if (! is_arm_elf (abfd))
4940     return TRUE;
4941
4942   /* We should have chosen a fix type by the time we get here.  */
4943   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
4944
4945   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
4946     return TRUE;
4947
4948   for (sec = abfd->sections; sec != NULL; sec = sec->next)
4949     {
4950       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
4951       struct _arm_elf_section_data *sec_data;
4952
4953       /* If we don't have executable progbits, we're not interested in this
4954          section.  Also skip if section is to be excluded.  */
4955       if (elf_section_type (sec) != SHT_PROGBITS
4956           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
4957           || (sec->flags & SEC_EXCLUDE) != 0
4958           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
4959         continue;
4960
4961       sec_data = elf32_arm_section_data (sec);
4962
4963       if (sec_data->mapcount == 0)
4964         continue;
4965
4966       if (elf_section_data (sec)->this_hdr.contents != NULL)
4967         contents = elf_section_data (sec)->this_hdr.contents;
4968       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
4969         goto error_return;
4970
4971       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
4972              elf32_arm_compare_mapping);
4973
4974       for (span = 0; span < sec_data->mapcount; span++)
4975         {
4976           unsigned int span_start = sec_data->map[span].vma;
4977           unsigned int span_end = (span == sec_data->mapcount - 1)
4978                                   ? sec->size : sec_data->map[span + 1].vma;
4979           char span_type = sec_data->map[span].type;
4980
4981           /* FIXME: Only ARM mode is supported at present.  We may need to
4982              support Thumb-2 mode also at some point.  */
4983           if (span_type != 'a')
4984             continue;
4985
4986           for (i = span_start; i < span_end;)
4987             {
4988               unsigned int next_i = i + 4;
4989               unsigned int insn = bfd_big_endian (abfd)
4990                 ? (contents[i] << 24)
4991                   | (contents[i + 1] << 16)
4992                   | (contents[i + 2] << 8)
4993                   | contents[i + 3]
4994                 : (contents[i + 3] << 24)
4995                   | (contents[i + 2] << 16)
4996                   | (contents[i + 1] << 8)
4997                   | contents[i];
4998               unsigned int writemask = 0;
4999               enum bfd_arm_vfp11_pipe pipe;
5000
5001               switch (state)
5002                 {
5003                 case 0:
5004                   pipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
5005                                                     &numregs);
5006                   /* I'm assuming the VFP11 erratum can trigger with denorm
5007                      operands on either the FMAC or the DS pipeline. This might
5008                      lead to slightly overenthusiastic veneer insertion.  */
5009                   if (pipe == VFP11_FMAC || pipe == VFP11_DS)
5010                     {
5011                       state = use_vector ? 1 : 2;
5012                       first_fmac = i;
5013                       veneer_of_insn = insn;
5014                     }
5015                   break;
5016
5017                 case 1:
5018                   {
5019                     int other_regs[3], other_numregs;
5020                     pipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
5021                                                       other_regs,
5022                                                       &other_numregs);
5023                     if (pipe != VFP11_BAD
5024                         && bfd_arm_vfp11_antidependency (writemask, regs,
5025                                                          numregs))
5026                       state = 3;
5027                     else
5028                       state = 2;
5029                   }
5030                   break;
5031
5032                 case 2:
5033                   {
5034                     int other_regs[3], other_numregs;
5035                     pipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
5036                                                       other_regs,
5037                                                       &other_numregs);
5038                     if (pipe != VFP11_BAD
5039                         && bfd_arm_vfp11_antidependency (writemask, regs,
5040                                                          numregs))
5041                       state = 3;
5042                     else
5043                       {
5044                         state = 0;
5045                         next_i = first_fmac + 4;
5046                       }
5047                   }
5048                   break;
5049
5050                 case 3:
5051                   abort ();  /* Should be unreachable.  */
5052                 }
5053
5054               if (state == 3)
5055                 {
5056                   elf32_vfp11_erratum_list *newerr
5057                     = bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
5058                   int errcount;
5059
5060                   errcount = ++(elf32_arm_section_data (sec)->erratumcount);
5061
5062                   newerr->u.b.vfp_insn = veneer_of_insn;
5063
5064                   switch (span_type)
5065                     {
5066                     case 'a':
5067                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
5068                       break;
5069
5070                     default:
5071                       abort ();
5072                     }
5073
5074                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
5075                                                first_fmac);
5076
5077                   newerr->vma = -1;
5078
5079                   newerr->next = sec_data->erratumlist;
5080                   sec_data->erratumlist = newerr;
5081
5082                   state = 0;
5083                 }
5084
5085               i = next_i;
5086             }
5087         }
5088
5089       if (contents != NULL
5090           && elf_section_data (sec)->this_hdr.contents != contents)
5091         free (contents);
5092       contents = NULL;
5093     }
5094
5095   return TRUE;
5096
5097 error_return:
5098   if (contents != NULL
5099       && elf_section_data (sec)->this_hdr.contents != contents)
5100     free (contents);
5101
5102   return FALSE;
5103 }
5104
5105 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
5106    after sections have been laid out, using specially-named symbols.  */
5107
5108 void
5109 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
5110                                           struct bfd_link_info *link_info)
5111 {
5112   asection *sec;
5113   struct elf32_arm_link_hash_table *globals;
5114   char *tmp_name;
5115
5116   if (link_info->relocatable)
5117     return;
5118
5119   /* Skip if this bfd does not correspond to an ELF image.  */
5120   if (! is_arm_elf (abfd))
5121     return;
5122
5123   globals = elf32_arm_hash_table (link_info);
5124
5125   tmp_name = bfd_malloc ((bfd_size_type) strlen
5126                            (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
5127
5128   for (sec = abfd->sections; sec != NULL; sec = sec->next)
5129     {
5130       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
5131       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
5132
5133       for (; errnode != NULL; errnode = errnode->next)
5134         {
5135           struct elf_link_hash_entry *myh;
5136           bfd_vma vma;
5137
5138           switch (errnode->type)
5139             {
5140             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
5141             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
5142               /* Find veneer symbol.  */
5143               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
5144                        errnode->u.b.veneer->u.v.id);
5145
5146               myh = elf_link_hash_lookup
5147                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
5148
5149               if (myh == NULL)
5150                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
5151                                          "`%s'"), abfd, tmp_name);
5152
5153               vma = myh->root.u.def.section->output_section->vma
5154                     + myh->root.u.def.section->output_offset
5155                     + myh->root.u.def.value;
5156
5157               errnode->u.b.veneer->vma = vma;
5158               break;
5159
5160             case VFP11_ERRATUM_ARM_VENEER:
5161             case VFP11_ERRATUM_THUMB_VENEER:
5162               /* Find return location.  */
5163               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
5164                        errnode->u.v.id);
5165
5166               myh = elf_link_hash_lookup
5167                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
5168
5169               if (myh == NULL)
5170                 (*_bfd_error_handler) (_("%B: unable to find VFP11 veneer "
5171                                          "`%s'"), abfd, tmp_name);
5172
5173               vma = myh->root.u.def.section->output_section->vma
5174                     + myh->root.u.def.section->output_offset
5175                     + myh->root.u.def.value;
5176
5177               errnode->u.v.branch->vma = vma;
5178               break;
5179
5180             default:
5181               abort ();
5182             }
5183         }
5184     }
5185
5186   free (tmp_name);
5187 }
5188
5189
5190 /* Set target relocation values needed during linking.  */
5191
5192 void
5193 bfd_elf32_arm_set_target_relocs (struct bfd *output_bfd,
5194                                  struct bfd_link_info *link_info,
5195                                  int target1_is_rel,
5196                                  char * target2_type,
5197                                  int fix_v4bx,
5198                                  int use_blx,
5199                                  bfd_arm_vfp11_fix vfp11_fix,
5200                                  int no_enum_warn, int no_wchar_warn,
5201                                  int pic_veneer)
5202 {
5203   struct elf32_arm_link_hash_table *globals;
5204
5205   globals = elf32_arm_hash_table (link_info);
5206
5207   globals->target1_is_rel = target1_is_rel;
5208   if (strcmp (target2_type, "rel") == 0)
5209     globals->target2_reloc = R_ARM_REL32;
5210   else if (strcmp (target2_type, "abs") == 0)
5211     globals->target2_reloc = R_ARM_ABS32;
5212   else if (strcmp (target2_type, "got-rel") == 0)
5213     globals->target2_reloc = R_ARM_GOT_PREL;
5214   else
5215     {
5216       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
5217                           target2_type);
5218     }
5219   globals->fix_v4bx = fix_v4bx;
5220   globals->use_blx |= use_blx;
5221   globals->vfp11_fix = vfp11_fix;
5222   globals->pic_veneer = pic_veneer;
5223
5224   BFD_ASSERT (is_arm_elf (output_bfd));
5225   elf_arm_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
5226   elf_arm_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
5227 }
5228
5229 /* Replace the target offset of a Thumb bl or b.w instruction.  */
5230
5231 static void
5232 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
5233 {
5234   bfd_vma upper;
5235   bfd_vma lower;
5236   int reloc_sign;
5237
5238   BFD_ASSERT ((offset & 1) == 0);
5239
5240   upper = bfd_get_16 (abfd, insn);
5241   lower = bfd_get_16 (abfd, insn + 2);
5242   reloc_sign = (offset < 0) ? 1 : 0;
5243   upper = (upper & ~(bfd_vma) 0x7ff)
5244           | ((offset >> 12) & 0x3ff)
5245           | (reloc_sign << 10);
5246   lower = (lower & ~(bfd_vma) 0x2fff)
5247           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
5248           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
5249           | ((offset >> 1) & 0x7ff);
5250   bfd_put_16 (abfd, upper, insn);
5251   bfd_put_16 (abfd, lower, insn + 2);
5252 }
5253
5254 /* Thumb code calling an ARM function.  */
5255
5256 static int
5257 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
5258                          const char *           name,
5259                          bfd *                  input_bfd,
5260                          bfd *                  output_bfd,
5261                          asection *             input_section,
5262                          bfd_byte *             hit_data,
5263                          asection *             sym_sec,
5264                          bfd_vma                offset,
5265                          bfd_signed_vma         addend,
5266                          bfd_vma                val,
5267                          char **error_message)
5268 {
5269   asection * s = 0;
5270   bfd_vma my_offset;
5271   long int ret_offset;
5272   struct elf_link_hash_entry * myh;
5273   struct elf32_arm_link_hash_table * globals;
5274
5275   myh = find_thumb_glue (info, name, error_message);
5276   if (myh == NULL)
5277     return FALSE;
5278
5279   globals = elf32_arm_hash_table (info);
5280
5281   BFD_ASSERT (globals != NULL);
5282   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5283
5284   my_offset = myh->root.u.def.value;
5285
5286   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
5287                                THUMB2ARM_GLUE_SECTION_NAME);
5288
5289   BFD_ASSERT (s != NULL);
5290   BFD_ASSERT (s->contents != NULL);
5291   BFD_ASSERT (s->output_section != NULL);
5292
5293   if ((my_offset & 0x01) == 0x01)
5294     {
5295       if (sym_sec != NULL
5296           && sym_sec->owner != NULL
5297           && !INTERWORK_FLAG (sym_sec->owner))
5298         {
5299           (*_bfd_error_handler)
5300             (_("%B(%s): warning: interworking not enabled.\n"
5301                "  first occurrence: %B: thumb call to arm"),
5302              sym_sec->owner, input_bfd, name);
5303
5304           return FALSE;
5305         }
5306
5307       --my_offset;
5308       myh->root.u.def.value = my_offset;
5309
5310       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
5311                       s->contents + my_offset);
5312
5313       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
5314                       s->contents + my_offset + 2);
5315
5316       ret_offset =
5317         /* Address of destination of the stub.  */
5318         ((bfd_signed_vma) val)
5319         - ((bfd_signed_vma)
5320            /* Offset from the start of the current section
5321               to the start of the stubs.  */
5322            (s->output_offset
5323             /* Offset of the start of this stub from the start of the stubs.  */
5324             + my_offset
5325             /* Address of the start of the current section.  */
5326             + s->output_section->vma)
5327            /* The branch instruction is 4 bytes into the stub.  */
5328            + 4
5329            /* ARM branches work from the pc of the instruction + 8.  */
5330            + 8);
5331
5332       put_arm_insn (globals, output_bfd,
5333                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
5334                     s->contents + my_offset + 4);
5335     }
5336
5337   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
5338
5339   /* Now go back and fix up the original BL insn to point to here.  */
5340   ret_offset =
5341     /* Address of where the stub is located.  */
5342     (s->output_section->vma + s->output_offset + my_offset)
5343      /* Address of where the BL is located.  */
5344     - (input_section->output_section->vma + input_section->output_offset
5345        + offset)
5346     /* Addend in the relocation.  */
5347     - addend
5348     /* Biassing for PC-relative addressing.  */
5349     - 8;
5350
5351   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
5352
5353   return TRUE;
5354 }
5355
5356 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
5357
5358 static struct elf_link_hash_entry *
5359 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
5360                              const char *           name,
5361                              bfd *                  input_bfd,
5362                              bfd *                  output_bfd,
5363                              asection *             sym_sec,
5364                              bfd_vma                val,
5365                              asection *             s,
5366                              char **                error_message)
5367 {
5368   bfd_vma my_offset;
5369   long int ret_offset;
5370   struct elf_link_hash_entry * myh;
5371   struct elf32_arm_link_hash_table * globals;
5372
5373   myh = find_arm_glue (info, name, error_message);
5374   if (myh == NULL)
5375     return NULL;
5376
5377   globals = elf32_arm_hash_table (info);
5378
5379   BFD_ASSERT (globals != NULL);
5380   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5381
5382   my_offset = myh->root.u.def.value;
5383
5384   if ((my_offset & 0x01) == 0x01)
5385     {
5386       if (sym_sec != NULL
5387           && sym_sec->owner != NULL
5388           && !INTERWORK_FLAG (sym_sec->owner))
5389         {
5390           (*_bfd_error_handler)
5391             (_("%B(%s): warning: interworking not enabled.\n"
5392                "  first occurrence: %B: arm call to thumb"),
5393              sym_sec->owner, input_bfd, name);
5394         }
5395
5396       --my_offset;
5397       myh->root.u.def.value = my_offset;
5398
5399       if (info->shared || globals->root.is_relocatable_executable
5400           || globals->pic_veneer)
5401         {
5402           /* For relocatable objects we can't use absolute addresses,
5403              so construct the address from a relative offset.  */
5404           /* TODO: If the offset is small it's probably worth
5405              constructing the address with adds.  */
5406           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
5407                         s->contents + my_offset);
5408           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
5409                         s->contents + my_offset + 4);
5410           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
5411                         s->contents + my_offset + 8);
5412           /* Adjust the offset by 4 for the position of the add,
5413              and 8 for the pipeline offset.  */
5414           ret_offset = (val - (s->output_offset
5415                                + s->output_section->vma
5416                                + my_offset + 12))
5417                        | 1;
5418           bfd_put_32 (output_bfd, ret_offset,
5419                       s->contents + my_offset + 12);
5420         }
5421       else if (globals->use_blx)
5422         {
5423           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
5424                         s->contents + my_offset);
5425
5426           /* It's a thumb address.  Add the low order bit.  */
5427           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
5428                       s->contents + my_offset + 4);
5429         }
5430       else
5431         {
5432           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
5433                         s->contents + my_offset);
5434
5435           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
5436                         s->contents + my_offset + 4);
5437
5438           /* It's a thumb address.  Add the low order bit.  */
5439           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
5440                       s->contents + my_offset + 8);
5441
5442           my_offset += 12;
5443         }
5444     }
5445
5446   BFD_ASSERT (my_offset <= globals->arm_glue_size);
5447
5448   return myh;
5449 }
5450
5451 /* Arm code calling a Thumb function.  */
5452
5453 static int
5454 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
5455                          const char *           name,
5456                          bfd *                  input_bfd,
5457                          bfd *                  output_bfd,
5458                          asection *             input_section,
5459                          bfd_byte *             hit_data,
5460                          asection *             sym_sec,
5461                          bfd_vma                offset,
5462                          bfd_signed_vma         addend,
5463                          bfd_vma                val,
5464                          char **error_message)
5465 {
5466   unsigned long int tmp;
5467   bfd_vma my_offset;
5468   asection * s;
5469   long int ret_offset;
5470   struct elf_link_hash_entry * myh;
5471   struct elf32_arm_link_hash_table * globals;
5472
5473   globals = elf32_arm_hash_table (info);
5474
5475   BFD_ASSERT (globals != NULL);
5476   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5477
5478   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
5479                                ARM2THUMB_GLUE_SECTION_NAME);
5480   BFD_ASSERT (s != NULL);
5481   BFD_ASSERT (s->contents != NULL);
5482   BFD_ASSERT (s->output_section != NULL);
5483
5484   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
5485                                      sym_sec, val, s, error_message);
5486   if (!myh)
5487     return FALSE;
5488
5489   my_offset = myh->root.u.def.value;
5490   tmp = bfd_get_32 (input_bfd, hit_data);
5491   tmp = tmp & 0xFF000000;
5492
5493   /* Somehow these are both 4 too far, so subtract 8.  */
5494   ret_offset = (s->output_offset
5495                 + my_offset
5496                 + s->output_section->vma
5497                 - (input_section->output_offset
5498                    + input_section->output_section->vma
5499                    + offset + addend)
5500                 - 8);
5501
5502   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
5503
5504   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
5505
5506   return TRUE;
5507 }
5508
5509 /* Populate Arm stub for an exported Thumb function.  */
5510
5511 static bfd_boolean
5512 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
5513 {
5514   struct bfd_link_info * info = (struct bfd_link_info *) inf;
5515   asection * s;
5516   struct elf_link_hash_entry * myh;
5517   struct elf32_arm_link_hash_entry *eh;
5518   struct elf32_arm_link_hash_table * globals;
5519   asection *sec;
5520   bfd_vma val;
5521   char *error_message;
5522
5523   eh = elf32_arm_hash_entry (h);
5524   /* Allocate stubs for exported Thumb functions on v4t.  */
5525   if (eh->export_glue == NULL)
5526     return TRUE;
5527
5528   globals = elf32_arm_hash_table (info);
5529
5530   BFD_ASSERT (globals != NULL);
5531   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5532
5533   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
5534                                ARM2THUMB_GLUE_SECTION_NAME);
5535   BFD_ASSERT (s != NULL);
5536   BFD_ASSERT (s->contents != NULL);
5537   BFD_ASSERT (s->output_section != NULL);
5538
5539   sec = eh->export_glue->root.u.def.section;
5540
5541   BFD_ASSERT (sec->output_section != NULL);
5542
5543   val = eh->export_glue->root.u.def.value + sec->output_offset
5544         + sec->output_section->vma;
5545
5546   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
5547                                      h->root.u.def.section->owner,
5548                                      globals->obfd, sec, val, s,
5549                                      &error_message);
5550   BFD_ASSERT (myh);
5551   return TRUE;
5552 }
5553
5554 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
5555
5556 static bfd_vma
5557 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
5558 {
5559   bfd_byte *p;
5560   bfd_vma glue_addr;
5561   asection *s;
5562   struct elf32_arm_link_hash_table *globals;
5563
5564   globals = elf32_arm_hash_table (info);
5565
5566   BFD_ASSERT (globals != NULL);
5567   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
5568
5569   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
5570                                ARM_BX_GLUE_SECTION_NAME);
5571   BFD_ASSERT (s != NULL);
5572   BFD_ASSERT (s->contents != NULL);
5573   BFD_ASSERT (s->output_section != NULL);
5574
5575   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
5576
5577   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
5578
5579   if ((globals->bx_glue_offset[reg] & 1) == 0)
5580     {
5581       p = s->contents + glue_addr;
5582       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
5583       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
5584       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
5585       globals->bx_glue_offset[reg] |= 1;
5586     }
5587
5588   return glue_addr + s->output_section->vma + s->output_offset;
5589 }
5590
5591 /* Generate Arm stubs for exported Thumb symbols.  */
5592 static void
5593 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
5594                                   struct bfd_link_info *link_info)
5595 {
5596   struct elf32_arm_link_hash_table * globals;
5597
5598   if (link_info == NULL)
5599     /* Ignore this if we are not called by the ELF backend linker.  */
5600     return;
5601
5602   globals = elf32_arm_hash_table (link_info);
5603   /* If blx is available then exported Thumb symbols are OK and there is
5604      nothing to do.  */
5605   if (globals->use_blx)
5606     return;
5607
5608   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
5609                           link_info);
5610 }
5611
5612 /* Some relocations map to different relocations depending on the
5613    target.  Return the real relocation.  */
5614
5615 static int
5616 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
5617                      int r_type)
5618 {
5619   switch (r_type)
5620     {
5621     case R_ARM_TARGET1:
5622       if (globals->target1_is_rel)
5623         return R_ARM_REL32;
5624       else
5625         return R_ARM_ABS32;
5626
5627     case R_ARM_TARGET2:
5628       return globals->target2_reloc;
5629
5630     default:
5631       return r_type;
5632     }
5633 }
5634
5635 /* Return the base VMA address which should be subtracted from real addresses
5636    when resolving @dtpoff relocation.
5637    This is PT_TLS segment p_vaddr.  */
5638
5639 static bfd_vma
5640 dtpoff_base (struct bfd_link_info *info)
5641 {
5642   /* If tls_sec is NULL, we should have signalled an error already.  */
5643   if (elf_hash_table (info)->tls_sec == NULL)
5644     return 0;
5645   return elf_hash_table (info)->tls_sec->vma;
5646 }
5647
5648 /* Return the relocation value for @tpoff relocation
5649    if STT_TLS virtual address is ADDRESS.  */
5650
5651 static bfd_vma
5652 tpoff (struct bfd_link_info *info, bfd_vma address)
5653 {
5654   struct elf_link_hash_table *htab = elf_hash_table (info);
5655   bfd_vma base;
5656
5657   /* If tls_sec is NULL, we should have signalled an error already.  */
5658   if (htab->tls_sec == NULL)
5659     return 0;
5660   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
5661   return address - htab->tls_sec->vma + base;
5662 }
5663
5664 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
5665    VALUE is the relocation value.  */
5666
5667 static bfd_reloc_status_type
5668 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
5669 {
5670   if (value > 0xfff)
5671     return bfd_reloc_overflow;
5672
5673   value |= bfd_get_32 (abfd, data) & 0xfffff000;
5674   bfd_put_32 (abfd, value, data);
5675   return bfd_reloc_ok;
5676 }
5677
5678 /* For a given value of n, calculate the value of G_n as required to
5679    deal with group relocations.  We return it in the form of an
5680    encoded constant-and-rotation, together with the final residual.  If n is
5681    specified as less than zero, then final_residual is filled with the
5682    input value and no further action is performed.  */
5683
5684 static bfd_vma
5685 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
5686 {
5687   int current_n;
5688   bfd_vma g_n;
5689   bfd_vma encoded_g_n = 0;
5690   bfd_vma residual = value; /* Also known as Y_n.  */
5691
5692   for (current_n = 0; current_n <= n; current_n++)
5693     {
5694       int shift;
5695
5696       /* Calculate which part of the value to mask.  */
5697       if (residual == 0)
5698         shift = 0;
5699       else
5700         {
5701           int msb;
5702
5703           /* Determine the most significant bit in the residual and
5704              align the resulting value to a 2-bit boundary.  */
5705           for (msb = 30; msb >= 0; msb -= 2)
5706             if (residual & (3 << msb))
5707               break;
5708
5709           /* The desired shift is now (msb - 6), or zero, whichever
5710              is the greater.  */
5711           shift = msb - 6;
5712           if (shift < 0)
5713             shift = 0;
5714         }
5715
5716       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
5717       g_n = residual & (0xff << shift);
5718       encoded_g_n = (g_n >> shift)
5719                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
5720
5721       /* Calculate the residual for the next time around.  */
5722       residual &= ~g_n;
5723     }
5724
5725   *final_residual = residual;
5726
5727   return encoded_g_n;
5728 }
5729
5730 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
5731    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
5732
5733 static int
5734 identify_add_or_sub (bfd_vma insn)
5735 {
5736   int opcode = insn & 0x1e00000;
5737
5738   if (opcode == 1 << 23) /* ADD */
5739     return 1;
5740
5741   if (opcode == 1 << 22) /* SUB */
5742     return -1;
5743
5744   return 0;
5745 }
5746
5747 /* Perform a relocation as part of a final link.  */
5748
5749 static bfd_reloc_status_type
5750 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
5751                                bfd *                        input_bfd,
5752                                bfd *                        output_bfd,
5753                                asection *                   input_section,
5754                                bfd_byte *                   contents,
5755                                Elf_Internal_Rela *          rel,
5756                                bfd_vma                      value,
5757                                struct bfd_link_info *       info,
5758                                asection *                   sym_sec,
5759                                const char *                 sym_name,
5760                                int                          sym_flags,
5761                                struct elf_link_hash_entry * h,
5762                                bfd_boolean *                unresolved_reloc_p,
5763                                char **                      error_message)
5764 {
5765   unsigned long                 r_type = howto->type;
5766   unsigned long                 r_symndx;
5767   bfd_byte *                    hit_data = contents + rel->r_offset;
5768   bfd *                         dynobj = NULL;
5769   Elf_Internal_Shdr *           symtab_hdr;
5770   struct elf_link_hash_entry ** sym_hashes;
5771   bfd_vma *                     local_got_offsets;
5772   asection *                    sgot = NULL;
5773   asection *                    splt = NULL;
5774   asection *                    sreloc = NULL;
5775   bfd_vma                       addend;
5776   bfd_signed_vma                signed_addend;
5777   struct elf32_arm_link_hash_table * globals;
5778
5779   globals = elf32_arm_hash_table (info);
5780
5781   BFD_ASSERT (is_arm_elf (input_bfd));
5782
5783   /* Some relocation types map to different relocations depending on the
5784      target.  We pick the right one here.  */
5785   r_type = arm_real_reloc_type (globals, r_type);
5786   if (r_type != howto->type)
5787     howto = elf32_arm_howto_from_type (r_type);
5788
5789   /* If the start address has been set, then set the EF_ARM_HASENTRY
5790      flag.  Setting this more than once is redundant, but the cost is
5791      not too high, and it keeps the code simple.
5792
5793      The test is done  here, rather than somewhere else, because the
5794      start address is only set just before the final link commences.
5795
5796      Note - if the user deliberately sets a start address of 0, the
5797      flag will not be set.  */
5798   if (bfd_get_start_address (output_bfd) != 0)
5799     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
5800
5801   dynobj = elf_hash_table (info)->dynobj;
5802   if (dynobj)
5803     {
5804       sgot = bfd_get_section_by_name (dynobj, ".got");
5805       splt = bfd_get_section_by_name (dynobj, ".plt");
5806     }
5807   symtab_hdr = & elf_symtab_hdr (input_bfd);
5808   sym_hashes = elf_sym_hashes (input_bfd);
5809   local_got_offsets = elf_local_got_offsets (input_bfd);
5810   r_symndx = ELF32_R_SYM (rel->r_info);
5811
5812   if (globals->use_rel)
5813     {
5814       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
5815
5816       if (addend & ((howto->src_mask + 1) >> 1))
5817         {
5818           signed_addend = -1;
5819           signed_addend &= ~ howto->src_mask;
5820           signed_addend |= addend;
5821         }
5822       else
5823         signed_addend = addend;
5824     }
5825   else
5826     addend = signed_addend = rel->r_addend;
5827
5828   switch (r_type)
5829     {
5830     case R_ARM_NONE:
5831       /* We don't need to find a value for this symbol.  It's just a
5832          marker.  */
5833       *unresolved_reloc_p = FALSE;
5834       return bfd_reloc_ok;
5835
5836     case R_ARM_ABS12:
5837       if (!globals->vxworks_p)
5838         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
5839
5840     case R_ARM_PC24:
5841     case R_ARM_ABS32:
5842     case R_ARM_ABS32_NOI:
5843     case R_ARM_REL32:
5844     case R_ARM_REL32_NOI:
5845     case R_ARM_CALL:
5846     case R_ARM_JUMP24:
5847     case R_ARM_XPC25:
5848     case R_ARM_PREL31:
5849     case R_ARM_PLT32:
5850       /* Handle relocations which should use the PLT entry.  ABS32/REL32
5851          will use the symbol's value, which may point to a PLT entry, but we
5852          don't need to handle that here.  If we created a PLT entry, all
5853          branches in this object should go to it.  */
5854       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
5855            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI)
5856           && h != NULL
5857           && splt != NULL
5858           && h->plt.offset != (bfd_vma) -1)
5859         {
5860           /* If we've created a .plt section, and assigned a PLT entry to
5861              this function, it should not be known to bind locally.  If
5862              it were, we would have cleared the PLT entry.  */
5863           BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
5864
5865           value = (splt->output_section->vma
5866                    + splt->output_offset
5867                    + h->plt.offset);
5868           *unresolved_reloc_p = FALSE;
5869           return _bfd_final_link_relocate (howto, input_bfd, input_section,
5870                                            contents, rel->r_offset, value,
5871                                            rel->r_addend);
5872         }
5873
5874       /* When generating a shared object or relocatable executable, these
5875          relocations are copied into the output file to be resolved at
5876          run time.  */
5877       if ((info->shared || globals->root.is_relocatable_executable)
5878           && (input_section->flags & SEC_ALLOC)
5879           && !(elf32_arm_hash_table (info)->vxworks_p
5880                && strcmp (input_section->output_section->name,
5881                           ".tls_vars") == 0)
5882           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
5883               || !SYMBOL_CALLS_LOCAL (info, h))
5884           && (h == NULL
5885               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5886               || h->root.type != bfd_link_hash_undefweak)
5887           && r_type != R_ARM_PC24
5888           && r_type != R_ARM_CALL
5889           && r_type != R_ARM_JUMP24
5890           && r_type != R_ARM_PREL31
5891           && r_type != R_ARM_PLT32)
5892         {
5893           Elf_Internal_Rela outrel;
5894           bfd_byte *loc;
5895           bfd_boolean skip, relocate;
5896
5897           *unresolved_reloc_p = FALSE;
5898
5899           if (sreloc == NULL)
5900             {
5901               const char * name;
5902
5903               name = (bfd_elf_string_from_elf_section
5904                       (input_bfd,
5905                        elf_elfheader (input_bfd)->e_shstrndx,
5906                        elf_section_data (input_section)->rel_hdr.sh_name));
5907               if (name == NULL)
5908                 return bfd_reloc_notsupported;
5909
5910               BFD_ASSERT (reloc_section_p (globals, name, input_section));
5911
5912               sreloc = bfd_get_section_by_name (dynobj, name);
5913               BFD_ASSERT (sreloc != NULL);
5914             }
5915
5916           skip = FALSE;
5917           relocate = FALSE;
5918
5919           outrel.r_addend = addend;
5920           outrel.r_offset =
5921             _bfd_elf_section_offset (output_bfd, info, input_section,
5922                                      rel->r_offset);
5923           if (outrel.r_offset == (bfd_vma) -1)
5924             skip = TRUE;
5925           else if (outrel.r_offset == (bfd_vma) -2)
5926             skip = TRUE, relocate = TRUE;
5927           outrel.r_offset += (input_section->output_section->vma
5928                               + input_section->output_offset);
5929
5930           if (skip)
5931             memset (&outrel, 0, sizeof outrel);
5932           else if (h != NULL
5933                    && h->dynindx != -1
5934                    && (!info->shared
5935                        || !info->symbolic
5936                        || !h->def_regular))
5937             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5938           else
5939             {
5940               int symbol;
5941
5942               /* This symbol is local, or marked to become local.  */
5943               if (sym_flags == STT_ARM_TFUNC)
5944                 value |= 1;
5945               if (globals->symbian_p)
5946                 {
5947                   asection *osec;
5948
5949                   /* On Symbian OS, the data segment and text segement
5950                      can be relocated independently.  Therefore, we
5951                      must indicate the segment to which this
5952                      relocation is relative.  The BPABI allows us to
5953                      use any symbol in the right segment; we just use
5954                      the section symbol as it is convenient.  (We
5955                      cannot use the symbol given by "h" directly as it
5956                      will not appear in the dynamic symbol table.)
5957
5958                      Note that the dynamic linker ignores the section
5959                      symbol value, so we don't subtract osec->vma
5960                      from the emitted reloc addend.  */
5961                   if (sym_sec)
5962                     osec = sym_sec->output_section;
5963                   else
5964                     osec = input_section->output_section;
5965                   symbol = elf_section_data (osec)->dynindx;
5966                   if (symbol == 0)
5967                     {
5968                       struct elf_link_hash_table *htab = elf_hash_table (info);
5969
5970                       if ((osec->flags & SEC_READONLY) == 0
5971                           && htab->data_index_section != NULL)
5972                         osec = htab->data_index_section;
5973                       else
5974                         osec = htab->text_index_section;
5975                       symbol = elf_section_data (osec)->dynindx;
5976                     }
5977                   BFD_ASSERT (symbol != 0);
5978                 }
5979               else
5980                 /* On SVR4-ish systems, the dynamic loader cannot
5981                    relocate the text and data segments independently,
5982                    so the symbol does not matter.  */
5983                 symbol = 0;
5984               outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
5985               if (globals->use_rel)
5986                 relocate = TRUE;
5987               else
5988                 outrel.r_addend += value;
5989             }
5990
5991           loc = sreloc->contents;
5992           loc += sreloc->reloc_count++ * RELOC_SIZE (globals);
5993           SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
5994
5995           /* If this reloc is against an external symbol, we do not want to
5996              fiddle with the addend.  Otherwise, we need to include the symbol
5997              value so that it becomes an addend for the dynamic reloc.  */
5998           if (! relocate)
5999             return bfd_reloc_ok;
6000
6001           return _bfd_final_link_relocate (howto, input_bfd, input_section,
6002                                            contents, rel->r_offset, value,
6003                                            (bfd_vma) 0);
6004         }
6005       else switch (r_type)
6006         {
6007         case R_ARM_ABS12:
6008           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
6009
6010         case R_ARM_XPC25:         /* Arm BLX instruction.  */
6011         case R_ARM_CALL:
6012         case R_ARM_JUMP24:
6013         case R_ARM_PC24:          /* Arm B/BL instruction.  */
6014         case R_ARM_PLT32:
6015           {
6016           bfd_vma from;
6017           bfd_signed_vma branch_offset;
6018           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
6019
6020           from = (input_section->output_section->vma
6021                   + input_section->output_offset
6022                   + rel->r_offset);
6023           branch_offset = (bfd_signed_vma)(value - from);
6024
6025           if (r_type == R_ARM_XPC25)
6026             {
6027               /* Check for Arm calling Arm function.  */
6028               /* FIXME: Should we translate the instruction into a BL
6029                  instruction instead ?  */
6030               if (sym_flags != STT_ARM_TFUNC)
6031                 (*_bfd_error_handler)
6032                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
6033                    input_bfd,
6034                    h ? h->root.root.string : "(local)");
6035             }
6036           else if (r_type != R_ARM_CALL)
6037             {
6038               /* Check for Arm calling Thumb function.  */
6039               if (sym_flags == STT_ARM_TFUNC)
6040                 {
6041                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
6042                                                output_bfd, input_section,
6043                                                hit_data, sym_sec, rel->r_offset,
6044                                                signed_addend, value,
6045                                                error_message))
6046                     return bfd_reloc_ok;
6047                   else
6048                     return bfd_reloc_dangerous;
6049                 }
6050             }
6051
6052           /* Check if a stub has to be inserted because the
6053              destination is too far or we are changing mode.  */
6054           if (r_type == R_ARM_CALL)
6055             {
6056               if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
6057                   || branch_offset < ARM_MAX_BWD_BRANCH_OFFSET
6058                   || sym_flags == STT_ARM_TFUNC)
6059                 {
6060                   /* The target is out of reach, so redirect the
6061                      branch to the local stub for this function.  */
6062
6063                   stub_entry = elf32_arm_get_stub_entry (input_section,
6064                                                          sym_sec, h,
6065                                                          rel, globals);
6066                   if (stub_entry != NULL)
6067                     value = (stub_entry->stub_offset
6068                              + stub_entry->stub_sec->output_offset
6069                              + stub_entry->stub_sec->output_section->vma);
6070                 }
6071             }
6072
6073           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
6074              where:
6075               S is the address of the symbol in the relocation.
6076               P is address of the instruction being relocated.
6077               A is the addend (extracted from the instruction) in bytes.
6078
6079              S is held in 'value'.
6080              P is the base address of the section containing the
6081                instruction plus the offset of the reloc into that
6082                section, ie:
6083                  (input_section->output_section->vma +
6084                   input_section->output_offset +
6085                   rel->r_offset).
6086              A is the addend, converted into bytes, ie:
6087                  (signed_addend * 4)
6088
6089              Note: None of these operations have knowledge of the pipeline
6090              size of the processor, thus it is up to the assembler to
6091              encode this information into the addend.  */
6092           value -= (input_section->output_section->vma
6093                     + input_section->output_offset);
6094           value -= rel->r_offset;
6095           if (globals->use_rel)
6096             value += (signed_addend << howto->size);
6097           else
6098             /* RELA addends do not have to be adjusted by howto->size.  */
6099             value += signed_addend;
6100
6101           signed_addend = value;
6102           signed_addend >>= howto->rightshift;
6103
6104           /* A branch to an undefined weak symbol is turned into a jump to
6105              the next instruction.  */
6106           if (h && h->root.type == bfd_link_hash_undefweak)
6107             {
6108               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000)
6109                       | 0x0affffff;
6110             }
6111           else
6112             {
6113               /* Perform a signed range check.  */
6114               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
6115                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
6116                 return bfd_reloc_overflow;
6117
6118               addend = (value & 2);
6119
6120               value = (signed_addend & howto->dst_mask)
6121                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
6122
6123               /* Set the H bit in the BLX instruction.  */
6124               if (sym_flags == STT_ARM_TFUNC)
6125                 {
6126                   if (addend)
6127                     value |= (1 << 24);
6128                   else
6129                     value &= ~(bfd_vma)(1 << 24);
6130                 }
6131               if (r_type == R_ARM_CALL)
6132                 {
6133                   /* Select the correct instruction (BL or BLX).  */
6134                   /* Only if we are not handling a BL to a stub. In this
6135                      case, mode switching is performed by the stub.  */
6136                   if (sym_flags == STT_ARM_TFUNC && !stub_entry)
6137                     value |= (1 << 28);
6138                   else
6139                     {
6140                       value &= ~(bfd_vma)(1 << 28);
6141                       value |= (1 << 24);
6142                     }
6143                 }
6144             }
6145           }
6146           break;
6147
6148         case R_ARM_ABS32:
6149           value += addend;
6150           if (sym_flags == STT_ARM_TFUNC)
6151             value |= 1;
6152           break;
6153
6154         case R_ARM_ABS32_NOI:
6155           value += addend;
6156           break;
6157
6158         case R_ARM_REL32:
6159           value += addend;
6160           if (sym_flags == STT_ARM_TFUNC)
6161             value |= 1;
6162           value -= (input_section->output_section->vma
6163                     + input_section->output_offset + rel->r_offset);
6164           break;
6165
6166         case R_ARM_REL32_NOI:
6167           value += addend;
6168           value -= (input_section->output_section->vma
6169                     + input_section->output_offset + rel->r_offset);
6170           break;
6171
6172         case R_ARM_PREL31:
6173           value -= (input_section->output_section->vma
6174                     + input_section->output_offset + rel->r_offset);
6175           value += signed_addend;
6176           if (! h || h->root.type != bfd_link_hash_undefweak)
6177             {
6178               /* Check for overflow.  */
6179               if ((value ^ (value >> 1)) & (1 << 30))
6180                 return bfd_reloc_overflow;
6181             }
6182           value &= 0x7fffffff;
6183           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
6184           if (sym_flags == STT_ARM_TFUNC)
6185             value |= 1;
6186           break;
6187         }
6188
6189       bfd_put_32 (input_bfd, value, hit_data);
6190       return bfd_reloc_ok;
6191
6192     case R_ARM_ABS8:
6193       value += addend;
6194       if ((long) value > 0x7f || (long) value < -0x80)
6195         return bfd_reloc_overflow;
6196
6197       bfd_put_8 (input_bfd, value, hit_data);
6198       return bfd_reloc_ok;
6199
6200     case R_ARM_ABS16:
6201       value += addend;
6202
6203       if ((long) value > 0x7fff || (long) value < -0x8000)
6204         return bfd_reloc_overflow;
6205
6206       bfd_put_16 (input_bfd, value, hit_data);
6207       return bfd_reloc_ok;
6208
6209     case R_ARM_THM_ABS5:
6210       /* Support ldr and str instructions for the thumb.  */
6211       if (globals->use_rel)
6212         {
6213           /* Need to refetch addend.  */
6214           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
6215           /* ??? Need to determine shift amount from operand size.  */
6216           addend >>= howto->rightshift;
6217         }
6218       value += addend;
6219
6220       /* ??? Isn't value unsigned?  */
6221       if ((long) value > 0x1f || (long) value < -0x10)
6222         return bfd_reloc_overflow;
6223
6224       /* ??? Value needs to be properly shifted into place first.  */
6225       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
6226       bfd_put_16 (input_bfd, value, hit_data);
6227       return bfd_reloc_ok;
6228
6229     case R_ARM_THM_ALU_PREL_11_0:
6230       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
6231       {
6232         bfd_vma insn;
6233         bfd_signed_vma relocation;
6234
6235         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
6236              | bfd_get_16 (input_bfd, hit_data + 2);
6237
6238         if (globals->use_rel)
6239           {
6240             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
6241                           | ((insn & (1 << 26)) >> 15);
6242             if (insn & 0xf00000)
6243               signed_addend = -signed_addend;
6244           }
6245
6246         relocation = value + signed_addend;
6247         relocation -= (input_section->output_section->vma
6248                        + input_section->output_offset
6249                        + rel->r_offset);
6250
6251         value = abs (relocation);
6252
6253         if (value >= 0x1000)
6254           return bfd_reloc_overflow;
6255
6256         insn = (insn & 0xfb0f8f00) | (value & 0xff)
6257              | ((value & 0x700) << 4)
6258              | ((value & 0x800) << 15);
6259         if (relocation < 0)
6260           insn |= 0xa00000;
6261
6262         bfd_put_16 (input_bfd, insn >> 16, hit_data);
6263         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
6264
6265         return bfd_reloc_ok;
6266       }
6267
6268     case R_ARM_THM_PC12:
6269       /* Corresponds to: ldr.w reg, [pc, #offset].  */
6270       {
6271         bfd_vma insn;
6272         bfd_signed_vma relocation;
6273
6274         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
6275              | bfd_get_16 (input_bfd, hit_data + 2);
6276
6277         if (globals->use_rel)
6278           {
6279             signed_addend = insn & 0xfff;
6280             if (!(insn & (1 << 23)))
6281               signed_addend = -signed_addend;
6282           }
6283
6284         relocation = value + signed_addend;
6285         relocation -= (input_section->output_section->vma
6286                        + input_section->output_offset
6287                        + rel->r_offset);
6288
6289         value = abs (relocation);
6290
6291         if (value >= 0x1000)
6292           return bfd_reloc_overflow;
6293
6294         insn = (insn & 0xff7ff000) | value;
6295         if (relocation >= 0)
6296           insn |= (1 << 23);
6297
6298         bfd_put_16 (input_bfd, insn >> 16, hit_data);
6299         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
6300
6301         return bfd_reloc_ok;
6302       }
6303
6304     case R_ARM_THM_XPC22:
6305     case R_ARM_THM_CALL:
6306     case R_ARM_THM_JUMP24:
6307       /* Thumb BL (branch long instruction).  */
6308       {
6309         bfd_vma relocation;
6310         bfd_vma reloc_sign;
6311         bfd_boolean overflow = FALSE;
6312         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
6313         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
6314         bfd_signed_vma reloc_signed_max;
6315         bfd_signed_vma reloc_signed_min;
6316         bfd_vma check;
6317         bfd_signed_vma signed_check;
6318         int bitsize;
6319         int thumb2 = using_thumb2 (globals);
6320
6321         /* A branch to an undefined weak symbol is turned into a jump to
6322            the next instruction.  */
6323         if (h && h->root.type == bfd_link_hash_undefweak)
6324           {
6325             bfd_put_16 (input_bfd, 0xe000, hit_data);
6326             bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
6327             return bfd_reloc_ok;
6328           }
6329
6330         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
6331            with Thumb-1) involving the J1 and J2 bits.  */
6332         if (globals->use_rel)
6333           {
6334             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
6335             bfd_vma upper = upper_insn & 0x3ff;
6336             bfd_vma lower = lower_insn & 0x7ff;
6337             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
6338             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
6339             bfd_vma i1 = j1 ^ s ? 0 : 1;
6340             bfd_vma i2 = j2 ^ s ? 0 : 1;
6341
6342             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
6343             /* Sign extend.  */
6344             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
6345
6346             signed_addend = addend;
6347           }
6348
6349         if (r_type == R_ARM_THM_XPC22)
6350           {
6351             /* Check for Thumb to Thumb call.  */
6352             /* FIXME: Should we translate the instruction into a BL
6353                instruction instead ?  */
6354             if (sym_flags == STT_ARM_TFUNC)
6355               (*_bfd_error_handler)
6356                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
6357                  input_bfd,
6358                  h ? h->root.root.string : "(local)");
6359           }
6360         else
6361           {
6362             /* If it is not a call to Thumb, assume call to Arm.
6363                If it is a call relative to a section name, then it is not a
6364                function call at all, but rather a long jump.  Calls through
6365                the PLT do not require stubs.  */
6366             if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
6367                 && (h == NULL || splt == NULL
6368                     || h->plt.offset == (bfd_vma) -1))
6369               {
6370                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
6371                   {
6372                     /* Convert BL to BLX.  */
6373                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
6374                   }
6375                 else if (r_type != R_ARM_THM_CALL)
6376                   {
6377                     if (elf32_thumb_to_arm_stub
6378                         (info, sym_name, input_bfd, output_bfd, input_section,
6379                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
6380                          error_message))
6381                       return bfd_reloc_ok;
6382                     else
6383                       return bfd_reloc_dangerous;
6384                   }
6385               }
6386             else if (sym_flags == STT_ARM_TFUNC && globals->use_blx
6387                      && r_type == R_ARM_THM_CALL)
6388               {
6389                 /* Make sure this is a BL.  */
6390                 lower_insn |= 0x1800;
6391               }
6392           }
6393
6394         /* Handle calls via the PLT.  */
6395         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
6396           {
6397             value = (splt->output_section->vma
6398                      + splt->output_offset
6399                      + h->plt.offset);
6400             if (globals->use_blx && r_type == R_ARM_THM_CALL)
6401               {
6402                 /* If the Thumb BLX instruction is available, convert the
6403                    BL to a BLX instruction to call the ARM-mode PLT entry.  */
6404                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
6405               }
6406             else
6407               /* Target the Thumb stub before the ARM PLT entry.  */
6408               value -= PLT_THUMB_STUB_SIZE;
6409             *unresolved_reloc_p = FALSE;
6410           }
6411
6412         if (r_type == R_ARM_THM_CALL)
6413           {
6414             /* Check if a stub has to be inserted because the destination
6415                is too far.  */
6416             bfd_vma from;
6417             bfd_signed_vma branch_offset;
6418             struct elf32_arm_stub_hash_entry *stub_entry = NULL;
6419
6420             from = (input_section->output_section->vma
6421                     + input_section->output_offset
6422                     + rel->r_offset);
6423             branch_offset = (bfd_signed_vma)(value - from);
6424
6425             if ((!thumb2
6426                  && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
6427                      || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
6428                 ||
6429                 (thumb2
6430                  && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
6431                      || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
6432                 || ((sym_flags != STT_ARM_TFUNC) && !globals->use_blx))
6433               {
6434                 /* The target is out of reach or we are changing modes, so
6435                    redirect the branch to the local stub for this
6436                    function.  */
6437                 stub_entry = elf32_arm_get_stub_entry (input_section,
6438                                                        sym_sec, h,
6439                                                        rel, globals);
6440                 if (stub_entry != NULL)
6441                   value = (stub_entry->stub_offset
6442                            + stub_entry->stub_sec->output_offset
6443                            + stub_entry->stub_sec->output_section->vma);
6444
6445                 /* If this call becomes a call to Arm, force BLX.  */
6446                 if (globals->use_blx)
6447                   {
6448                     if ((stub_entry
6449                          && !arm_stub_is_thumb (stub_entry->stub_type))
6450                         || (sym_flags != STT_ARM_TFUNC))
6451                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
6452                   }
6453               }
6454           }
6455
6456         relocation = value + signed_addend;
6457
6458         relocation -= (input_section->output_section->vma
6459                        + input_section->output_offset
6460                        + rel->r_offset);
6461
6462         check = relocation >> howto->rightshift;
6463
6464         /* If this is a signed value, the rightshift just dropped
6465            leading 1 bits (assuming twos complement).  */
6466         if ((bfd_signed_vma) relocation >= 0)
6467           signed_check = check;
6468         else
6469           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
6470
6471         /* Calculate the permissable maximum and minimum values for
6472            this relocation according to whether we're relocating for
6473            Thumb-2 or not.  */
6474         bitsize = howto->bitsize;
6475         if (!thumb2)
6476           bitsize -= 2;
6477         reloc_signed_max = ((1 << (bitsize - 1)) - 1) >> howto->rightshift;
6478         reloc_signed_min = ~reloc_signed_max;
6479
6480         /* Assumes two's complement.  */
6481         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
6482           overflow = TRUE;
6483
6484         if ((lower_insn & 0x5000) == 0x4000)
6485           /* For a BLX instruction, make sure that the relocation is rounded up
6486              to a word boundary.  This follows the semantics of the instruction
6487              which specifies that bit 1 of the target address will come from bit
6488              1 of the base address.  */
6489           relocation = (relocation + 2) & ~ 3;
6490
6491         /* Put RELOCATION back into the insn.  Assumes two's complement.
6492            We use the Thumb-2 encoding, which is safe even if dealing with
6493            a Thumb-1 instruction by virtue of our overflow check above.  */
6494         reloc_sign = (signed_check < 0) ? 1 : 0;
6495         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
6496                      | ((relocation >> 12) & 0x3ff)
6497                      | (reloc_sign << 10);
6498         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
6499                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
6500                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
6501                      | ((relocation >> 1) & 0x7ff);
6502
6503         /* Put the relocated value back in the object file:  */
6504         bfd_put_16 (input_bfd, upper_insn, hit_data);
6505         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
6506
6507         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
6508       }
6509       break;
6510
6511     case R_ARM_THM_JUMP19:
6512       /* Thumb32 conditional branch instruction.  */
6513       {
6514         bfd_vma relocation;
6515         bfd_boolean overflow = FALSE;
6516         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
6517         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
6518         bfd_signed_vma reloc_signed_max = 0xffffe;
6519         bfd_signed_vma reloc_signed_min = -0x100000;
6520         bfd_signed_vma signed_check;
6521
6522         /* Need to refetch the addend, reconstruct the top three bits,
6523            and squish the two 11 bit pieces together.  */
6524         if (globals->use_rel)
6525           {
6526             bfd_vma S     = (upper_insn & 0x0400) >> 10;
6527             bfd_vma upper = (upper_insn & 0x003f);
6528             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
6529             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
6530             bfd_vma lower = (lower_insn & 0x07ff);
6531
6532             upper |= J1 << 6;
6533             upper |= J2 << 7;
6534             upper |= (!S) << 8;
6535             upper -= 0x0100; /* Sign extend.  */
6536
6537             addend = (upper << 12) | (lower << 1);
6538             signed_addend = addend;
6539           }
6540
6541         /* Handle calls via the PLT.  */
6542         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
6543           {
6544             value = (splt->output_section->vma
6545                      + splt->output_offset
6546                      + h->plt.offset);
6547             /* Target the Thumb stub before the ARM PLT entry.  */
6548             value -= PLT_THUMB_STUB_SIZE;
6549             *unresolved_reloc_p = FALSE;
6550           }
6551
6552         /* ??? Should handle interworking?  GCC might someday try to
6553            use this for tail calls.  */
6554
6555         relocation = value + signed_addend;
6556         relocation -= (input_section->output_section->vma
6557                        + input_section->output_offset
6558                        + rel->r_offset);
6559         signed_check = (bfd_signed_vma) relocation;
6560
6561         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
6562           overflow = TRUE;
6563
6564         /* Put RELOCATION back into the insn.  */
6565         {
6566           bfd_vma S  = (relocation & 0x00100000) >> 20;
6567           bfd_vma J2 = (relocation & 0x00080000) >> 19;
6568           bfd_vma J1 = (relocation & 0x00040000) >> 18;
6569           bfd_vma hi = (relocation & 0x0003f000) >> 12;
6570           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
6571
6572           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
6573           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
6574         }
6575
6576         /* Put the relocated value back in the object file:  */
6577         bfd_put_16 (input_bfd, upper_insn, hit_data);
6578         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
6579
6580         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
6581       }
6582
6583     case R_ARM_THM_JUMP11:
6584     case R_ARM_THM_JUMP8:
6585     case R_ARM_THM_JUMP6:
6586       /* Thumb B (branch) instruction).  */
6587       {
6588         bfd_signed_vma relocation;
6589         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
6590         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
6591         bfd_signed_vma signed_check;
6592
6593         /* CZB cannot jump backward.  */
6594         if (r_type == R_ARM_THM_JUMP6)
6595           reloc_signed_min = 0;
6596
6597         if (globals->use_rel)
6598           {
6599             /* Need to refetch addend.  */
6600             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
6601             if (addend & ((howto->src_mask + 1) >> 1))
6602               {
6603                 signed_addend = -1;
6604                 signed_addend &= ~ howto->src_mask;
6605                 signed_addend |= addend;
6606               }
6607             else
6608               signed_addend = addend;
6609             /* The value in the insn has been right shifted.  We need to
6610                undo this, so that we can perform the address calculation
6611                in terms of bytes.  */
6612             signed_addend <<= howto->rightshift;
6613           }
6614         relocation = value + signed_addend;
6615
6616         relocation -= (input_section->output_section->vma
6617                        + input_section->output_offset
6618                        + rel->r_offset);
6619
6620         relocation >>= howto->rightshift;
6621         signed_check = relocation;
6622
6623         if (r_type == R_ARM_THM_JUMP6)
6624           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
6625         else
6626           relocation &= howto->dst_mask;
6627         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
6628
6629         bfd_put_16 (input_bfd, relocation, hit_data);
6630
6631         /* Assumes two's complement.  */
6632         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
6633           return bfd_reloc_overflow;
6634
6635         return bfd_reloc_ok;
6636       }
6637
6638     case R_ARM_ALU_PCREL7_0:
6639     case R_ARM_ALU_PCREL15_8:
6640     case R_ARM_ALU_PCREL23_15:
6641       {
6642         bfd_vma insn;
6643         bfd_vma relocation;
6644
6645         insn = bfd_get_32 (input_bfd, hit_data);
6646         if (globals->use_rel)
6647           {
6648             /* Extract the addend.  */
6649             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
6650             signed_addend = addend;
6651           }
6652         relocation = value + signed_addend;
6653
6654         relocation -= (input_section->output_section->vma
6655                        + input_section->output_offset
6656                        + rel->r_offset);
6657         insn = (insn & ~0xfff)
6658                | ((howto->bitpos << 7) & 0xf00)
6659                | ((relocation >> howto->bitpos) & 0xff);
6660         bfd_put_32 (input_bfd, value, hit_data);
6661       }
6662       return bfd_reloc_ok;
6663
6664     case R_ARM_GNU_VTINHERIT:
6665     case R_ARM_GNU_VTENTRY:
6666       return bfd_reloc_ok;
6667
6668     case R_ARM_GOTOFF32:
6669       /* Relocation is relative to the start of the
6670          global offset table.  */
6671
6672       BFD_ASSERT (sgot != NULL);
6673       if (sgot == NULL)
6674         return bfd_reloc_notsupported;
6675
6676       /* If we are addressing a Thumb function, we need to adjust the
6677          address by one, so that attempts to call the function pointer will
6678          correctly interpret it as Thumb code.  */
6679       if (sym_flags == STT_ARM_TFUNC)
6680         value += 1;
6681
6682       /* Note that sgot->output_offset is not involved in this
6683          calculation.  We always want the start of .got.  If we
6684          define _GLOBAL_OFFSET_TABLE in a different way, as is
6685          permitted by the ABI, we might have to change this
6686          calculation.  */
6687       value -= sgot->output_section->vma;
6688       return _bfd_final_link_relocate (howto, input_bfd, input_section,
6689                                        contents, rel->r_offset, value,
6690                                        rel->r_addend);
6691
6692     case R_ARM_GOTPC:
6693       /* Use global offset table as symbol value.  */
6694       BFD_ASSERT (sgot != NULL);
6695
6696       if (sgot == NULL)
6697         return bfd_reloc_notsupported;
6698
6699       *unresolved_reloc_p = FALSE;
6700       value = sgot->output_section->vma;
6701       return _bfd_final_link_relocate (howto, input_bfd, input_section,
6702                                        contents, rel->r_offset, value,
6703                                        rel->r_addend);
6704
6705     case R_ARM_GOT32:
6706     case R_ARM_GOT_PREL:
6707       /* Relocation is to the entry for this symbol in the
6708          global offset table.  */
6709       if (sgot == NULL)
6710         return bfd_reloc_notsupported;
6711
6712       if (h != NULL)
6713         {
6714           bfd_vma off;
6715           bfd_boolean dyn;
6716
6717           off = h->got.offset;
6718           BFD_ASSERT (off != (bfd_vma) -1);
6719           dyn = globals->root.dynamic_sections_created;
6720
6721           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6722               || (info->shared
6723                   && SYMBOL_REFERENCES_LOCAL (info, h))
6724               || (ELF_ST_VISIBILITY (h->other)
6725                   && h->root.type == bfd_link_hash_undefweak))
6726             {
6727               /* This is actually a static link, or it is a -Bsymbolic link
6728                  and the symbol is defined locally.  We must initialize this
6729                  entry in the global offset table.  Since the offset must
6730                  always be a multiple of 4, we use the least significant bit
6731                  to record whether we have initialized it already.
6732
6733                  When doing a dynamic link, we create a .rel(a).got relocation
6734                  entry to initialize the value.  This is done in the
6735                  finish_dynamic_symbol routine.  */
6736               if ((off & 1) != 0)
6737                 off &= ~1;
6738               else
6739                 {
6740                   /* If we are addressing a Thumb function, we need to
6741                      adjust the address by one, so that attempts to
6742                      call the function pointer will correctly
6743                      interpret it as Thumb code.  */
6744                   if (sym_flags == STT_ARM_TFUNC)
6745                     value |= 1;
6746
6747                   bfd_put_32 (output_bfd, value, sgot->contents + off);
6748                   h->got.offset |= 1;
6749                 }
6750             }
6751           else
6752             *unresolved_reloc_p = FALSE;
6753
6754           value = sgot->output_offset + off;
6755         }
6756       else
6757         {
6758           bfd_vma off;
6759
6760           BFD_ASSERT (local_got_offsets != NULL &&
6761                       local_got_offsets[r_symndx] != (bfd_vma) -1);
6762
6763           off = local_got_offsets[r_symndx];
6764
6765           /* The offset must always be a multiple of 4.  We use the
6766              least significant bit to record whether we have already
6767              generated the necessary reloc.  */
6768           if ((off & 1) != 0)
6769             off &= ~1;
6770           else
6771             {
6772               /* If we are addressing a Thumb function, we need to
6773                  adjust the address by one, so that attempts to
6774                  call the function pointer will correctly
6775                  interpret it as Thumb code.  */
6776               if (sym_flags == STT_ARM_TFUNC)
6777                 value |= 1;
6778
6779               if (globals->use_rel)
6780                 bfd_put_32 (output_bfd, value, sgot->contents + off);
6781
6782               if (info->shared)
6783                 {
6784                   asection * srelgot;
6785                   Elf_Internal_Rela outrel;
6786                   bfd_byte *loc;
6787
6788                   srelgot = (bfd_get_section_by_name
6789                              (dynobj, RELOC_SECTION (globals, ".got")));
6790                   BFD_ASSERT (srelgot != NULL);
6791
6792                   outrel.r_addend = addend + value;
6793                   outrel.r_offset = (sgot->output_section->vma
6794                                      + sgot->output_offset
6795                                      + off);
6796                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
6797                   loc = srelgot->contents;
6798                   loc += srelgot->reloc_count++ * RELOC_SIZE (globals);
6799                   SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
6800                 }
6801
6802               local_got_offsets[r_symndx] |= 1;
6803             }
6804
6805           value = sgot->output_offset + off;
6806         }
6807       if (r_type != R_ARM_GOT32)
6808         value += sgot->output_section->vma;
6809
6810       return _bfd_final_link_relocate (howto, input_bfd, input_section,
6811                                        contents, rel->r_offset, value,
6812                                        rel->r_addend);
6813
6814     case R_ARM_TLS_LDO32:
6815       value = value - dtpoff_base (info);
6816
6817       return _bfd_final_link_relocate (howto, input_bfd, input_section,
6818                                        contents, rel->r_offset, value,
6819                                        rel->r_addend);
6820
6821     case R_ARM_TLS_LDM32:
6822       {
6823         bfd_vma off;
6824
6825         if (globals->sgot == NULL)
6826           abort ();
6827
6828         off = globals->tls_ldm_got.offset;
6829
6830         if ((off & 1) != 0)
6831           off &= ~1;
6832         else
6833           {
6834             /* If we don't know the module number, create a relocation
6835                for it.  */
6836             if (info->shared)
6837               {
6838                 Elf_Internal_Rela outrel;
6839                 bfd_byte *loc;
6840
6841                 if (globals->srelgot == NULL)
6842                   abort ();
6843
6844                 outrel.r_addend = 0;
6845                 outrel.r_offset = (globals->sgot->output_section->vma
6846                                    + globals->sgot->output_offset + off);
6847                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
6848
6849                 if (globals->use_rel)
6850                   bfd_put_32 (output_bfd, outrel.r_addend,
6851                               globals->sgot->contents + off);
6852
6853                 loc = globals->srelgot->contents;
6854                 loc += globals->srelgot->reloc_count++ * RELOC_SIZE (globals);
6855                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
6856               }
6857             else
6858               bfd_put_32 (output_bfd, 1, globals->sgot->contents + off);
6859
6860             globals->tls_ldm_got.offset |= 1;
6861           }
6862
6863         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
6864           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
6865
6866         return _bfd_final_link_relocate (howto, input_bfd, input_section,
6867                                          contents, rel->r_offset, value,
6868                                          rel->r_addend);
6869       }
6870
6871     case R_ARM_TLS_GD32:
6872     case R_ARM_TLS_IE32:
6873       {
6874         bfd_vma off;
6875         int indx;
6876         char tls_type;
6877
6878         if (globals->sgot == NULL)
6879           abort ();
6880
6881         indx = 0;
6882         if (h != NULL)
6883           {
6884             bfd_boolean dyn;
6885             dyn = globals->root.dynamic_sections_created;
6886             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6887                 && (!info->shared
6888                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
6889               {
6890                 *unresolved_reloc_p = FALSE;
6891                 indx = h->dynindx;
6892               }
6893             off = h->got.offset;
6894             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
6895           }
6896         else
6897           {
6898             if (local_got_offsets == NULL)
6899               abort ();
6900             off = local_got_offsets[r_symndx];
6901             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
6902           }
6903
6904         if (tls_type == GOT_UNKNOWN)
6905           abort ();
6906
6907         if ((off & 1) != 0)
6908           off &= ~1;
6909         else
6910           {
6911             bfd_boolean need_relocs = FALSE;
6912             Elf_Internal_Rela outrel;
6913             bfd_byte *loc = NULL;
6914             int cur_off = off;
6915
6916             /* The GOT entries have not been initialized yet.  Do it
6917                now, and emit any relocations.  If both an IE GOT and a
6918                GD GOT are necessary, we emit the GD first.  */
6919
6920             if ((info->shared || indx != 0)
6921                 && (h == NULL
6922                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6923                     || h->root.type != bfd_link_hash_undefweak))
6924               {
6925                 need_relocs = TRUE;
6926                 if (globals->srelgot == NULL)
6927                   abort ();
6928                 loc = globals->srelgot->contents;
6929                 loc += globals->srelgot->reloc_count * RELOC_SIZE (globals);
6930               }
6931
6932             if (tls_type & GOT_TLS_GD)
6933               {
6934                 if (need_relocs)
6935                   {
6936                     outrel.r_addend = 0;
6937                     outrel.r_offset = (globals->sgot->output_section->vma
6938                                        + globals->sgot->output_offset
6939                                        + cur_off);
6940                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
6941
6942                     if (globals->use_rel)
6943                       bfd_put_32 (output_bfd, outrel.r_addend,
6944                                   globals->sgot->contents + cur_off);
6945
6946                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
6947                     globals->srelgot->reloc_count++;
6948                     loc += RELOC_SIZE (globals);
6949
6950                     if (indx == 0)
6951                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
6952                                   globals->sgot->contents + cur_off + 4);
6953                     else
6954                       {
6955                         outrel.r_addend = 0;
6956                         outrel.r_info = ELF32_R_INFO (indx,
6957                                                       R_ARM_TLS_DTPOFF32);
6958                         outrel.r_offset += 4;
6959
6960                         if (globals->use_rel)
6961                           bfd_put_32 (output_bfd, outrel.r_addend,
6962                                       globals->sgot->contents + cur_off + 4);
6963
6964
6965                         SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
6966                         globals->srelgot->reloc_count++;
6967                         loc += RELOC_SIZE (globals);
6968                       }
6969                   }
6970                 else
6971                   {
6972                     /* If we are not emitting relocations for a
6973                        general dynamic reference, then we must be in a
6974                        static link or an executable link with the
6975                        symbol binding locally.  Mark it as belonging
6976                        to module 1, the executable.  */
6977                     bfd_put_32 (output_bfd, 1,
6978                                 globals->sgot->contents + cur_off);
6979                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
6980                                 globals->sgot->contents + cur_off + 4);
6981                   }
6982
6983                 cur_off += 8;
6984               }
6985
6986             if (tls_type & GOT_TLS_IE)
6987               {
6988                 if (need_relocs)
6989                   {
6990                     if (indx == 0)
6991                       outrel.r_addend = value - dtpoff_base (info);
6992                     else
6993                       outrel.r_addend = 0;
6994                     outrel.r_offset = (globals->sgot->output_section->vma
6995                                        + globals->sgot->output_offset
6996                                        + cur_off);
6997                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
6998
6999                     if (globals->use_rel)
7000                       bfd_put_32 (output_bfd, outrel.r_addend,
7001                                   globals->sgot->contents + cur_off);
7002
7003                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
7004                     globals->srelgot->reloc_count++;
7005                     loc += RELOC_SIZE (globals);
7006                   }
7007                 else
7008                   bfd_put_32 (output_bfd, tpoff (info, value),
7009                               globals->sgot->contents + cur_off);
7010                 cur_off += 4;
7011               }
7012
7013             if (h != NULL)
7014               h->got.offset |= 1;
7015             else
7016               local_got_offsets[r_symndx] |= 1;
7017           }
7018
7019         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
7020           off += 8;
7021         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
7022           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
7023
7024         return _bfd_final_link_relocate (howto, input_bfd, input_section,
7025                                          contents, rel->r_offset, value,
7026                                          rel->r_addend);
7027       }
7028
7029     case R_ARM_TLS_LE32:
7030       if (info->shared)
7031         {
7032           (*_bfd_error_handler)
7033             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
7034              input_bfd, input_section,
7035              (long) rel->r_offset, howto->name);
7036           return FALSE;
7037         }
7038       else
7039         value = tpoff (info, value);
7040
7041       return _bfd_final_link_relocate (howto, input_bfd, input_section,
7042                                        contents, rel->r_offset, value,
7043                                        rel->r_addend);
7044
7045     case R_ARM_V4BX:
7046       if (globals->fix_v4bx)
7047         {
7048           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7049
7050           /* Ensure that we have a BX instruction.  */
7051           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
7052
7053           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
7054             {
7055               /* Branch to veneer.  */
7056               bfd_vma glue_addr;
7057               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
7058               glue_addr -= input_section->output_section->vma
7059                            + input_section->output_offset
7060                            + rel->r_offset + 8;
7061               insn = (insn & 0xf0000000) | 0x0a000000
7062                      | ((glue_addr >> 2) & 0x00ffffff);
7063             }
7064           else
7065             {
7066               /* Preserve Rm (lowest four bits) and the condition code
7067                  (highest four bits). Other bits encode MOV PC,Rm.  */
7068               insn = (insn & 0xf000000f) | 0x01a0f000;
7069             }
7070
7071           bfd_put_32 (input_bfd, insn, hit_data);
7072         }
7073       return bfd_reloc_ok;
7074
7075     case R_ARM_MOVW_ABS_NC:
7076     case R_ARM_MOVT_ABS:
7077     case R_ARM_MOVW_PREL_NC:
7078     case R_ARM_MOVT_PREL:
7079     /* Until we properly support segment-base-relative addressing then
7080        we assume the segment base to be zero, as for the group relocations.
7081        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
7082        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
7083     case R_ARM_MOVW_BREL_NC:
7084     case R_ARM_MOVW_BREL:
7085     case R_ARM_MOVT_BREL:
7086       {
7087         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7088
7089         if (globals->use_rel)
7090           {
7091             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
7092             signed_addend = (addend ^ 0x8000) - 0x8000;
7093           }
7094
7095         value += signed_addend;
7096
7097         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
7098           value -= (input_section->output_section->vma
7099                     + input_section->output_offset + rel->r_offset);
7100
7101         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
7102           return bfd_reloc_overflow;
7103
7104         if (sym_flags == STT_ARM_TFUNC)
7105           value |= 1;
7106
7107         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
7108             || r_type == R_ARM_MOVT_BREL)
7109           value >>= 16;
7110
7111         insn &= 0xfff0f000;
7112         insn |= value & 0xfff;
7113         insn |= (value & 0xf000) << 4;
7114         bfd_put_32 (input_bfd, insn, hit_data);
7115       }
7116       return bfd_reloc_ok;
7117
7118     case R_ARM_THM_MOVW_ABS_NC:
7119     case R_ARM_THM_MOVT_ABS:
7120     case R_ARM_THM_MOVW_PREL_NC:
7121     case R_ARM_THM_MOVT_PREL:
7122     /* Until we properly support segment-base-relative addressing then
7123        we assume the segment base to be zero, as for the above relocations.
7124        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
7125        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
7126        as R_ARM_THM_MOVT_ABS.  */
7127     case R_ARM_THM_MOVW_BREL_NC:
7128     case R_ARM_THM_MOVW_BREL:
7129     case R_ARM_THM_MOVT_BREL:
7130       {
7131         bfd_vma insn;
7132
7133         insn = bfd_get_16 (input_bfd, hit_data) << 16;
7134         insn |= bfd_get_16 (input_bfd, hit_data + 2);
7135
7136         if (globals->use_rel)
7137           {
7138             addend = ((insn >> 4)  & 0xf000)
7139                    | ((insn >> 15) & 0x0800)
7140                    | ((insn >> 4)  & 0x0700)
7141                    | (insn         & 0x00ff);
7142             signed_addend = (addend ^ 0x8000) - 0x8000;
7143           }
7144
7145         value += signed_addend;
7146
7147         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
7148           value -= (input_section->output_section->vma
7149                     + input_section->output_offset + rel->r_offset);
7150
7151         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
7152           return bfd_reloc_overflow;
7153
7154         if (sym_flags == STT_ARM_TFUNC)
7155           value |= 1;
7156
7157         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
7158             || r_type == R_ARM_THM_MOVT_BREL)
7159           value >>= 16;
7160
7161         insn &= 0xfbf08f00;
7162         insn |= (value & 0xf000) << 4;
7163         insn |= (value & 0x0800) << 15;
7164         insn |= (value & 0x0700) << 4;
7165         insn |= (value & 0x00ff);
7166
7167         bfd_put_16 (input_bfd, insn >> 16, hit_data);
7168         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
7169       }
7170       return bfd_reloc_ok;
7171
7172     case R_ARM_ALU_PC_G0_NC:
7173     case R_ARM_ALU_PC_G1_NC:
7174     case R_ARM_ALU_PC_G0:
7175     case R_ARM_ALU_PC_G1:
7176     case R_ARM_ALU_PC_G2:
7177     case R_ARM_ALU_SB_G0_NC:
7178     case R_ARM_ALU_SB_G1_NC:
7179     case R_ARM_ALU_SB_G0:
7180     case R_ARM_ALU_SB_G1:
7181     case R_ARM_ALU_SB_G2:
7182       {
7183         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7184         bfd_vma pc = input_section->output_section->vma
7185                      + input_section->output_offset + rel->r_offset;
7186         /* sb should be the origin of the *segment* containing the symbol.
7187            It is not clear how to obtain this OS-dependent value, so we
7188            make an arbitrary choice of zero.  */
7189         bfd_vma sb = 0;
7190         bfd_vma residual;
7191         bfd_vma g_n;
7192         bfd_signed_vma signed_value;
7193         int group = 0;
7194
7195         /* Determine which group of bits to select.  */
7196         switch (r_type)
7197           {
7198           case R_ARM_ALU_PC_G0_NC:
7199           case R_ARM_ALU_PC_G0:
7200           case R_ARM_ALU_SB_G0_NC:
7201           case R_ARM_ALU_SB_G0:
7202             group = 0;
7203             break;
7204
7205           case R_ARM_ALU_PC_G1_NC:
7206           case R_ARM_ALU_PC_G1:
7207           case R_ARM_ALU_SB_G1_NC:
7208           case R_ARM_ALU_SB_G1:
7209             group = 1;
7210             break;
7211
7212           case R_ARM_ALU_PC_G2:
7213           case R_ARM_ALU_SB_G2:
7214             group = 2;
7215             break;
7216
7217           default:
7218             abort ();
7219           }
7220
7221         /* If REL, extract the addend from the insn.  If RELA, it will
7222            have already been fetched for us.  */
7223         if (globals->use_rel)
7224           {
7225             int negative;
7226             bfd_vma constant = insn & 0xff;
7227             bfd_vma rotation = (insn & 0xf00) >> 8;
7228
7229             if (rotation == 0)
7230               signed_addend = constant;
7231             else
7232               {
7233                 /* Compensate for the fact that in the instruction, the
7234                    rotation is stored in multiples of 2 bits.  */
7235                 rotation *= 2;
7236
7237                 /* Rotate "constant" right by "rotation" bits.  */
7238                 signed_addend = (constant >> rotation) |
7239                                 (constant << (8 * sizeof (bfd_vma) - rotation));
7240               }
7241
7242             /* Determine if the instruction is an ADD or a SUB.
7243                (For REL, this determines the sign of the addend.)  */
7244             negative = identify_add_or_sub (insn);
7245             if (negative == 0)
7246               {
7247                 (*_bfd_error_handler)
7248                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
7249                   input_bfd, input_section,
7250                   (long) rel->r_offset, howto->name);
7251                 return bfd_reloc_overflow;
7252               }
7253
7254             signed_addend *= negative;
7255           }
7256
7257         /* Compute the value (X) to go in the place.  */
7258         if (r_type == R_ARM_ALU_PC_G0_NC
7259             || r_type == R_ARM_ALU_PC_G1_NC
7260             || r_type == R_ARM_ALU_PC_G0
7261             || r_type == R_ARM_ALU_PC_G1
7262             || r_type == R_ARM_ALU_PC_G2)
7263           /* PC relative.  */
7264           signed_value = value - pc + signed_addend;
7265         else
7266           /* Section base relative.  */
7267           signed_value = value - sb + signed_addend;
7268
7269         /* If the target symbol is a Thumb function, then set the
7270            Thumb bit in the address.  */
7271         if (sym_flags == STT_ARM_TFUNC)
7272           signed_value |= 1;
7273
7274         /* Calculate the value of the relevant G_n, in encoded
7275            constant-with-rotation format.  */
7276         g_n = calculate_group_reloc_mask (abs (signed_value), group,
7277                                           &residual);
7278
7279         /* Check for overflow if required.  */
7280         if ((r_type == R_ARM_ALU_PC_G0
7281              || r_type == R_ARM_ALU_PC_G1
7282              || r_type == R_ARM_ALU_PC_G2
7283              || r_type == R_ARM_ALU_SB_G0
7284              || r_type == R_ARM_ALU_SB_G1
7285              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
7286           {
7287             (*_bfd_error_handler)
7288               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
7289               input_bfd, input_section,
7290               (long) rel->r_offset, abs (signed_value), howto->name);
7291             return bfd_reloc_overflow;
7292           }
7293
7294         /* Mask out the value and the ADD/SUB part of the opcode; take care
7295            not to destroy the S bit.  */
7296         insn &= 0xff1ff000;
7297
7298         /* Set the opcode according to whether the value to go in the
7299            place is negative.  */
7300         if (signed_value < 0)
7301           insn |= 1 << 22;
7302         else
7303           insn |= 1 << 23;
7304
7305         /* Encode the offset.  */
7306         insn |= g_n;
7307
7308         bfd_put_32 (input_bfd, insn, hit_data);
7309       }
7310       return bfd_reloc_ok;
7311
7312     case R_ARM_LDR_PC_G0:
7313     case R_ARM_LDR_PC_G1:
7314     case R_ARM_LDR_PC_G2:
7315     case R_ARM_LDR_SB_G0:
7316     case R_ARM_LDR_SB_G1:
7317     case R_ARM_LDR_SB_G2:
7318       {
7319         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7320         bfd_vma pc = input_section->output_section->vma
7321                      + input_section->output_offset + rel->r_offset;
7322         bfd_vma sb = 0; /* See note above.  */
7323         bfd_vma residual;
7324         bfd_signed_vma signed_value;
7325         int group = 0;
7326
7327         /* Determine which groups of bits to calculate.  */
7328         switch (r_type)
7329           {
7330           case R_ARM_LDR_PC_G0:
7331           case R_ARM_LDR_SB_G0:
7332             group = 0;
7333             break;
7334
7335           case R_ARM_LDR_PC_G1:
7336           case R_ARM_LDR_SB_G1:
7337             group = 1;
7338             break;
7339
7340           case R_ARM_LDR_PC_G2:
7341           case R_ARM_LDR_SB_G2:
7342             group = 2;
7343             break;
7344
7345           default:
7346             abort ();
7347           }
7348
7349         /* If REL, extract the addend from the insn.  If RELA, it will
7350            have already been fetched for us.  */
7351         if (globals->use_rel)
7352           {
7353             int negative = (insn & (1 << 23)) ? 1 : -1;
7354             signed_addend = negative * (insn & 0xfff);
7355           }
7356
7357         /* Compute the value (X) to go in the place.  */
7358         if (r_type == R_ARM_LDR_PC_G0
7359             || r_type == R_ARM_LDR_PC_G1
7360             || r_type == R_ARM_LDR_PC_G2)
7361           /* PC relative.  */
7362           signed_value = value - pc + signed_addend;
7363         else
7364           /* Section base relative.  */
7365           signed_value = value - sb + signed_addend;
7366
7367         /* Calculate the value of the relevant G_{n-1} to obtain
7368            the residual at that stage.  */
7369         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
7370
7371         /* Check for overflow.  */
7372         if (residual >= 0x1000)
7373           {
7374             (*_bfd_error_handler)
7375               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
7376               input_bfd, input_section,
7377               (long) rel->r_offset, abs (signed_value), howto->name);
7378             return bfd_reloc_overflow;
7379           }
7380
7381         /* Mask out the value and U bit.  */
7382         insn &= 0xff7ff000;
7383
7384         /* Set the U bit if the value to go in the place is non-negative.  */
7385         if (signed_value >= 0)
7386           insn |= 1 << 23;
7387
7388         /* Encode the offset.  */
7389         insn |= residual;
7390
7391         bfd_put_32 (input_bfd, insn, hit_data);
7392       }
7393       return bfd_reloc_ok;
7394
7395     case R_ARM_LDRS_PC_G0:
7396     case R_ARM_LDRS_PC_G1:
7397     case R_ARM_LDRS_PC_G2:
7398     case R_ARM_LDRS_SB_G0:
7399     case R_ARM_LDRS_SB_G1:
7400     case R_ARM_LDRS_SB_G2:
7401       {
7402         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7403         bfd_vma pc = input_section->output_section->vma
7404                      + input_section->output_offset + rel->r_offset;
7405         bfd_vma sb = 0; /* See note above.  */
7406         bfd_vma residual;
7407         bfd_signed_vma signed_value;
7408         int group = 0;
7409
7410         /* Determine which groups of bits to calculate.  */
7411         switch (r_type)
7412           {
7413           case R_ARM_LDRS_PC_G0:
7414           case R_ARM_LDRS_SB_G0:
7415             group = 0;
7416             break;
7417
7418           case R_ARM_LDRS_PC_G1:
7419           case R_ARM_LDRS_SB_G1:
7420             group = 1;
7421             break;
7422
7423           case R_ARM_LDRS_PC_G2:
7424           case R_ARM_LDRS_SB_G2:
7425             group = 2;
7426             break;
7427
7428           default:
7429             abort ();
7430           }
7431
7432         /* If REL, extract the addend from the insn.  If RELA, it will
7433            have already been fetched for us.  */
7434         if (globals->use_rel)
7435           {
7436             int negative = (insn & (1 << 23)) ? 1 : -1;
7437             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
7438           }
7439
7440         /* Compute the value (X) to go in the place.  */
7441         if (r_type == R_ARM_LDRS_PC_G0
7442             || r_type == R_ARM_LDRS_PC_G1
7443             || r_type == R_ARM_LDRS_PC_G2)
7444           /* PC relative.  */
7445           signed_value = value - pc + signed_addend;
7446         else
7447           /* Section base relative.  */
7448           signed_value = value - sb + signed_addend;
7449
7450         /* Calculate the value of the relevant G_{n-1} to obtain
7451            the residual at that stage.  */
7452         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
7453
7454         /* Check for overflow.  */
7455         if (residual >= 0x100)
7456           {
7457             (*_bfd_error_handler)
7458               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
7459               input_bfd, input_section,
7460               (long) rel->r_offset, abs (signed_value), howto->name);
7461             return bfd_reloc_overflow;
7462           }
7463
7464         /* Mask out the value and U bit.  */
7465         insn &= 0xff7ff0f0;
7466
7467         /* Set the U bit if the value to go in the place is non-negative.  */
7468         if (signed_value >= 0)
7469           insn |= 1 << 23;
7470
7471         /* Encode the offset.  */
7472         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
7473
7474         bfd_put_32 (input_bfd, insn, hit_data);
7475       }
7476       return bfd_reloc_ok;
7477
7478     case R_ARM_LDC_PC_G0:
7479     case R_ARM_LDC_PC_G1:
7480     case R_ARM_LDC_PC_G2:
7481     case R_ARM_LDC_SB_G0:
7482     case R_ARM_LDC_SB_G1:
7483     case R_ARM_LDC_SB_G2:
7484       {
7485         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
7486         bfd_vma pc = input_section->output_section->vma
7487                      + input_section->output_offset + rel->r_offset;
7488         bfd_vma sb = 0; /* See note above.  */
7489         bfd_vma residual;
7490         bfd_signed_vma signed_value;
7491         int group = 0;
7492
7493         /* Determine which groups of bits to calculate.  */
7494         switch (r_type)
7495           {
7496           case R_ARM_LDC_PC_G0:
7497           case R_ARM_LDC_SB_G0:
7498             group = 0;
7499             break;
7500
7501           case R_ARM_LDC_PC_G1:
7502           case R_ARM_LDC_SB_G1:
7503             group = 1;
7504             break;
7505
7506           case R_ARM_LDC_PC_G2:
7507           case R_ARM_LDC_SB_G2:
7508             group = 2;
7509             break;
7510
7511           default:
7512             abort ();
7513           }
7514
7515         /* If REL, extract the addend from the insn.  If RELA, it will
7516            have already been fetched for us.  */
7517         if (globals->use_rel)
7518           {
7519             int negative = (insn & (1 << 23)) ? 1 : -1;
7520             signed_addend = negative * ((insn & 0xff) << 2);
7521           }
7522
7523         /* Compute the value (X) to go in the place.  */
7524         if (r_type == R_ARM_LDC_PC_G0
7525             || r_type == R_ARM_LDC_PC_G1
7526             || r_type == R_ARM_LDC_PC_G2)
7527           /* PC relative.  */
7528           signed_value = value - pc + signed_addend;
7529         else
7530           /* Section base relative.  */
7531           signed_value = value - sb + signed_addend;
7532
7533         /* Calculate the value of the relevant G_{n-1} to obtain
7534            the residual at that stage.  */
7535         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
7536
7537         /* Check for overflow.  (The absolute value to go in the place must be
7538            divisible by four and, after having been divided by four, must
7539            fit in eight bits.)  */
7540         if ((residual & 0x3) != 0 || residual >= 0x400)
7541           {
7542             (*_bfd_error_handler)
7543               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
7544               input_bfd, input_section,
7545               (long) rel->r_offset, abs (signed_value), howto->name);
7546             return bfd_reloc_overflow;
7547           }
7548
7549         /* Mask out the value and U bit.  */
7550         insn &= 0xff7fff00;
7551
7552         /* Set the U bit if the value to go in the place is non-negative.  */
7553         if (signed_value >= 0)
7554           insn |= 1 << 23;
7555
7556         /* Encode the offset.  */
7557         insn |= residual >> 2;
7558
7559         bfd_put_32 (input_bfd, insn, hit_data);
7560       }
7561       return bfd_reloc_ok;
7562
7563     default:
7564       return bfd_reloc_notsupported;
7565     }
7566 }
7567
7568 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
7569 static void
7570 arm_add_to_rel (bfd *              abfd,
7571                 bfd_byte *         address,
7572                 reloc_howto_type * howto,
7573                 bfd_signed_vma     increment)
7574 {
7575   bfd_signed_vma addend;
7576
7577   if (howto->type == R_ARM_THM_CALL
7578       || howto->type == R_ARM_THM_JUMP24)
7579     {
7580       int upper_insn, lower_insn;
7581       int upper, lower;
7582
7583       upper_insn = bfd_get_16 (abfd, address);
7584       lower_insn = bfd_get_16 (abfd, address + 2);
7585       upper = upper_insn & 0x7ff;
7586       lower = lower_insn & 0x7ff;
7587
7588       addend = (upper << 12) | (lower << 1);
7589       addend += increment;
7590       addend >>= 1;
7591
7592       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
7593       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
7594
7595       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
7596       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
7597     }
7598   else
7599     {
7600       bfd_vma        contents;
7601
7602       contents = bfd_get_32 (abfd, address);
7603
7604       /* Get the (signed) value from the instruction.  */
7605       addend = contents & howto->src_mask;
7606       if (addend & ((howto->src_mask + 1) >> 1))
7607         {
7608           bfd_signed_vma mask;
7609
7610           mask = -1;
7611           mask &= ~ howto->src_mask;
7612           addend |= mask;
7613         }
7614
7615       /* Add in the increment, (which is a byte value).  */
7616       switch (howto->type)
7617         {
7618         default:
7619           addend += increment;
7620           break;
7621
7622         case R_ARM_PC24:
7623         case R_ARM_PLT32:
7624         case R_ARM_CALL:
7625         case R_ARM_JUMP24:
7626           addend <<= howto->size;
7627           addend += increment;
7628
7629           /* Should we check for overflow here ?  */
7630
7631           /* Drop any undesired bits.  */
7632           addend >>= howto->rightshift;
7633           break;
7634         }
7635
7636       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
7637
7638       bfd_put_32 (abfd, contents, address);
7639     }
7640 }
7641
7642 #define IS_ARM_TLS_RELOC(R_TYPE)        \
7643   ((R_TYPE) == R_ARM_TLS_GD32           \
7644    || (R_TYPE) == R_ARM_TLS_LDO32       \
7645    || (R_TYPE) == R_ARM_TLS_LDM32       \
7646    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
7647    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
7648    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
7649    || (R_TYPE) == R_ARM_TLS_LE32        \
7650    || (R_TYPE) == R_ARM_TLS_IE32)
7651
7652 /* Relocate an ARM ELF section.  */
7653
7654 static bfd_boolean
7655 elf32_arm_relocate_section (bfd *                  output_bfd,
7656                             struct bfd_link_info * info,
7657                             bfd *                  input_bfd,
7658                             asection *             input_section,
7659                             bfd_byte *             contents,
7660                             Elf_Internal_Rela *    relocs,
7661                             Elf_Internal_Sym *     local_syms,
7662                             asection **            local_sections)
7663 {
7664   Elf_Internal_Shdr *symtab_hdr;
7665   struct elf_link_hash_entry **sym_hashes;
7666   Elf_Internal_Rela *rel;
7667   Elf_Internal_Rela *relend;
7668   const char *name;
7669   struct elf32_arm_link_hash_table * globals;
7670
7671   globals = elf32_arm_hash_table (info);
7672
7673   symtab_hdr = & elf_symtab_hdr (input_bfd);
7674   sym_hashes = elf_sym_hashes (input_bfd);
7675
7676   rel = relocs;
7677   relend = relocs + input_section->reloc_count;
7678   for (; rel < relend; rel++)
7679     {
7680       int                          r_type;
7681       reloc_howto_type *           howto;
7682       unsigned long                r_symndx;
7683       Elf_Internal_Sym *           sym;
7684       asection *                   sec;
7685       struct elf_link_hash_entry * h;
7686       bfd_vma                      relocation;
7687       bfd_reloc_status_type        r;
7688       arelent                      bfd_reloc;
7689       char                         sym_type;
7690       bfd_boolean                  unresolved_reloc = FALSE;
7691       char *error_message = NULL;
7692
7693       r_symndx = ELF32_R_SYM (rel->r_info);
7694       r_type   = ELF32_R_TYPE (rel->r_info);
7695       r_type   = arm_real_reloc_type (globals, r_type);
7696
7697       if (   r_type == R_ARM_GNU_VTENTRY
7698           || r_type == R_ARM_GNU_VTINHERIT)
7699         continue;
7700
7701       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
7702       howto = bfd_reloc.howto;
7703
7704       h = NULL;
7705       sym = NULL;
7706       sec = NULL;
7707
7708       if (r_symndx < symtab_hdr->sh_info)
7709         {
7710           sym = local_syms + r_symndx;
7711           sym_type = ELF32_ST_TYPE (sym->st_info);
7712           sec = local_sections[r_symndx];
7713           if (globals->use_rel)
7714             {
7715               relocation = (sec->output_section->vma
7716                             + sec->output_offset
7717                             + sym->st_value);
7718               if (!info->relocatable
7719                   && (sec->flags & SEC_MERGE)
7720                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7721                 {
7722                   asection *msec;
7723                   bfd_vma addend, value;
7724
7725                   switch (r_type)
7726                     {
7727                     case R_ARM_MOVW_ABS_NC:
7728                     case R_ARM_MOVT_ABS:
7729                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
7730                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
7731                       addend = (addend ^ 0x8000) - 0x8000;
7732                       break;
7733
7734                     case R_ARM_THM_MOVW_ABS_NC:
7735                     case R_ARM_THM_MOVT_ABS:
7736                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
7737                               << 16;
7738                       value |= bfd_get_16 (input_bfd,
7739                                            contents + rel->r_offset + 2);
7740                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
7741                                | ((value & 0x04000000) >> 15);
7742                       addend = (addend ^ 0x8000) - 0x8000;
7743                       break;
7744
7745                     default:
7746                       if (howto->rightshift
7747                           || (howto->src_mask & (howto->src_mask + 1)))
7748                         {
7749                           (*_bfd_error_handler)
7750                             (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
7751                              input_bfd, input_section,
7752                              (long) rel->r_offset, howto->name);
7753                           return FALSE;
7754                         }
7755
7756                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
7757
7758                       /* Get the (signed) value from the instruction.  */
7759                       addend = value & howto->src_mask;
7760                       if (addend & ((howto->src_mask + 1) >> 1))
7761                         {
7762                           bfd_signed_vma mask;
7763
7764                           mask = -1;
7765                           mask &= ~ howto->src_mask;
7766                           addend |= mask;
7767                         }
7768                       break;
7769                     }
7770
7771                   msec = sec;
7772                   addend =
7773                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
7774                     - relocation;
7775                   addend += msec->output_section->vma + msec->output_offset;
7776
7777                   /* Cases here must match those in the preceeding
7778                      switch statement.  */
7779                   switch (r_type)
7780                     {
7781                     case R_ARM_MOVW_ABS_NC:
7782                     case R_ARM_MOVT_ABS:
7783                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
7784                               | (addend & 0xfff);
7785                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
7786                       break;
7787
7788                     case R_ARM_THM_MOVW_ABS_NC:
7789                     case R_ARM_THM_MOVT_ABS:
7790                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
7791                               | (addend & 0xff) | ((addend & 0x0800) << 15);
7792                       bfd_put_16 (input_bfd, value >> 16,
7793                                   contents + rel->r_offset);
7794                       bfd_put_16 (input_bfd, value,
7795                                   contents + rel->r_offset + 2);
7796                       break;
7797
7798                     default:
7799                       value = (value & ~ howto->dst_mask)
7800                               | (addend & howto->dst_mask);
7801                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
7802                       break;
7803                     }
7804                 }
7805             }
7806           else
7807             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
7808         }
7809       else
7810         {
7811           bfd_boolean warned;
7812
7813           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
7814                                    r_symndx, symtab_hdr, sym_hashes,
7815                                    h, sec, relocation,
7816                                    unresolved_reloc, warned);
7817
7818           sym_type = h->type;
7819         }
7820
7821       if (sec != NULL && elf_discarded_section (sec))
7822         {
7823           /* For relocs against symbols from removed linkonce sections,
7824              or sections discarded by a linker script, we just want the
7825              section contents zeroed.  Avoid any special processing.  */
7826           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
7827           rel->r_info = 0;
7828           rel->r_addend = 0;
7829           continue;
7830         }
7831
7832       if (info->relocatable)
7833         {
7834           /* This is a relocatable link.  We don't have to change
7835              anything, unless the reloc is against a section symbol,
7836              in which case we have to adjust according to where the
7837              section symbol winds up in the output section.  */
7838           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7839             {
7840               if (globals->use_rel)
7841                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
7842                                 howto, (bfd_signed_vma) sec->output_offset);
7843               else
7844                 rel->r_addend += sec->output_offset;
7845             }
7846           continue;
7847         }
7848
7849       if (h != NULL)
7850         name = h->root.root.string;
7851       else
7852         {
7853           name = (bfd_elf_string_from_elf_section
7854                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
7855           if (name == NULL || *name == '\0')
7856             name = bfd_section_name (input_bfd, sec);
7857         }
7858
7859       if (r_symndx != 0
7860           && r_type != R_ARM_NONE
7861           && (h == NULL
7862               || h->root.type == bfd_link_hash_defined
7863               || h->root.type == bfd_link_hash_defweak)
7864           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
7865         {
7866           (*_bfd_error_handler)
7867             ((sym_type == STT_TLS
7868               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
7869               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
7870              input_bfd,
7871              input_section,
7872              (long) rel->r_offset,
7873              howto->name,
7874              name);
7875         }
7876
7877       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
7878                                          input_section, contents, rel,
7879                                          relocation, info, sec, name,
7880                                          (h ? ELF_ST_TYPE (h->type) :
7881                                           ELF_ST_TYPE (sym->st_info)), h,
7882                                          &unresolved_reloc, &error_message);
7883
7884       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
7885          because such sections are not SEC_ALLOC and thus ld.so will
7886          not process them.  */
7887       if (unresolved_reloc
7888           && !((input_section->flags & SEC_DEBUGGING) != 0
7889                && h->def_dynamic))
7890         {
7891           (*_bfd_error_handler)
7892             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
7893              input_bfd,
7894              input_section,
7895              (long) rel->r_offset,
7896              howto->name,
7897              h->root.root.string);
7898           return FALSE;
7899         }
7900
7901       if (r != bfd_reloc_ok)
7902         {
7903           switch (r)
7904             {
7905             case bfd_reloc_overflow:
7906               /* If the overflowing reloc was to an undefined symbol,
7907                  we have already printed one error message and there
7908                  is no point complaining again.  */
7909               if ((! h ||
7910                    h->root.type != bfd_link_hash_undefined)
7911                   && (!((*info->callbacks->reloc_overflow)
7912                         (info, (h ? &h->root : NULL), name, howto->name,
7913                          (bfd_vma) 0, input_bfd, input_section,
7914                          rel->r_offset))))
7915                   return FALSE;
7916               break;
7917
7918             case bfd_reloc_undefined:
7919               if (!((*info->callbacks->undefined_symbol)
7920                     (info, name, input_bfd, input_section,
7921                      rel->r_offset, TRUE)))
7922                 return FALSE;
7923               break;
7924
7925             case bfd_reloc_outofrange:
7926               error_message = _("out of range");
7927               goto common_error;
7928
7929             case bfd_reloc_notsupported:
7930               error_message = _("unsupported relocation");
7931               goto common_error;
7932
7933             case bfd_reloc_dangerous:
7934               /* error_message should already be set.  */
7935               goto common_error;
7936
7937             default:
7938               error_message = _("unknown error");
7939               /* Fall through.  */
7940
7941             common_error:
7942               BFD_ASSERT (error_message != NULL);
7943               if (!((*info->callbacks->reloc_dangerous)
7944                     (info, error_message, input_bfd, input_section,
7945                      rel->r_offset)))
7946                 return FALSE;
7947               break;
7948             }
7949         }
7950     }
7951
7952   return TRUE;
7953 }
7954
7955 /* Set the right machine number.  */
7956
7957 static bfd_boolean
7958 elf32_arm_object_p (bfd *abfd)
7959 {
7960   unsigned int mach;
7961
7962   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
7963
7964   if (mach != bfd_mach_arm_unknown)
7965     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
7966
7967   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
7968     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
7969
7970   else
7971     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
7972
7973   return TRUE;
7974 }
7975
7976 /* Function to keep ARM specific flags in the ELF header.  */
7977
7978 static bfd_boolean
7979 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
7980 {
7981   if (elf_flags_init (abfd)
7982       && elf_elfheader (abfd)->e_flags != flags)
7983     {
7984       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
7985         {
7986           if (flags & EF_ARM_INTERWORK)
7987             (*_bfd_error_handler)
7988               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
7989                abfd);
7990           else
7991             _bfd_error_handler
7992               (_("Warning: Clearing the interworking flag of %B due to outside request"),
7993                abfd);
7994         }
7995     }
7996   else
7997     {
7998       elf_elfheader (abfd)->e_flags = flags;
7999       elf_flags_init (abfd) = TRUE;
8000     }
8001
8002   return TRUE;
8003 }
8004
8005 /* Copy backend specific data from one object module to another.  */
8006
8007 static bfd_boolean
8008 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8009 {
8010   flagword in_flags;
8011   flagword out_flags;
8012
8013   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
8014     return TRUE;
8015
8016   in_flags  = elf_elfheader (ibfd)->e_flags;
8017   out_flags = elf_elfheader (obfd)->e_flags;
8018
8019   if (elf_flags_init (obfd)
8020       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
8021       && in_flags != out_flags)
8022     {
8023       /* Cannot mix APCS26 and APCS32 code.  */
8024       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
8025         return FALSE;
8026
8027       /* Cannot mix float APCS and non-float APCS code.  */
8028       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
8029         return FALSE;
8030
8031       /* If the src and dest have different interworking flags
8032          then turn off the interworking bit.  */
8033       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
8034         {
8035           if (out_flags & EF_ARM_INTERWORK)
8036             _bfd_error_handler
8037               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
8038                obfd, ibfd);
8039
8040           in_flags &= ~EF_ARM_INTERWORK;
8041         }
8042
8043       /* Likewise for PIC, though don't warn for this case.  */
8044       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
8045         in_flags &= ~EF_ARM_PIC;
8046     }
8047
8048   elf_elfheader (obfd)->e_flags = in_flags;
8049   elf_flags_init (obfd) = TRUE;
8050
8051   /* Also copy the EI_OSABI field.  */
8052   elf_elfheader (obfd)->e_ident[EI_OSABI] =
8053     elf_elfheader (ibfd)->e_ident[EI_OSABI];
8054
8055   /* Copy object attributes.  */
8056   _bfd_elf_copy_obj_attributes (ibfd, obfd);
8057
8058   return TRUE;
8059 }
8060
8061 /* Values for Tag_ABI_PCS_R9_use.  */
8062 enum
8063 {
8064   AEABI_R9_V6,
8065   AEABI_R9_SB,
8066   AEABI_R9_TLS,
8067   AEABI_R9_unused
8068 };
8069
8070 /* Values for Tag_ABI_PCS_RW_data.  */
8071 enum
8072 {
8073   AEABI_PCS_RW_data_absolute,
8074   AEABI_PCS_RW_data_PCrel,
8075   AEABI_PCS_RW_data_SBrel,
8076   AEABI_PCS_RW_data_unused
8077 };
8078
8079 /* Values for Tag_ABI_enum_size.  */
8080 enum
8081 {
8082   AEABI_enum_unused,
8083   AEABI_enum_short,
8084   AEABI_enum_wide,
8085   AEABI_enum_forced_wide
8086 };
8087
8088 /* Determine whether an object attribute tag takes an integer, a
8089    string or both.  */
8090
8091 static int
8092 elf32_arm_obj_attrs_arg_type (int tag)
8093 {
8094   if (tag == Tag_compatibility)
8095     return 3;
8096   else if (tag == 4 || tag == 5)
8097     return 2;
8098   else if (tag < 32)
8099     return 1;
8100   else
8101     return (tag & 1) != 0 ? 2 : 1;
8102 }
8103
8104 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
8105    are conflicting attributes.  */
8106
8107 static bfd_boolean
8108 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
8109 {
8110   obj_attribute *in_attr;
8111   obj_attribute *out_attr;
8112   obj_attribute_list *in_list;
8113   /* Some tags have 0 = don't care, 1 = strong requirement,
8114      2 = weak requirement.  */
8115   static const int order_312[3] = {3, 1, 2};
8116   /* For use with Tag_VFP_arch.  */
8117   static const int order_01243[5] = {0, 1, 2, 4, 3};
8118   int i;
8119
8120   if (!elf_known_obj_attributes_proc (obfd)[0].i)
8121     {
8122       /* This is the first object.  Copy the attributes.  */
8123       _bfd_elf_copy_obj_attributes (ibfd, obfd);
8124
8125       /* Use the Tag_null value to indicate the attributes have been
8126          initialized.  */
8127       elf_known_obj_attributes_proc (obfd)[0].i = 1;
8128
8129       return TRUE;
8130     }
8131
8132   in_attr = elf_known_obj_attributes_proc (ibfd);
8133   out_attr = elf_known_obj_attributes_proc (obfd);
8134   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
8135   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
8136     {
8137       /* Ignore mismatches if teh object doesn't use floating point.  */
8138       if (out_attr[Tag_ABI_FP_number_model].i == 0)
8139         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
8140       else if (in_attr[Tag_ABI_FP_number_model].i != 0)
8141         {
8142           _bfd_error_handler
8143             (_("ERROR: %B uses VFP register arguments, %B does not"),
8144              ibfd, obfd);
8145           return FALSE;
8146         }
8147     }
8148
8149   for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
8150     {
8151       /* Merge this attribute with existing attributes.  */
8152       switch (i)
8153         {
8154         case Tag_CPU_raw_name:
8155         case Tag_CPU_name:
8156           /* Use whichever has the greatest architecture requirements.  We
8157              won't necessarily have both the above tags, so make sure input
8158              name is non-NULL.  */
8159           if (in_attr[Tag_CPU_arch].i > out_attr[Tag_CPU_arch].i
8160               && in_attr[i].s)
8161             out_attr[i].s = _bfd_elf_attr_strdup (obfd, in_attr[i].s);
8162           break;
8163
8164         case Tag_ABI_optimization_goals:
8165         case Tag_ABI_FP_optimization_goals:
8166           /* Use the first value seen.  */
8167           break;
8168
8169         case Tag_CPU_arch:
8170         case Tag_ARM_ISA_use:
8171         case Tag_THUMB_ISA_use:
8172         case Tag_WMMX_arch:
8173         case Tag_NEON_arch:
8174           /* ??? Do NEON and WMMX conflict?  */
8175         case Tag_ABI_FP_rounding:
8176         case Tag_ABI_FP_denormal:
8177         case Tag_ABI_FP_exceptions:
8178         case Tag_ABI_FP_user_exceptions:
8179         case Tag_ABI_FP_number_model:
8180         case Tag_ABI_align8_preserved:
8181         case Tag_ABI_HardFP_use:
8182           /* Use the largest value specified.  */
8183           if (in_attr[i].i > out_attr[i].i)
8184             out_attr[i].i = in_attr[i].i;
8185           break;
8186
8187         case Tag_CPU_arch_profile:
8188           /* Warn if conflicting architecture profiles used.  */
8189           if (out_attr[i].i && in_attr[i].i && in_attr[i].i != out_attr[i].i)
8190             {
8191               _bfd_error_handler
8192                 (_("ERROR: %B: Conflicting architecture profiles %c/%c"),
8193                  ibfd, in_attr[i].i, out_attr[i].i);
8194               return FALSE;
8195             }
8196           if (in_attr[i].i)
8197             out_attr[i].i = in_attr[i].i;
8198           break;
8199         case Tag_VFP_arch:
8200           if (in_attr[i].i > 4 || out_attr[i].i > 4
8201               || order_01243[in_attr[i].i] > order_01243[out_attr[i].i])
8202             out_attr[i].i = in_attr[i].i;
8203           break;
8204         case Tag_PCS_config:
8205           if (out_attr[i].i == 0)
8206             out_attr[i].i = in_attr[i].i;
8207           else if (in_attr[i].i != 0 && out_attr[i].i != 0)
8208             {
8209               /* It's sometimes ok to mix different configs, so this is only
8210                  a warning.  */
8211               _bfd_error_handler
8212                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
8213             }
8214           break;
8215         case Tag_ABI_PCS_R9_use:
8216           if (in_attr[i].i != out_attr[i].i
8217               && out_attr[i].i != AEABI_R9_unused
8218               && in_attr[i].i != AEABI_R9_unused)
8219             {
8220               _bfd_error_handler
8221                 (_("ERROR: %B: Conflicting use of R9"), ibfd);
8222               return FALSE;
8223             }
8224           if (out_attr[i].i == AEABI_R9_unused)
8225             out_attr[i].i = in_attr[i].i;
8226           break;
8227         case Tag_ABI_PCS_RW_data:
8228           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
8229               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
8230               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
8231             {
8232               _bfd_error_handler
8233                 (_("ERROR: %B: SB relative addressing conflicts with use of R9"),
8234                  ibfd);
8235               return FALSE;
8236             }
8237           /* Use the smallest value specified.  */
8238           if (in_attr[i].i < out_attr[i].i)
8239             out_attr[i].i = in_attr[i].i;
8240           break;
8241         case Tag_ABI_PCS_RO_data:
8242           /* Use the smallest value specified.  */
8243           if (in_attr[i].i < out_attr[i].i)
8244             out_attr[i].i = in_attr[i].i;
8245           break;
8246         case Tag_ABI_PCS_GOT_use:
8247           if (in_attr[i].i > 2 || out_attr[i].i > 2
8248               || order_312[in_attr[i].i] < order_312[out_attr[i].i])
8249             out_attr[i].i = in_attr[i].i;
8250           break;
8251         case Tag_ABI_PCS_wchar_t:
8252           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
8253               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
8254             {
8255               _bfd_error_handler
8256                 (_("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"),
8257                  ibfd, in_attr[i].i, out_attr[i].i);
8258             }
8259           else if (in_attr[i].i && !out_attr[i].i)
8260             out_attr[i].i = in_attr[i].i;
8261           break;
8262         case Tag_ABI_align8_needed:
8263           /* ??? Check against Tag_ABI_align8_preserved.  */
8264           if (in_attr[i].i > 2 || out_attr[i].i > 2
8265               || order_312[in_attr[i].i] < order_312[out_attr[i].i])
8266             out_attr[i].i = in_attr[i].i;
8267           break;
8268         case Tag_ABI_enum_size:
8269           if (in_attr[i].i != AEABI_enum_unused)
8270             {
8271               if (out_attr[i].i == AEABI_enum_unused
8272                   || out_attr[i].i == AEABI_enum_forced_wide)
8273                 {
8274                   /* The existing object is compatible with anything.
8275                      Use whatever requirements the new object has.  */
8276                   out_attr[i].i = in_attr[i].i;
8277                 }
8278               else if (in_attr[i].i != AEABI_enum_forced_wide
8279                        && out_attr[i].i != in_attr[i].i
8280                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
8281                 {
8282                   const char *aeabi_enum_names[] =
8283                     { "", "variable-size", "32-bit", "" };
8284                   _bfd_error_handler
8285                     (_("warning: %B uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
8286                      ibfd, aeabi_enum_names[in_attr[i].i],
8287                      aeabi_enum_names[out_attr[i].i]);
8288                 }
8289             }
8290           break;
8291         case Tag_ABI_VFP_args:
8292           /* Aready done.  */
8293           break;
8294         case Tag_ABI_WMMX_args:
8295           if (in_attr[i].i != out_attr[i].i)
8296             {
8297               _bfd_error_handler
8298                 (_("ERROR: %B uses iWMMXt register arguments, %B does not"),
8299                  ibfd, obfd);
8300               return FALSE;
8301             }
8302           break;
8303         default: /* All known attributes should be explicitly covered.   */
8304           abort ();
8305         }
8306
8307       if (in_attr[i].type && !out_attr[i].type)
8308         switch (in_attr[i].type)
8309           {
8310           case 1:
8311             if (out_attr[i].i)
8312               out_attr[i].type = 1;
8313             break;
8314
8315           case 2:
8316             if (out_attr[i].s)
8317               out_attr[i].type = 2;
8318             break;
8319
8320           default:
8321             abort ();
8322           }
8323     }
8324
8325   /* Merge Tag_compatibility attributes and any common GNU ones.  */
8326   _bfd_elf_merge_object_attributes (ibfd, obfd);
8327
8328   /* Check for any attributes not known on ARM.  */
8329   in_list = elf_other_obj_attributes_proc (ibfd);
8330   while (in_list && in_list->tag == Tag_compatibility)
8331     in_list = in_list->next;
8332
8333   for (; in_list; in_list = in_list->next)
8334     {
8335       if ((in_list->tag & 128) < 64)
8336         {
8337           _bfd_error_handler
8338             (_("Warning: %B: Unknown EABI object attribute %d"),
8339              ibfd, in_list->tag);
8340           break;
8341         }
8342     }
8343   return TRUE;
8344 }
8345
8346
8347 /* Return TRUE if the two EABI versions are incompatible.  */
8348
8349 static bfd_boolean
8350 elf32_arm_versions_compatible (unsigned iver, unsigned over)
8351 {
8352   /* v4 and v5 are the same spec before and after it was released,
8353      so allow mixing them.  */
8354   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
8355       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
8356     return TRUE;
8357
8358   return (iver == over);
8359 }
8360
8361 /* Merge backend specific data from an object file to the output
8362    object file when linking.  */
8363
8364 static bfd_boolean
8365 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
8366 {
8367   flagword out_flags;
8368   flagword in_flags;
8369   bfd_boolean flags_compatible = TRUE;
8370   asection *sec;
8371
8372   /* Check if we have the same endianess.  */
8373   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
8374     return FALSE;
8375
8376   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
8377     return TRUE;
8378
8379   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
8380     return FALSE;
8381
8382   /* The input BFD must have had its flags initialised.  */
8383   /* The following seems bogus to me -- The flags are initialized in
8384      the assembler but I don't think an elf_flags_init field is
8385      written into the object.  */
8386   /* BFD_ASSERT (elf_flags_init (ibfd)); */
8387
8388   in_flags  = elf_elfheader (ibfd)->e_flags;
8389   out_flags = elf_elfheader (obfd)->e_flags;
8390
8391   /* In theory there is no reason why we couldn't handle this.  However
8392      in practice it isn't even close to working and there is no real
8393      reason to want it.  */
8394   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
8395       && !(ibfd->flags & DYNAMIC)
8396       && (in_flags & EF_ARM_BE8))
8397     {
8398       _bfd_error_handler (_("ERROR: %B is already in final BE8 format"),
8399                           ibfd);
8400       return FALSE;
8401     }
8402
8403   if (!elf_flags_init (obfd))
8404     {
8405       /* If the input is the default architecture and had the default
8406          flags then do not bother setting the flags for the output
8407          architecture, instead allow future merges to do this.  If no
8408          future merges ever set these flags then they will retain their
8409          uninitialised values, which surprise surprise, correspond
8410          to the default values.  */
8411       if (bfd_get_arch_info (ibfd)->the_default
8412           && elf_elfheader (ibfd)->e_flags == 0)
8413         return TRUE;
8414
8415       elf_flags_init (obfd) = TRUE;
8416       elf_elfheader (obfd)->e_flags = in_flags;
8417
8418       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
8419           && bfd_get_arch_info (obfd)->the_default)
8420         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
8421
8422       return TRUE;
8423     }
8424
8425   /* Determine what should happen if the input ARM architecture
8426      does not match the output ARM architecture.  */
8427   if (! bfd_arm_merge_machines (ibfd, obfd))
8428     return FALSE;
8429
8430   /* Identical flags must be compatible.  */
8431   if (in_flags == out_flags)
8432     return TRUE;
8433
8434   /* Check to see if the input BFD actually contains any sections.  If
8435      not, its flags may not have been initialised either, but it
8436      cannot actually cause any incompatiblity.  Do not short-circuit
8437      dynamic objects; their section list may be emptied by
8438     elf_link_add_object_symbols.
8439
8440     Also check to see if there are no code sections in the input.
8441     In this case there is no need to check for code specific flags.
8442     XXX - do we need to worry about floating-point format compatability
8443     in data sections ?  */
8444   if (!(ibfd->flags & DYNAMIC))
8445     {
8446       bfd_boolean null_input_bfd = TRUE;
8447       bfd_boolean only_data_sections = TRUE;
8448
8449       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8450         {
8451           /* Ignore synthetic glue sections.  */
8452           if (strcmp (sec->name, ".glue_7")
8453               && strcmp (sec->name, ".glue_7t"))
8454             {
8455               if ((bfd_get_section_flags (ibfd, sec)
8456                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
8457                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
8458                 only_data_sections = FALSE;
8459
8460               null_input_bfd = FALSE;
8461               break;
8462             }
8463         }
8464
8465       if (null_input_bfd || only_data_sections)
8466         return TRUE;
8467     }
8468
8469   /* Complain about various flag mismatches.  */
8470   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
8471                                       EF_ARM_EABI_VERSION (out_flags)))
8472     {
8473       _bfd_error_handler
8474         (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
8475          ibfd, obfd,
8476          (in_flags & EF_ARM_EABIMASK) >> 24,
8477          (out_flags & EF_ARM_EABIMASK) >> 24);
8478       return FALSE;
8479     }
8480
8481   /* Not sure what needs to be checked for EABI versions >= 1.  */
8482   /* VxWorks libraries do not use these flags.  */
8483   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
8484       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
8485       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
8486     {
8487       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
8488         {
8489           _bfd_error_handler
8490             (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
8491              ibfd, obfd,
8492              in_flags & EF_ARM_APCS_26 ? 26 : 32,
8493              out_flags & EF_ARM_APCS_26 ? 26 : 32);
8494           flags_compatible = FALSE;
8495         }
8496
8497       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
8498         {
8499           if (in_flags & EF_ARM_APCS_FLOAT)
8500             _bfd_error_handler
8501               (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
8502                ibfd, obfd);
8503           else
8504             _bfd_error_handler
8505               (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
8506                ibfd, obfd);
8507
8508           flags_compatible = FALSE;
8509         }
8510
8511       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
8512         {
8513           if (in_flags & EF_ARM_VFP_FLOAT)
8514             _bfd_error_handler
8515               (_("ERROR: %B uses VFP instructions, whereas %B does not"),
8516                ibfd, obfd);
8517           else
8518             _bfd_error_handler
8519               (_("ERROR: %B uses FPA instructions, whereas %B does not"),
8520                ibfd, obfd);
8521
8522           flags_compatible = FALSE;
8523         }
8524
8525       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
8526         {
8527           if (in_flags & EF_ARM_MAVERICK_FLOAT)
8528             _bfd_error_handler
8529               (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
8530                ibfd, obfd);
8531           else
8532             _bfd_error_handler
8533               (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
8534                ibfd, obfd);
8535
8536           flags_compatible = FALSE;
8537         }
8538
8539 #ifdef EF_ARM_SOFT_FLOAT
8540       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
8541         {
8542           /* We can allow interworking between code that is VFP format
8543              layout, and uses either soft float or integer regs for
8544              passing floating point arguments and results.  We already
8545              know that the APCS_FLOAT flags match; similarly for VFP
8546              flags.  */
8547           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
8548               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
8549             {
8550               if (in_flags & EF_ARM_SOFT_FLOAT)
8551                 _bfd_error_handler
8552                   (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
8553                    ibfd, obfd);
8554               else
8555                 _bfd_error_handler
8556                   (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
8557                    ibfd, obfd);
8558
8559               flags_compatible = FALSE;
8560             }
8561         }
8562 #endif
8563
8564       /* Interworking mismatch is only a warning.  */
8565       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
8566         {
8567           if (in_flags & EF_ARM_INTERWORK)
8568             {
8569               _bfd_error_handler
8570                 (_("Warning: %B supports interworking, whereas %B does not"),
8571                  ibfd, obfd);
8572             }
8573           else
8574             {
8575               _bfd_error_handler
8576                 (_("Warning: %B does not support interworking, whereas %B does"),
8577                  ibfd, obfd);
8578             }
8579         }
8580     }
8581
8582   return flags_compatible;
8583 }
8584
8585 /* Display the flags field.  */
8586
8587 static bfd_boolean
8588 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
8589 {
8590   FILE * file = (FILE *) ptr;
8591   unsigned long flags;
8592
8593   BFD_ASSERT (abfd != NULL && ptr != NULL);
8594
8595   /* Print normal ELF private data.  */
8596   _bfd_elf_print_private_bfd_data (abfd, ptr);
8597
8598   flags = elf_elfheader (abfd)->e_flags;
8599   /* Ignore init flag - it may not be set, despite the flags field
8600      containing valid data.  */
8601
8602   /* xgettext:c-format */
8603   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
8604
8605   switch (EF_ARM_EABI_VERSION (flags))
8606     {
8607     case EF_ARM_EABI_UNKNOWN:
8608       /* The following flag bits are GNU extensions and not part of the
8609          official ARM ELF extended ABI.  Hence they are only decoded if
8610          the EABI version is not set.  */
8611       if (flags & EF_ARM_INTERWORK)
8612         fprintf (file, _(" [interworking enabled]"));
8613
8614       if (flags & EF_ARM_APCS_26)
8615         fprintf (file, " [APCS-26]");
8616       else
8617         fprintf (file, " [APCS-32]");
8618
8619       if (flags & EF_ARM_VFP_FLOAT)
8620         fprintf (file, _(" [VFP float format]"));
8621       else if (flags & EF_ARM_MAVERICK_FLOAT)
8622         fprintf (file, _(" [Maverick float format]"));
8623       else
8624         fprintf (file, _(" [FPA float format]"));
8625
8626       if (flags & EF_ARM_APCS_FLOAT)
8627         fprintf (file, _(" [floats passed in float registers]"));
8628
8629       if (flags & EF_ARM_PIC)
8630         fprintf (file, _(" [position independent]"));
8631
8632       if (flags & EF_ARM_NEW_ABI)
8633         fprintf (file, _(" [new ABI]"));
8634
8635       if (flags & EF_ARM_OLD_ABI)
8636         fprintf (file, _(" [old ABI]"));
8637
8638       if (flags & EF_ARM_SOFT_FLOAT)
8639         fprintf (file, _(" [software FP]"));
8640
8641       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
8642                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
8643                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
8644                  | EF_ARM_MAVERICK_FLOAT);
8645       break;
8646
8647     case EF_ARM_EABI_VER1:
8648       fprintf (file, _(" [Version1 EABI]"));
8649
8650       if (flags & EF_ARM_SYMSARESORTED)
8651         fprintf (file, _(" [sorted symbol table]"));
8652       else
8653         fprintf (file, _(" [unsorted symbol table]"));
8654
8655       flags &= ~ EF_ARM_SYMSARESORTED;
8656       break;
8657
8658     case EF_ARM_EABI_VER2:
8659       fprintf (file, _(" [Version2 EABI]"));
8660
8661       if (flags & EF_ARM_SYMSARESORTED)
8662         fprintf (file, _(" [sorted symbol table]"));
8663       else
8664         fprintf (file, _(" [unsorted symbol table]"));
8665
8666       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
8667         fprintf (file, _(" [dynamic symbols use segment index]"));
8668
8669       if (flags & EF_ARM_MAPSYMSFIRST)
8670         fprintf (file, _(" [mapping symbols precede others]"));
8671
8672       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
8673                  | EF_ARM_MAPSYMSFIRST);
8674       break;
8675
8676     case EF_ARM_EABI_VER3:
8677       fprintf (file, _(" [Version3 EABI]"));
8678       break;
8679
8680     case EF_ARM_EABI_VER4:
8681       fprintf (file, _(" [Version4 EABI]"));
8682       goto eabi;
8683
8684     case EF_ARM_EABI_VER5:
8685       fprintf (file, _(" [Version5 EABI]"));
8686     eabi:
8687       if (flags & EF_ARM_BE8)
8688         fprintf (file, _(" [BE8]"));
8689
8690       if (flags & EF_ARM_LE8)
8691         fprintf (file, _(" [LE8]"));
8692
8693       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
8694       break;
8695
8696     default:
8697       fprintf (file, _(" <EABI version unrecognised>"));
8698       break;
8699     }
8700
8701   flags &= ~ EF_ARM_EABIMASK;
8702
8703   if (flags & EF_ARM_RELEXEC)
8704     fprintf (file, _(" [relocatable executable]"));
8705
8706   if (flags & EF_ARM_HASENTRY)
8707     fprintf (file, _(" [has entry point]"));
8708
8709   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
8710
8711   if (flags)
8712     fprintf (file, _("<Unrecognised flag bits set>"));
8713
8714   fputc ('\n', file);
8715
8716   return TRUE;
8717 }
8718
8719 static int
8720 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
8721 {
8722   switch (ELF_ST_TYPE (elf_sym->st_info))
8723     {
8724     case STT_ARM_TFUNC:
8725       return ELF_ST_TYPE (elf_sym->st_info);
8726
8727     case STT_ARM_16BIT:
8728       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
8729          This allows us to distinguish between data used by Thumb instructions
8730          and non-data (which is probably code) inside Thumb regions of an
8731          executable.  */
8732       if (type != STT_OBJECT && type != STT_TLS)
8733         return ELF_ST_TYPE (elf_sym->st_info);
8734       break;
8735
8736     default:
8737       break;
8738     }
8739
8740   return type;
8741 }
8742
8743 static asection *
8744 elf32_arm_gc_mark_hook (asection *sec,
8745                         struct bfd_link_info *info,
8746                         Elf_Internal_Rela *rel,
8747                         struct elf_link_hash_entry *h,
8748                         Elf_Internal_Sym *sym)
8749 {
8750   if (h != NULL)
8751     switch (ELF32_R_TYPE (rel->r_info))
8752       {
8753       case R_ARM_GNU_VTINHERIT:
8754       case R_ARM_GNU_VTENTRY:
8755         return NULL;
8756       }
8757
8758   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
8759 }
8760
8761 /* Update the got entry reference counts for the section being removed.  */
8762
8763 static bfd_boolean
8764 elf32_arm_gc_sweep_hook (bfd *                     abfd,
8765                          struct bfd_link_info *    info,
8766                          asection *                sec,
8767                          const Elf_Internal_Rela * relocs)
8768 {
8769   Elf_Internal_Shdr *symtab_hdr;
8770   struct elf_link_hash_entry **sym_hashes;
8771   bfd_signed_vma *local_got_refcounts;
8772   const Elf_Internal_Rela *rel, *relend;
8773   struct elf32_arm_link_hash_table * globals;
8774
8775   if (info->relocatable)
8776     return TRUE;
8777
8778   globals = elf32_arm_hash_table (info);
8779
8780   elf_section_data (sec)->local_dynrel = NULL;
8781
8782   symtab_hdr = & elf_symtab_hdr (abfd);
8783   sym_hashes = elf_sym_hashes (abfd);
8784   local_got_refcounts = elf_local_got_refcounts (abfd);
8785
8786   check_use_blx (globals);
8787
8788   relend = relocs + sec->reloc_count;
8789   for (rel = relocs; rel < relend; rel++)
8790     {
8791       unsigned long r_symndx;
8792       struct elf_link_hash_entry *h = NULL;
8793       int r_type;
8794
8795       r_symndx = ELF32_R_SYM (rel->r_info);
8796       if (r_symndx >= symtab_hdr->sh_info)
8797         {
8798           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8799           while (h->root.type == bfd_link_hash_indirect
8800                  || h->root.type == bfd_link_hash_warning)
8801             h = (struct elf_link_hash_entry *) h->root.u.i.link;
8802         }
8803
8804       r_type = ELF32_R_TYPE (rel->r_info);
8805       r_type = arm_real_reloc_type (globals, r_type);
8806       switch (r_type)
8807         {
8808         case R_ARM_GOT32:
8809         case R_ARM_GOT_PREL:
8810         case R_ARM_TLS_GD32:
8811         case R_ARM_TLS_IE32:
8812           if (h != NULL)
8813             {
8814               if (h->got.refcount > 0)
8815                 h->got.refcount -= 1;
8816             }
8817           else if (local_got_refcounts != NULL)
8818             {
8819               if (local_got_refcounts[r_symndx] > 0)
8820                 local_got_refcounts[r_symndx] -= 1;
8821             }
8822           break;
8823
8824         case R_ARM_TLS_LDM32:
8825           elf32_arm_hash_table (info)->tls_ldm_got.refcount -= 1;
8826           break;
8827
8828         case R_ARM_ABS32:
8829         case R_ARM_ABS32_NOI:
8830         case R_ARM_REL32:
8831         case R_ARM_REL32_NOI:
8832         case R_ARM_PC24:
8833         case R_ARM_PLT32:
8834         case R_ARM_CALL:
8835         case R_ARM_JUMP24:
8836         case R_ARM_PREL31:
8837         case R_ARM_THM_CALL:
8838         case R_ARM_THM_JUMP24:
8839         case R_ARM_THM_JUMP19:
8840         case R_ARM_MOVW_ABS_NC:
8841         case R_ARM_MOVT_ABS:
8842         case R_ARM_MOVW_PREL_NC:
8843         case R_ARM_MOVT_PREL:
8844         case R_ARM_THM_MOVW_ABS_NC:
8845         case R_ARM_THM_MOVT_ABS:
8846         case R_ARM_THM_MOVW_PREL_NC:
8847         case R_ARM_THM_MOVT_PREL:
8848           /* Should the interworking branches be here also?  */
8849
8850           if (h != NULL)
8851             {
8852               struct elf32_arm_link_hash_entry *eh;
8853               struct elf32_arm_relocs_copied **pp;
8854               struct elf32_arm_relocs_copied *p;
8855
8856               eh = (struct elf32_arm_link_hash_entry *) h;
8857
8858               if (h->plt.refcount > 0)
8859                 {
8860                   h->plt.refcount -= 1;
8861                   if (r_type == R_ARM_THM_CALL)
8862                     eh->plt_maybe_thumb_refcount--;
8863
8864                   if (r_type == R_ARM_THM_JUMP24
8865                       || r_type == R_ARM_THM_JUMP19)
8866                     eh->plt_thumb_refcount--;
8867                 }
8868
8869               if (r_type == R_ARM_ABS32
8870                   || r_type == R_ARM_REL32
8871                   || r_type == R_ARM_ABS32_NOI
8872                   || r_type == R_ARM_REL32_NOI)
8873                 {
8874                   for (pp = &eh->relocs_copied; (p = *pp) != NULL;
8875                        pp = &p->next)
8876                   if (p->section == sec)
8877                     {
8878                       p->count -= 1;
8879                       if (ELF32_R_TYPE (rel->r_info) == R_ARM_REL32
8880                           || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32_NOI)
8881                         p->pc_count -= 1;
8882                       if (p->count == 0)
8883                         *pp = p->next;
8884                       break;
8885                     }
8886                 }
8887             }
8888           break;
8889
8890         default:
8891           break;
8892         }
8893     }
8894
8895   return TRUE;
8896 }
8897
8898 /* Look through the relocs for a section during the first phase.  */
8899
8900 static bfd_boolean
8901 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
8902                         asection *sec, const Elf_Internal_Rela *relocs)
8903 {
8904   Elf_Internal_Shdr *symtab_hdr;
8905   struct elf_link_hash_entry **sym_hashes;
8906   const Elf_Internal_Rela *rel;
8907   const Elf_Internal_Rela *rel_end;
8908   bfd *dynobj;
8909   asection *sreloc;
8910   bfd_vma *local_got_offsets;
8911   struct elf32_arm_link_hash_table *htab;
8912   bfd_boolean needs_plt;
8913
8914   if (info->relocatable)
8915     return TRUE;
8916
8917   BFD_ASSERT (is_arm_elf (abfd));
8918
8919   htab = elf32_arm_hash_table (info);
8920   sreloc = NULL;
8921
8922   /* Create dynamic sections for relocatable executables so that we can
8923      copy relocations.  */
8924   if (htab->root.is_relocatable_executable
8925       && ! htab->root.dynamic_sections_created)
8926     {
8927       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
8928         return FALSE;
8929     }
8930
8931   dynobj = elf_hash_table (info)->dynobj;
8932   local_got_offsets = elf_local_got_offsets (abfd);
8933
8934   symtab_hdr = & elf_symtab_hdr (abfd);
8935   sym_hashes = elf_sym_hashes (abfd);
8936
8937   rel_end = relocs + sec->reloc_count;
8938   for (rel = relocs; rel < rel_end; rel++)
8939     {
8940       struct elf_link_hash_entry *h;
8941       struct elf32_arm_link_hash_entry *eh;
8942       unsigned long r_symndx;
8943       int r_type;
8944
8945       r_symndx = ELF32_R_SYM (rel->r_info);
8946       r_type = ELF32_R_TYPE (rel->r_info);
8947       r_type = arm_real_reloc_type (htab, r_type);
8948
8949       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
8950         {
8951           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
8952                                  r_symndx);
8953           return FALSE;
8954         }
8955
8956       if (r_symndx < symtab_hdr->sh_info)
8957         h = NULL;
8958       else
8959         {
8960           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8961           while (h->root.type == bfd_link_hash_indirect
8962                  || h->root.type == bfd_link_hash_warning)
8963             h = (struct elf_link_hash_entry *) h->root.u.i.link;
8964         }
8965
8966       eh = (struct elf32_arm_link_hash_entry *) h;
8967
8968       switch (r_type)
8969         {
8970           case R_ARM_GOT32:
8971           case R_ARM_GOT_PREL:
8972           case R_ARM_TLS_GD32:
8973           case R_ARM_TLS_IE32:
8974             /* This symbol requires a global offset table entry.  */
8975             {
8976               int tls_type, old_tls_type;
8977
8978               switch (r_type)
8979                 {
8980                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
8981                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
8982                 default: tls_type = GOT_NORMAL; break;
8983                 }
8984
8985               if (h != NULL)
8986                 {
8987                   h->got.refcount++;
8988                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
8989                 }
8990               else
8991                 {
8992                   bfd_signed_vma *local_got_refcounts;
8993
8994                   /* This is a global offset table entry for a local symbol.  */
8995                   local_got_refcounts = elf_local_got_refcounts (abfd);
8996                   if (local_got_refcounts == NULL)
8997                     {
8998                       bfd_size_type size;
8999
9000                       size = symtab_hdr->sh_info;
9001                       size *= (sizeof (bfd_signed_vma) + sizeof (char));
9002                       local_got_refcounts = bfd_zalloc (abfd, size);
9003                       if (local_got_refcounts == NULL)
9004                         return FALSE;
9005                       elf_local_got_refcounts (abfd) = local_got_refcounts;
9006                       elf32_arm_local_got_tls_type (abfd)
9007                         = (char *) (local_got_refcounts + symtab_hdr->sh_info);
9008                     }
9009                   local_got_refcounts[r_symndx] += 1;
9010                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
9011                 }
9012
9013               /* We will already have issued an error message if there is a
9014                  TLS / non-TLS mismatch, based on the symbol type.  We don't
9015                  support any linker relaxations.  So just combine any TLS
9016                  types needed.  */
9017               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
9018                   && tls_type != GOT_NORMAL)
9019                 tls_type |= old_tls_type;
9020
9021               if (old_tls_type != tls_type)
9022                 {
9023                   if (h != NULL)
9024                     elf32_arm_hash_entry (h)->tls_type = tls_type;
9025                   else
9026                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
9027                 }
9028             }
9029             /* Fall through.  */
9030
9031           case R_ARM_TLS_LDM32:
9032             if (r_type == R_ARM_TLS_LDM32)
9033                 htab->tls_ldm_got.refcount++;
9034             /* Fall through.  */
9035
9036           case R_ARM_GOTOFF32:
9037           case R_ARM_GOTPC:
9038             if (htab->sgot == NULL)
9039               {
9040                 if (htab->root.dynobj == NULL)
9041                   htab->root.dynobj = abfd;
9042                 if (!create_got_section (htab->root.dynobj, info))
9043                   return FALSE;
9044               }
9045             break;
9046
9047           case R_ARM_ABS12:
9048             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
9049                ldr __GOTT_INDEX__ offsets.  */
9050             if (!htab->vxworks_p)
9051               break;
9052             /* Fall through.  */
9053
9054           case R_ARM_PC24:
9055           case R_ARM_PLT32:
9056           case R_ARM_CALL:
9057           case R_ARM_JUMP24:
9058           case R_ARM_PREL31:
9059           case R_ARM_THM_CALL:
9060           case R_ARM_THM_JUMP24:
9061           case R_ARM_THM_JUMP19:
9062             needs_plt = 1;
9063             goto normal_reloc;
9064
9065           case R_ARM_ABS32:
9066           case R_ARM_ABS32_NOI:
9067           case R_ARM_REL32:
9068           case R_ARM_REL32_NOI:
9069           case R_ARM_MOVW_ABS_NC:
9070           case R_ARM_MOVT_ABS:
9071           case R_ARM_MOVW_PREL_NC:
9072           case R_ARM_MOVT_PREL:
9073           case R_ARM_THM_MOVW_ABS_NC:
9074           case R_ARM_THM_MOVT_ABS:
9075           case R_ARM_THM_MOVW_PREL_NC:
9076           case R_ARM_THM_MOVT_PREL:
9077             needs_plt = 0;
9078           normal_reloc:
9079
9080             /* Should the interworking branches be listed here?  */
9081             if (h != NULL)
9082               {
9083                 /* If this reloc is in a read-only section, we might
9084                    need a copy reloc.  We can't check reliably at this
9085                    stage whether the section is read-only, as input
9086                    sections have not yet been mapped to output sections.
9087                    Tentatively set the flag for now, and correct in
9088                    adjust_dynamic_symbol.  */
9089                 if (!info->shared)
9090                   h->non_got_ref = 1;
9091
9092                 /* We may need a .plt entry if the function this reloc
9093                    refers to is in a different object.  We can't tell for
9094                    sure yet, because something later might force the
9095                    symbol local.  */
9096                 if (needs_plt)
9097                   h->needs_plt = 1;
9098
9099                 /* If we create a PLT entry, this relocation will reference
9100                    it, even if it's an ABS32 relocation.  */
9101                 h->plt.refcount += 1;
9102
9103                 /* It's too early to use htab->use_blx here, so we have to
9104                    record possible blx references separately from
9105                    relocs that definitely need a thumb stub.  */
9106
9107                 if (r_type == R_ARM_THM_CALL)
9108                   eh->plt_maybe_thumb_refcount += 1;
9109
9110                 if (r_type == R_ARM_THM_JUMP24
9111                     || r_type == R_ARM_THM_JUMP19)
9112                   eh->plt_thumb_refcount += 1;
9113               }
9114
9115             /* If we are creating a shared library or relocatable executable,
9116                and this is a reloc against a global symbol, or a non PC
9117                relative reloc against a local symbol, then we need to copy
9118                the reloc into the shared library.  However, if we are linking
9119                with -Bsymbolic, we do not need to copy a reloc against a
9120                global symbol which is defined in an object we are
9121                including in the link (i.e., DEF_REGULAR is set).  At
9122                this point we have not seen all the input files, so it is
9123                possible that DEF_REGULAR is not set now but will be set
9124                later (it is never cleared).  We account for that
9125                possibility below by storing information in the
9126                relocs_copied field of the hash table entry.  */
9127             if ((info->shared || htab->root.is_relocatable_executable)
9128                 && (sec->flags & SEC_ALLOC) != 0
9129                 && ((r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI)
9130                     || (h != NULL && ! h->needs_plt
9131                         && (! info->symbolic || ! h->def_regular))))
9132               {
9133                 struct elf32_arm_relocs_copied *p, **head;
9134
9135                 /* When creating a shared object, we must copy these
9136                    reloc types into the output file.  We create a reloc
9137                    section in dynobj and make room for this reloc.  */
9138                 if (sreloc == NULL)
9139                   {
9140                     const char * name;
9141
9142                     name = (bfd_elf_string_from_elf_section
9143                             (abfd,
9144                              elf_elfheader (abfd)->e_shstrndx,
9145                              elf_section_data (sec)->rel_hdr.sh_name));
9146                     if (name == NULL)
9147                       return FALSE;
9148
9149                     BFD_ASSERT (reloc_section_p (htab, name, sec));
9150
9151                     sreloc = bfd_get_section_by_name (dynobj, name);
9152                     if (sreloc == NULL)
9153                       {
9154                         flagword flags;
9155
9156                         flags = (SEC_HAS_CONTENTS | SEC_READONLY
9157                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED);
9158                         if ((sec->flags & SEC_ALLOC) != 0
9159                             /* BPABI objects never have dynamic
9160                                relocations mapped.  */
9161                             && !htab->symbian_p)
9162                           flags |= SEC_ALLOC | SEC_LOAD;
9163                         sreloc = bfd_make_section_with_flags (dynobj,
9164                                                               name,
9165                                                               flags);
9166                         if (sreloc == NULL
9167                             || ! bfd_set_section_alignment (dynobj, sreloc, 2))
9168                           return FALSE;
9169                       }
9170
9171                     elf_section_data (sec)->sreloc = sreloc;
9172                   }
9173
9174                 /* If this is a global symbol, we count the number of
9175                    relocations we need for this symbol.  */
9176                 if (h != NULL)
9177                   {
9178                     head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
9179                   }
9180                 else
9181                   {
9182                     /* Track dynamic relocs needed for local syms too.
9183                        We really need local syms available to do this
9184                        easily.  Oh well.  */
9185
9186                     asection *s;
9187                     void *vpp;
9188
9189                     s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
9190                                                    sec, r_symndx);
9191                     if (s == NULL)
9192                       return FALSE;
9193
9194                     vpp = &elf_section_data (s)->local_dynrel;
9195                     head = (struct elf32_arm_relocs_copied **) vpp;
9196                   }
9197
9198                 p = *head;
9199                 if (p == NULL || p->section != sec)
9200                   {
9201                     bfd_size_type amt = sizeof *p;
9202
9203                     p = bfd_alloc (htab->root.dynobj, amt);
9204                     if (p == NULL)
9205                       return FALSE;
9206                     p->next = *head;
9207                     *head = p;
9208                     p->section = sec;
9209                     p->count = 0;
9210                     p->pc_count = 0;
9211                   }
9212
9213                 if (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
9214                   p->pc_count += 1;
9215                 p->count += 1;
9216               }
9217             break;
9218
9219         /* This relocation describes the C++ object vtable hierarchy.
9220            Reconstruct it for later use during GC.  */
9221         case R_ARM_GNU_VTINHERIT:
9222           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9223             return FALSE;
9224           break;
9225
9226         /* This relocation describes which C++ vtable entries are actually
9227            used.  Record for later use during GC.  */
9228         case R_ARM_GNU_VTENTRY:
9229           BFD_ASSERT (h != NULL);
9230           if (h != NULL
9231               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9232             return FALSE;
9233           break;
9234         }
9235     }
9236
9237   return TRUE;
9238 }
9239
9240 /* Unwinding tables are not referenced directly.  This pass marks them as
9241    required if the corresponding code section is marked.  */
9242
9243 static bfd_boolean
9244 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
9245                                   elf_gc_mark_hook_fn gc_mark_hook)
9246 {
9247   bfd *sub;
9248   Elf_Internal_Shdr **elf_shdrp;
9249   bfd_boolean again;
9250
9251   /* Marking EH data may cause additional code sections to be marked,
9252      requiring multiple passes.  */
9253   again = TRUE;
9254   while (again)
9255     {
9256       again = FALSE;
9257       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9258         {
9259           asection *o;
9260
9261           if (! is_arm_elf (sub))
9262             continue;
9263
9264           elf_shdrp = elf_elfsections (sub);
9265           for (o = sub->sections; o != NULL; o = o->next)
9266             {
9267               Elf_Internal_Shdr *hdr;
9268
9269               hdr = &elf_section_data (o)->this_hdr;
9270               if (hdr->sh_type == SHT_ARM_EXIDX
9271                   && hdr->sh_link
9272                   && hdr->sh_link < elf_numsections (sub)
9273                   && !o->gc_mark
9274                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
9275                 {
9276                   again = TRUE;
9277                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9278                     return FALSE;
9279                 }
9280             }
9281         }
9282     }
9283
9284   return TRUE;
9285 }
9286
9287 /* Treat mapping symbols as special target symbols.  */
9288
9289 static bfd_boolean
9290 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
9291 {
9292   return bfd_is_arm_special_symbol_name (sym->name,
9293                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
9294 }
9295
9296 /* This is a copy of elf_find_function() from elf.c except that
9297    ARM mapping symbols are ignored when looking for function names
9298    and STT_ARM_TFUNC is considered to a function type.  */
9299
9300 static bfd_boolean
9301 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
9302                        asection *    section,
9303                        asymbol **    symbols,
9304                        bfd_vma       offset,
9305                        const char ** filename_ptr,
9306                        const char ** functionname_ptr)
9307 {
9308   const char * filename = NULL;
9309   asymbol * func = NULL;
9310   bfd_vma low_func = 0;
9311   asymbol ** p;
9312
9313   for (p = symbols; *p != NULL; p++)
9314     {
9315       elf_symbol_type *q;
9316
9317       q = (elf_symbol_type *) *p;
9318
9319       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
9320         {
9321         default:
9322           break;
9323         case STT_FILE:
9324           filename = bfd_asymbol_name (&q->symbol);
9325           break;
9326         case STT_FUNC:
9327         case STT_ARM_TFUNC:
9328         case STT_NOTYPE:
9329           /* Skip mapping symbols.  */
9330           if ((q->symbol.flags & BSF_LOCAL)
9331               && bfd_is_arm_special_symbol_name (q->symbol.name,
9332                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
9333             continue;
9334           /* Fall through.  */
9335           if (bfd_get_section (&q->symbol) == section
9336               && q->symbol.value >= low_func
9337               && q->symbol.value <= offset)
9338             {
9339               func = (asymbol *) q;
9340               low_func = q->symbol.value;
9341             }
9342           break;
9343         }
9344     }
9345
9346   if (func == NULL)
9347     return FALSE;
9348
9349   if (filename_ptr)
9350     *filename_ptr = filename;
9351   if (functionname_ptr)
9352     *functionname_ptr = bfd_asymbol_name (func);
9353
9354   return TRUE;
9355 }
9356
9357
9358 /* Find the nearest line to a particular section and offset, for error
9359    reporting.   This code is a duplicate of the code in elf.c, except
9360    that it uses arm_elf_find_function.  */
9361
9362 static bfd_boolean
9363 elf32_arm_find_nearest_line (bfd *          abfd,
9364                              asection *     section,
9365                              asymbol **     symbols,
9366                              bfd_vma        offset,
9367                              const char **  filename_ptr,
9368                              const char **  functionname_ptr,
9369                              unsigned int * line_ptr)
9370 {
9371   bfd_boolean found = FALSE;
9372
9373   /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
9374
9375   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
9376                                      filename_ptr, functionname_ptr,
9377                                      line_ptr, 0,
9378                                      & elf_tdata (abfd)->dwarf2_find_line_info))
9379     {
9380       if (!*functionname_ptr)
9381         arm_elf_find_function (abfd, section, symbols, offset,
9382                                *filename_ptr ? NULL : filename_ptr,
9383                                functionname_ptr);
9384
9385       return TRUE;
9386     }
9387
9388   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9389                                              & found, filename_ptr,
9390                                              functionname_ptr, line_ptr,
9391                                              & elf_tdata (abfd)->line_info))
9392     return FALSE;
9393
9394   if (found && (*functionname_ptr || *line_ptr))
9395     return TRUE;
9396
9397   if (symbols == NULL)
9398     return FALSE;
9399
9400   if (! arm_elf_find_function (abfd, section, symbols, offset,
9401                                filename_ptr, functionname_ptr))
9402     return FALSE;
9403
9404   *line_ptr = 0;
9405   return TRUE;
9406 }
9407
9408 static bfd_boolean
9409 elf32_arm_find_inliner_info (bfd *          abfd,
9410                              const char **  filename_ptr,
9411                              const char **  functionname_ptr,
9412                              unsigned int * line_ptr)
9413 {
9414   bfd_boolean found;
9415   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9416                                          functionname_ptr, line_ptr,
9417                                          & elf_tdata (abfd)->dwarf2_find_line_info);
9418   return found;
9419 }
9420
9421 /* Adjust a symbol defined by a dynamic object and referenced by a
9422    regular object.  The current definition is in some section of the
9423    dynamic object, but we're not including those sections.  We have to
9424    change the definition to something the rest of the link can
9425    understand.  */
9426
9427 static bfd_boolean
9428 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
9429                                  struct elf_link_hash_entry * h)
9430 {
9431   bfd * dynobj;
9432   asection * s;
9433   struct elf32_arm_link_hash_entry * eh;
9434   struct elf32_arm_link_hash_table *globals;
9435
9436   globals = elf32_arm_hash_table (info);
9437   dynobj = elf_hash_table (info)->dynobj;
9438
9439   /* Make sure we know what is going on here.  */
9440   BFD_ASSERT (dynobj != NULL
9441               && (h->needs_plt
9442                   || h->u.weakdef != NULL
9443                   || (h->def_dynamic
9444                       && h->ref_regular
9445                       && !h->def_regular)));
9446
9447   eh = (struct elf32_arm_link_hash_entry *) h;
9448
9449   /* If this is a function, put it in the procedure linkage table.  We
9450      will fill in the contents of the procedure linkage table later,
9451      when we know the address of the .got section.  */
9452   if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
9453       || h->needs_plt)
9454     {
9455       if (h->plt.refcount <= 0
9456           || SYMBOL_CALLS_LOCAL (info, h)
9457           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9458               && h->root.type == bfd_link_hash_undefweak))
9459         {
9460           /* This case can occur if we saw a PLT32 reloc in an input
9461              file, but the symbol was never referred to by a dynamic
9462              object, or if all references were garbage collected.  In
9463              such a case, we don't actually need to build a procedure
9464              linkage table, and we can just do a PC24 reloc instead.  */
9465           h->plt.offset = (bfd_vma) -1;
9466           eh->plt_thumb_refcount = 0;
9467           eh->plt_maybe_thumb_refcount = 0;
9468           h->needs_plt = 0;
9469         }
9470
9471       return TRUE;
9472     }
9473   else
9474     {
9475       /* It's possible that we incorrectly decided a .plt reloc was
9476          needed for an R_ARM_PC24 or similar reloc to a non-function sym
9477          in check_relocs.  We can't decide accurately between function
9478          and non-function syms in check-relocs; Objects loaded later in
9479          the link may change h->type.  So fix it now.  */
9480       h->plt.offset = (bfd_vma) -1;
9481       eh->plt_thumb_refcount = 0;
9482       eh->plt_maybe_thumb_refcount = 0;
9483     }
9484
9485   /* If this is a weak symbol, and there is a real definition, the
9486      processor independent code will have arranged for us to see the
9487      real definition first, and we can just use the same value.  */
9488   if (h->u.weakdef != NULL)
9489     {
9490       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
9491                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
9492       h->root.u.def.section = h->u.weakdef->root.u.def.section;
9493       h->root.u.def.value = h->u.weakdef->root.u.def.value;
9494       return TRUE;
9495     }
9496
9497   /* If there are no non-GOT references, we do not need a copy
9498      relocation.  */
9499   if (!h->non_got_ref)
9500     return TRUE;
9501
9502   /* This is a reference to a symbol defined by a dynamic object which
9503      is not a function.  */
9504
9505   /* If we are creating a shared library, we must presume that the
9506      only references to the symbol are via the global offset table.
9507      For such cases we need not do anything here; the relocations will
9508      be handled correctly by relocate_section.  Relocatable executables
9509      can reference data in shared objects directly, so we don't need to
9510      do anything here.  */
9511   if (info->shared || globals->root.is_relocatable_executable)
9512     return TRUE;
9513
9514   if (h->size == 0)
9515     {
9516       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
9517                              h->root.root.string);
9518       return TRUE;
9519     }
9520
9521   /* We must allocate the symbol in our .dynbss section, which will
9522      become part of the .bss section of the executable.  There will be
9523      an entry for this symbol in the .dynsym section.  The dynamic
9524      object will contain position independent code, so all references
9525      from the dynamic object to this symbol will go through the global
9526      offset table.  The dynamic linker will use the .dynsym entry to
9527      determine the address it must put in the global offset table, so
9528      both the dynamic object and the regular object will refer to the
9529      same memory location for the variable.  */
9530   s = bfd_get_section_by_name (dynobj, ".dynbss");
9531   BFD_ASSERT (s != NULL);
9532
9533   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
9534      copy the initial value out of the dynamic object and into the
9535      runtime process image.  We need to remember the offset into the
9536      .rel(a).bss section we are going to use.  */
9537   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9538     {
9539       asection *srel;
9540
9541       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (globals, ".bss"));
9542       BFD_ASSERT (srel != NULL);
9543       srel->size += RELOC_SIZE (globals);
9544       h->needs_copy = 1;
9545     }
9546
9547   return _bfd_elf_adjust_dynamic_copy (h, s);
9548 }
9549
9550 /* Allocate space in .plt, .got and associated reloc sections for
9551    dynamic relocs.  */
9552
9553 static bfd_boolean
9554 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
9555 {
9556   struct bfd_link_info *info;
9557   struct elf32_arm_link_hash_table *htab;
9558   struct elf32_arm_link_hash_entry *eh;
9559   struct elf32_arm_relocs_copied *p;
9560   bfd_signed_vma thumb_refs;
9561
9562   eh = (struct elf32_arm_link_hash_entry *) h;
9563
9564   if (h->root.type == bfd_link_hash_indirect)
9565     return TRUE;
9566
9567   if (h->root.type == bfd_link_hash_warning)
9568     /* When warning symbols are created, they **replace** the "real"
9569        entry in the hash table, thus we never get to see the real
9570        symbol in a hash traversal.  So look at it now.  */
9571     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9572
9573   info = (struct bfd_link_info *) inf;
9574   htab = elf32_arm_hash_table (info);
9575
9576   if (htab->root.dynamic_sections_created
9577       && h->plt.refcount > 0)
9578     {
9579       /* Make sure this symbol is output as a dynamic symbol.
9580          Undefined weak syms won't yet be marked as dynamic.  */
9581       if (h->dynindx == -1
9582           && !h->forced_local)
9583         {
9584           if (! bfd_elf_link_record_dynamic_symbol (info, h))
9585             return FALSE;
9586         }
9587
9588       if (info->shared
9589           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
9590         {
9591           asection *s = htab->splt;
9592
9593           /* If this is the first .plt entry, make room for the special
9594              first entry.  */
9595           if (s->size == 0)
9596             s->size += htab->plt_header_size;
9597
9598           h->plt.offset = s->size;
9599
9600           /* If we will insert a Thumb trampoline before this PLT, leave room
9601              for it.  */
9602           thumb_refs = eh->plt_thumb_refcount;
9603           if (!htab->use_blx)
9604             thumb_refs += eh->plt_maybe_thumb_refcount;
9605
9606           if (thumb_refs > 0)
9607             {
9608               h->plt.offset += PLT_THUMB_STUB_SIZE;
9609               s->size += PLT_THUMB_STUB_SIZE;
9610             }
9611
9612           /* If this symbol is not defined in a regular file, and we are
9613              not generating a shared library, then set the symbol to this
9614              location in the .plt.  This is required to make function
9615              pointers compare as equal between the normal executable and
9616              the shared library.  */
9617           if (! info->shared
9618               && !h->def_regular)
9619             {
9620               h->root.u.def.section = s;
9621               h->root.u.def.value = h->plt.offset;
9622
9623               /* Make sure the function is not marked as Thumb, in case
9624                  it is the target of an ABS32 relocation, which will
9625                  point to the PLT entry.  */
9626               if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
9627                 h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
9628             }
9629
9630           /* Make room for this entry.  */
9631           s->size += htab->plt_entry_size;
9632
9633           if (!htab->symbian_p)
9634             {
9635               /* We also need to make an entry in the .got.plt section, which
9636                  will be placed in the .got section by the linker script.  */
9637               eh->plt_got_offset = htab->sgotplt->size;
9638               htab->sgotplt->size += 4;
9639             }
9640
9641           /* We also need to make an entry in the .rel(a).plt section.  */
9642           htab->srelplt->size += RELOC_SIZE (htab);
9643
9644           /* VxWorks executables have a second set of relocations for
9645              each PLT entry.  They go in a separate relocation section,
9646              which is processed by the kernel loader.  */
9647           if (htab->vxworks_p && !info->shared)
9648             {
9649               /* There is a relocation for the initial PLT entry:
9650                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
9651               if (h->plt.offset == htab->plt_header_size)
9652                 htab->srelplt2->size += RELOC_SIZE (htab);
9653
9654               /* There are two extra relocations for each subsequent
9655                  PLT entry: an R_ARM_32 relocation for the GOT entry,
9656                  and an R_ARM_32 relocation for the PLT entry.  */
9657               htab->srelplt2->size += RELOC_SIZE (htab) * 2;
9658             }
9659         }
9660       else
9661         {
9662           h->plt.offset = (bfd_vma) -1;
9663           h->needs_plt = 0;
9664         }
9665     }
9666   else
9667     {
9668       h->plt.offset = (bfd_vma) -1;
9669       h->needs_plt = 0;
9670     }
9671
9672   if (h->got.refcount > 0)
9673     {
9674       asection *s;
9675       bfd_boolean dyn;
9676       int tls_type = elf32_arm_hash_entry (h)->tls_type;
9677       int indx;
9678
9679       /* Make sure this symbol is output as a dynamic symbol.
9680          Undefined weak syms won't yet be marked as dynamic.  */
9681       if (h->dynindx == -1
9682           && !h->forced_local)
9683         {
9684           if (! bfd_elf_link_record_dynamic_symbol (info, h))
9685             return FALSE;
9686         }
9687
9688       if (!htab->symbian_p)
9689         {
9690           s = htab->sgot;
9691           h->got.offset = s->size;
9692
9693           if (tls_type == GOT_UNKNOWN)
9694             abort ();
9695
9696           if (tls_type == GOT_NORMAL)
9697             /* Non-TLS symbols need one GOT slot.  */
9698             s->size += 4;
9699           else
9700             {
9701               if (tls_type & GOT_TLS_GD)
9702                 /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  */
9703                 s->size += 8;
9704               if (tls_type & GOT_TLS_IE)
9705                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
9706                 s->size += 4;
9707             }
9708
9709           dyn = htab->root.dynamic_sections_created;
9710
9711           indx = 0;
9712           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
9713               && (!info->shared
9714                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
9715             indx = h->dynindx;
9716
9717           if (tls_type != GOT_NORMAL
9718               && (info->shared || indx != 0)
9719               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9720                   || h->root.type != bfd_link_hash_undefweak))
9721             {
9722               if (tls_type & GOT_TLS_IE)
9723                 htab->srelgot->size += RELOC_SIZE (htab);
9724
9725               if (tls_type & GOT_TLS_GD)
9726                 htab->srelgot->size += RELOC_SIZE (htab);
9727
9728               if ((tls_type & GOT_TLS_GD) && indx != 0)
9729                 htab->srelgot->size += RELOC_SIZE (htab);
9730             }
9731           else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9732                     || h->root.type != bfd_link_hash_undefweak)
9733                    && (info->shared
9734                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
9735             htab->srelgot->size += RELOC_SIZE (htab);
9736         }
9737     }
9738   else
9739     h->got.offset = (bfd_vma) -1;
9740
9741   /* Allocate stubs for exported Thumb functions on v4t.  */
9742   if (!htab->use_blx && h->dynindx != -1
9743       && h->def_regular
9744       && ELF_ST_TYPE (h->type) == STT_ARM_TFUNC
9745       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9746     {
9747       struct elf_link_hash_entry * th;
9748       struct bfd_link_hash_entry * bh;
9749       struct elf_link_hash_entry * myh;
9750       char name[1024];
9751       asection *s;
9752       bh = NULL;
9753       /* Create a new symbol to regist the real location of the function.  */
9754       s = h->root.u.def.section;
9755       sprintf (name, "__real_%s", h->root.root.string);
9756       _bfd_generic_link_add_one_symbol (info, s->owner,
9757                                         name, BSF_GLOBAL, s,
9758                                         h->root.u.def.value,
9759                                         NULL, TRUE, FALSE, &bh);
9760
9761       myh = (struct elf_link_hash_entry *) bh;
9762       myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
9763       myh->forced_local = 1;
9764       eh->export_glue = myh;
9765       th = record_arm_to_thumb_glue (info, h);
9766       /* Point the symbol at the stub.  */
9767       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
9768       h->root.u.def.section = th->root.u.def.section;
9769       h->root.u.def.value = th->root.u.def.value & ~1;
9770     }
9771
9772   if (eh->relocs_copied == NULL)
9773     return TRUE;
9774
9775   /* In the shared -Bsymbolic case, discard space allocated for
9776      dynamic pc-relative relocs against symbols which turn out to be
9777      defined in regular objects.  For the normal shared case, discard
9778      space for pc-relative relocs that have become local due to symbol
9779      visibility changes.  */
9780
9781   if (info->shared || htab->root.is_relocatable_executable)
9782     {
9783       /* The only relocs that use pc_count are R_ARM_REL32 and
9784          R_ARM_REL32_NOI, which will appear on something like
9785          ".long foo - .".  We want calls to protected symbols to resolve
9786          directly to the function rather than going via the plt.  If people
9787          want function pointer comparisons to work as expected then they
9788          should avoid writing assembly like ".long foo - .".  */
9789       if (SYMBOL_CALLS_LOCAL (info, h))
9790         {
9791           struct elf32_arm_relocs_copied **pp;
9792
9793           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
9794             {
9795               p->count -= p->pc_count;
9796               p->pc_count = 0;
9797               if (p->count == 0)
9798                 *pp = p->next;
9799               else
9800                 pp = &p->next;
9801             }
9802         }
9803
9804       if (elf32_arm_hash_table (info)->vxworks_p)
9805         {
9806           struct elf32_arm_relocs_copied **pp;
9807
9808           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
9809             {
9810               if (strcmp (p->section->output_section->name, ".tls_vars") == 0)
9811                 *pp = p->next;
9812               else
9813                 pp = &p->next;
9814             }
9815         }
9816
9817       /* Also discard relocs on undefined weak syms with non-default
9818          visibility.  */
9819       if (eh->relocs_copied != NULL
9820           && h->root.type == bfd_link_hash_undefweak)
9821         {
9822           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9823             eh->relocs_copied = NULL;
9824
9825           /* Make sure undefined weak symbols are output as a dynamic
9826              symbol in PIEs.  */
9827           else if (h->dynindx == -1
9828                    && !h->forced_local)
9829             {
9830               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9831                 return FALSE;
9832             }
9833         }
9834
9835       else if (htab->root.is_relocatable_executable && h->dynindx == -1
9836                && h->root.type == bfd_link_hash_new)
9837         {
9838           /* Output absolute symbols so that we can create relocations
9839              against them.  For normal symbols we output a relocation
9840              against the section that contains them.  */
9841           if (! bfd_elf_link_record_dynamic_symbol (info, h))
9842             return FALSE;
9843         }
9844
9845     }
9846   else
9847     {
9848       /* For the non-shared case, discard space for relocs against
9849          symbols which turn out to need copy relocs or are not
9850          dynamic.  */
9851
9852       if (!h->non_got_ref
9853           && ((h->def_dynamic
9854                && !h->def_regular)
9855               || (htab->root.dynamic_sections_created
9856                   && (h->root.type == bfd_link_hash_undefweak
9857                       || h->root.type == bfd_link_hash_undefined))))
9858         {
9859           /* Make sure this symbol is output as a dynamic symbol.
9860              Undefined weak syms won't yet be marked as dynamic.  */
9861           if (h->dynindx == -1
9862               && !h->forced_local)
9863             {
9864               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9865                 return FALSE;
9866             }
9867
9868           /* If that succeeded, we know we'll be keeping all the
9869              relocs.  */
9870           if (h->dynindx != -1)
9871             goto keep;
9872         }
9873
9874       eh->relocs_copied = NULL;
9875
9876     keep: ;
9877     }
9878
9879   /* Finally, allocate space.  */
9880   for (p = eh->relocs_copied; p != NULL; p = p->next)
9881     {
9882       asection *sreloc = elf_section_data (p->section)->sreloc;
9883       sreloc->size += p->count * RELOC_SIZE (htab);
9884     }
9885
9886   return TRUE;
9887 }
9888
9889 /* Find any dynamic relocs that apply to read-only sections.  */
9890
9891 static bfd_boolean
9892 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
9893 {
9894   struct elf32_arm_link_hash_entry * eh;
9895   struct elf32_arm_relocs_copied * p;
9896
9897   if (h->root.type == bfd_link_hash_warning)
9898     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9899
9900   eh = (struct elf32_arm_link_hash_entry *) h;
9901   for (p = eh->relocs_copied; p != NULL; p = p->next)
9902     {
9903       asection *s = p->section;
9904
9905       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9906         {
9907           struct bfd_link_info *info = (struct bfd_link_info *) inf;
9908
9909           info->flags |= DF_TEXTREL;
9910
9911           /* Not an error, just cut short the traversal.  */
9912           return FALSE;
9913         }
9914     }
9915   return TRUE;
9916 }
9917
9918 void
9919 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
9920                                  int byteswap_code)
9921 {
9922   struct elf32_arm_link_hash_table *globals;
9923
9924   globals = elf32_arm_hash_table (info);
9925   globals->byteswap_code = byteswap_code;
9926 }
9927
9928 /* Set the sizes of the dynamic sections.  */
9929
9930 static bfd_boolean
9931 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
9932                                  struct bfd_link_info * info)
9933 {
9934   bfd * dynobj;
9935   asection * s;
9936   bfd_boolean plt;
9937   bfd_boolean relocs;
9938   bfd *ibfd;
9939   struct elf32_arm_link_hash_table *htab;
9940
9941   htab = elf32_arm_hash_table (info);
9942   dynobj = elf_hash_table (info)->dynobj;
9943   BFD_ASSERT (dynobj != NULL);
9944   check_use_blx (htab);
9945
9946   if (elf_hash_table (info)->dynamic_sections_created)
9947     {
9948       /* Set the contents of the .interp section to the interpreter.  */
9949       if (info->executable)
9950         {
9951           s = bfd_get_section_by_name (dynobj, ".interp");
9952           BFD_ASSERT (s != NULL);
9953           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9954           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9955         }
9956     }
9957
9958   /* Set up .got offsets for local syms, and space for local dynamic
9959      relocs.  */
9960   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9961     {
9962       bfd_signed_vma *local_got;
9963       bfd_signed_vma *end_local_got;
9964       char *local_tls_type;
9965       bfd_size_type locsymcount;
9966       Elf_Internal_Shdr *symtab_hdr;
9967       asection *srel;
9968       bfd_boolean is_vxworks = elf32_arm_hash_table (info)->vxworks_p;
9969
9970       if (! is_arm_elf (ibfd))
9971         continue;
9972
9973       for (s = ibfd->sections; s != NULL; s = s->next)
9974         {
9975           struct elf32_arm_relocs_copied *p;
9976
9977           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9978             {
9979               if (!bfd_is_abs_section (p->section)
9980                   && bfd_is_abs_section (p->section->output_section))
9981                 {
9982                   /* Input section has been discarded, either because
9983                      it is a copy of a linkonce section or due to
9984                      linker script /DISCARD/, so we'll be discarding
9985                      the relocs too.  */
9986                 }
9987               else if (is_vxworks
9988                        && strcmp (p->section->output_section->name,
9989                                   ".tls_vars") == 0)
9990                 {
9991                   /* Relocations in vxworks .tls_vars sections are
9992                      handled specially by the loader.  */
9993                 }
9994               else if (p->count != 0)
9995                 {
9996                   srel = elf_section_data (p->section)->sreloc;
9997                   srel->size += p->count * RELOC_SIZE (htab);
9998                   if ((p->section->output_section->flags & SEC_READONLY) != 0)
9999                     info->flags |= DF_TEXTREL;
10000                 }
10001             }
10002         }
10003
10004       local_got = elf_local_got_refcounts (ibfd);
10005       if (!local_got)
10006         continue;
10007
10008       symtab_hdr = & elf_symtab_hdr (ibfd);
10009       locsymcount = symtab_hdr->sh_info;
10010       end_local_got = local_got + locsymcount;
10011       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
10012       s = htab->sgot;
10013       srel = htab->srelgot;
10014       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
10015         {
10016           if (*local_got > 0)
10017             {
10018               *local_got = s->size;
10019               if (*local_tls_type & GOT_TLS_GD)
10020                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
10021                 s->size += 8;
10022               if (*local_tls_type & GOT_TLS_IE)
10023                 s->size += 4;
10024               if (*local_tls_type == GOT_NORMAL)
10025                 s->size += 4;
10026
10027               if (info->shared || *local_tls_type == GOT_TLS_GD)
10028                 srel->size += RELOC_SIZE (htab);
10029             }
10030           else
10031             *local_got = (bfd_vma) -1;
10032         }
10033     }
10034
10035   if (htab->tls_ldm_got.refcount > 0)
10036     {
10037       /* Allocate two GOT entries and one dynamic relocation (if necessary)
10038          for R_ARM_TLS_LDM32 relocations.  */
10039       htab->tls_ldm_got.offset = htab->sgot->size;
10040       htab->sgot->size += 8;
10041       if (info->shared)
10042         htab->srelgot->size += RELOC_SIZE (htab);
10043     }
10044   else
10045     htab->tls_ldm_got.offset = -1;
10046
10047   /* Allocate global sym .plt and .got entries, and space for global
10048      sym dynamic relocs.  */
10049   elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
10050
10051   /* Here we rummage through the found bfds to collect glue information.  */
10052   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10053     {
10054       if (! is_arm_elf (ibfd))
10055         continue;
10056
10057       /* Initialise mapping tables for code/data.  */
10058       bfd_elf32_arm_init_maps (ibfd);
10059
10060       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
10061           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info))
10062         /* xgettext:c-format */
10063         _bfd_error_handler (_("Errors encountered processing file %s"),
10064                             ibfd->filename);
10065     }
10066
10067   /* The check_relocs and adjust_dynamic_symbol entry points have
10068      determined the sizes of the various dynamic sections.  Allocate
10069      memory for them.  */
10070   plt = FALSE;
10071   relocs = FALSE;
10072   for (s = dynobj->sections; s != NULL; s = s->next)
10073     {
10074       const char * name;
10075
10076       if ((s->flags & SEC_LINKER_CREATED) == 0)
10077         continue;
10078
10079       /* It's OK to base decisions on the section name, because none
10080          of the dynobj section names depend upon the input files.  */
10081       name = bfd_get_section_name (dynobj, s);
10082
10083       if (strcmp (name, ".plt") == 0)
10084         {
10085           /* Remember whether there is a PLT.  */
10086           plt = s->size != 0;
10087         }
10088       else if (CONST_STRNEQ (name, ".rel"))
10089         {
10090           if (s->size != 0)
10091             {
10092               /* Remember whether there are any reloc sections other
10093                  than .rel(a).plt and .rela.plt.unloaded.  */
10094               if (s != htab->srelplt && s != htab->srelplt2)
10095                 relocs = TRUE;
10096
10097               /* We use the reloc_count field as a counter if we need
10098                  to copy relocs into the output file.  */
10099               s->reloc_count = 0;
10100             }
10101         }
10102       else if (! CONST_STRNEQ (name, ".got")
10103                && strcmp (name, ".dynbss") != 0)
10104         {
10105           /* It's not one of our sections, so don't allocate space.  */
10106           continue;
10107         }
10108
10109       if (s->size == 0)
10110         {
10111           /* If we don't need this section, strip it from the
10112              output file.  This is mostly to handle .rel(a).bss and
10113              .rel(a).plt.  We must create both sections in
10114              create_dynamic_sections, because they must be created
10115              before the linker maps input sections to output
10116              sections.  The linker does that before
10117              adjust_dynamic_symbol is called, and it is that
10118              function which decides whether anything needs to go
10119              into these sections.  */
10120           s->flags |= SEC_EXCLUDE;
10121           continue;
10122         }
10123
10124       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10125         continue;
10126
10127       /* Allocate memory for the section contents.  */
10128       s->contents = bfd_zalloc (dynobj, s->size);
10129       if (s->contents == NULL)
10130         return FALSE;
10131     }
10132
10133   if (elf_hash_table (info)->dynamic_sections_created)
10134     {
10135       /* Add some entries to the .dynamic section.  We fill in the
10136          values later, in elf32_arm_finish_dynamic_sections, but we
10137          must add the entries now so that we get the correct size for
10138          the .dynamic section.  The DT_DEBUG entry is filled in by the
10139          dynamic linker and used by the debugger.  */
10140 #define add_dynamic_entry(TAG, VAL) \
10141   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10142
10143      if (info->executable)
10144         {
10145           if (!add_dynamic_entry (DT_DEBUG, 0))
10146             return FALSE;
10147         }
10148
10149       if (plt)
10150         {
10151           if (   !add_dynamic_entry (DT_PLTGOT, 0)
10152               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10153               || !add_dynamic_entry (DT_PLTREL,
10154                                      htab->use_rel ? DT_REL : DT_RELA)
10155               || !add_dynamic_entry (DT_JMPREL, 0))
10156             return FALSE;
10157         }
10158
10159       if (relocs)
10160         {
10161           if (htab->use_rel)
10162             {
10163               if (!add_dynamic_entry (DT_REL, 0)
10164                   || !add_dynamic_entry (DT_RELSZ, 0)
10165                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
10166                 return FALSE;
10167             }
10168           else
10169             {
10170               if (!add_dynamic_entry (DT_RELA, 0)
10171                   || !add_dynamic_entry (DT_RELASZ, 0)
10172                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
10173                 return FALSE;
10174             }
10175         }
10176
10177       /* If any dynamic relocs apply to a read-only section,
10178          then we need a DT_TEXTREL entry.  */
10179       if ((info->flags & DF_TEXTREL) == 0)
10180         elf_link_hash_traverse (& htab->root, elf32_arm_readonly_dynrelocs,
10181                                 info);
10182
10183       if ((info->flags & DF_TEXTREL) != 0)
10184         {
10185           if (!add_dynamic_entry (DT_TEXTREL, 0))
10186             return FALSE;
10187         }
10188       if (htab->vxworks_p
10189           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10190         return FALSE;
10191     }
10192 #undef add_dynamic_entry
10193
10194   return TRUE;
10195 }
10196
10197 /* Finish up dynamic symbol handling.  We set the contents of various
10198    dynamic sections here.  */
10199
10200 static bfd_boolean
10201 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
10202                                  struct bfd_link_info * info,
10203                                  struct elf_link_hash_entry * h,
10204                                  Elf_Internal_Sym * sym)
10205 {
10206   bfd * dynobj;
10207   struct elf32_arm_link_hash_table *htab;
10208   struct elf32_arm_link_hash_entry *eh;
10209
10210   dynobj = elf_hash_table (info)->dynobj;
10211   htab = elf32_arm_hash_table (info);
10212   eh = (struct elf32_arm_link_hash_entry *) h;
10213
10214   if (h->plt.offset != (bfd_vma) -1)
10215     {
10216       asection * splt;
10217       asection * srel;
10218       bfd_byte *loc;
10219       bfd_vma plt_index;
10220       Elf_Internal_Rela rel;
10221
10222       /* This symbol has an entry in the procedure linkage table.  Set
10223          it up.  */
10224
10225       BFD_ASSERT (h->dynindx != -1);
10226
10227       splt = bfd_get_section_by_name (dynobj, ".plt");
10228       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".plt"));
10229       BFD_ASSERT (splt != NULL && srel != NULL);
10230
10231       /* Fill in the entry in the procedure linkage table.  */
10232       if (htab->symbian_p)
10233         {
10234           put_arm_insn (htab, output_bfd,
10235                       elf32_arm_symbian_plt_entry[0],
10236                       splt->contents + h->plt.offset);
10237           bfd_put_32 (output_bfd,
10238                       elf32_arm_symbian_plt_entry[1],
10239                       splt->contents + h->plt.offset + 4);
10240
10241           /* Fill in the entry in the .rel.plt section.  */
10242           rel.r_offset = (splt->output_section->vma
10243                           + splt->output_offset
10244                           + h->plt.offset + 4);
10245           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
10246
10247           /* Get the index in the procedure linkage table which
10248              corresponds to this symbol.  This is the index of this symbol
10249              in all the symbols for which we are making plt entries.  The
10250              first entry in the procedure linkage table is reserved.  */
10251           plt_index = ((h->plt.offset - htab->plt_header_size)
10252                        / htab->plt_entry_size);
10253         }
10254       else
10255         {
10256           bfd_vma got_offset, got_address, plt_address;
10257           bfd_vma got_displacement;
10258           asection * sgot;
10259           bfd_byte * ptr;
10260
10261           sgot = bfd_get_section_by_name (dynobj, ".got.plt");
10262           BFD_ASSERT (sgot != NULL);
10263
10264           /* Get the offset into the .got.plt table of the entry that
10265              corresponds to this function.  */
10266           got_offset = eh->plt_got_offset;
10267
10268           /* Get the index in the procedure linkage table which
10269              corresponds to this symbol.  This is the index of this symbol
10270              in all the symbols for which we are making plt entries.  The
10271              first three entries in .got.plt are reserved; after that
10272              symbols appear in the same order as in .plt.  */
10273           plt_index = (got_offset - 12) / 4;
10274
10275           /* Calculate the address of the GOT entry.  */
10276           got_address = (sgot->output_section->vma
10277                          + sgot->output_offset
10278                          + got_offset);
10279
10280           /* ...and the address of the PLT entry.  */
10281           plt_address = (splt->output_section->vma
10282                          + splt->output_offset
10283                          + h->plt.offset);
10284
10285           ptr = htab->splt->contents + h->plt.offset;
10286           if (htab->vxworks_p && info->shared)
10287             {
10288               unsigned int i;
10289               bfd_vma val;
10290
10291               for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
10292                 {
10293                   val = elf32_arm_vxworks_shared_plt_entry[i];
10294                   if (i == 2)
10295                     val |= got_address - sgot->output_section->vma;
10296                   if (i == 5)
10297                     val |= plt_index * RELOC_SIZE (htab);
10298                   if (i == 2 || i == 5)
10299                     bfd_put_32 (output_bfd, val, ptr);
10300                   else
10301                     put_arm_insn (htab, output_bfd, val, ptr);
10302                 }
10303             }
10304           else if (htab->vxworks_p)
10305             {
10306               unsigned int i;
10307               bfd_vma val;
10308
10309               for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
10310                 {
10311                   val = elf32_arm_vxworks_exec_plt_entry[i];
10312                   if (i == 2)
10313                     val |= got_address;
10314                   if (i == 4)
10315                     val |= 0xffffff & -((h->plt.offset + i * 4 + 8) >> 2);
10316                   if (i == 5)
10317                     val |= plt_index * RELOC_SIZE (htab);
10318                   if (i == 2 || i == 5)
10319                     bfd_put_32 (output_bfd, val, ptr);
10320                   else
10321                     put_arm_insn (htab, output_bfd, val, ptr);
10322                 }
10323
10324               loc = (htab->srelplt2->contents
10325                      + (plt_index * 2 + 1) * RELOC_SIZE (htab));
10326
10327               /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
10328                  referencing the GOT for this PLT entry.  */
10329               rel.r_offset = plt_address + 8;
10330               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
10331               rel.r_addend = got_offset;
10332               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
10333               loc += RELOC_SIZE (htab);
10334
10335               /* Create the R_ARM_ABS32 relocation referencing the
10336                  beginning of the PLT for this GOT entry.  */
10337               rel.r_offset = got_address;
10338               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
10339               rel.r_addend = 0;
10340               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
10341             }
10342           else
10343             {
10344               bfd_signed_vma thumb_refs;
10345               /* Calculate the displacement between the PLT slot and the
10346                  entry in the GOT.  The eight-byte offset accounts for the
10347                  value produced by adding to pc in the first instruction
10348                  of the PLT stub.  */
10349               got_displacement = got_address - (plt_address + 8);
10350
10351               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
10352
10353               thumb_refs = eh->plt_thumb_refcount;
10354               if (!htab->use_blx)
10355                 thumb_refs += eh->plt_maybe_thumb_refcount;
10356
10357               if (thumb_refs > 0)
10358                 {
10359                   put_thumb_insn (htab, output_bfd,
10360                                   elf32_arm_plt_thumb_stub[0], ptr - 4);
10361                   put_thumb_insn (htab, output_bfd,
10362                                   elf32_arm_plt_thumb_stub[1], ptr - 2);
10363                 }
10364
10365               put_arm_insn (htab, output_bfd,
10366                             elf32_arm_plt_entry[0]
10367                             | ((got_displacement & 0x0ff00000) >> 20),
10368                             ptr + 0);
10369               put_arm_insn (htab, output_bfd,
10370                             elf32_arm_plt_entry[1]
10371                             | ((got_displacement & 0x000ff000) >> 12),
10372                             ptr+ 4);
10373               put_arm_insn (htab, output_bfd,
10374                             elf32_arm_plt_entry[2]
10375                             | (got_displacement & 0x00000fff),
10376                             ptr + 8);
10377 #ifdef FOUR_WORD_PLT
10378               bfd_put_32 (output_bfd, elf32_arm_plt_entry[3], ptr + 12);
10379 #endif
10380             }
10381
10382           /* Fill in the entry in the global offset table.  */
10383           bfd_put_32 (output_bfd,
10384                       (splt->output_section->vma
10385                        + splt->output_offset),
10386                       sgot->contents + got_offset);
10387
10388           /* Fill in the entry in the .rel(a).plt section.  */
10389           rel.r_addend = 0;
10390           rel.r_offset = got_address;
10391           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
10392         }
10393
10394       loc = srel->contents + plt_index * RELOC_SIZE (htab);
10395       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
10396
10397       if (!h->def_regular)
10398         {
10399           /* Mark the symbol as undefined, rather than as defined in
10400              the .plt section.  Leave the value alone.  */
10401           sym->st_shndx = SHN_UNDEF;
10402           /* If the symbol is weak, we do need to clear the value.
10403              Otherwise, the PLT entry would provide a definition for
10404              the symbol even if the symbol wasn't defined anywhere,
10405              and so the symbol would never be NULL.  */
10406           if (!h->ref_regular_nonweak)
10407             sym->st_value = 0;
10408         }
10409     }
10410
10411   if (h->got.offset != (bfd_vma) -1
10412       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
10413       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
10414     {
10415       asection * sgot;
10416       asection * srel;
10417       Elf_Internal_Rela rel;
10418       bfd_byte *loc;
10419       bfd_vma offset;
10420
10421       /* This symbol has an entry in the global offset table.  Set it
10422          up.  */
10423       sgot = bfd_get_section_by_name (dynobj, ".got");
10424       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".got"));
10425       BFD_ASSERT (sgot != NULL && srel != NULL);
10426
10427       offset = (h->got.offset & ~(bfd_vma) 1);
10428       rel.r_addend = 0;
10429       rel.r_offset = (sgot->output_section->vma
10430                       + sgot->output_offset
10431                       + offset);
10432
10433       /* If this is a static link, or it is a -Bsymbolic link and the
10434          symbol is defined locally or was forced to be local because
10435          of a version file, we just want to emit a RELATIVE reloc.
10436          The entry in the global offset table will already have been
10437          initialized in the relocate_section function.  */
10438       if (info->shared
10439           && SYMBOL_REFERENCES_LOCAL (info, h))
10440         {
10441           BFD_ASSERT ((h->got.offset & 1) != 0);
10442           rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
10443           if (!htab->use_rel)
10444             {
10445               rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + offset);
10446               bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
10447             }
10448         }
10449       else
10450         {
10451           BFD_ASSERT ((h->got.offset & 1) == 0);
10452           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
10453           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
10454         }
10455
10456       loc = srel->contents + srel->reloc_count++ * RELOC_SIZE (htab);
10457       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
10458     }
10459
10460   if (h->needs_copy)
10461     {
10462       asection * s;
10463       Elf_Internal_Rela rel;
10464       bfd_byte *loc;
10465
10466       /* This symbol needs a copy reloc.  Set it up.  */
10467       BFD_ASSERT (h->dynindx != -1
10468                   && (h->root.type == bfd_link_hash_defined
10469                       || h->root.type == bfd_link_hash_defweak));
10470
10471       s = bfd_get_section_by_name (h->root.u.def.section->owner,
10472                                    RELOC_SECTION (htab, ".bss"));
10473       BFD_ASSERT (s != NULL);
10474
10475       rel.r_addend = 0;
10476       rel.r_offset = (h->root.u.def.value
10477                       + h->root.u.def.section->output_section->vma
10478                       + h->root.u.def.section->output_offset);
10479       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
10480       loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
10481       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
10482     }
10483
10484   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
10485      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
10486      to the ".got" section.  */
10487   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
10488       || (!htab->vxworks_p && h == htab->root.hgot))
10489     sym->st_shndx = SHN_ABS;
10490
10491   return TRUE;
10492 }
10493
10494 /* Finish up the dynamic sections.  */
10495
10496 static bfd_boolean
10497 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
10498 {
10499   bfd * dynobj;
10500   asection * sgot;
10501   asection * sdyn;
10502
10503   dynobj = elf_hash_table (info)->dynobj;
10504
10505   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
10506   BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
10507   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10508
10509   if (elf_hash_table (info)->dynamic_sections_created)
10510     {
10511       asection *splt;
10512       Elf32_External_Dyn *dyncon, *dynconend;
10513       struct elf32_arm_link_hash_table *htab;
10514
10515       htab = elf32_arm_hash_table (info);
10516       splt = bfd_get_section_by_name (dynobj, ".plt");
10517       BFD_ASSERT (splt != NULL && sdyn != NULL);
10518
10519       dyncon = (Elf32_External_Dyn *) sdyn->contents;
10520       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
10521
10522       for (; dyncon < dynconend; dyncon++)
10523         {
10524           Elf_Internal_Dyn dyn;
10525           const char * name;
10526           asection * s;
10527
10528           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
10529
10530           switch (dyn.d_tag)
10531             {
10532               unsigned int type;
10533
10534             default:
10535               if (htab->vxworks_p
10536                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10537                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10538               break;
10539
10540             case DT_HASH:
10541               name = ".hash";
10542               goto get_vma_if_bpabi;
10543             case DT_STRTAB:
10544               name = ".dynstr";
10545               goto get_vma_if_bpabi;
10546             case DT_SYMTAB:
10547               name = ".dynsym";
10548               goto get_vma_if_bpabi;
10549             case DT_VERSYM:
10550               name = ".gnu.version";
10551               goto get_vma_if_bpabi;
10552             case DT_VERDEF:
10553               name = ".gnu.version_d";
10554               goto get_vma_if_bpabi;
10555             case DT_VERNEED:
10556               name = ".gnu.version_r";
10557               goto get_vma_if_bpabi;
10558
10559             case DT_PLTGOT:
10560               name = ".got";
10561               goto get_vma;
10562             case DT_JMPREL:
10563               name = RELOC_SECTION (htab, ".plt");
10564             get_vma:
10565               s = bfd_get_section_by_name (output_bfd, name);
10566               BFD_ASSERT (s != NULL);
10567               if (!htab->symbian_p)
10568                 dyn.d_un.d_ptr = s->vma;
10569               else
10570                 /* In the BPABI, tags in the PT_DYNAMIC section point
10571                    at the file offset, not the memory address, for the
10572                    convenience of the post linker.  */
10573                 dyn.d_un.d_ptr = s->filepos;
10574               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10575               break;
10576
10577             get_vma_if_bpabi:
10578               if (htab->symbian_p)
10579                 goto get_vma;
10580               break;
10581
10582             case DT_PLTRELSZ:
10583               s = bfd_get_section_by_name (output_bfd,
10584                                            RELOC_SECTION (htab, ".plt"));
10585               BFD_ASSERT (s != NULL);
10586               dyn.d_un.d_val = s->size;
10587               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10588               break;
10589
10590             case DT_RELSZ:
10591             case DT_RELASZ:
10592               if (!htab->symbian_p)
10593                 {
10594                   /* My reading of the SVR4 ABI indicates that the
10595                      procedure linkage table relocs (DT_JMPREL) should be
10596                      included in the overall relocs (DT_REL).  This is
10597                      what Solaris does.  However, UnixWare can not handle
10598                      that case.  Therefore, we override the DT_RELSZ entry
10599                      here to make it not include the JMPREL relocs.  Since
10600                      the linker script arranges for .rel(a).plt to follow all
10601                      other relocation sections, we don't have to worry
10602                      about changing the DT_REL entry.  */
10603                   s = bfd_get_section_by_name (output_bfd,
10604                                                RELOC_SECTION (htab, ".plt"));
10605                   if (s != NULL)
10606                     dyn.d_un.d_val -= s->size;
10607                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10608                   break;
10609                 }
10610               /* Fall through.  */
10611
10612             case DT_REL:
10613             case DT_RELA:
10614               /* In the BPABI, the DT_REL tag must point at the file
10615                  offset, not the VMA, of the first relocation
10616                  section.  So, we use code similar to that in
10617                  elflink.c, but do not check for SHF_ALLOC on the
10618                  relcoation section, since relocations sections are
10619                  never allocated under the BPABI.  The comments above
10620                  about Unixware notwithstanding, we include all of the
10621                  relocations here.  */
10622               if (htab->symbian_p)
10623                 {
10624                   unsigned int i;
10625                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10626                           ? SHT_REL : SHT_RELA);
10627                   dyn.d_un.d_val = 0;
10628                   for (i = 1; i < elf_numsections (output_bfd); i++)
10629                     {
10630                       Elf_Internal_Shdr *hdr
10631                         = elf_elfsections (output_bfd)[i];
10632                       if (hdr->sh_type == type)
10633                         {
10634                           if (dyn.d_tag == DT_RELSZ
10635                               || dyn.d_tag == DT_RELASZ)
10636                             dyn.d_un.d_val += hdr->sh_size;
10637                           else if ((ufile_ptr) hdr->sh_offset
10638                                    <= dyn.d_un.d_val - 1)
10639                             dyn.d_un.d_val = hdr->sh_offset;
10640                         }
10641                     }
10642                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10643                 }
10644               break;
10645
10646               /* Set the bottom bit of DT_INIT/FINI if the
10647                  corresponding function is Thumb.  */
10648             case DT_INIT:
10649               name = info->init_function;
10650               goto get_sym;
10651             case DT_FINI:
10652               name = info->fini_function;
10653             get_sym:
10654               /* If it wasn't set by elf_bfd_final_link
10655                  then there is nothing to adjust.  */
10656               if (dyn.d_un.d_val != 0)
10657                 {
10658                   struct elf_link_hash_entry * eh;
10659
10660                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
10661                                              FALSE, FALSE, TRUE);
10662                   if (eh != NULL
10663                       && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
10664                     {
10665                       dyn.d_un.d_val |= 1;
10666                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10667                     }
10668                 }
10669               break;
10670             }
10671         }
10672
10673       /* Fill in the first entry in the procedure linkage table.  */
10674       if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
10675         {
10676           const bfd_vma *plt0_entry;
10677           bfd_vma got_address, plt_address, got_displacement;
10678
10679           /* Calculate the addresses of the GOT and PLT.  */
10680           got_address = sgot->output_section->vma + sgot->output_offset;
10681           plt_address = splt->output_section->vma + splt->output_offset;
10682
10683           if (htab->vxworks_p)
10684             {
10685               /* The VxWorks GOT is relocated by the dynamic linker.
10686                  Therefore, we must emit relocations rather than simply
10687                  computing the values now.  */
10688               Elf_Internal_Rela rel;
10689
10690               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
10691               put_arm_insn (htab, output_bfd, plt0_entry[0],
10692                             splt->contents + 0);
10693               put_arm_insn (htab, output_bfd, plt0_entry[1],
10694                             splt->contents + 4);
10695               put_arm_insn (htab, output_bfd, plt0_entry[2],
10696                             splt->contents + 8);
10697               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
10698
10699               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
10700               rel.r_offset = plt_address + 12;
10701               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
10702               rel.r_addend = 0;
10703               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
10704                                      htab->srelplt2->contents);
10705             }
10706           else
10707             {
10708               got_displacement = got_address - (plt_address + 16);
10709
10710               plt0_entry = elf32_arm_plt0_entry;
10711               put_arm_insn (htab, output_bfd, plt0_entry[0],
10712                             splt->contents + 0);
10713               put_arm_insn (htab, output_bfd, plt0_entry[1],
10714                             splt->contents + 4);
10715               put_arm_insn (htab, output_bfd, plt0_entry[2],
10716                             splt->contents + 8);
10717               put_arm_insn (htab, output_bfd, plt0_entry[3],
10718                             splt->contents + 12);
10719
10720 #ifdef FOUR_WORD_PLT
10721               /* The displacement value goes in the otherwise-unused
10722                  last word of the second entry.  */
10723               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
10724 #else
10725               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
10726 #endif
10727             }
10728         }
10729
10730       /* UnixWare sets the entsize of .plt to 4, although that doesn't
10731          really seem like the right value.  */
10732       if (splt->output_section->owner == output_bfd)
10733         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
10734
10735       if (htab->vxworks_p && !info->shared && htab->splt->size > 0)
10736         {
10737           /* Correct the .rel(a).plt.unloaded relocations.  They will have
10738              incorrect symbol indexes.  */
10739           int num_plts;
10740           unsigned char *p;
10741
10742           num_plts = ((htab->splt->size - htab->plt_header_size)
10743                       / htab->plt_entry_size);
10744           p = htab->srelplt2->contents + RELOC_SIZE (htab);
10745
10746           for (; num_plts; num_plts--)
10747             {
10748               Elf_Internal_Rela rel;
10749
10750               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
10751               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
10752               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
10753               p += RELOC_SIZE (htab);
10754
10755               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
10756               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
10757               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
10758               p += RELOC_SIZE (htab);
10759             }
10760         }
10761     }
10762
10763   /* Fill in the first three entries in the global offset table.  */
10764   if (sgot)
10765     {
10766       if (sgot->size > 0)
10767         {
10768           if (sdyn == NULL)
10769             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
10770           else
10771             bfd_put_32 (output_bfd,
10772                         sdyn->output_section->vma + sdyn->output_offset,
10773                         sgot->contents);
10774           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
10775           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
10776         }
10777
10778       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
10779     }
10780
10781   return TRUE;
10782 }
10783
10784 static void
10785 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10786 {
10787   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
10788   struct elf32_arm_link_hash_table *globals;
10789
10790   i_ehdrp = elf_elfheader (abfd);
10791
10792   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
10793     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
10794   else
10795     i_ehdrp->e_ident[EI_OSABI] = 0;
10796   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
10797
10798   if (link_info)
10799     {
10800       globals = elf32_arm_hash_table (link_info);
10801       if (globals->byteswap_code)
10802         i_ehdrp->e_flags |= EF_ARM_BE8;
10803     }
10804 }
10805
10806 static enum elf_reloc_type_class
10807 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
10808 {
10809   switch ((int) ELF32_R_TYPE (rela->r_info))
10810     {
10811     case R_ARM_RELATIVE:
10812       return reloc_class_relative;
10813     case R_ARM_JUMP_SLOT:
10814       return reloc_class_plt;
10815     case R_ARM_COPY:
10816       return reloc_class_copy;
10817     default:
10818       return reloc_class_normal;
10819     }
10820 }
10821
10822 /* Set the right machine number for an Arm ELF file.  */
10823
10824 static bfd_boolean
10825 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
10826 {
10827   if (hdr->sh_type == SHT_NOTE)
10828     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
10829
10830   return TRUE;
10831 }
10832
10833 static void
10834 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
10835 {
10836   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
10837 }
10838
10839 /* Return TRUE if this is an unwinding table entry.  */
10840
10841 static bfd_boolean
10842 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
10843 {
10844   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
10845           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
10846 }
10847
10848
10849 /* Set the type and flags for an ARM section.  We do this by
10850    the section name, which is a hack, but ought to work.  */
10851
10852 static bfd_boolean
10853 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
10854 {
10855   const char * name;
10856
10857   name = bfd_get_section_name (abfd, sec);
10858
10859   if (is_arm_elf_unwind_section_name (abfd, name))
10860     {
10861       hdr->sh_type = SHT_ARM_EXIDX;
10862       hdr->sh_flags |= SHF_LINK_ORDER;
10863     }
10864   return TRUE;
10865 }
10866
10867 /* Handle an ARM specific section when reading an object file.  This is
10868    called when bfd_section_from_shdr finds a section with an unknown
10869    type.  */
10870
10871 static bfd_boolean
10872 elf32_arm_section_from_shdr (bfd *abfd,
10873                              Elf_Internal_Shdr * hdr,
10874                              const char *name,
10875                              int shindex)
10876 {
10877   /* There ought to be a place to keep ELF backend specific flags, but
10878      at the moment there isn't one.  We just keep track of the
10879      sections by their name, instead.  Fortunately, the ABI gives
10880      names for all the ARM specific sections, so we will probably get
10881      away with this.  */
10882   switch (hdr->sh_type)
10883     {
10884     case SHT_ARM_EXIDX:
10885     case SHT_ARM_PREEMPTMAP:
10886     case SHT_ARM_ATTRIBUTES:
10887       break;
10888
10889     default:
10890       return FALSE;
10891     }
10892
10893   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
10894     return FALSE;
10895
10896   return TRUE;
10897 }
10898
10899 /* A structure used to record a list of sections, independently
10900    of the next and prev fields in the asection structure.  */
10901 typedef struct section_list
10902 {
10903   asection * sec;
10904   struct section_list * next;
10905   struct section_list * prev;
10906 }
10907 section_list;
10908
10909 /* Unfortunately we need to keep a list of sections for which
10910    an _arm_elf_section_data structure has been allocated.  This
10911    is because it is possible for functions like elf32_arm_write_section
10912    to be called on a section which has had an elf_data_structure
10913    allocated for it (and so the used_by_bfd field is valid) but
10914    for which the ARM extended version of this structure - the
10915    _arm_elf_section_data structure - has not been allocated.  */
10916 static section_list * sections_with_arm_elf_section_data = NULL;
10917
10918 static void
10919 record_section_with_arm_elf_section_data (asection * sec)
10920 {
10921   struct section_list * entry;
10922
10923   entry = bfd_malloc (sizeof (* entry));
10924   if (entry == NULL)
10925     return;
10926   entry->sec = sec;
10927   entry->next = sections_with_arm_elf_section_data;
10928   entry->prev = NULL;
10929   if (entry->next != NULL)
10930     entry->next->prev = entry;
10931   sections_with_arm_elf_section_data = entry;
10932 }
10933
10934 static struct section_list *
10935 find_arm_elf_section_entry (asection * sec)
10936 {
10937   struct section_list * entry;
10938   static struct section_list * last_entry = NULL;
10939
10940   /* This is a short cut for the typical case where the sections are added
10941      to the sections_with_arm_elf_section_data list in forward order and
10942      then looked up here in backwards order.  This makes a real difference
10943      to the ld-srec/sec64k.exp linker test.  */
10944   entry = sections_with_arm_elf_section_data;
10945   if (last_entry != NULL)
10946     {
10947       if (last_entry->sec == sec)
10948         entry = last_entry;
10949       else if (last_entry->next != NULL
10950                && last_entry->next->sec == sec)
10951         entry = last_entry->next;
10952     }
10953
10954   for (; entry; entry = entry->next)
10955     if (entry->sec == sec)
10956       break;
10957
10958   if (entry)
10959     /* Record the entry prior to this one - it is the entry we are most
10960        likely to want to locate next time.  Also this way if we have been
10961        called from unrecord_section_with_arm_elf_section_data() we will not
10962        be caching a pointer that is about to be freed.  */
10963     last_entry = entry->prev;
10964
10965   return entry;
10966 }
10967
10968 static _arm_elf_section_data *
10969 get_arm_elf_section_data (asection * sec)
10970 {
10971   struct section_list * entry;
10972
10973   entry = find_arm_elf_section_entry (sec);
10974
10975   if (entry)
10976     return elf32_arm_section_data (entry->sec);
10977   else
10978     return NULL;
10979 }
10980
10981 static void
10982 unrecord_section_with_arm_elf_section_data (asection * sec)
10983 {
10984   struct section_list * entry;
10985
10986   entry = find_arm_elf_section_entry (sec);
10987
10988   if (entry)
10989     {
10990       if (entry->prev != NULL)
10991         entry->prev->next = entry->next;
10992       if (entry->next != NULL)
10993         entry->next->prev = entry->prev;
10994       if (entry == sections_with_arm_elf_section_data)
10995         sections_with_arm_elf_section_data = entry->next;
10996       free (entry);
10997     }
10998 }
10999
11000
11001 typedef struct
11002 {
11003   void *finfo;
11004   struct bfd_link_info *info;
11005   asection *sec;
11006   int sec_shndx;
11007   bfd_boolean (*func) (void *, const char *, Elf_Internal_Sym *,
11008                        asection *, struct elf_link_hash_entry *);
11009 } output_arch_syminfo;
11010
11011 enum map_symbol_type
11012 {
11013   ARM_MAP_ARM,
11014   ARM_MAP_THUMB,
11015   ARM_MAP_DATA
11016 };
11017
11018
11019 /* Output a single mapping symbol.  */
11020
11021 static bfd_boolean
11022 elf32_arm_output_map_sym (output_arch_syminfo *osi,
11023                           enum map_symbol_type type,
11024                           bfd_vma offset)
11025 {
11026   static const char *names[3] = {"$a", "$t", "$d"};
11027   struct elf32_arm_link_hash_table *htab;
11028   Elf_Internal_Sym sym;
11029
11030   htab = elf32_arm_hash_table (osi->info);
11031   sym.st_value = osi->sec->output_section->vma
11032                  + osi->sec->output_offset
11033                  + offset;
11034   sym.st_size = 0;
11035   sym.st_other = 0;
11036   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
11037   sym.st_shndx = osi->sec_shndx;
11038   if (!osi->func (osi->finfo, names[type], &sym, osi->sec, NULL))
11039     return FALSE;
11040   return TRUE;
11041 }
11042
11043
11044 /* Output mapping symbols for PLT entries associated with H.  */
11045
11046 static bfd_boolean
11047 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
11048 {
11049   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
11050   struct elf32_arm_link_hash_table *htab;
11051   struct elf32_arm_link_hash_entry *eh;
11052   bfd_vma addr;
11053
11054   htab = elf32_arm_hash_table (osi->info);
11055
11056   if (h->root.type == bfd_link_hash_indirect)
11057     return TRUE;
11058
11059   if (h->root.type == bfd_link_hash_warning)
11060     /* When warning symbols are created, they **replace** the "real"
11061        entry in the hash table, thus we never get to see the real
11062        symbol in a hash traversal.  So look at it now.  */
11063     h = (struct elf_link_hash_entry *) h->root.u.i.link;
11064
11065   if (h->plt.offset == (bfd_vma) -1)
11066     return TRUE;
11067
11068   eh = (struct elf32_arm_link_hash_entry *) h;
11069   addr = h->plt.offset;
11070   if (htab->symbian_p)
11071     {
11072       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11073         return FALSE;
11074       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
11075         return FALSE;
11076     }
11077   else if (htab->vxworks_p)
11078     {
11079       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11080         return FALSE;
11081       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
11082         return FALSE;
11083       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
11084         return FALSE;
11085       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
11086         return FALSE;
11087     }
11088   else
11089     {
11090       bfd_signed_vma thumb_refs;
11091
11092       thumb_refs = eh->plt_thumb_refcount;
11093       if (!htab->use_blx)
11094         thumb_refs += eh->plt_maybe_thumb_refcount;
11095
11096       if (thumb_refs > 0)
11097         {
11098           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
11099             return FALSE;
11100         }
11101 #ifdef FOUR_WORD_PLT
11102       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11103         return FALSE;
11104       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
11105         return FALSE;
11106 #else
11107       /* A three-word PLT with no Thumb thunk contains only Arm code,
11108          so only need to output a mapping symbol for the first PLT entry and
11109          entries with thumb thunks.  */
11110       if (thumb_refs > 0 || addr == 20)
11111         {
11112           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11113             return FALSE;
11114         }
11115 #endif
11116     }
11117
11118   return TRUE;
11119 }
11120
11121 /* Output a single local symbol for a generated stub.  */
11122
11123 static bfd_boolean
11124 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
11125                            bfd_vma offset, bfd_vma size)
11126 {
11127   struct elf32_arm_link_hash_table *htab;
11128   Elf_Internal_Sym sym;
11129
11130   htab = elf32_arm_hash_table (osi->info);
11131   sym.st_value = osi->sec->output_section->vma
11132                  + osi->sec->output_offset
11133                  + offset;
11134   sym.st_size = size;
11135   sym.st_other = 0;
11136   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
11137   sym.st_shndx = osi->sec_shndx;
11138   if (!osi->func (osi->finfo, name, &sym, osi->sec, NULL))
11139     return FALSE;
11140   return TRUE;
11141 }
11142
11143 static bfd_boolean
11144 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
11145                   void * in_arg)
11146 {
11147   struct elf32_arm_stub_hash_entry *stub_entry;
11148   struct bfd_link_info *info;
11149   struct elf32_arm_link_hash_table *htab;
11150   asection *stub_sec;
11151   bfd_vma addr;
11152   char *stub_name;
11153   output_arch_syminfo *osi;
11154
11155   /* Massage our args to the form they really have.  */
11156   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
11157   osi = (output_arch_syminfo *) in_arg;
11158
11159   info = osi->info;
11160
11161   htab = elf32_arm_hash_table (info);
11162   stub_sec = stub_entry->stub_sec;
11163
11164   /* Ensure this stub is attached to the current section being
11165      processed.  */
11166   if (stub_sec != osi->sec)
11167     return TRUE;
11168
11169   addr = (bfd_vma) stub_entry->stub_offset;
11170   stub_name = stub_entry->output_name;
11171
11172   switch (stub_entry->stub_type)
11173     {
11174     case arm_stub_long_branch:
11175       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, 8))
11176         return FALSE;
11177       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11178         return FALSE;
11179       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
11180         return FALSE;
11181       break;
11182     case arm_thumb_v4t_stub_long_branch:
11183       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, 12))
11184         return FALSE;
11185       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11186         return FALSE;
11187       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
11188         return FALSE;
11189       break;
11190     case arm_thumb_thumb_stub_long_branch:
11191       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1, 16))
11192         return FALSE;
11193       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
11194         return FALSE;
11195       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
11196         return FALSE;
11197       break;
11198     case arm_thumb_arm_v4t_stub_long_branch:
11199       if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1, 20))
11200         return FALSE;
11201       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
11202         return FALSE;
11203       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 8))
11204         return FALSE;
11205       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 16))
11206         return FALSE;
11207       break;
11208     case arm_stub_pic_long_branch:
11209       if (!elf32_arm_output_stub_sym (osi, stub_name, addr, 12))
11210         return FALSE;
11211       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
11212         return FALSE;
11213       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
11214         return FALSE;
11215       break;
11216     default:
11217       BFD_FAIL ();
11218     }
11219
11220   return TRUE;
11221 }
11222
11223 /* Output mapping symbols for linker generated sections.  */
11224
11225 static bfd_boolean
11226 elf32_arm_output_arch_local_syms (bfd *output_bfd,
11227                                   struct bfd_link_info *info,
11228                                   void *finfo,
11229                                   bfd_boolean (*func) (void *, const char *,
11230                                                        Elf_Internal_Sym *,
11231                                                        asection *,
11232                                                        struct elf_link_hash_entry *))
11233 {
11234   output_arch_syminfo osi;
11235   struct elf32_arm_link_hash_table *htab;
11236   bfd_vma offset;
11237   bfd_size_type size;
11238
11239   htab = elf32_arm_hash_table (info);
11240   check_use_blx (htab);
11241
11242   osi.finfo = finfo;
11243   osi.info = info;
11244   osi.func = func;
11245
11246   /* ARM->Thumb glue.  */
11247   if (htab->arm_glue_size > 0)
11248     {
11249       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
11250                                          ARM2THUMB_GLUE_SECTION_NAME);
11251
11252       osi.sec_shndx = _bfd_elf_section_from_bfd_section
11253           (output_bfd, osi.sec->output_section);
11254       if (info->shared || htab->root.is_relocatable_executable
11255           || htab->pic_veneer)
11256         size = ARM2THUMB_PIC_GLUE_SIZE;
11257       else if (htab->use_blx)
11258         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
11259       else
11260         size = ARM2THUMB_STATIC_GLUE_SIZE;
11261
11262       for (offset = 0; offset < htab->arm_glue_size; offset += size)
11263         {
11264           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
11265           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
11266         }
11267     }
11268
11269   /* Thumb->ARM glue.  */
11270   if (htab->thumb_glue_size > 0)
11271     {
11272       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
11273                                          THUMB2ARM_GLUE_SECTION_NAME);
11274
11275       osi.sec_shndx = _bfd_elf_section_from_bfd_section
11276           (output_bfd, osi.sec->output_section);
11277       size = THUMB2ARM_GLUE_SIZE;
11278
11279       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
11280         {
11281           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
11282           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
11283         }
11284     }
11285
11286   /* ARMv4 BX veneers.  */
11287   if (htab->bx_glue_size > 0)
11288     {
11289       osi.sec = bfd_get_section_by_name (htab->bfd_of_glue_owner,
11290                                          ARM_BX_GLUE_SECTION_NAME);
11291
11292       osi.sec_shndx = _bfd_elf_section_from_bfd_section
11293           (output_bfd, osi.sec->output_section);
11294
11295       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
11296     }
11297
11298   /* Long calls stubs.  */
11299   if (htab->stub_bfd && htab->stub_bfd->sections)
11300     {
11301       asection* stub_sec;
11302
11303       for (stub_sec = htab->stub_bfd->sections;
11304            stub_sec != NULL;
11305            stub_sec = stub_sec->next)
11306         {
11307           /* Ignore non-stub sections.  */
11308           if (!strstr (stub_sec->name, STUB_SUFFIX))
11309             continue;
11310
11311           osi.sec = stub_sec;
11312
11313           osi.sec_shndx = _bfd_elf_section_from_bfd_section
11314             (output_bfd, osi.sec->output_section);
11315
11316           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
11317         }
11318     }
11319
11320   /* Finally, output mapping symbols for the PLT.  */
11321   if (!htab->splt || htab->splt->size == 0)
11322     return TRUE;
11323
11324   osi.sec_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11325                                                      htab->splt->output_section);
11326   osi.sec = htab->splt;
11327   /* Output mapping symbols for the plt header.  SymbianOS does not have a
11328      plt header.  */
11329   if (htab->vxworks_p)
11330     {
11331       /* VxWorks shared libraries have no PLT header.  */
11332       if (!info->shared)
11333         {
11334           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
11335             return FALSE;
11336           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
11337             return FALSE;
11338         }
11339     }
11340   else if (!htab->symbian_p)
11341     {
11342       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
11343         return FALSE;
11344 #ifndef FOUR_WORD_PLT
11345       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
11346         return FALSE;
11347 #endif
11348     }
11349
11350   elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, (void *) &osi);
11351   return TRUE;
11352 }
11353
11354 /* Allocate target specific section data.  */
11355
11356 static bfd_boolean
11357 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
11358 {
11359   if (!sec->used_by_bfd)
11360     {
11361       _arm_elf_section_data *sdata;
11362       bfd_size_type amt = sizeof (*sdata);
11363
11364       sdata = bfd_zalloc (abfd, amt);
11365       if (sdata == NULL)
11366         return FALSE;
11367       sec->used_by_bfd = sdata;
11368     }
11369
11370   record_section_with_arm_elf_section_data (sec);
11371
11372   return _bfd_elf_new_section_hook (abfd, sec);
11373 }
11374
11375
11376 /* Used to order a list of mapping symbols by address.  */
11377
11378 static int
11379 elf32_arm_compare_mapping (const void * a, const void * b)
11380 {
11381   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
11382   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
11383
11384   if (amap->vma > bmap->vma)
11385     return 1;
11386   else if (amap->vma < bmap->vma)
11387     return -1;
11388   else if (amap->type > bmap->type)
11389     /* Ensure results do not depend on the host qsort for objects with
11390        multiple mapping symbols at the same address by sorting on type
11391        after vma.  */
11392     return 1;
11393   else if (amap->type < bmap->type)
11394     return -1;
11395   else
11396     return 0;
11397 }
11398
11399
11400 /* Do code byteswapping.  Return FALSE afterwards so that the section is
11401    written out as normal.  */
11402
11403 static bfd_boolean
11404 elf32_arm_write_section (bfd *output_bfd,
11405                          struct bfd_link_info *link_info,
11406                          asection *sec,
11407                          bfd_byte *contents)
11408 {
11409   int mapcount, errcount;
11410   _arm_elf_section_data *arm_data;
11411   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
11412   elf32_arm_section_map *map;
11413   elf32_vfp11_erratum_list *errnode;
11414   bfd_vma ptr;
11415   bfd_vma end;
11416   bfd_vma offset = sec->output_section->vma + sec->output_offset;
11417   bfd_byte tmp;
11418   int i;
11419
11420   /* If this section has not been allocated an _arm_elf_section_data
11421      structure then we cannot record anything.  */
11422   arm_data = get_arm_elf_section_data (sec);
11423   if (arm_data == NULL)
11424     return FALSE;
11425
11426   mapcount = arm_data->mapcount;
11427   map = arm_data->map;
11428   errcount = arm_data->erratumcount;
11429
11430   if (errcount != 0)
11431     {
11432       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
11433
11434       for (errnode = arm_data->erratumlist; errnode != 0;
11435            errnode = errnode->next)
11436         {
11437           bfd_vma index = errnode->vma - offset;
11438
11439           switch (errnode->type)
11440             {
11441             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
11442               {
11443                 bfd_vma branch_to_veneer;
11444                 /* Original condition code of instruction, plus bit mask for
11445                    ARM B instruction.  */
11446                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
11447                                   | 0x0a000000;
11448
11449                 /* The instruction is before the label.  */
11450                 index -= 4;
11451
11452                 /* Above offset included in -4 below.  */
11453                 branch_to_veneer = errnode->u.b.veneer->vma
11454                                    - errnode->vma - 4;
11455
11456                 if ((signed) branch_to_veneer < -(1 << 25)
11457                     || (signed) branch_to_veneer >= (1 << 25))
11458                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
11459                                            "range"), output_bfd);
11460
11461                 insn |= (branch_to_veneer >> 2) & 0xffffff;
11462                 contents[endianflip ^ index] = insn & 0xff;
11463                 contents[endianflip ^ (index + 1)] = (insn >> 8) & 0xff;
11464                 contents[endianflip ^ (index + 2)] = (insn >> 16) & 0xff;
11465                 contents[endianflip ^ (index + 3)] = (insn >> 24) & 0xff;
11466               }
11467               break;
11468
11469             case VFP11_ERRATUM_ARM_VENEER:
11470               {
11471                 bfd_vma branch_from_veneer;
11472                 unsigned int insn;
11473
11474                 /* Take size of veneer into account.  */
11475                 branch_from_veneer = errnode->u.v.branch->vma
11476                                      - errnode->vma - 12;
11477
11478                 if ((signed) branch_from_veneer < -(1 << 25)
11479                     || (signed) branch_from_veneer >= (1 << 25))
11480                   (*_bfd_error_handler) (_("%B: error: VFP11 veneer out of "
11481                                            "range"), output_bfd);
11482
11483                 /* Original instruction.  */
11484                 insn = errnode->u.v.branch->u.b.vfp_insn;
11485                 contents[endianflip ^ index] = insn & 0xff;
11486                 contents[endianflip ^ (index + 1)] = (insn >> 8) & 0xff;
11487                 contents[endianflip ^ (index + 2)] = (insn >> 16) & 0xff;
11488                 contents[endianflip ^ (index + 3)] = (insn >> 24) & 0xff;
11489
11490                 /* Branch back to insn after original insn.  */
11491                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
11492                 contents[endianflip ^ (index + 4)] = insn & 0xff;
11493                 contents[endianflip ^ (index + 5)] = (insn >> 8) & 0xff;
11494                 contents[endianflip ^ (index + 6)] = (insn >> 16) & 0xff;
11495                 contents[endianflip ^ (index + 7)] = (insn >> 24) & 0xff;
11496               }
11497               break;
11498
11499             default:
11500               abort ();
11501             }
11502         }
11503     }
11504
11505   if (mapcount == 0)
11506     return FALSE;
11507
11508   if (globals->byteswap_code)
11509     {
11510       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
11511
11512       ptr = map[0].vma;
11513       for (i = 0; i < mapcount; i++)
11514         {
11515           if (i == mapcount - 1)
11516             end = sec->size;
11517           else
11518             end = map[i + 1].vma;
11519
11520           switch (map[i].type)
11521             {
11522             case 'a':
11523               /* Byte swap code words.  */
11524               while (ptr + 3 < end)
11525                 {
11526                   tmp = contents[ptr];
11527                   contents[ptr] = contents[ptr + 3];
11528                   contents[ptr + 3] = tmp;
11529                   tmp = contents[ptr + 1];
11530                   contents[ptr + 1] = contents[ptr + 2];
11531                   contents[ptr + 2] = tmp;
11532                   ptr += 4;
11533                 }
11534               break;
11535
11536             case 't':
11537               /* Byte swap code halfwords.  */
11538               while (ptr + 1 < end)
11539                 {
11540                   tmp = contents[ptr];
11541                   contents[ptr] = contents[ptr + 1];
11542                   contents[ptr + 1] = tmp;
11543                   ptr += 2;
11544                 }
11545               break;
11546
11547             case 'd':
11548               /* Leave data alone.  */
11549               break;
11550             }
11551           ptr = end;
11552         }
11553     }
11554
11555   free (map);
11556   arm_data->mapcount = 0;
11557   arm_data->mapsize = 0;
11558   arm_data->map = NULL;
11559   unrecord_section_with_arm_elf_section_data (sec);
11560
11561   return FALSE;
11562 }
11563
11564 static void
11565 unrecord_section_via_map_over_sections (bfd * abfd ATTRIBUTE_UNUSED,
11566                                         asection * sec,
11567                                         void * ignore ATTRIBUTE_UNUSED)
11568 {
11569   unrecord_section_with_arm_elf_section_data (sec);
11570 }
11571
11572 static bfd_boolean
11573 elf32_arm_close_and_cleanup (bfd * abfd)
11574 {
11575   if (abfd->sections)
11576     bfd_map_over_sections (abfd,
11577                            unrecord_section_via_map_over_sections,
11578                            NULL);
11579
11580   return _bfd_elf_close_and_cleanup (abfd);
11581 }
11582
11583 static bfd_boolean
11584 elf32_arm_bfd_free_cached_info (bfd * abfd)
11585 {
11586   if (abfd->sections)
11587     bfd_map_over_sections (abfd,
11588                            unrecord_section_via_map_over_sections,
11589                            NULL);
11590
11591   return _bfd_free_cached_info (abfd);
11592 }
11593
11594 /* Display STT_ARM_TFUNC symbols as functions.  */
11595
11596 static void
11597 elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
11598                              asymbol *asym)
11599 {
11600   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
11601
11602   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
11603     elfsym->symbol.flags |= BSF_FUNCTION;
11604 }
11605
11606
11607 /* Mangle thumb function symbols as we read them in.  */
11608
11609 static bfd_boolean
11610 elf32_arm_swap_symbol_in (bfd * abfd,
11611                           const void *psrc,
11612                           const void *pshn,
11613                           Elf_Internal_Sym *dst)
11614 {
11615   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
11616     return FALSE;
11617
11618   /* New EABI objects mark thumb function symbols by setting the low bit of
11619      the address.  Turn these into STT_ARM_TFUNC.  */
11620   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
11621       && (dst->st_value & 1))
11622     {
11623       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
11624       dst->st_value &= ~(bfd_vma) 1;
11625     }
11626   return TRUE;
11627 }
11628
11629
11630 /* Mangle thumb function symbols as we write them out.  */
11631
11632 static void
11633 elf32_arm_swap_symbol_out (bfd *abfd,
11634                            const Elf_Internal_Sym *src,
11635                            void *cdst,
11636                            void *shndx)
11637 {
11638   Elf_Internal_Sym newsym;
11639
11640   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
11641      of the address set, as per the new EABI.  We do this unconditionally
11642      because objcopy does not set the elf header flags until after
11643      it writes out the symbol table.  */
11644   if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
11645     {
11646       newsym = *src;
11647       newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
11648       if (newsym.st_shndx != SHN_UNDEF)
11649         {
11650           /* Do this only for defined symbols. At link type, the static
11651              linker will simulate the work of dynamic linker of resolving
11652              symbols and will carry over the thumbness of found symbols to
11653              the output symbol table. It's not clear how it happens, but
11654              the thumbness of undefined symbols can well be different at
11655              runtime, and writing '1' for them will be confusing for users
11656              and possibly for dynamic linker itself.
11657           */
11658           newsym.st_value |= 1;
11659         }
11660
11661       src = &newsym;
11662     }
11663   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
11664 }
11665
11666 /* Add the PT_ARM_EXIDX program header.  */
11667
11668 static bfd_boolean
11669 elf32_arm_modify_segment_map (bfd *abfd,
11670                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
11671 {
11672   struct elf_segment_map *m;
11673   asection *sec;
11674
11675   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
11676   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
11677     {
11678       /* If there is already a PT_ARM_EXIDX header, then we do not
11679          want to add another one.  This situation arises when running
11680          "strip"; the input binary already has the header.  */
11681       m = elf_tdata (abfd)->segment_map;
11682       while (m && m->p_type != PT_ARM_EXIDX)
11683         m = m->next;
11684       if (!m)
11685         {
11686           m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
11687           if (m == NULL)
11688             return FALSE;
11689           m->p_type = PT_ARM_EXIDX;
11690           m->count = 1;
11691           m->sections[0] = sec;
11692
11693           m->next = elf_tdata (abfd)->segment_map;
11694           elf_tdata (abfd)->segment_map = m;
11695         }
11696     }
11697
11698   return TRUE;
11699 }
11700
11701 /* We may add a PT_ARM_EXIDX program header.  */
11702
11703 static int
11704 elf32_arm_additional_program_headers (bfd *abfd,
11705                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
11706 {
11707   asection *sec;
11708
11709   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
11710   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
11711     return 1;
11712   else
11713     return 0;
11714 }
11715
11716 /* We have two function types: STT_FUNC and STT_ARM_TFUNC.  */
11717
11718 static bfd_boolean
11719 elf32_arm_is_function_type (unsigned int type)
11720 {
11721   return (type == STT_FUNC) || (type == STT_ARM_TFUNC);
11722 }
11723
11724 /* We use this to override swap_symbol_in and swap_symbol_out.  */
11725 const struct elf_size_info elf32_arm_size_info =
11726 {
11727   sizeof (Elf32_External_Ehdr),
11728   sizeof (Elf32_External_Phdr),
11729   sizeof (Elf32_External_Shdr),
11730   sizeof (Elf32_External_Rel),
11731   sizeof (Elf32_External_Rela),
11732   sizeof (Elf32_External_Sym),
11733   sizeof (Elf32_External_Dyn),
11734   sizeof (Elf_External_Note),
11735   4,
11736   1,
11737   32, 2,
11738   ELFCLASS32, EV_CURRENT,
11739   bfd_elf32_write_out_phdrs,
11740   bfd_elf32_write_shdrs_and_ehdr,
11741   bfd_elf32_checksum_contents,
11742   bfd_elf32_write_relocs,
11743   elf32_arm_swap_symbol_in,
11744   elf32_arm_swap_symbol_out,
11745   bfd_elf32_slurp_reloc_table,
11746   bfd_elf32_slurp_symbol_table,
11747   bfd_elf32_swap_dyn_in,
11748   bfd_elf32_swap_dyn_out,
11749   bfd_elf32_swap_reloc_in,
11750   bfd_elf32_swap_reloc_out,
11751   bfd_elf32_swap_reloca_in,
11752   bfd_elf32_swap_reloca_out
11753 };
11754
11755 #define ELF_ARCH                        bfd_arch_arm
11756 #define ELF_MACHINE_CODE                EM_ARM
11757 #ifdef __QNXTARGET__
11758 #define ELF_MAXPAGESIZE                 0x1000
11759 #else
11760 #define ELF_MAXPAGESIZE                 0x8000
11761 #endif
11762 #define ELF_MINPAGESIZE                 0x1000
11763 #define ELF_COMMONPAGESIZE              0x1000
11764
11765 #define bfd_elf32_mkobject                      elf32_arm_mkobject
11766
11767 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
11768 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
11769 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
11770 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
11771 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
11772 #define bfd_elf32_bfd_link_hash_table_free      elf32_arm_hash_table_free
11773 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
11774 #define bfd_elf32_bfd_reloc_name_lookup elf32_arm_reloc_name_lookup
11775 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
11776 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
11777 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
11778 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
11779 #define bfd_elf32_close_and_cleanup             elf32_arm_close_and_cleanup
11780 #define bfd_elf32_bfd_free_cached_info          elf32_arm_bfd_free_cached_info
11781
11782 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
11783 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
11784 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
11785 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
11786 #define elf_backend_check_relocs                elf32_arm_check_relocs
11787 #define elf_backend_relocate_section            elf32_arm_relocate_section
11788 #define elf_backend_write_section               elf32_arm_write_section
11789 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
11790 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
11791 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
11792 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
11793 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
11794 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
11795 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
11796 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
11797 #define elf_backend_object_p                    elf32_arm_object_p
11798 #define elf_backend_section_flags               elf32_arm_section_flags
11799 #define elf_backend_fake_sections               elf32_arm_fake_sections
11800 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
11801 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
11802 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
11803 #define elf_backend_symbol_processing           elf32_arm_symbol_processing
11804 #define elf_backend_size_info                   elf32_arm_size_info
11805 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
11806 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
11807 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
11808 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
11809 #define elf_backend_is_function_type            elf32_arm_is_function_type
11810
11811 #define elf_backend_can_refcount       1
11812 #define elf_backend_can_gc_sections    1
11813 #define elf_backend_plt_readonly       1
11814 #define elf_backend_want_got_plt       1
11815 #define elf_backend_want_plt_sym       0
11816 #define elf_backend_may_use_rel_p      1
11817 #define elf_backend_may_use_rela_p     0
11818 #define elf_backend_default_use_rela_p 0
11819
11820 #define elf_backend_got_header_size     12
11821
11822 #undef  elf_backend_obj_attrs_vendor
11823 #define elf_backend_obj_attrs_vendor            "aeabi"
11824 #undef  elf_backend_obj_attrs_section
11825 #define elf_backend_obj_attrs_section           ".ARM.attributes"
11826 #undef  elf_backend_obj_attrs_arg_type
11827 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
11828 #undef  elf_backend_obj_attrs_section_type
11829 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
11830
11831 #include "elf32-target.h"
11832
11833 /* VxWorks Targets.  */
11834
11835 #undef  TARGET_LITTLE_SYM
11836 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
11837 #undef  TARGET_LITTLE_NAME
11838 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
11839 #undef  TARGET_BIG_SYM
11840 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
11841 #undef  TARGET_BIG_NAME
11842 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
11843
11844 /* Like elf32_arm_link_hash_table_create -- but overrides
11845    appropriately for VxWorks.  */
11846
11847 static struct bfd_link_hash_table *
11848 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
11849 {
11850   struct bfd_link_hash_table *ret;
11851
11852   ret = elf32_arm_link_hash_table_create (abfd);
11853   if (ret)
11854     {
11855       struct elf32_arm_link_hash_table *htab
11856         = (struct elf32_arm_link_hash_table *) ret;
11857       htab->use_rel = 0;
11858       htab->vxworks_p = 1;
11859     }
11860   return ret;
11861 }
11862
11863 static void
11864 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
11865 {
11866   elf32_arm_final_write_processing (abfd, linker);
11867   elf_vxworks_final_write_processing (abfd, linker);
11868 }
11869
11870 #undef  elf32_bed
11871 #define elf32_bed elf32_arm_vxworks_bed
11872
11873 #undef  bfd_elf32_bfd_link_hash_table_create
11874 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
11875 #undef  elf_backend_add_symbol_hook
11876 #define elf_backend_add_symbol_hook             elf_vxworks_add_symbol_hook
11877 #undef  elf_backend_final_write_processing
11878 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
11879 #undef  elf_backend_emit_relocs
11880 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
11881
11882 #undef  elf_backend_may_use_rel_p
11883 #define elf_backend_may_use_rel_p       0
11884 #undef  elf_backend_may_use_rela_p
11885 #define elf_backend_may_use_rela_p      1
11886 #undef  elf_backend_default_use_rela_p
11887 #define elf_backend_default_use_rela_p  1
11888 #undef  elf_backend_want_plt_sym
11889 #define elf_backend_want_plt_sym        1
11890 #undef  ELF_MAXPAGESIZE
11891 #define ELF_MAXPAGESIZE                 0x1000
11892
11893 #include "elf32-target.h"
11894
11895
11896 /* Symbian OS Targets.  */
11897
11898 #undef  TARGET_LITTLE_SYM
11899 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
11900 #undef  TARGET_LITTLE_NAME
11901 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
11902 #undef  TARGET_BIG_SYM
11903 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
11904 #undef  TARGET_BIG_NAME
11905 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
11906
11907 /* Like elf32_arm_link_hash_table_create -- but overrides
11908    appropriately for Symbian OS.  */
11909
11910 static struct bfd_link_hash_table *
11911 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
11912 {
11913   struct bfd_link_hash_table *ret;
11914
11915   ret = elf32_arm_link_hash_table_create (abfd);
11916   if (ret)
11917     {
11918       struct elf32_arm_link_hash_table *htab
11919         = (struct elf32_arm_link_hash_table *)ret;
11920       /* There is no PLT header for Symbian OS.  */
11921       htab->plt_header_size = 0;
11922       /* The PLT entries are each one instruction and one word.  */
11923       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
11924       htab->symbian_p = 1;
11925       /* Symbian uses armv5t or above, so use_blx is always true.  */
11926       htab->use_blx = 1;
11927       htab->root.is_relocatable_executable = 1;
11928     }
11929   return ret;
11930 }
11931
11932 static const struct bfd_elf_special_section
11933 elf32_arm_symbian_special_sections[] =
11934 {
11935   /* In a BPABI executable, the dynamic linking sections do not go in
11936      the loadable read-only segment.  The post-linker may wish to
11937      refer to these sections, but they are not part of the final
11938      program image.  */
11939   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
11940   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
11941   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
11942   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
11943   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
11944   /* These sections do not need to be writable as the SymbianOS
11945      postlinker will arrange things so that no dynamic relocation is
11946      required.  */
11947   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
11948   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
11949   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
11950   { NULL,                             0, 0, 0,                 0 }
11951 };
11952
11953 static void
11954 elf32_arm_symbian_begin_write_processing (bfd *abfd,
11955                                           struct bfd_link_info *link_info)
11956 {
11957   /* BPABI objects are never loaded directly by an OS kernel; they are
11958      processed by a postlinker first, into an OS-specific format.  If
11959      the D_PAGED bit is set on the file, BFD will align segments on
11960      page boundaries, so that an OS can directly map the file.  With
11961      BPABI objects, that just results in wasted space.  In addition,
11962      because we clear the D_PAGED bit, map_sections_to_segments will
11963      recognize that the program headers should not be mapped into any
11964      loadable segment.  */
11965   abfd->flags &= ~D_PAGED;
11966   elf32_arm_begin_write_processing (abfd, link_info);
11967 }
11968
11969 static bfd_boolean
11970 elf32_arm_symbian_modify_segment_map (bfd *abfd,
11971                                       struct bfd_link_info *info)
11972 {
11973   struct elf_segment_map *m;
11974   asection *dynsec;
11975
11976   /* BPABI shared libraries and executables should have a PT_DYNAMIC
11977      segment.  However, because the .dynamic section is not marked
11978      with SEC_LOAD, the generic ELF code will not create such a
11979      segment.  */
11980   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
11981   if (dynsec)
11982     {
11983       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11984         if (m->p_type == PT_DYNAMIC)
11985           break;
11986
11987       if (m == NULL)
11988         {
11989           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
11990           m->next = elf_tdata (abfd)->segment_map;
11991           elf_tdata (abfd)->segment_map = m;
11992         }
11993     }
11994
11995   /* Also call the generic arm routine.  */
11996   return elf32_arm_modify_segment_map (abfd, info);
11997 }
11998
11999 /* Return address for Ith PLT stub in section PLT, for relocation REL
12000    or (bfd_vma) -1 if it should not be included.  */
12001
12002 static bfd_vma
12003 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
12004                                const arelent *rel ATTRIBUTE_UNUSED)
12005 {
12006   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
12007 }
12008
12009
12010 #undef  elf32_bed
12011 #define elf32_bed elf32_arm_symbian_bed
12012
12013 /* The dynamic sections are not allocated on SymbianOS; the postlinker
12014    will process them and then discard them.  */
12015 #undef  ELF_DYNAMIC_SEC_FLAGS
12016 #define ELF_DYNAMIC_SEC_FLAGS \
12017   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
12018
12019 #undef elf_backend_add_symbol_hook
12020 #undef elf_backend_emit_relocs
12021
12022 #undef  bfd_elf32_bfd_link_hash_table_create
12023 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
12024 #undef  elf_backend_special_sections
12025 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
12026 #undef  elf_backend_begin_write_processing
12027 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
12028 #undef  elf_backend_final_write_processing
12029 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
12030
12031 #undef  elf_backend_modify_segment_map
12032 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
12033
12034 /* There is no .got section for BPABI objects, and hence no header.  */
12035 #undef  elf_backend_got_header_size
12036 #define elf_backend_got_header_size 0
12037
12038 /* Similarly, there is no .got.plt section.  */
12039 #undef  elf_backend_want_got_plt
12040 #define elf_backend_want_got_plt 0
12041
12042 #undef  elf_backend_plt_sym_val
12043 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
12044
12045 #undef  elf_backend_may_use_rel_p
12046 #define elf_backend_may_use_rel_p       1
12047 #undef  elf_backend_may_use_rela_p
12048 #define elf_backend_may_use_rela_p      0
12049 #undef  elf_backend_default_use_rela_p
12050 #define elf_backend_default_use_rela_p  0
12051 #undef  elf_backend_want_plt_sym
12052 #define elf_backend_want_plt_sym        0
12053 #undef  ELF_MAXPAGESIZE
12054 #define ELF_MAXPAGESIZE                 0x8000
12055
12056 #include "elf32-target.h"