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