bfd/
[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 const 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 /* The name of the dynamic interpreter.  This is put in the .interp
1879    section.  */
1880 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1881
1882 #ifdef FOUR_WORD_PLT
1883
1884 /* The first entry in a procedure linkage table looks like
1885    this.  It is set up so that any shared library function that is
1886    called before the relocation has been set up calls the dynamic
1887    linker first.  */
1888 static const bfd_vma elf32_arm_plt0_entry [] =
1889   {
1890     0xe52de004,         /* str   lr, [sp, #-4]! */
1891     0xe59fe010,         /* ldr   lr, [pc, #16]  */
1892     0xe08fe00e,         /* add   lr, pc, lr     */
1893     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1894   };
1895
1896 /* Subsequent entries in a procedure linkage table look like
1897    this.  */
1898 static const bfd_vma elf32_arm_plt_entry [] =
1899   {
1900     0xe28fc600,         /* add   ip, pc, #NN    */
1901     0xe28cca00,         /* add   ip, ip, #NN    */
1902     0xe5bcf000,         /* ldr   pc, [ip, #NN]! */
1903     0x00000000,         /* unused               */
1904   };
1905
1906 #else
1907
1908 /* The first entry in a procedure linkage table looks like
1909    this.  It is set up so that any shared library function that is
1910    called before the relocation has been set up calls the dynamic
1911    linker first.  */
1912 static const bfd_vma elf32_arm_plt0_entry [] =
1913   {
1914     0xe52de004,         /* str   lr, [sp, #-4]! */
1915     0xe59fe004,         /* ldr   lr, [pc, #4]   */
1916     0xe08fe00e,         /* add   lr, pc, lr     */
1917     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1918     0x00000000,         /* &GOT[0] - .          */
1919   };
1920
1921 /* Subsequent entries in a procedure linkage table look like
1922    this.  */
1923 static const bfd_vma elf32_arm_plt_entry [] =
1924   {
1925     0xe28fc600,         /* add   ip, pc, #0xNN00000 */
1926     0xe28cca00,         /* add   ip, ip, #0xNN000   */
1927     0xe5bcf000,         /* ldr   pc, [ip, #0xNNN]!  */
1928   };
1929
1930 #endif
1931
1932 /* The format of the first entry in the procedure linkage table
1933    for a VxWorks executable.  */
1934 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
1935   {
1936     0xe52dc008,         /* str    ip,[sp,#-8]!                  */
1937     0xe59fc000,         /* ldr    ip,[pc]                       */
1938     0xe59cf008,         /* ldr    pc,[ip,#8]                    */
1939     0x00000000,         /* .long  _GLOBAL_OFFSET_TABLE_         */
1940   };
1941
1942 /* The format of subsequent entries in a VxWorks executable.  */
1943 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
1944   {
1945     0xe59fc000,         /* ldr    ip,[pc]                       */
1946     0xe59cf000,         /* ldr    pc,[ip]                       */
1947     0x00000000,         /* .long  @got                          */
1948     0xe59fc000,         /* ldr    ip,[pc]                       */
1949     0xea000000,         /* b      _PLT                          */
1950     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1951   };
1952
1953 /* The format of entries in a VxWorks shared library.  */
1954 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
1955   {
1956     0xe59fc000,         /* ldr    ip,[pc]                       */
1957     0xe79cf009,         /* ldr    pc,[ip,r9]                    */
1958     0x00000000,         /* .long  @got                          */
1959     0xe59fc000,         /* ldr    ip,[pc]                       */
1960     0xe599f008,         /* ldr    pc,[r9,#8]                    */
1961     0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)  */
1962   };
1963
1964 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
1965 #define PLT_THUMB_STUB_SIZE 4
1966 static const bfd_vma elf32_arm_plt_thumb_stub [] =
1967   {
1968     0x4778,             /* bx pc */
1969     0x46c0              /* nop   */
1970   };
1971
1972 /* The entries in a PLT when using a DLL-based target with multiple
1973    address spaces.  */
1974 static const bfd_vma elf32_arm_symbian_plt_entry [] = 
1975   {
1976     0xe51ff004,         /* ldr   pc, [pc, #-4] */
1977     0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
1978   };
1979
1980 /* Used to build a map of a section.  This is required for mixed-endian
1981    code/data.  */
1982
1983 typedef struct elf32_elf_section_map
1984 {
1985   bfd_vma vma;
1986   char type;
1987 }
1988 elf32_arm_section_map;
1989
1990 typedef struct _arm_elf_section_data
1991 {
1992   struct bfd_elf_section_data elf;
1993   unsigned int mapcount;
1994   elf32_arm_section_map *map;
1995 }
1996 _arm_elf_section_data;
1997
1998 #define elf32_arm_section_data(sec) \
1999   ((_arm_elf_section_data *) elf_section_data (sec))
2000
2001 /* The size of the thread control block.  */
2002 #define TCB_SIZE        8
2003
2004 #define NUM_KNOWN_ATTRIBUTES 32
2005
2006 typedef struct aeabi_attribute
2007 {
2008   int type;
2009   unsigned int i;
2010   char *s;
2011 } aeabi_attribute;
2012
2013 typedef struct aeabi_attribute_list
2014 {
2015   struct aeabi_attribute_list *next;
2016   int tag;
2017   aeabi_attribute attr;
2018 } aeabi_attribute_list;
2019
2020 struct elf32_arm_obj_tdata
2021 {
2022   struct elf_obj_tdata root;
2023
2024   /* tls_type for each local got entry.  */
2025   char *local_got_tls_type;
2026
2027   aeabi_attribute known_eabi_attributes[NUM_KNOWN_ATTRIBUTES];
2028   aeabi_attribute_list *other_eabi_attributes;
2029 };
2030
2031 #define elf32_arm_tdata(abfd) \
2032   ((struct elf32_arm_obj_tdata *) (abfd)->tdata.any)
2033
2034 #define elf32_arm_local_got_tls_type(abfd) \
2035   (elf32_arm_tdata (abfd)->local_got_tls_type)
2036
2037 static bfd_boolean
2038 elf32_arm_mkobject (bfd *abfd)
2039 {
2040   bfd_size_type amt = sizeof (struct elf32_arm_obj_tdata);
2041   abfd->tdata.any = bfd_zalloc (abfd, amt);
2042   if (abfd->tdata.any == NULL)
2043     return FALSE;
2044   return TRUE;
2045 }
2046
2047 /* The ARM linker needs to keep track of the number of relocs that it
2048    decides to copy in check_relocs for each symbol.  This is so that
2049    it can discard PC relative relocs if it doesn't need them when
2050    linking with -Bsymbolic.  We store the information in a field
2051    extending the regular ELF linker hash table.  */
2052
2053 /* This structure keeps track of the number of relocs we have copied
2054    for a given symbol.  */
2055 struct elf32_arm_relocs_copied
2056   {
2057     /* Next section.  */
2058     struct elf32_arm_relocs_copied * next;
2059     /* A section in dynobj.  */
2060     asection * section;
2061     /* Number of relocs copied in this section.  */
2062     bfd_size_type count;
2063     /* Number of PC-relative relocs copied in this section.  */
2064     bfd_size_type pc_count;
2065   };
2066
2067 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
2068
2069 /* Arm ELF linker hash entry.  */
2070 struct elf32_arm_link_hash_entry
2071   {
2072     struct elf_link_hash_entry root;
2073
2074     /* Number of PC relative relocs copied for this symbol.  */
2075     struct elf32_arm_relocs_copied * relocs_copied;
2076
2077     /* We reference count Thumb references to a PLT entry separately,
2078        so that we can emit the Thumb trampoline only if needed.  */
2079     bfd_signed_vma plt_thumb_refcount;
2080
2081     /* Since PLT entries have variable size if the Thumb prologue is
2082        used, we need to record the index into .got.plt instead of
2083        recomputing it from the PLT offset.  */
2084     bfd_signed_vma plt_got_offset;
2085
2086 #define GOT_UNKNOWN     0
2087 #define GOT_NORMAL      1
2088 #define GOT_TLS_GD      2
2089 #define GOT_TLS_IE      4
2090     unsigned char tls_type;
2091
2092     /* The symbol marking the real symbol location for exported thumb
2093        symbols with Arm stubs.  */
2094     struct elf_link_hash_entry *export_glue;
2095   };
2096
2097 /* Traverse an arm ELF linker hash table.  */
2098 #define elf32_arm_link_hash_traverse(table, func, info)                 \
2099   (elf_link_hash_traverse                                               \
2100    (&(table)->root,                                                     \
2101     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
2102     (info)))
2103
2104 /* Get the ARM elf linker hash table from a link_info structure.  */
2105 #define elf32_arm_hash_table(info) \
2106   ((struct elf32_arm_link_hash_table *) ((info)->hash))
2107
2108 /* ARM ELF linker hash table.  */
2109 struct elf32_arm_link_hash_table
2110   {
2111     /* The main hash table.  */
2112     struct elf_link_hash_table root;
2113
2114     /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
2115     bfd_size_type thumb_glue_size;
2116
2117     /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
2118     bfd_size_type arm_glue_size;
2119
2120     /* An arbitrary input BFD chosen to hold the glue sections.  */
2121     bfd * bfd_of_glue_owner;
2122
2123     /* Nonzero to output a BE8 image.  */
2124     int byteswap_code;
2125
2126     /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
2127        Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
2128     int target1_is_rel;
2129
2130     /* The relocation to use for R_ARM_TARGET2 relocations.  */
2131     int target2_reloc;
2132
2133     /* Nonzero to fix BX instructions for ARMv4 targets.  */
2134     int fix_v4bx;
2135
2136     /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
2137     int use_blx;
2138
2139     /* The number of bytes in the initial entry in the PLT.  */
2140     bfd_size_type plt_header_size;
2141
2142     /* The number of bytes in the subsequent PLT etries.  */
2143     bfd_size_type plt_entry_size;
2144
2145     /* True if the target system is VxWorks.  */
2146     int vxworks_p;
2147
2148     /* True if the target system is Symbian OS.  */
2149     int symbian_p;
2150
2151     /* True if the target uses REL relocations.  */
2152     int use_rel;
2153
2154     /* Short-cuts to get to dynamic linker sections.  */
2155     asection *sgot;
2156     asection *sgotplt;
2157     asection *srelgot;
2158     asection *splt;
2159     asection *srelplt;
2160     asection *sdynbss;
2161     asection *srelbss;
2162
2163     /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2164     asection *srelplt2;
2165
2166     /* Data for R_ARM_TLS_LDM32 relocations.  */
2167     union {
2168       bfd_signed_vma refcount;
2169       bfd_vma offset;
2170     } tls_ldm_got;
2171     
2172     /* Small local sym to section mapping cache.  */
2173     struct sym_sec_cache sym_sec;
2174
2175     /* For convenience in allocate_dynrelocs.  */
2176     bfd * obfd;
2177   };
2178
2179 /* Create an entry in an ARM ELF linker hash table.  */
2180
2181 static struct bfd_hash_entry *
2182 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
2183                              struct bfd_hash_table * table,
2184                              const char * string)
2185 {
2186   struct elf32_arm_link_hash_entry * ret =
2187     (struct elf32_arm_link_hash_entry *) entry;
2188
2189   /* Allocate the structure if it has not already been allocated by a
2190      subclass.  */
2191   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
2192     ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
2193   if (ret == NULL)
2194     return (struct bfd_hash_entry *) ret;
2195
2196   /* Call the allocation method of the superclass.  */
2197   ret = ((struct elf32_arm_link_hash_entry *)
2198          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2199                                      table, string));
2200   if (ret != NULL)
2201     {
2202       ret->relocs_copied = NULL;
2203       ret->tls_type = GOT_UNKNOWN;
2204       ret->plt_thumb_refcount = 0;
2205       ret->plt_got_offset = -1;
2206       ret->export_glue = NULL;
2207     }
2208
2209   return (struct bfd_hash_entry *) ret;
2210 }
2211
2212 /* Return true if NAME is the name of the relocation section associated
2213    with S.  */
2214
2215 static bfd_boolean
2216 reloc_section_p (struct elf32_arm_link_hash_table *htab,
2217                  const char *name, asection *s)
2218 {
2219   if (htab->use_rel)
2220     return strncmp (name, ".rel", 4) == 0 && strcmp (s->name, name + 4) == 0;
2221   else
2222     return strncmp (name, ".rela", 5) == 0 && strcmp (s->name, name + 5) == 0;
2223 }
2224
2225 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
2226    shortcuts to them in our hash table.  */
2227
2228 static bfd_boolean
2229 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2230 {
2231   struct elf32_arm_link_hash_table *htab;
2232
2233   htab = elf32_arm_hash_table (info);
2234   /* BPABI objects never have a GOT, or associated sections.  */
2235   if (htab->symbian_p)
2236     return TRUE;
2237
2238   if (! _bfd_elf_create_got_section (dynobj, info))
2239     return FALSE;
2240
2241   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2242   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2243   if (!htab->sgot || !htab->sgotplt)
2244     abort ();
2245
2246   htab->srelgot = bfd_make_section_with_flags (dynobj,
2247                                                RELOC_SECTION (htab, ".got"),
2248                                                (SEC_ALLOC | SEC_LOAD
2249                                                 | SEC_HAS_CONTENTS
2250                                                 | SEC_IN_MEMORY
2251                                                 | SEC_LINKER_CREATED
2252                                                 | SEC_READONLY));
2253   if (htab->srelgot == NULL
2254       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
2255     return FALSE;
2256   return TRUE;
2257 }
2258
2259 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
2260    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
2261    hash table.  */
2262
2263 static bfd_boolean
2264 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2265 {
2266   struct elf32_arm_link_hash_table *htab;
2267
2268   htab = elf32_arm_hash_table (info);
2269   if (!htab->sgot && !create_got_section (dynobj, info))
2270     return FALSE;
2271
2272   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2273     return FALSE;
2274
2275   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2276   htab->srelplt = bfd_get_section_by_name (dynobj,
2277                                            RELOC_SECTION (htab, ".plt"));
2278   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2279   if (!info->shared)
2280     htab->srelbss = bfd_get_section_by_name (dynobj,
2281                                              RELOC_SECTION (htab, ".bss"));
2282
2283   if (htab->vxworks_p)
2284     {
2285       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
2286         return FALSE;
2287
2288       if (info->shared)
2289         {
2290           htab->plt_header_size = 0;
2291           htab->plt_entry_size
2292             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
2293         }
2294       else
2295         {
2296           htab->plt_header_size
2297             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
2298           htab->plt_entry_size
2299             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
2300         }
2301     }
2302
2303   if (!htab->splt 
2304       || !htab->srelplt
2305       || !htab->sdynbss
2306       || (!info->shared && !htab->srelbss))
2307     abort ();
2308
2309   return TRUE;
2310 }
2311
2312 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2313
2314 static void
2315 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
2316                                 struct elf_link_hash_entry *dir,
2317                                 struct elf_link_hash_entry *ind)
2318 {
2319   struct elf32_arm_link_hash_entry *edir, *eind;
2320
2321   edir = (struct elf32_arm_link_hash_entry *) dir;
2322   eind = (struct elf32_arm_link_hash_entry *) ind;
2323
2324   if (eind->relocs_copied != NULL)
2325     {
2326       if (edir->relocs_copied != NULL)
2327         {
2328           struct elf32_arm_relocs_copied **pp;
2329           struct elf32_arm_relocs_copied *p;
2330
2331           /* Add reloc counts against the indirect sym to the direct sym
2332              list.  Merge any entries against the same section.  */
2333           for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
2334             {
2335               struct elf32_arm_relocs_copied *q;
2336
2337               for (q = edir->relocs_copied; q != NULL; q = q->next)
2338                 if (q->section == p->section)
2339                   {
2340                     q->pc_count += p->pc_count;
2341                     q->count += p->count;
2342                     *pp = p->next;
2343                     break;
2344                   }
2345               if (q == NULL)
2346                 pp = &p->next;
2347             }
2348           *pp = edir->relocs_copied;
2349         }
2350
2351       edir->relocs_copied = eind->relocs_copied;
2352       eind->relocs_copied = NULL;
2353     }
2354
2355   /* Copy over PLT info.  */
2356   edir->plt_thumb_refcount += eind->plt_thumb_refcount;
2357   eind->plt_thumb_refcount = 0;
2358
2359   if (ind->root.type == bfd_link_hash_indirect
2360       && dir->got.refcount <= 0)
2361     {
2362       edir->tls_type = eind->tls_type;
2363       eind->tls_type = GOT_UNKNOWN;
2364     }
2365
2366   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2367 }
2368
2369 /* Create an ARM elf linker hash table.  */
2370
2371 static struct bfd_link_hash_table *
2372 elf32_arm_link_hash_table_create (bfd *abfd)
2373 {
2374   struct elf32_arm_link_hash_table *ret;
2375   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
2376
2377   ret = bfd_malloc (amt);
2378   if (ret == NULL)
2379     return NULL;
2380
2381   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
2382                                       elf32_arm_link_hash_newfunc,
2383                                       sizeof (struct elf32_arm_link_hash_entry)))
2384     {
2385       free (ret);
2386       return NULL;
2387     }
2388
2389   ret->sgot = NULL;
2390   ret->sgotplt = NULL;
2391   ret->srelgot = NULL;
2392   ret->splt = NULL;
2393   ret->srelplt = NULL;
2394   ret->sdynbss = NULL;
2395   ret->srelbss = NULL;
2396   ret->srelplt2 = NULL;
2397   ret->thumb_glue_size = 0;
2398   ret->arm_glue_size = 0;
2399   ret->bfd_of_glue_owner = NULL;
2400   ret->byteswap_code = 0;
2401   ret->target1_is_rel = 0;
2402   ret->target2_reloc = R_ARM_NONE;
2403 #ifdef FOUR_WORD_PLT
2404   ret->plt_header_size = 16;
2405   ret->plt_entry_size = 16;
2406 #else
2407   ret->plt_header_size = 20;
2408   ret->plt_entry_size = 12;
2409 #endif
2410   ret->fix_v4bx = 0;
2411   ret->use_blx = 0;
2412   ret->vxworks_p = 0;
2413   ret->symbian_p = 0;
2414   ret->use_rel = 1;
2415   ret->sym_sec.abfd = NULL;
2416   ret->obfd = abfd;
2417   ret->tls_ldm_got.refcount = 0;
2418
2419   return &ret->root.root;
2420 }
2421
2422 /* Locate the Thumb encoded calling stub for NAME.  */
2423
2424 static struct elf_link_hash_entry *
2425 find_thumb_glue (struct bfd_link_info *link_info,
2426                  const char *name,
2427                  bfd *input_bfd)
2428 {
2429   char *tmp_name;
2430   struct elf_link_hash_entry *hash;
2431   struct elf32_arm_link_hash_table *hash_table;
2432
2433   /* We need a pointer to the armelf specific hash table.  */
2434   hash_table = elf32_arm_hash_table (link_info);
2435
2436   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2437                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
2438
2439   BFD_ASSERT (tmp_name);
2440
2441   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
2442
2443   hash = elf_link_hash_lookup
2444     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2445
2446   if (hash == NULL)
2447     /* xgettext:c-format */
2448     (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
2449                            input_bfd, tmp_name, name);
2450
2451   free (tmp_name);
2452
2453   return hash;
2454 }
2455
2456 /* Locate the ARM encoded calling stub for NAME.  */
2457
2458 static struct elf_link_hash_entry *
2459 find_arm_glue (struct bfd_link_info *link_info,
2460                const char *name,
2461                bfd *input_bfd)
2462 {
2463   char *tmp_name;
2464   struct elf_link_hash_entry *myh;
2465   struct elf32_arm_link_hash_table *hash_table;
2466
2467   /* We need a pointer to the elfarm specific hash table.  */
2468   hash_table = elf32_arm_hash_table (link_info);
2469
2470   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2471                          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
2472
2473   BFD_ASSERT (tmp_name);
2474
2475   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
2476
2477   myh = elf_link_hash_lookup
2478     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2479
2480   if (myh == NULL)
2481     /* xgettext:c-format */
2482     (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
2483                            input_bfd, tmp_name, name);
2484
2485   free (tmp_name);
2486
2487   return myh;
2488 }
2489
2490 /* ARM->Thumb glue (static images):
2491
2492    .arm
2493    __func_from_arm:
2494    ldr r12, __func_addr
2495    bx  r12
2496    __func_addr:
2497    .word func    @ behave as if you saw a ARM_32 reloc.  
2498
2499    (relocatable images)
2500    .arm
2501    __func_from_arm:
2502    ldr r12, __func_offset
2503    add r12, r12, pc
2504    bx  r12
2505    __func_offset:
2506    .word func - .
2507    */
2508
2509 #define ARM2THUMB_STATIC_GLUE_SIZE 12
2510 static const insn32 a2t1_ldr_insn = 0xe59fc000;
2511 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
2512 static const insn32 a2t3_func_addr_insn = 0x00000001;
2513
2514 #define ARM2THUMB_PIC_GLUE_SIZE 16
2515 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
2516 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
2517 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
2518
2519 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
2520
2521    .thumb                               .thumb
2522    .align 2                             .align 2
2523    __func_from_thumb:              __func_from_thumb:
2524    bx pc                                push {r6, lr}
2525    nop                                  ldr  r6, __func_addr
2526    .arm                                         mov  lr, pc
2527    __func_change_to_arm:                        bx   r6
2528    b func                       .arm
2529    __func_back_to_thumb:
2530    ldmia r13! {r6, lr}
2531    bx    lr
2532    __func_addr:
2533    .word        func  */
2534
2535 #define THUMB2ARM_GLUE_SIZE 8
2536 static const insn16 t2a1_bx_pc_insn = 0x4778;
2537 static const insn16 t2a2_noop_insn = 0x46c0;
2538 static const insn32 t2a3_b_insn = 0xea000000;
2539
2540 #ifndef ELFARM_NABI_C_INCLUDED
2541 bfd_boolean
2542 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
2543 {
2544   asection * s;
2545   bfd_byte * foo;
2546   struct elf32_arm_link_hash_table * globals;
2547
2548   globals = elf32_arm_hash_table (info);
2549
2550   BFD_ASSERT (globals != NULL);
2551
2552   if (globals->arm_glue_size != 0)
2553     {
2554       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2555
2556       s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2557                                    ARM2THUMB_GLUE_SECTION_NAME);
2558
2559       BFD_ASSERT (s != NULL);
2560
2561       foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
2562
2563       s->size = globals->arm_glue_size;
2564       s->contents = foo;
2565     }
2566
2567   if (globals->thumb_glue_size != 0)
2568     {
2569       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2570
2571       s = bfd_get_section_by_name
2572         (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
2573
2574       BFD_ASSERT (s != NULL);
2575
2576       foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
2577
2578       s->size = globals->thumb_glue_size;
2579       s->contents = foo;
2580     }
2581
2582   return TRUE;
2583 }
2584
2585 /* Allocate space and symbols for calling a Thumb function from Arm mode.
2586    returns the symbol identifying teh stub.  */
2587 static struct elf_link_hash_entry *
2588 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
2589                           struct elf_link_hash_entry * h)
2590 {
2591   const char * name = h->root.root.string;
2592   asection * s;
2593   char * tmp_name;
2594   struct elf_link_hash_entry * myh;
2595   struct bfd_link_hash_entry * bh;
2596   struct elf32_arm_link_hash_table * globals;
2597   bfd_vma val;
2598
2599   globals = elf32_arm_hash_table (link_info);
2600
2601   BFD_ASSERT (globals != NULL);
2602   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2603
2604   s = bfd_get_section_by_name
2605     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
2606
2607   BFD_ASSERT (s != NULL);
2608
2609   tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
2610
2611   BFD_ASSERT (tmp_name);
2612
2613   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
2614
2615   myh = elf_link_hash_lookup
2616     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
2617
2618   if (myh != NULL)
2619     {
2620       /* We've already seen this guy.  */
2621       free (tmp_name);
2622       return myh;
2623     }
2624
2625   /* The only trick here is using hash_table->arm_glue_size as the value.
2626      Even though the section isn't allocated yet, this is where we will be
2627      putting it.  */
2628   bh = NULL;
2629   val = globals->arm_glue_size + 1;
2630   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
2631                                     tmp_name, BSF_GLOBAL, s, val,
2632                                     NULL, TRUE, FALSE, &bh);
2633
2634   myh = (struct elf_link_hash_entry *) bh;
2635   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
2636   myh->forced_local = 1;
2637
2638   free (tmp_name);
2639
2640   if ((link_info->shared || globals->root.is_relocatable_executable))
2641     globals->arm_glue_size += ARM2THUMB_PIC_GLUE_SIZE;
2642   else
2643     globals->arm_glue_size += ARM2THUMB_STATIC_GLUE_SIZE;
2644
2645   return myh;
2646 }
2647
2648 static void
2649 record_thumb_to_arm_glue (struct bfd_link_info *link_info,
2650                           struct elf_link_hash_entry *h)
2651 {
2652   const char *name = h->root.root.string;
2653   asection *s;
2654   char *tmp_name;
2655   struct elf_link_hash_entry *myh;
2656   struct bfd_link_hash_entry *bh;
2657   struct elf32_arm_link_hash_table *hash_table;
2658   bfd_vma val;
2659
2660   hash_table = elf32_arm_hash_table (link_info);
2661
2662   BFD_ASSERT (hash_table != NULL);
2663   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
2664
2665   s = bfd_get_section_by_name
2666     (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
2667
2668   BFD_ASSERT (s != NULL);
2669
2670   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2671                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
2672
2673   BFD_ASSERT (tmp_name);
2674
2675   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
2676
2677   myh = elf_link_hash_lookup
2678     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2679
2680   if (myh != NULL)
2681     {
2682       /* We've already seen this guy.  */
2683       free (tmp_name);
2684       return;
2685     }
2686
2687   bh = NULL;
2688   val = hash_table->thumb_glue_size + 1;
2689   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
2690                                     tmp_name, BSF_GLOBAL, s, val,
2691                                     NULL, TRUE, FALSE, &bh);
2692
2693   /* If we mark it 'Thumb', the disassembler will do a better job.  */
2694   myh = (struct elf_link_hash_entry *) bh;
2695   myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
2696   myh->forced_local = 1;
2697
2698   free (tmp_name);
2699
2700 #define CHANGE_TO_ARM "__%s_change_to_arm"
2701 #define BACK_FROM_ARM "__%s_back_from_arm"
2702
2703   /* Allocate another symbol to mark where we switch to Arm mode.  */
2704   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2705                          + strlen (CHANGE_TO_ARM) + 1);
2706
2707   BFD_ASSERT (tmp_name);
2708
2709   sprintf (tmp_name, CHANGE_TO_ARM, name);
2710
2711   bh = NULL;
2712   val = hash_table->thumb_glue_size + 4,
2713   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
2714                                     tmp_name, BSF_LOCAL, s, val,
2715                                     NULL, TRUE, FALSE, &bh);
2716
2717   free (tmp_name);
2718
2719   hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
2720
2721   return;
2722 }
2723
2724 /* Add the glue sections to ABFD.  This function is called from the
2725    linker scripts in ld/emultempl/{armelf}.em.  */
2726
2727 bfd_boolean
2728 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
2729                                         struct bfd_link_info *info)
2730 {
2731   flagword flags;
2732   asection *sec;
2733
2734   /* If we are only performing a partial
2735      link do not bother adding the glue.  */
2736   if (info->relocatable)
2737     return TRUE;
2738
2739   sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
2740
2741   if (sec == NULL)
2742     {
2743       /* Note: we do not include the flag SEC_LINKER_CREATED, as this
2744          will prevent elf_link_input_bfd() from processing the contents
2745          of this section.  */
2746       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
2747
2748       sec = bfd_make_section_with_flags (abfd,
2749                                          ARM2THUMB_GLUE_SECTION_NAME,
2750                                          flags);
2751
2752       if (sec == NULL
2753           || !bfd_set_section_alignment (abfd, sec, 2))
2754         return FALSE;
2755
2756       /* Set the gc mark to prevent the section from being removed by garbage
2757          collection, despite the fact that no relocs refer to this section.  */
2758       sec->gc_mark = 1;
2759     }
2760
2761   sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
2762
2763   if (sec == NULL)
2764     {
2765       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2766         | SEC_CODE | SEC_READONLY;
2767
2768       sec = bfd_make_section_with_flags (abfd,
2769                                          THUMB2ARM_GLUE_SECTION_NAME,
2770                                          flags);
2771
2772       if (sec == NULL
2773           || !bfd_set_section_alignment (abfd, sec, 2))
2774         return FALSE;
2775
2776       sec->gc_mark = 1;
2777     }
2778
2779   return TRUE;
2780 }
2781
2782 /* Select a BFD to be used to hold the sections used by the glue code.
2783    This function is called from the linker scripts in ld/emultempl/
2784    {armelf/pe}.em  */
2785
2786 bfd_boolean
2787 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
2788 {
2789   struct elf32_arm_link_hash_table *globals;
2790
2791   /* If we are only performing a partial link
2792      do not bother getting a bfd to hold the glue.  */
2793   if (info->relocatable)
2794     return TRUE;
2795
2796   /* Make sure we don't attach the glue sections to a dynamic object.  */
2797   BFD_ASSERT (!(abfd->flags & DYNAMIC));
2798
2799   globals = elf32_arm_hash_table (info);
2800
2801   BFD_ASSERT (globals != NULL);
2802
2803   if (globals->bfd_of_glue_owner != NULL)
2804     return TRUE;
2805
2806   /* Save the bfd for later use.  */
2807   globals->bfd_of_glue_owner = abfd;
2808
2809   return TRUE;
2810 }
2811
2812 static void check_use_blx(struct elf32_arm_link_hash_table *globals)
2813 {
2814   if (elf32_arm_get_eabi_attr_int (globals->obfd, Tag_CPU_arch) > 2)
2815     globals->use_blx = 1;
2816 }
2817
2818 bfd_boolean
2819 bfd_elf32_arm_process_before_allocation (bfd *abfd,
2820                                          struct bfd_link_info *link_info,
2821                                          int byteswap_code)
2822 {
2823   Elf_Internal_Shdr *symtab_hdr;
2824   Elf_Internal_Rela *internal_relocs = NULL;
2825   Elf_Internal_Rela *irel, *irelend;
2826   bfd_byte *contents = NULL;
2827
2828   asection *sec;
2829   struct elf32_arm_link_hash_table *globals;
2830
2831   /* If we are only performing a partial link do not bother
2832      to construct any glue.  */
2833   if (link_info->relocatable)
2834     return TRUE;
2835
2836   /* Here we have a bfd that is to be included on the link.  We have a hook
2837      to do reloc rummaging, before section sizes are nailed down.  */
2838   globals = elf32_arm_hash_table (link_info);
2839   check_use_blx (globals);
2840
2841   BFD_ASSERT (globals != NULL);
2842   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2843
2844   if (byteswap_code && !bfd_big_endian (abfd))
2845     {
2846       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
2847                           abfd);
2848       return FALSE;
2849     }
2850   globals->byteswap_code = byteswap_code;
2851
2852   /* Rummage around all the relocs and map the glue vectors.  */
2853   sec = abfd->sections;
2854
2855   if (sec == NULL)
2856     return TRUE;
2857
2858   for (; sec != NULL; sec = sec->next)
2859     {
2860       if (sec->reloc_count == 0)
2861         continue;
2862
2863       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2864
2865       /* Load the relocs.  */
2866       internal_relocs
2867         = _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
2868                                      (Elf_Internal_Rela *) NULL, FALSE);
2869
2870       if (internal_relocs == NULL)
2871         goto error_return;
2872
2873       irelend = internal_relocs + sec->reloc_count;
2874       for (irel = internal_relocs; irel < irelend; irel++)
2875         {
2876           long r_type;
2877           unsigned long r_index;
2878
2879           struct elf_link_hash_entry *h;
2880
2881           r_type = ELF32_R_TYPE (irel->r_info);
2882           r_index = ELF32_R_SYM (irel->r_info);
2883
2884           /* These are the only relocation types we care about.  */
2885           if (   r_type != R_ARM_PC24
2886               && r_type != R_ARM_PLT32
2887               && r_type != R_ARM_CALL
2888               && r_type != R_ARM_JUMP24
2889               && r_type != R_ARM_THM_CALL)
2890             continue;
2891
2892           /* Get the section contents if we haven't done so already.  */
2893           if (contents == NULL)
2894             {
2895               /* Get cached copy if it exists.  */
2896               if (elf_section_data (sec)->this_hdr.contents != NULL)
2897                 contents = elf_section_data (sec)->this_hdr.contents;
2898               else
2899                 {
2900                   /* Go get them off disk.  */
2901                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2902                     goto error_return;
2903                 }
2904             }
2905
2906           /* If the relocation is not against a symbol it cannot concern us.  */
2907           h = NULL;
2908
2909           /* We don't care about local symbols.  */
2910           if (r_index < symtab_hdr->sh_info)
2911             continue;
2912
2913           /* This is an external symbol.  */
2914           r_index -= symtab_hdr->sh_info;
2915           h = (struct elf_link_hash_entry *)
2916             elf_sym_hashes (abfd)[r_index];
2917
2918           /* If the relocation is against a static symbol it must be within
2919              the current section and so cannot be a cross ARM/Thumb relocation.  */
2920           if (h == NULL)
2921             continue;
2922
2923           /* If the call will go through a PLT entry then we do not need
2924              glue.  */
2925           if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
2926             continue;
2927
2928           switch (r_type)
2929             {
2930             case R_ARM_PC24:
2931             case R_ARM_PLT32:
2932             case R_ARM_CALL:
2933             case R_ARM_JUMP24:
2934               /* This one is a call from arm code.  We need to look up
2935                  the target of the call.  If it is a thumb target, we
2936                  insert glue.  */
2937               if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC
2938                   && !(r_type == R_ARM_CALL && globals->use_blx))
2939                 record_arm_to_thumb_glue (link_info, h);
2940               break;
2941
2942             case R_ARM_THM_CALL:
2943               /* This one is a call from thumb code.  We look
2944                  up the target of the call.  If it is not a thumb
2945                  target, we insert glue.  */
2946               if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC && !globals->use_blx)
2947                 record_thumb_to_arm_glue (link_info, h);
2948               break;
2949
2950             default:
2951               abort ();
2952             }
2953         }
2954
2955       if (contents != NULL
2956           && elf_section_data (sec)->this_hdr.contents != contents)
2957         free (contents);
2958       contents = NULL;
2959
2960       if (internal_relocs != NULL
2961           && elf_section_data (sec)->relocs != internal_relocs)
2962         free (internal_relocs);
2963       internal_relocs = NULL;
2964     }
2965
2966   return TRUE;
2967
2968 error_return:
2969   if (contents != NULL
2970       && elf_section_data (sec)->this_hdr.contents != contents)
2971     free (contents);
2972   if (internal_relocs != NULL
2973       && elf_section_data (sec)->relocs != internal_relocs)
2974     free (internal_relocs);
2975
2976   return FALSE;
2977 }
2978 #endif
2979
2980
2981 /* Set target relocation values needed during linking.  */
2982
2983 void
2984 bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
2985                                  int target1_is_rel,
2986                                  char * target2_type,
2987                                  int fix_v4bx,
2988                                  int use_blx)
2989 {
2990   struct elf32_arm_link_hash_table *globals;
2991
2992   globals = elf32_arm_hash_table (link_info);
2993
2994   globals->target1_is_rel = target1_is_rel;
2995   if (strcmp (target2_type, "rel") == 0)
2996     globals->target2_reloc = R_ARM_REL32;
2997   else if (strcmp (target2_type, "abs") == 0)
2998     globals->target2_reloc = R_ARM_ABS32;
2999   else if (strcmp (target2_type, "got-rel") == 0)
3000     globals->target2_reloc = R_ARM_GOT_PREL;
3001   else
3002     {
3003       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
3004                           target2_type);
3005     }
3006   globals->fix_v4bx = fix_v4bx;
3007   globals->use_blx |= use_blx;
3008 }
3009
3010 /* The thumb form of a long branch is a bit finicky, because the offset
3011    encoding is split over two fields, each in it's own instruction. They
3012    can occur in any order. So given a thumb form of long branch, and an
3013    offset, insert the offset into the thumb branch and return finished
3014    instruction.
3015
3016    It takes two thumb instructions to encode the target address. Each has
3017    11 bits to invest. The upper 11 bits are stored in one (identified by
3018    H-0.. see below), the lower 11 bits are stored in the other (identified
3019    by H-1).
3020
3021    Combine together and shifted left by 1 (it's a half word address) and
3022    there you have it.
3023
3024    Op: 1111 = F,
3025    H-0, upper address-0 = 000
3026    Op: 1111 = F,
3027    H-1, lower address-0 = 800
3028
3029    They can be ordered either way, but the arm tools I've seen always put
3030    the lower one first. It probably doesn't matter. krk@cygnus.com
3031
3032    XXX:  Actually the order does matter.  The second instruction (H-1)
3033    moves the computed address into the PC, so it must be the second one
3034    in the sequence.  The problem, however is that whilst little endian code
3035    stores the instructions in HI then LOW order, big endian code does the
3036    reverse.  nickc@cygnus.com.  */
3037
3038 #define LOW_HI_ORDER      0xF800F000
3039 #define HI_LOW_ORDER      0xF000F800
3040
3041 static insn32
3042 insert_thumb_branch (insn32 br_insn, int rel_off)
3043 {
3044   unsigned int low_bits;
3045   unsigned int high_bits;
3046
3047   BFD_ASSERT ((rel_off & 1) != 1);
3048
3049   rel_off >>= 1;                                /* Half word aligned address.  */
3050   low_bits = rel_off & 0x000007FF;              /* The bottom 11 bits.  */
3051   high_bits = (rel_off >> 11) & 0x000007FF;     /* The top 11 bits.  */
3052
3053   if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
3054     br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
3055   else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
3056     br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
3057   else
3058     /* FIXME: abort is probably not the right call. krk@cygnus.com  */
3059     abort ();   /* Error - not a valid branch instruction form.  */
3060
3061   return br_insn;
3062 }
3063
3064
3065 /* Store an Arm insn into an output section not processed by
3066    elf32_arm_write_section.  */
3067
3068 static void
3069 put_arm_insn (struct elf32_arm_link_hash_table *htab,
3070              bfd * output_bfd, bfd_vma val, void * ptr)
3071 {
3072     if (htab->byteswap_code != bfd_little_endian (output_bfd))
3073       bfd_putl32 (val, ptr);
3074     else
3075       bfd_putb32 (val, ptr);
3076 }
3077
3078
3079 /* Store a 16-bit Thumb insn into an output section not processed by
3080    elf32_arm_write_section.  */
3081
3082 static void
3083 put_thumb_insn (struct elf32_arm_link_hash_table *htab,
3084                bfd * output_bfd, bfd_vma val, void * ptr)
3085 {
3086     if (htab->byteswap_code != bfd_little_endian (output_bfd))
3087       bfd_putl16 (val, ptr);
3088     else
3089       bfd_putb16 (val, ptr);
3090 }
3091
3092
3093 /* Thumb code calling an ARM function.  */
3094
3095 static int
3096 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
3097                          const char *           name,
3098                          bfd *                  input_bfd,
3099                          bfd *                  output_bfd,
3100                          asection *             input_section,
3101                          bfd_byte *             hit_data,
3102                          asection *             sym_sec,
3103                          bfd_vma                offset,
3104                          bfd_signed_vma         addend,
3105                          bfd_vma                val)
3106 {
3107   asection * s = 0;
3108   bfd_vma my_offset;
3109   unsigned long int tmp;
3110   long int ret_offset;
3111   struct elf_link_hash_entry * myh;
3112   struct elf32_arm_link_hash_table * globals;
3113
3114   myh = find_thumb_glue (info, name, input_bfd);
3115   if (myh == NULL)
3116     return FALSE;
3117
3118   globals = elf32_arm_hash_table (info);
3119
3120   BFD_ASSERT (globals != NULL);
3121   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
3122
3123   my_offset = myh->root.u.def.value;
3124
3125   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
3126                                THUMB2ARM_GLUE_SECTION_NAME);
3127
3128   BFD_ASSERT (s != NULL);
3129   BFD_ASSERT (s->contents != NULL);
3130   BFD_ASSERT (s->output_section != NULL);
3131
3132   if ((my_offset & 0x01) == 0x01)
3133     {
3134       if (sym_sec != NULL
3135           && sym_sec->owner != NULL
3136           && !INTERWORK_FLAG (sym_sec->owner))
3137         {
3138           (*_bfd_error_handler)
3139             (_("%B(%s): warning: interworking not enabled.\n"
3140                "  first occurrence: %B: thumb call to arm"),
3141              sym_sec->owner, input_bfd, name);
3142
3143           return FALSE;
3144         }
3145
3146       --my_offset;
3147       myh->root.u.def.value = my_offset;
3148
3149       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
3150                       s->contents + my_offset);
3151
3152       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
3153                       s->contents + my_offset + 2);
3154
3155       ret_offset =
3156         /* Address of destination of the stub.  */
3157         ((bfd_signed_vma) val)
3158         - ((bfd_signed_vma)
3159            /* Offset from the start of the current section
3160               to the start of the stubs.  */
3161            (s->output_offset
3162             /* Offset of the start of this stub from the start of the stubs.  */
3163             + my_offset
3164             /* Address of the start of the current section.  */
3165             + s->output_section->vma)
3166            /* The branch instruction is 4 bytes into the stub.  */
3167            + 4
3168            /* ARM branches work from the pc of the instruction + 8.  */
3169            + 8);
3170
3171       put_arm_insn (globals, output_bfd,
3172                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
3173                     s->contents + my_offset + 4);
3174     }
3175
3176   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
3177
3178   /* Now go back and fix up the original BL insn to point to here.  */
3179   ret_offset =
3180     /* Address of where the stub is located.  */
3181     (s->output_section->vma + s->output_offset + my_offset)
3182      /* Address of where the BL is located.  */
3183     - (input_section->output_section->vma + input_section->output_offset
3184        + offset)
3185     /* Addend in the relocation.  */
3186     - addend
3187     /* Biassing for PC-relative addressing.  */
3188     - 8;
3189
3190   tmp = bfd_get_32 (input_bfd, hit_data
3191                     - input_section->vma);
3192
3193   bfd_put_32 (output_bfd,
3194               (bfd_vma) insert_thumb_branch (tmp, ret_offset),
3195               hit_data - input_section->vma);
3196
3197   return TRUE;
3198 }
3199
3200 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
3201
3202 static struct elf_link_hash_entry *
3203 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
3204                              const char *           name,
3205                              bfd *                  input_bfd,
3206                              bfd *                  output_bfd,
3207                              asection *             sym_sec,
3208                              bfd_vma                val,
3209                              asection               *s)
3210 {
3211   bfd_vma my_offset;
3212   long int ret_offset;
3213   struct elf_link_hash_entry * myh;
3214   struct elf32_arm_link_hash_table * globals;
3215
3216   myh = find_arm_glue (info, name, input_bfd);
3217   if (myh == NULL)
3218     return NULL;
3219
3220   globals = elf32_arm_hash_table (info);
3221
3222   BFD_ASSERT (globals != NULL);
3223   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
3224
3225   my_offset = myh->root.u.def.value;
3226
3227   if ((my_offset & 0x01) == 0x01)
3228     {
3229       if (sym_sec != NULL
3230           && sym_sec->owner != NULL
3231           && !INTERWORK_FLAG (sym_sec->owner))
3232         {
3233           (*_bfd_error_handler)
3234             (_("%B(%s): warning: interworking not enabled.\n"
3235                "  first occurrence: %B: arm call to thumb"),
3236              sym_sec->owner, input_bfd, name);
3237         }
3238
3239       --my_offset;
3240       myh->root.u.def.value = my_offset;
3241
3242       if ((info->shared || globals->root.is_relocatable_executable))
3243         {
3244           /* For relocatable objects we can't use absolute addresses,
3245              so construct the address from a relative offset.  */
3246           /* TODO: If the offset is small it's probably worth
3247              constructing the address with adds.  */
3248           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
3249                         s->contents + my_offset);
3250           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
3251                         s->contents + my_offset + 4);
3252           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
3253                         s->contents + my_offset + 8);
3254           /* Adjust the offset by 4 for the position of the add,
3255              and 8 for the pipeline offset.  */
3256           ret_offset = (val - (s->output_offset
3257                                + s->output_section->vma
3258                                + my_offset + 12))
3259                        | 1;
3260           bfd_put_32 (output_bfd, ret_offset,
3261                       s->contents + my_offset + 12);
3262         }
3263       else
3264         {
3265           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
3266                         s->contents + my_offset);
3267
3268           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
3269                         s->contents + my_offset + 4);
3270
3271           /* It's a thumb address.  Add the low order bit.  */
3272           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
3273                       s->contents + my_offset + 8);
3274         }
3275     }
3276
3277   BFD_ASSERT (my_offset <= globals->arm_glue_size);
3278
3279   return myh;
3280 }
3281
3282 /* Arm code calling a Thumb function.  */
3283
3284 static int
3285 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
3286                          const char *           name,
3287                          bfd *                  input_bfd,
3288                          bfd *                  output_bfd,
3289                          asection *             input_section,
3290                          bfd_byte *             hit_data,
3291                          asection *             sym_sec,
3292                          bfd_vma                offset,
3293                          bfd_signed_vma         addend,
3294                          bfd_vma                val)
3295 {
3296   unsigned long int tmp;
3297   bfd_vma my_offset;
3298   asection * s;
3299   long int ret_offset;
3300   struct elf_link_hash_entry * myh;
3301   struct elf32_arm_link_hash_table * globals;
3302
3303   globals = elf32_arm_hash_table (info);
3304
3305   BFD_ASSERT (globals != NULL);
3306   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
3307
3308   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
3309                                ARM2THUMB_GLUE_SECTION_NAME);
3310   BFD_ASSERT (s != NULL);
3311   BFD_ASSERT (s->contents != NULL);
3312   BFD_ASSERT (s->output_section != NULL);
3313
3314   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
3315                                      sym_sec, val, s);
3316   if (!myh)
3317     return FALSE;
3318
3319   my_offset = myh->root.u.def.value;
3320   tmp = bfd_get_32 (input_bfd, hit_data);
3321   tmp = tmp & 0xFF000000;
3322
3323   /* Somehow these are both 4 too far, so subtract 8.  */
3324   ret_offset = (s->output_offset
3325                 + my_offset
3326                 + s->output_section->vma
3327                 - (input_section->output_offset
3328                    + input_section->output_section->vma
3329                    + offset + addend)
3330                 - 8);
3331
3332   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
3333
3334   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
3335
3336   return TRUE;
3337 }
3338
3339 /* Populate Arm stub for an exported Thumb function.  */
3340
3341 static bfd_boolean
3342 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
3343 {
3344   struct bfd_link_info * info = (struct bfd_link_info *) inf;
3345   asection * s;
3346   struct elf_link_hash_entry * myh;
3347   struct elf32_arm_link_hash_entry *eh;
3348   struct elf32_arm_link_hash_table * globals;
3349   asection *sec;
3350   bfd_vma val;
3351
3352   eh = elf32_arm_hash_entry(h);
3353   /* Allocate stubs for exported Thumb functions on v4t.  */
3354   if (eh->export_glue == NULL)
3355     return TRUE;
3356
3357   globals = elf32_arm_hash_table (info);
3358
3359   BFD_ASSERT (globals != NULL);
3360   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
3361
3362   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
3363                                ARM2THUMB_GLUE_SECTION_NAME);
3364   BFD_ASSERT (s != NULL);
3365   BFD_ASSERT (s->contents != NULL);
3366   BFD_ASSERT (s->output_section != NULL);
3367
3368   sec = eh->export_glue->root.u.def.section;
3369   val = eh->export_glue->root.u.def.value + sec->output_offset
3370         + sec->output_section->vma;
3371   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
3372                                      h->root.u.def.section->owner,
3373                                      globals->obfd, sec, val, s);
3374   BFD_ASSERT (myh);
3375   return TRUE;
3376 }
3377
3378 /* Generate Arm stubs for exported Thumb symbols.  */
3379 static void
3380 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED, 
3381                                   struct bfd_link_info *link_info)
3382 {
3383   struct elf32_arm_link_hash_table * globals;
3384
3385   if (!link_info)
3386     return;
3387
3388   globals = elf32_arm_hash_table (link_info);
3389   if (globals->use_blx)
3390     return;
3391
3392   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
3393                           link_info);
3394 }
3395
3396 /* Some relocations map to different relocations depending on the
3397    target.  Return the real relocation.  */
3398 static int
3399 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
3400                      int r_type)
3401 {
3402   switch (r_type)
3403     {
3404     case R_ARM_TARGET1:
3405       if (globals->target1_is_rel)
3406         return R_ARM_REL32;
3407       else
3408         return R_ARM_ABS32;
3409
3410     case R_ARM_TARGET2:
3411       return globals->target2_reloc;
3412
3413     default:
3414       return r_type;
3415     }
3416 }
3417
3418 /* Return the base VMA address which should be subtracted from real addresses
3419    when resolving @dtpoff relocation.
3420    This is PT_TLS segment p_vaddr.  */
3421
3422 static bfd_vma
3423 dtpoff_base (struct bfd_link_info *info)
3424 {
3425   /* If tls_sec is NULL, we should have signalled an error already.  */
3426   if (elf_hash_table (info)->tls_sec == NULL)
3427     return 0;
3428   return elf_hash_table (info)->tls_sec->vma;
3429 }
3430
3431 /* Return the relocation value for @tpoff relocation
3432    if STT_TLS virtual address is ADDRESS.  */
3433
3434 static bfd_vma
3435 tpoff (struct bfd_link_info *info, bfd_vma address)
3436 {
3437   struct elf_link_hash_table *htab = elf_hash_table (info);
3438   bfd_vma base;
3439
3440   /* If tls_sec is NULL, we should have signalled an error already.  */
3441   if (htab->tls_sec == NULL)
3442     return 0;
3443   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
3444   return address - htab->tls_sec->vma + base;
3445 }
3446
3447 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
3448    VALUE is the relocation value.  */
3449
3450 static bfd_reloc_status_type
3451 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
3452 {
3453   if (value > 0xfff)
3454     return bfd_reloc_overflow;
3455
3456   value |= bfd_get_32 (abfd, data) & 0xfffff000;
3457   bfd_put_32 (abfd, value, data);
3458   return bfd_reloc_ok;
3459 }
3460
3461 /* For a given value of n, calculate the value of G_n as required to
3462    deal with group relocations.  We return it in the form of an
3463    encoded constant-and-rotation, together with the final residual.  If n is
3464    specified as less than zero, then final_residual is filled with the
3465    input value and no further action is performed.  */
3466
3467 static bfd_vma
3468 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
3469 {
3470   int current_n;
3471   bfd_vma g_n;
3472   bfd_vma encoded_g_n = 0;
3473   bfd_vma residual = value; /* Also known as Y_n.  */
3474
3475   for (current_n = 0; current_n <= n; current_n++)
3476     {
3477       int shift;
3478
3479       /* Calculate which part of the value to mask.  */
3480       if (residual == 0)
3481         shift = 0;
3482       else
3483         {
3484           int msb;
3485
3486           /* Determine the most significant bit in the residual and
3487              align the resulting value to a 2-bit boundary.  */
3488           for (msb = 30; msb >= 0; msb -= 2)
3489             if (residual & (3 << msb))
3490               break;
3491
3492           /* The desired shift is now (msb - 6), or zero, whichever
3493              is the greater.  */
3494           shift = msb - 6;
3495           if (shift < 0)
3496             shift = 0;
3497         }
3498
3499       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
3500       g_n = residual & (0xff << shift);
3501       encoded_g_n = (g_n >> shift)
3502                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
3503
3504       /* Calculate the residual for the next time around.  */
3505       residual &= ~g_n;
3506     }
3507
3508   *final_residual = residual;
3509
3510   return encoded_g_n;
3511 }
3512
3513 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
3514    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
3515 static int
3516 identify_add_or_sub(bfd_vma insn)
3517 {
3518   int opcode = insn & 0x1e00000;
3519
3520   if (opcode == 1 << 23) /* ADD */
3521     return 1;
3522
3523   if (opcode == 1 << 22) /* SUB */
3524     return -1;
3525
3526   return 0;
3527 }
3528
3529 /* Determine if we're dealing with a Thumb-2 object.  */
3530
3531 static int using_thumb2 (struct elf32_arm_link_hash_table *globals)
3532 {
3533   int arch = elf32_arm_get_eabi_attr_int (globals->obfd, Tag_CPU_arch);
3534   return arch == TAG_CPU_ARCH_V6T2 || arch >= TAG_CPU_ARCH_V7;
3535 }
3536
3537 /* Perform a relocation as part of a final link.  */
3538
3539 static bfd_reloc_status_type
3540 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
3541                                bfd *                        input_bfd,
3542                                bfd *                        output_bfd,
3543                                asection *                   input_section,
3544                                bfd_byte *                   contents,
3545                                Elf_Internal_Rela *          rel,
3546                                bfd_vma                      value,
3547                                struct bfd_link_info *       info,
3548                                asection *                   sym_sec,
3549                                const char *                 sym_name,
3550                                int                          sym_flags,
3551                                struct elf_link_hash_entry * h,
3552                                bfd_boolean *                unresolved_reloc_p)
3553 {
3554   unsigned long                 r_type = howto->type;
3555   unsigned long                 r_symndx;
3556   bfd_byte *                    hit_data = contents + rel->r_offset;
3557   bfd *                         dynobj = NULL;
3558   Elf_Internal_Shdr *           symtab_hdr;
3559   struct elf_link_hash_entry ** sym_hashes;
3560   bfd_vma *                     local_got_offsets;
3561   asection *                    sgot = NULL;
3562   asection *                    splt = NULL;
3563   asection *                    sreloc = NULL;
3564   bfd_vma                       addend;
3565   bfd_signed_vma                signed_addend;
3566   struct elf32_arm_link_hash_table * globals;
3567
3568   globals = elf32_arm_hash_table (info);
3569
3570   /* Some relocation type map to different relocations depending on the
3571      target.  We pick the right one here.  */
3572   r_type = arm_real_reloc_type (globals, r_type);
3573   if (r_type != howto->type)
3574     howto = elf32_arm_howto_from_type (r_type);
3575
3576   /* If the start address has been set, then set the EF_ARM_HASENTRY
3577      flag.  Setting this more than once is redundant, but the cost is
3578      not too high, and it keeps the code simple.
3579
3580      The test is done  here, rather than somewhere else, because the
3581      start address is only set just before the final link commences.
3582
3583      Note - if the user deliberately sets a start address of 0, the
3584      flag will not be set.  */
3585   if (bfd_get_start_address (output_bfd) != 0)
3586     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
3587
3588   dynobj = elf_hash_table (info)->dynobj;
3589   if (dynobj)
3590     {
3591       sgot = bfd_get_section_by_name (dynobj, ".got");
3592       splt = bfd_get_section_by_name (dynobj, ".plt");
3593     }
3594   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
3595   sym_hashes = elf_sym_hashes (input_bfd);
3596   local_got_offsets = elf_local_got_offsets (input_bfd);
3597   r_symndx = ELF32_R_SYM (rel->r_info);
3598
3599   if (globals->use_rel)
3600     {
3601       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
3602
3603       if (addend & ((howto->src_mask + 1) >> 1))
3604         {
3605           signed_addend = -1;
3606           signed_addend &= ~ howto->src_mask;
3607           signed_addend |= addend;
3608         }
3609       else
3610         signed_addend = addend;
3611     }
3612   else
3613     addend = signed_addend = rel->r_addend;
3614
3615   switch (r_type)
3616     {
3617     case R_ARM_NONE:
3618       /* We don't need to find a value for this symbol.  It's just a
3619          marker.  */
3620       *unresolved_reloc_p = FALSE;
3621       return bfd_reloc_ok;
3622
3623     case R_ARM_ABS12:
3624       if (!globals->vxworks_p)
3625         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
3626
3627     case R_ARM_PC24:
3628     case R_ARM_ABS32:
3629     case R_ARM_ABS32_NOI:
3630     case R_ARM_REL32:
3631     case R_ARM_REL32_NOI:
3632     case R_ARM_CALL:
3633     case R_ARM_JUMP24:
3634     case R_ARM_XPC25:
3635     case R_ARM_PREL31:
3636     case R_ARM_PLT32:
3637       /* r_symndx will be zero only for relocs against symbols
3638          from removed linkonce sections, or sections discarded by
3639          a linker script.  */
3640       if (r_symndx == 0)
3641         return bfd_reloc_ok;
3642
3643       /* Handle relocations which should use the PLT entry.  ABS32/REL32
3644          will use the symbol's value, which may point to a PLT entry, but we
3645          don't need to handle that here.  If we created a PLT entry, all
3646          branches in this object should go to it.  */
3647       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
3648            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI)
3649           && h != NULL
3650           && splt != NULL
3651           && h->plt.offset != (bfd_vma) -1)
3652         {
3653           /* If we've created a .plt section, and assigned a PLT entry to
3654              this function, it should not be known to bind locally.  If
3655              it were, we would have cleared the PLT entry.  */
3656           BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3657
3658           value = (splt->output_section->vma
3659                    + splt->output_offset
3660                    + h->plt.offset);
3661           *unresolved_reloc_p = FALSE;
3662           return _bfd_final_link_relocate (howto, input_bfd, input_section,
3663                                            contents, rel->r_offset, value,
3664                                            rel->r_addend);
3665         }
3666
3667       /* When generating a shared object or relocatable executable, these
3668          relocations are copied into the output file to be resolved at
3669          run time.  */
3670       if ((info->shared || globals->root.is_relocatable_executable)
3671           && (input_section->flags & SEC_ALLOC)
3672           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
3673               || !SYMBOL_CALLS_LOCAL (info, h))
3674           && (h == NULL
3675               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3676               || h->root.type != bfd_link_hash_undefweak)
3677           && r_type != R_ARM_PC24
3678           && r_type != R_ARM_CALL
3679           && r_type != R_ARM_JUMP24
3680           && r_type != R_ARM_PREL31
3681           && r_type != R_ARM_PLT32)
3682         {
3683           Elf_Internal_Rela outrel;
3684           bfd_byte *loc;
3685           bfd_boolean skip, relocate;
3686
3687           *unresolved_reloc_p = FALSE;
3688
3689           if (sreloc == NULL)
3690             {
3691               const char * name;
3692
3693               name = (bfd_elf_string_from_elf_section
3694                       (input_bfd,
3695                        elf_elfheader (input_bfd)->e_shstrndx,
3696                        elf_section_data (input_section)->rel_hdr.sh_name));
3697               if (name == NULL)
3698                 return bfd_reloc_notsupported;
3699
3700               BFD_ASSERT (reloc_section_p (globals, name, input_section));
3701
3702               sreloc = bfd_get_section_by_name (dynobj, name);
3703               BFD_ASSERT (sreloc != NULL);
3704             }
3705
3706           skip = FALSE;
3707           relocate = FALSE;
3708
3709           outrel.r_addend = addend;
3710           outrel.r_offset =
3711             _bfd_elf_section_offset (output_bfd, info, input_section,
3712                                      rel->r_offset);
3713           if (outrel.r_offset == (bfd_vma) -1)
3714             skip = TRUE;
3715           else if (outrel.r_offset == (bfd_vma) -2)
3716             skip = TRUE, relocate = TRUE;
3717           outrel.r_offset += (input_section->output_section->vma
3718                               + input_section->output_offset);
3719
3720           if (skip)
3721             memset (&outrel, 0, sizeof outrel);
3722           else if (h != NULL
3723                    && h->dynindx != -1
3724                    && (!info->shared
3725                        || !info->symbolic
3726                        || !h->def_regular))
3727             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3728           else
3729             {
3730               int symbol;
3731
3732               /* This symbol is local, or marked to become local.  */
3733               if (sym_flags == STT_ARM_TFUNC)
3734                 value |= 1;
3735               if (globals->symbian_p)
3736                 {
3737                   /* On Symbian OS, the data segment and text segement
3738                      can be relocated independently.  Therefore, we
3739                      must indicate the segment to which this
3740                      relocation is relative.  The BPABI allows us to
3741                      use any symbol in the right segment; we just use
3742                      the section symbol as it is convenient.  (We
3743                      cannot use the symbol given by "h" directly as it
3744                      will not appear in the dynamic symbol table.)  */
3745                   if (sym_sec)
3746                     symbol = elf_section_data (sym_sec->output_section)->dynindx;
3747                   else
3748                     symbol = elf_section_data (input_section->output_section)->dynindx;
3749                   BFD_ASSERT (symbol != 0);
3750                 }
3751               else
3752                 /* On SVR4-ish systems, the dynamic loader cannot
3753                    relocate the text and data segments independently,
3754                    so the symbol does not matter.  */
3755                 symbol = 0;
3756               outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
3757               if (globals->use_rel)
3758                 relocate = TRUE;
3759               else
3760                 outrel.r_addend += value;
3761             }
3762
3763           loc = sreloc->contents;
3764           loc += sreloc->reloc_count++ * RELOC_SIZE (globals);
3765           SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
3766
3767           /* If this reloc is against an external symbol, we do not want to
3768              fiddle with the addend.  Otherwise, we need to include the symbol
3769              value so that it becomes an addend for the dynamic reloc.  */
3770           if (! relocate)
3771             return bfd_reloc_ok;
3772
3773           return _bfd_final_link_relocate (howto, input_bfd, input_section,
3774                                            contents, rel->r_offset, value,
3775                                            (bfd_vma) 0);
3776         }
3777       else switch (r_type)
3778         {
3779         case R_ARM_ABS12:
3780           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
3781
3782         case R_ARM_XPC25:         /* Arm BLX instruction.  */
3783         case R_ARM_CALL:
3784         case R_ARM_JUMP24:
3785         case R_ARM_PC24:          /* Arm B/BL instruction */
3786         case R_ARM_PLT32:
3787           if (r_type == R_ARM_XPC25)
3788             {
3789               /* Check for Arm calling Arm function.  */
3790               /* FIXME: Should we translate the instruction into a BL
3791                  instruction instead ?  */
3792               if (sym_flags != STT_ARM_TFUNC)
3793                 (*_bfd_error_handler)
3794                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
3795                    input_bfd,
3796                    h ? h->root.root.string : "(local)");
3797             }
3798           else if (r_type != R_ARM_CALL || !globals->use_blx)
3799             {
3800               /* Check for Arm calling Thumb function.  */
3801               if (sym_flags == STT_ARM_TFUNC)
3802                 {
3803                   elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
3804                                            output_bfd, input_section,
3805                                            hit_data, sym_sec, rel->r_offset,
3806                                            signed_addend, value);
3807                   return bfd_reloc_ok;
3808                 }
3809             }
3810
3811           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
3812              where:
3813               S is the address of the symbol in the relocation.
3814               P is address of the instruction being relocated.
3815               A is the addend (extracted from the instruction) in bytes.
3816
3817              S is held in 'value'.
3818              P is the base address of the section containing the
3819                instruction plus the offset of the reloc into that
3820                section, ie:
3821                  (input_section->output_section->vma +
3822                   input_section->output_offset +
3823                   rel->r_offset).
3824              A is the addend, converted into bytes, ie:
3825                  (signed_addend * 4)
3826
3827              Note: None of these operations have knowledge of the pipeline
3828              size of the processor, thus it is up to the assembler to
3829              encode this information into the addend.  */
3830           value -= (input_section->output_section->vma
3831                     + input_section->output_offset);
3832           value -= rel->r_offset;
3833           if (globals->use_rel)
3834             value += (signed_addend << howto->size);
3835           else
3836             /* RELA addends do not have to be adjusted by howto->size.  */
3837             value += signed_addend;
3838
3839           signed_addend = value;
3840           signed_addend >>= howto->rightshift;
3841
3842           /* It is not an error for an undefined weak reference to be
3843              out of range.  Any program that branches to such a symbol
3844              is going to crash anyway, so there is no point worrying
3845              about getting the destination exactly right.  */
3846           if (! h || h->root.type != bfd_link_hash_undefweak)
3847             {
3848               /* Perform a signed range check.  */
3849               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
3850                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
3851                 return bfd_reloc_overflow;
3852             }
3853
3854           addend = (value & 2);
3855
3856           value = (signed_addend & howto->dst_mask)
3857             | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
3858
3859           /* Set the H bit in the BLX instruction.  */
3860           if (sym_flags == STT_ARM_TFUNC)
3861             {
3862               if (addend)
3863                 value |= (1 << 24);
3864               else
3865                 value &= ~(bfd_vma)(1 << 24);
3866             }
3867           if (r_type == R_ARM_CALL)
3868             {
3869               /* Select the correct instruction (BL or BLX).  */
3870               if (sym_flags == STT_ARM_TFUNC)
3871                 value |= (1 << 28);
3872               else
3873                 {
3874                   value &= ~(bfd_vma)(1 << 28);
3875                   value |= (1 << 24);
3876                 }
3877             }
3878           break;
3879
3880         case R_ARM_ABS32:
3881           value += addend;
3882           if (sym_flags == STT_ARM_TFUNC)
3883             value |= 1;
3884           break;
3885
3886         case R_ARM_ABS32_NOI:
3887           value += addend;
3888           break;
3889
3890         case R_ARM_REL32:
3891           value += addend;
3892           if (sym_flags == STT_ARM_TFUNC)
3893             value |= 1;
3894           value -= (input_section->output_section->vma
3895                     + input_section->output_offset + rel->r_offset);
3896           break;
3897
3898         case R_ARM_REL32_NOI:
3899           value += addend;
3900           value -= (input_section->output_section->vma
3901                     + input_section->output_offset + rel->r_offset);
3902           break;
3903
3904         case R_ARM_PREL31:
3905           value -= (input_section->output_section->vma
3906                     + input_section->output_offset + rel->r_offset);
3907           value += signed_addend;
3908           if (! h || h->root.type != bfd_link_hash_undefweak)
3909             {
3910               /* Check for overflow */
3911               if ((value ^ (value >> 1)) & (1 << 30))
3912                 return bfd_reloc_overflow;
3913             }
3914           value &= 0x7fffffff;
3915           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
3916           if (sym_flags == STT_ARM_TFUNC)
3917             value |= 1;
3918           break;
3919         }
3920
3921       bfd_put_32 (input_bfd, value, hit_data);
3922       return bfd_reloc_ok;
3923
3924     case R_ARM_ABS8:
3925       value += addend;
3926       if ((long) value > 0x7f || (long) value < -0x80)
3927         return bfd_reloc_overflow;
3928
3929       bfd_put_8 (input_bfd, value, hit_data);
3930       return bfd_reloc_ok;
3931
3932     case R_ARM_ABS16:
3933       value += addend;
3934
3935       if ((long) value > 0x7fff || (long) value < -0x8000)
3936         return bfd_reloc_overflow;
3937
3938       bfd_put_16 (input_bfd, value, hit_data);
3939       return bfd_reloc_ok;
3940
3941     case R_ARM_THM_ABS5:
3942       /* Support ldr and str instructions for the thumb.  */
3943       if (globals->use_rel)
3944         {
3945           /* Need to refetch addend.  */
3946           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
3947           /* ??? Need to determine shift amount from operand size.  */
3948           addend >>= howto->rightshift;
3949         }
3950       value += addend;
3951
3952       /* ??? Isn't value unsigned?  */
3953       if ((long) value > 0x1f || (long) value < -0x10)
3954         return bfd_reloc_overflow;
3955
3956       /* ??? Value needs to be properly shifted into place first.  */
3957       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
3958       bfd_put_16 (input_bfd, value, hit_data);
3959       return bfd_reloc_ok;
3960
3961     case R_ARM_THM_ALU_PREL_11_0:
3962       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
3963       {
3964         bfd_vma insn;
3965         bfd_signed_vma relocation;
3966
3967         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
3968              | bfd_get_16 (input_bfd, hit_data + 2);
3969
3970         if (globals->use_rel)
3971           {
3972             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
3973                           | ((insn & (1 << 26)) >> 15);
3974             if (insn & 0xf00000)
3975               signed_addend = -signed_addend;
3976           }
3977
3978         relocation = value + signed_addend;
3979         relocation -= (input_section->output_section->vma
3980                        + input_section->output_offset
3981                        + rel->r_offset);
3982
3983         value = abs (relocation);
3984
3985         if (value >= 0x1000)
3986           return bfd_reloc_overflow;
3987
3988         insn = (insn & 0xfb0f8f00) | (value & 0xff)
3989              | ((value & 0x700) << 4)
3990              | ((value & 0x800) << 15);
3991         if (relocation < 0)
3992           insn |= 0xa00000;
3993
3994         bfd_put_16 (input_bfd, insn >> 16, hit_data);
3995         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
3996
3997         return bfd_reloc_ok;
3998       }
3999
4000     case R_ARM_THM_PC12:
4001       /* Corresponds to: ldr.w reg, [pc, #offset].  */
4002       {
4003         bfd_vma insn;
4004         bfd_signed_vma relocation;
4005
4006         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
4007              | bfd_get_16 (input_bfd, hit_data + 2);
4008
4009         if (globals->use_rel)
4010           {
4011             signed_addend = insn & 0xfff;
4012             if (!(insn & (1 << 23)))
4013               signed_addend = -signed_addend;
4014           }
4015
4016         relocation = value + signed_addend;
4017         relocation -= (input_section->output_section->vma
4018                        + input_section->output_offset
4019                        + rel->r_offset);
4020
4021         value = abs (relocation);
4022
4023         if (value >= 0x1000)
4024           return bfd_reloc_overflow;
4025
4026         insn = (insn & 0xff7ff000) | value;
4027         if (relocation >= 0)
4028           insn |= (1 << 23);
4029
4030         bfd_put_16 (input_bfd, insn >> 16, hit_data);
4031         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
4032
4033         return bfd_reloc_ok;
4034       }
4035
4036     case R_ARM_THM_XPC22:
4037     case R_ARM_THM_CALL:
4038       /* Thumb BL (branch long instruction).  */
4039       {
4040         bfd_vma relocation;
4041         bfd_vma reloc_sign;
4042         bfd_boolean overflow = FALSE;
4043         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
4044         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
4045         bfd_signed_vma reloc_signed_max;
4046         bfd_signed_vma reloc_signed_min;
4047         bfd_vma check;
4048         bfd_signed_vma signed_check;
4049         int bitsize;
4050         int thumb2 = using_thumb2 (globals);
4051
4052         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
4053            with Thumb-1) involving the J1 and J2 bits.  */
4054         if (globals->use_rel)
4055           {
4056             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
4057             bfd_vma upper = upper_insn & 0x3ff;
4058             bfd_vma lower = lower_insn & 0x7ff;
4059             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
4060             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
4061             bfd_vma i1 = j1 ^ s ? 0 : 1;
4062             bfd_vma i2 = j2 ^ s ? 0 : 1;
4063
4064             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
4065             /* Sign extend.  */
4066             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
4067
4068             signed_addend = addend;
4069           }
4070
4071         if (r_type == R_ARM_THM_XPC22)
4072           {
4073             /* Check for Thumb to Thumb call.  */
4074             /* FIXME: Should we translate the instruction into a BL
4075                instruction instead ?  */
4076             if (sym_flags == STT_ARM_TFUNC)
4077               (*_bfd_error_handler)
4078                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
4079                  input_bfd,
4080                  h ? h->root.root.string : "(local)");
4081           }
4082         else
4083           {
4084             /* If it is not a call to Thumb, assume call to Arm.
4085                If it is a call relative to a section name, then it is not a
4086                function call at all, but rather a long jump.  Calls through
4087                the PLT do not require stubs.  */
4088             if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
4089                 && (h == NULL || splt == NULL
4090                     || h->plt.offset == (bfd_vma) -1))
4091               {
4092                 if (globals->use_blx)
4093                   {
4094                     /* Convert BL to BLX.  */
4095                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
4096                   }
4097                 else if (elf32_thumb_to_arm_stub
4098                     (info, sym_name, input_bfd, output_bfd, input_section,
4099                      hit_data, sym_sec, rel->r_offset, signed_addend, value))
4100                   return bfd_reloc_ok;
4101                 else
4102                   return bfd_reloc_dangerous;
4103               }
4104             else if (sym_flags == STT_ARM_TFUNC && globals->use_blx)
4105               {
4106                 /* Make sure this is a BL.  */
4107                 lower_insn |= 0x1800;
4108               }
4109           }
4110
4111         /* Handle calls via the PLT.  */
4112         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
4113           {
4114             value = (splt->output_section->vma
4115                      + splt->output_offset
4116                      + h->plt.offset);
4117             if (globals->use_blx)
4118               {
4119                 /* If the Thumb BLX instruction is available, convert the
4120                    BL to a BLX instruction to call the ARM-mode PLT entry.  */
4121                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
4122               }
4123             else
4124               /* Target the Thumb stub before the ARM PLT entry.  */
4125               value -= PLT_THUMB_STUB_SIZE;
4126             *unresolved_reloc_p = FALSE;
4127           }
4128
4129         relocation = value + signed_addend;
4130
4131         relocation -= (input_section->output_section->vma
4132                        + input_section->output_offset
4133                        + rel->r_offset);
4134
4135         check = relocation >> howto->rightshift;
4136
4137         /* If this is a signed value, the rightshift just dropped
4138            leading 1 bits (assuming twos complement).  */
4139         if ((bfd_signed_vma) relocation >= 0)
4140           signed_check = check;
4141         else
4142           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
4143
4144         /* Calculate the permissable maximum and minimum values for
4145            this relocation according to whether we're relocating for
4146            Thumb-2 or not.  */
4147         bitsize = howto->bitsize;
4148         if (!thumb2)
4149           bitsize -= 2;
4150         reloc_signed_max = ((1 << (bitsize - 1)) - 1) >> howto->rightshift;
4151         reloc_signed_min = ~reloc_signed_max;
4152
4153         /* Assumes two's complement.  */
4154         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
4155           overflow = TRUE;
4156
4157         if ((lower_insn & 0x1800) == 0x0800)
4158           /* For a BLX instruction, make sure that the relocation is rounded up
4159              to a word boundary.  This follows the semantics of the instruction
4160              which specifies that bit 1 of the target address will come from bit
4161              1 of the base address.  */
4162           relocation = (relocation + 2) & ~ 3;
4163
4164         /* Put RELOCATION back into the insn.  Assumes two's complement.
4165            We use the Thumb-2 encoding, which is safe even if dealing with
4166            a Thumb-1 instruction by virtue of our overflow check above.  */
4167         reloc_sign = (signed_check < 0) ? 1 : 0;
4168         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
4169                      | ((relocation >> 12) & 0x3ff)
4170                      | (reloc_sign << 10);
4171         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff) 
4172                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
4173                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
4174                      | ((relocation >> 1) & 0x7ff);
4175
4176         /* Put the relocated value back in the object file:  */
4177         bfd_put_16 (input_bfd, upper_insn, hit_data);
4178         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
4179
4180         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
4181       }
4182       break;
4183
4184     case R_ARM_THM_JUMP24:
4185       /* Thumb32 unconditional branch instruction.  */
4186       {
4187         bfd_vma relocation;
4188         bfd_boolean overflow = FALSE;
4189         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
4190         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
4191         bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
4192         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
4193         bfd_vma check;
4194         bfd_signed_vma signed_check;
4195
4196         /* Need to refetch the addend, reconstruct the top three bits, and glue the
4197            two pieces together.  */
4198         if (globals->use_rel)
4199           {
4200             bfd_vma S  = (upper_insn & 0x0400) >> 10;
4201             bfd_vma hi = (upper_insn & 0x03ff);
4202             bfd_vma I1 = (lower_insn & 0x2000) >> 13;
4203             bfd_vma I2 = (lower_insn & 0x0800) >> 11;
4204             bfd_vma lo = (lower_insn & 0x07ff);
4205
4206             I1 = !(I1 ^ S);
4207             I2 = !(I2 ^ S);
4208             S  = !S;
4209
4210             signed_addend = (S << 24) | (I1 << 23) | (I2 << 22) | (hi << 12) | (lo << 1);
4211             signed_addend -= (1 << 24); /* Sign extend.  */
4212           }
4213
4214         /* ??? Should handle interworking?  GCC might someday try to
4215            use this for tail calls.  */
4216
4217         relocation = value + signed_addend;
4218         relocation -= (input_section->output_section->vma
4219                        + input_section->output_offset
4220                        + rel->r_offset);
4221
4222         check = relocation >> howto->rightshift;
4223
4224         /* If this is a signed value, the rightshift just dropped
4225            leading 1 bits (assuming twos complement).  */
4226         if ((bfd_signed_vma) relocation >= 0)
4227           signed_check = check;
4228         else
4229           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
4230
4231         /* Assumes two's complement.  */
4232         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
4233           overflow = TRUE;
4234
4235         /* Put RELOCATION back into the insn.  */
4236         {
4237           bfd_vma S  = (relocation & 0x01000000) >> 24;
4238           bfd_vma I1 = (relocation & 0x00800000) >> 23;
4239           bfd_vma I2 = (relocation & 0x00400000) >> 22;
4240           bfd_vma hi = (relocation & 0x003ff000) >> 12;
4241           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
4242
4243           I1 = !(I1 ^ S);
4244           I2 = !(I2 ^ S);
4245
4246           upper_insn = (upper_insn & (bfd_vma) 0xf800) | (S << 10) | hi;
4247           lower_insn = (lower_insn & (bfd_vma) 0xd000) | (I1 << 13) | (I2 << 11) | lo;
4248         }
4249
4250         /* Put the relocated value back in the object file:  */
4251         bfd_put_16 (input_bfd, upper_insn, hit_data);
4252         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
4253
4254         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
4255       }
4256
4257     case R_ARM_THM_JUMP19:
4258       /* Thumb32 conditional branch instruction.  */
4259       {
4260         bfd_vma relocation;
4261         bfd_boolean overflow = FALSE;
4262         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
4263         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
4264         bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
4265         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
4266         bfd_vma check;
4267         bfd_signed_vma signed_check;
4268
4269         /* Need to refetch the addend, reconstruct the top three bits,
4270            and squish the two 11 bit pieces together.  */
4271         if (globals->use_rel)
4272           {
4273             bfd_vma S     = (upper_insn & 0x0400) >> 10;
4274             bfd_vma upper = (upper_insn & 0x001f);
4275             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
4276             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
4277             bfd_vma lower = (lower_insn & 0x07ff);
4278
4279             upper |= J2 << 6;
4280             upper |= J1 << 7;
4281             upper |= ~S << 8;
4282             upper -= 0x0100; /* Sign extend.  */
4283
4284             addend = (upper << 12) | (lower << 1);
4285             signed_addend = addend;
4286           }
4287
4288         /* ??? Should handle interworking?  GCC might someday try to
4289            use this for tail calls.  */
4290
4291         relocation = value + signed_addend;
4292         relocation -= (input_section->output_section->vma
4293                        + input_section->output_offset
4294                        + rel->r_offset);
4295
4296         check = relocation >> howto->rightshift;
4297
4298         /* If this is a signed value, the rightshift just dropped
4299            leading 1 bits (assuming twos complement).  */
4300         if ((bfd_signed_vma) relocation >= 0)
4301           signed_check = check;
4302         else
4303           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
4304
4305         /* Assumes two's complement.  */
4306         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
4307           overflow = TRUE;
4308
4309         /* Put RELOCATION back into the insn.  */
4310         {
4311           bfd_vma S  = (relocation & 0x00100000) >> 20;
4312           bfd_vma J2 = (relocation & 0x00080000) >> 19;
4313           bfd_vma J1 = (relocation & 0x00040000) >> 18;
4314           bfd_vma hi = (relocation & 0x0003f000) >> 12;
4315           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
4316
4317           upper_insn = (upper_insn & 0xfb30) | (S << 10) | hi;
4318           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
4319         }
4320
4321         /* Put the relocated value back in the object file:  */
4322         bfd_put_16 (input_bfd, upper_insn, hit_data);
4323         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
4324
4325         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
4326       }
4327
4328     case R_ARM_THM_JUMP11:
4329     case R_ARM_THM_JUMP8:
4330     case R_ARM_THM_JUMP6:
4331       /* Thumb B (branch) instruction).  */
4332       {
4333         bfd_signed_vma relocation;
4334         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
4335         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
4336         bfd_signed_vma signed_check;
4337
4338         /* CZB cannot jump backward.  */
4339         if (r_type == R_ARM_THM_JUMP6)
4340           reloc_signed_min = 0;
4341
4342         if (globals->use_rel)
4343           {
4344             /* Need to refetch addend.  */
4345             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
4346             if (addend & ((howto->src_mask + 1) >> 1))
4347               {
4348                 signed_addend = -1;
4349                 signed_addend &= ~ howto->src_mask;
4350                 signed_addend |= addend;
4351               }
4352             else
4353               signed_addend = addend;
4354             /* The value in the insn has been right shifted.  We need to
4355                undo this, so that we can perform the address calculation
4356                in terms of bytes.  */
4357             signed_addend <<= howto->rightshift;
4358           }
4359         relocation = value + signed_addend;
4360
4361         relocation -= (input_section->output_section->vma
4362                        + input_section->output_offset
4363                        + rel->r_offset);
4364
4365         relocation >>= howto->rightshift;
4366         signed_check = relocation;
4367
4368         if (r_type == R_ARM_THM_JUMP6)
4369           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
4370         else
4371           relocation &= howto->dst_mask;
4372         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
4373
4374         bfd_put_16 (input_bfd, relocation, hit_data);
4375
4376         /* Assumes two's complement.  */
4377         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
4378           return bfd_reloc_overflow;
4379
4380         return bfd_reloc_ok;
4381       }
4382
4383     case R_ARM_ALU_PCREL7_0:
4384     case R_ARM_ALU_PCREL15_8:
4385     case R_ARM_ALU_PCREL23_15:
4386       {
4387         bfd_vma insn;
4388         bfd_vma relocation;
4389
4390         insn = bfd_get_32 (input_bfd, hit_data);
4391         if (globals->use_rel)
4392           {
4393             /* Extract the addend.  */
4394             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
4395             signed_addend = addend;
4396           }
4397         relocation = value + signed_addend;
4398
4399         relocation -= (input_section->output_section->vma
4400                        + input_section->output_offset
4401                        + rel->r_offset);
4402         insn = (insn & ~0xfff)
4403                | ((howto->bitpos << 7) & 0xf00)
4404                | ((relocation >> howto->bitpos) & 0xff);
4405         bfd_put_32 (input_bfd, value, hit_data);
4406       }
4407       return bfd_reloc_ok;
4408
4409     case R_ARM_GNU_VTINHERIT:
4410     case R_ARM_GNU_VTENTRY:
4411       return bfd_reloc_ok;
4412
4413     case R_ARM_GOTOFF32:
4414       /* Relocation is relative to the start of the
4415          global offset table.  */
4416
4417       BFD_ASSERT (sgot != NULL);
4418       if (sgot == NULL)
4419         return bfd_reloc_notsupported;
4420
4421       /* If we are addressing a Thumb function, we need to adjust the
4422          address by one, so that attempts to call the function pointer will
4423          correctly interpret it as Thumb code.  */
4424       if (sym_flags == STT_ARM_TFUNC)
4425         value += 1;
4426
4427       /* Note that sgot->output_offset is not involved in this
4428          calculation.  We always want the start of .got.  If we
4429          define _GLOBAL_OFFSET_TABLE in a different way, as is
4430          permitted by the ABI, we might have to change this
4431          calculation.  */
4432       value -= sgot->output_section->vma;
4433       return _bfd_final_link_relocate (howto, input_bfd, input_section,
4434                                        contents, rel->r_offset, value,
4435                                        rel->r_addend);
4436
4437     case R_ARM_GOTPC:
4438       /* Use global offset table as symbol value.  */
4439       BFD_ASSERT (sgot != NULL);
4440
4441       if (sgot == NULL)
4442         return bfd_reloc_notsupported;
4443
4444       *unresolved_reloc_p = FALSE;
4445       value = sgot->output_section->vma;
4446       return _bfd_final_link_relocate (howto, input_bfd, input_section,
4447                                        contents, rel->r_offset, value,
4448                                        rel->r_addend);
4449
4450     case R_ARM_GOT32:
4451     case R_ARM_GOT_PREL:
4452       /* Relocation is to the entry for this symbol in the
4453          global offset table.  */
4454       if (sgot == NULL)
4455         return bfd_reloc_notsupported;
4456
4457       if (h != NULL)
4458         {
4459           bfd_vma off;
4460           bfd_boolean dyn;
4461
4462           off = h->got.offset;
4463           BFD_ASSERT (off != (bfd_vma) -1);
4464           dyn = globals->root.dynamic_sections_created;
4465
4466           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4467               || (info->shared
4468                   && SYMBOL_REFERENCES_LOCAL (info, h))
4469               || (ELF_ST_VISIBILITY (h->other)
4470                   && h->root.type == bfd_link_hash_undefweak))
4471             {
4472               /* This is actually a static link, or it is a -Bsymbolic link
4473                  and the symbol is defined locally.  We must initialize this
4474                  entry in the global offset table.  Since the offset must
4475                  always be a multiple of 4, we use the least significant bit
4476                  to record whether we have initialized it already.
4477
4478                  When doing a dynamic link, we create a .rel(a).got relocation
4479                  entry to initialize the value.  This is done in the
4480                  finish_dynamic_symbol routine.  */
4481               if ((off & 1) != 0)
4482                 off &= ~1;
4483               else
4484                 {
4485                   /* If we are addressing a Thumb function, we need to
4486                      adjust the address by one, so that attempts to
4487                      call the function pointer will correctly
4488                      interpret it as Thumb code.  */
4489                   if (sym_flags == STT_ARM_TFUNC)
4490                     value |= 1;
4491
4492                   bfd_put_32 (output_bfd, value, sgot->contents + off);
4493                   h->got.offset |= 1;
4494                 }
4495             }
4496           else
4497             *unresolved_reloc_p = FALSE;
4498
4499           value = sgot->output_offset + off;
4500         }
4501       else
4502         {
4503           bfd_vma off;
4504
4505           BFD_ASSERT (local_got_offsets != NULL &&
4506                       local_got_offsets[r_symndx] != (bfd_vma) -1);
4507
4508           off = local_got_offsets[r_symndx];
4509
4510           /* The offset must always be a multiple of 4.  We use the
4511              least significant bit to record whether we have already
4512              generated the necessary reloc.  */
4513           if ((off & 1) != 0)
4514             off &= ~1;
4515           else
4516             {
4517               /* If we are addressing a Thumb function, we need to
4518                  adjust the address by one, so that attempts to
4519                  call the function pointer will correctly
4520                  interpret it as Thumb code.  */
4521               if (sym_flags == STT_ARM_TFUNC)
4522                 value |= 1;
4523
4524               if (globals->use_rel)
4525                 bfd_put_32 (output_bfd, value, sgot->contents + off);
4526
4527               if (info->shared)
4528                 {
4529                   asection * srelgot;
4530                   Elf_Internal_Rela outrel;
4531                   bfd_byte *loc;
4532
4533                   srelgot = (bfd_get_section_by_name
4534                              (dynobj, RELOC_SECTION (globals, ".got")));
4535                   BFD_ASSERT (srelgot != NULL);
4536
4537                   outrel.r_addend = addend + value;
4538                   outrel.r_offset = (sgot->output_section->vma
4539                                      + sgot->output_offset
4540                                      + off);
4541                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
4542                   loc = srelgot->contents;
4543                   loc += srelgot->reloc_count++ * RELOC_SIZE (globals);
4544                   SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4545                 }
4546
4547               local_got_offsets[r_symndx] |= 1;
4548             }
4549
4550           value = sgot->output_offset + off;
4551         }
4552       if (r_type != R_ARM_GOT32)
4553         value += sgot->output_section->vma;
4554
4555       return _bfd_final_link_relocate (howto, input_bfd, input_section,
4556                                        contents, rel->r_offset, value,
4557                                        rel->r_addend);
4558
4559     case R_ARM_TLS_LDO32:
4560       value = value - dtpoff_base (info);
4561
4562       return _bfd_final_link_relocate (howto, input_bfd, input_section,
4563                                        contents, rel->r_offset, value,
4564                                        rel->r_addend);
4565
4566     case R_ARM_TLS_LDM32:
4567       {
4568         bfd_vma off;
4569
4570         if (globals->sgot == NULL)
4571           abort ();
4572
4573         off = globals->tls_ldm_got.offset;
4574
4575         if ((off & 1) != 0)
4576           off &= ~1;
4577         else
4578           {
4579             /* If we don't know the module number, create a relocation
4580                for it.  */
4581             if (info->shared)
4582               {
4583                 Elf_Internal_Rela outrel;
4584                 bfd_byte *loc;
4585
4586                 if (globals->srelgot == NULL)
4587                   abort ();
4588
4589                 outrel.r_addend = 0;
4590                 outrel.r_offset = (globals->sgot->output_section->vma
4591                                    + globals->sgot->output_offset + off);
4592                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
4593
4594                 if (globals->use_rel)
4595                   bfd_put_32 (output_bfd, outrel.r_addend,
4596                               globals->sgot->contents + off);
4597
4598                 loc = globals->srelgot->contents;
4599                 loc += globals->srelgot->reloc_count++ * RELOC_SIZE (globals);
4600                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4601               }
4602             else
4603               bfd_put_32 (output_bfd, 1, globals->sgot->contents + off);
4604
4605             globals->tls_ldm_got.offset |= 1;
4606           }
4607
4608         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off 
4609           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
4610
4611         return _bfd_final_link_relocate (howto, input_bfd, input_section,
4612                                          contents, rel->r_offset, value,
4613                                          rel->r_addend);
4614       }
4615
4616     case R_ARM_TLS_GD32:
4617     case R_ARM_TLS_IE32:
4618       {
4619         bfd_vma off;
4620         int indx;
4621         char tls_type;
4622
4623         if (globals->sgot == NULL)
4624           abort ();
4625
4626         indx = 0;
4627         if (h != NULL)
4628           {
4629             bfd_boolean dyn;
4630             dyn = globals->root.dynamic_sections_created;
4631             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4632                 && (!info->shared
4633                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
4634               {
4635                 *unresolved_reloc_p = FALSE;
4636                 indx = h->dynindx;
4637               }
4638             off = h->got.offset;
4639             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
4640           }
4641         else
4642           {
4643             if (local_got_offsets == NULL)
4644               abort ();
4645             off = local_got_offsets[r_symndx];
4646             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
4647           }
4648
4649         if (tls_type == GOT_UNKNOWN)
4650           abort ();
4651
4652         if ((off & 1) != 0)
4653           off &= ~1;
4654         else
4655           {
4656             bfd_boolean need_relocs = FALSE;
4657             Elf_Internal_Rela outrel;
4658             bfd_byte *loc = NULL;
4659             int cur_off = off;
4660
4661             /* The GOT entries have not been initialized yet.  Do it
4662                now, and emit any relocations.  If both an IE GOT and a
4663                GD GOT are necessary, we emit the GD first.  */
4664
4665             if ((info->shared || indx != 0)
4666                 && (h == NULL
4667                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4668                     || h->root.type != bfd_link_hash_undefweak))
4669               {
4670                 need_relocs = TRUE;
4671                 if (globals->srelgot == NULL)
4672                   abort ();
4673                 loc = globals->srelgot->contents;
4674                 loc += globals->srelgot->reloc_count * RELOC_SIZE (globals);
4675               }
4676
4677             if (tls_type & GOT_TLS_GD)
4678               {
4679                 if (need_relocs)
4680                   {
4681                     outrel.r_addend = 0;
4682                     outrel.r_offset = (globals->sgot->output_section->vma
4683                                        + globals->sgot->output_offset
4684                                        + cur_off);
4685                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
4686
4687                     if (globals->use_rel)
4688                       bfd_put_32 (output_bfd, outrel.r_addend,
4689                                   globals->sgot->contents + cur_off);
4690
4691                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4692                     globals->srelgot->reloc_count++;
4693                     loc += RELOC_SIZE (globals);
4694
4695                     if (indx == 0)
4696                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
4697                                   globals->sgot->contents + cur_off + 4);
4698                     else
4699                       {
4700                         outrel.r_addend = 0;
4701                         outrel.r_info = ELF32_R_INFO (indx,
4702                                                       R_ARM_TLS_DTPOFF32);
4703                         outrel.r_offset += 4;
4704
4705                         if (globals->use_rel)
4706                           bfd_put_32 (output_bfd, outrel.r_addend,
4707                                       globals->sgot->contents + cur_off + 4);
4708
4709
4710                         SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4711                         globals->srelgot->reloc_count++;
4712                         loc += RELOC_SIZE (globals);
4713                       }
4714                   }
4715                 else
4716                   {
4717                     /* If we are not emitting relocations for a
4718                        general dynamic reference, then we must be in a
4719                        static link or an executable link with the
4720                        symbol binding locally.  Mark it as belonging
4721                        to module 1, the executable.  */
4722                     bfd_put_32 (output_bfd, 1,
4723                                 globals->sgot->contents + cur_off);
4724                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
4725                                 globals->sgot->contents + cur_off + 4);
4726                   }
4727
4728                 cur_off += 8;
4729               }
4730
4731             if (tls_type & GOT_TLS_IE)
4732               {
4733                 if (need_relocs)
4734                   {
4735                     if (indx == 0)
4736                       outrel.r_addend = value - dtpoff_base (info);
4737                     else
4738                       outrel.r_addend = 0;
4739                     outrel.r_offset = (globals->sgot->output_section->vma
4740                                        + globals->sgot->output_offset
4741                                        + cur_off);
4742                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
4743
4744                     if (globals->use_rel)
4745                       bfd_put_32 (output_bfd, outrel.r_addend,
4746                                   globals->sgot->contents + cur_off);
4747
4748                     SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4749                     globals->srelgot->reloc_count++;
4750                     loc += RELOC_SIZE (globals);
4751                   }
4752                 else
4753                   bfd_put_32 (output_bfd, tpoff (info, value),
4754                               globals->sgot->contents + cur_off);
4755                 cur_off += 4;
4756               }
4757
4758             if (h != NULL)
4759               h->got.offset |= 1;
4760             else
4761               local_got_offsets[r_symndx] |= 1;
4762           }
4763
4764         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
4765           off += 8;
4766         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off 
4767           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
4768
4769         return _bfd_final_link_relocate (howto, input_bfd, input_section,
4770                                          contents, rel->r_offset, value,
4771                                          rel->r_addend);
4772       }
4773
4774     case R_ARM_TLS_LE32:
4775       if (info->shared)
4776         {
4777           (*_bfd_error_handler)
4778             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
4779              input_bfd, input_section,
4780              (long) rel->r_offset, howto->name);
4781           return FALSE;   
4782         }
4783       else
4784         value = tpoff (info, value);
4785       
4786       return _bfd_final_link_relocate (howto, input_bfd, input_section,
4787                                        contents, rel->r_offset, value,
4788                                        rel->r_addend);
4789
4790     case R_ARM_V4BX:
4791       if (globals->fix_v4bx)
4792         {
4793           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
4794
4795           /* Ensure that we have a BX instruction.  */
4796           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
4797
4798           /* Preserve Rm (lowest four bits) and the condition code
4799              (highest four bits). Other bits encode MOV PC,Rm.  */
4800           insn = (insn & 0xf000000f) | 0x01a0f000;
4801
4802           bfd_put_32 (input_bfd, insn, hit_data);
4803         }
4804       return bfd_reloc_ok;
4805
4806     case R_ARM_MOVW_ABS_NC:
4807     case R_ARM_MOVT_ABS:
4808     case R_ARM_MOVW_PREL_NC:
4809     case R_ARM_MOVT_PREL:
4810     /* Until we properly support segment-base-relative addressing then
4811        we assume the segment base to be zero, as for the group relocations.
4812        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
4813        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
4814     case R_ARM_MOVW_BREL_NC:
4815     case R_ARM_MOVW_BREL:
4816     case R_ARM_MOVT_BREL:
4817       {
4818         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
4819
4820         if (globals->use_rel)
4821           {
4822             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
4823             signed_addend = (addend ^ 0x10000) - 0x10000;
4824           }
4825
4826         value += signed_addend;
4827
4828         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
4829           value -= (input_section->output_section->vma
4830                     + input_section->output_offset + rel->r_offset);
4831
4832         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
4833           return bfd_reloc_overflow;
4834
4835         if (sym_flags == STT_ARM_TFUNC)
4836           value |= 1;
4837
4838         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
4839             || r_type == R_ARM_MOVT_BREL)
4840           value >>= 16;
4841
4842         insn &= 0xfff0f000;
4843         insn |= value & 0xfff;
4844         insn |= (value & 0xf000) << 4;
4845         bfd_put_32 (input_bfd, insn, hit_data);
4846       }
4847       return bfd_reloc_ok;
4848
4849     case R_ARM_THM_MOVW_ABS_NC:
4850     case R_ARM_THM_MOVT_ABS:
4851     case R_ARM_THM_MOVW_PREL_NC:
4852     case R_ARM_THM_MOVT_PREL:
4853     /* Until we properly support segment-base-relative addressing then
4854        we assume the segment base to be zero, as for the above relocations.
4855        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
4856        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
4857        as R_ARM_THM_MOVT_ABS.  */
4858     case R_ARM_THM_MOVW_BREL_NC:
4859     case R_ARM_THM_MOVW_BREL:
4860     case R_ARM_THM_MOVT_BREL:
4861       {
4862         bfd_vma insn;
4863         
4864         insn = bfd_get_16 (input_bfd, hit_data) << 16;
4865         insn |= bfd_get_16 (input_bfd, hit_data + 2);
4866
4867         if (globals->use_rel)
4868           {
4869             addend = ((insn >> 4)  & 0xf000)
4870                    | ((insn >> 15) & 0x0800)
4871                    | ((insn >> 4)  & 0x0700)
4872                    | (insn         & 0x00ff);
4873             signed_addend = (addend ^ 0x10000) - 0x10000;
4874           }
4875
4876         value += signed_addend;
4877
4878         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
4879           value -= (input_section->output_section->vma
4880                     + input_section->output_offset + rel->r_offset);
4881
4882         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
4883           return bfd_reloc_overflow;
4884
4885         if (sym_flags == STT_ARM_TFUNC)
4886           value |= 1;
4887
4888         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
4889             || r_type == R_ARM_THM_MOVT_BREL)
4890           value >>= 16;
4891
4892         insn &= 0xfbf08f00;
4893         insn |= (value & 0xf000) << 4;
4894         insn |= (value & 0x0800) << 15;
4895         insn |= (value & 0x0700) << 4;
4896         insn |= (value & 0x00ff);
4897
4898         bfd_put_16 (input_bfd, insn >> 16, hit_data);
4899         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
4900       }
4901       return bfd_reloc_ok;
4902
4903     case R_ARM_ALU_PC_G0_NC:
4904     case R_ARM_ALU_PC_G1_NC:
4905     case R_ARM_ALU_PC_G0:
4906     case R_ARM_ALU_PC_G1:
4907     case R_ARM_ALU_PC_G2:
4908     case R_ARM_ALU_SB_G0_NC:
4909     case R_ARM_ALU_SB_G1_NC:
4910     case R_ARM_ALU_SB_G0:
4911     case R_ARM_ALU_SB_G1:
4912     case R_ARM_ALU_SB_G2:
4913       {
4914         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
4915         bfd_vma pc = input_section->output_section->vma
4916                      + input_section->output_offset + rel->r_offset;
4917         /* sb should be the origin of the *segment* containing the symbol.
4918            It is not clear how to obtain this OS-dependent value, so we
4919            make an arbitrary choice of zero.  */
4920         bfd_vma sb = 0;
4921         bfd_vma residual;
4922         bfd_vma g_n;
4923         bfd_signed_vma signed_value;
4924         int group = 0;
4925
4926         /* Determine which group of bits to select.  */
4927         switch (r_type)
4928           {
4929           case R_ARM_ALU_PC_G0_NC:
4930           case R_ARM_ALU_PC_G0:
4931           case R_ARM_ALU_SB_G0_NC:
4932           case R_ARM_ALU_SB_G0:
4933             group = 0;
4934             break;
4935
4936           case R_ARM_ALU_PC_G1_NC:
4937           case R_ARM_ALU_PC_G1:
4938           case R_ARM_ALU_SB_G1_NC:
4939           case R_ARM_ALU_SB_G1:
4940             group = 1;
4941             break;
4942
4943           case R_ARM_ALU_PC_G2:
4944           case R_ARM_ALU_SB_G2:
4945             group = 2;
4946             break;
4947
4948           default:
4949             abort();
4950           }
4951
4952         /* If REL, extract the addend from the insn.  If RELA, it will
4953            have already been fetched for us.  */
4954         if (globals->use_rel)
4955           {
4956             int negative;
4957             bfd_vma constant = insn & 0xff;
4958             bfd_vma rotation = (insn & 0xf00) >> 8;
4959
4960             if (rotation == 0)
4961               signed_addend = constant;
4962             else
4963               {
4964                 /* Compensate for the fact that in the instruction, the
4965                    rotation is stored in multiples of 2 bits.  */
4966                 rotation *= 2;
4967
4968                 /* Rotate "constant" right by "rotation" bits.  */
4969                 signed_addend = (constant >> rotation) |
4970                                 (constant << (8 * sizeof (bfd_vma) - rotation));
4971               }
4972
4973             /* Determine if the instruction is an ADD or a SUB.
4974                (For REL, this determines the sign of the addend.)  */
4975             negative = identify_add_or_sub (insn);
4976             if (negative == 0)
4977               {
4978                 (*_bfd_error_handler)
4979                   (_("%B(%A+0x%lx): Only ADD or SUB instructions are allowed for ALU group relocations"),
4980                   input_bfd, input_section,
4981                   (long) rel->r_offset, howto->name);
4982                 return bfd_reloc_overflow;        
4983               }
4984
4985             signed_addend *= negative;
4986           }
4987
4988         /* Compute the value (X) to go in the place.  */
4989         if (r_type == R_ARM_ALU_PC_G0_NC
4990             || r_type == R_ARM_ALU_PC_G1_NC
4991             || r_type == R_ARM_ALU_PC_G0
4992             || r_type == R_ARM_ALU_PC_G1
4993             || r_type == R_ARM_ALU_PC_G2)
4994           /* PC relative.  */
4995           signed_value = value - pc + signed_addend;
4996         else
4997           /* Section base relative.  */
4998           signed_value = value - sb + signed_addend;
4999
5000         /* If the target symbol is a Thumb function, then set the
5001            Thumb bit in the address.  */
5002         if (sym_flags == STT_ARM_TFUNC)
5003           signed_value |= 1;
5004
5005         /* Calculate the value of the relevant G_n, in encoded
5006            constant-with-rotation format.  */
5007         g_n = calculate_group_reloc_mask (abs (signed_value), group,
5008                                           &residual);
5009
5010         /* Check for overflow if required.  */
5011         if ((r_type == R_ARM_ALU_PC_G0
5012              || r_type == R_ARM_ALU_PC_G1
5013              || r_type == R_ARM_ALU_PC_G2
5014              || r_type == R_ARM_ALU_SB_G0
5015              || r_type == R_ARM_ALU_SB_G1
5016              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
5017           {
5018             (*_bfd_error_handler)
5019               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
5020               input_bfd, input_section,
5021               (long) rel->r_offset, abs (signed_value), howto->name);
5022             return bfd_reloc_overflow;
5023           }
5024
5025         /* Mask out the value and the ADD/SUB part of the opcode; take care
5026            not to destroy the S bit.  */
5027         insn &= 0xff1ff000;
5028
5029         /* Set the opcode according to whether the value to go in the
5030            place is negative.  */
5031         if (signed_value < 0)
5032           insn |= 1 << 22;
5033         else
5034           insn |= 1 << 23;
5035
5036         /* Encode the offset.  */
5037         insn |= g_n;
5038
5039         bfd_put_32 (input_bfd, insn, hit_data);
5040       }
5041       return bfd_reloc_ok;
5042
5043     case R_ARM_LDR_PC_G0:
5044     case R_ARM_LDR_PC_G1:
5045     case R_ARM_LDR_PC_G2:
5046     case R_ARM_LDR_SB_G0:
5047     case R_ARM_LDR_SB_G1:
5048     case R_ARM_LDR_SB_G2:
5049       {
5050         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
5051         bfd_vma pc = input_section->output_section->vma
5052                      + input_section->output_offset + rel->r_offset;
5053         bfd_vma sb = 0; /* See note above.  */
5054         bfd_vma residual;
5055         bfd_signed_vma signed_value;
5056         int group = 0;
5057
5058         /* Determine which groups of bits to calculate.  */
5059         switch (r_type)
5060           {
5061           case R_ARM_LDR_PC_G0:
5062           case R_ARM_LDR_SB_G0:
5063             group = 0;
5064             break;
5065
5066           case R_ARM_LDR_PC_G1:
5067           case R_ARM_LDR_SB_G1:
5068             group = 1;
5069             break;
5070
5071           case R_ARM_LDR_PC_G2:
5072           case R_ARM_LDR_SB_G2:
5073             group = 2;
5074             break;
5075
5076           default:
5077             abort();
5078           }
5079
5080         /* If REL, extract the addend from the insn.  If RELA, it will
5081            have already been fetched for us.  */
5082         if (globals->use_rel)
5083           {
5084             int negative = (insn & (1 << 23)) ? 1 : -1;
5085             signed_addend = negative * (insn & 0xfff);
5086           }
5087
5088         /* Compute the value (X) to go in the place.  */
5089         if (r_type == R_ARM_LDR_PC_G0
5090             || r_type == R_ARM_LDR_PC_G1
5091             || r_type == R_ARM_LDR_PC_G2)
5092           /* PC relative.  */
5093           signed_value = value - pc + signed_addend;
5094         else
5095           /* Section base relative.  */
5096           signed_value = value - sb + signed_addend;
5097
5098         /* Calculate the value of the relevant G_{n-1} to obtain
5099            the residual at that stage.  */
5100         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
5101
5102         /* Check for overflow.  */
5103         if (residual >= 0x1000)
5104           {
5105             (*_bfd_error_handler)
5106               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
5107               input_bfd, input_section,
5108               (long) rel->r_offset, abs (signed_value), howto->name);
5109             return bfd_reloc_overflow;
5110           }
5111
5112         /* Mask out the value and U bit.  */
5113         insn &= 0xff7ff000;
5114
5115         /* Set the U bit if the value to go in the place is non-negative.  */
5116         if (signed_value >= 0)
5117           insn |= 1 << 23;
5118
5119         /* Encode the offset.  */
5120         insn |= residual;
5121
5122         bfd_put_32 (input_bfd, insn, hit_data);
5123       }
5124       return bfd_reloc_ok;
5125
5126     case R_ARM_LDRS_PC_G0:
5127     case R_ARM_LDRS_PC_G1:
5128     case R_ARM_LDRS_PC_G2:
5129     case R_ARM_LDRS_SB_G0:
5130     case R_ARM_LDRS_SB_G1:
5131     case R_ARM_LDRS_SB_G2:
5132       {
5133         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
5134         bfd_vma pc = input_section->output_section->vma
5135                      + input_section->output_offset + rel->r_offset;
5136         bfd_vma sb = 0; /* See note above.  */
5137         bfd_vma residual;
5138         bfd_signed_vma signed_value;
5139         int group = 0;
5140
5141         /* Determine which groups of bits to calculate.  */
5142         switch (r_type)
5143           {
5144           case R_ARM_LDRS_PC_G0:
5145           case R_ARM_LDRS_SB_G0:
5146             group = 0;
5147             break;
5148
5149           case R_ARM_LDRS_PC_G1:
5150           case R_ARM_LDRS_SB_G1:
5151             group = 1;
5152             break;
5153
5154           case R_ARM_LDRS_PC_G2:
5155           case R_ARM_LDRS_SB_G2:
5156             group = 2;
5157             break;
5158
5159           default:
5160             abort();
5161           }
5162
5163         /* If REL, extract the addend from the insn.  If RELA, it will
5164            have already been fetched for us.  */
5165         if (globals->use_rel)
5166           {
5167             int negative = (insn & (1 << 23)) ? 1 : -1;
5168             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
5169           }
5170
5171         /* Compute the value (X) to go in the place.  */
5172         if (r_type == R_ARM_LDRS_PC_G0
5173             || r_type == R_ARM_LDRS_PC_G1
5174             || r_type == R_ARM_LDRS_PC_G2)
5175           /* PC relative.  */
5176           signed_value = value - pc + signed_addend;
5177         else
5178           /* Section base relative.  */
5179           signed_value = value - sb + signed_addend;
5180
5181         /* Calculate the value of the relevant G_{n-1} to obtain
5182            the residual at that stage.  */
5183         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
5184
5185         /* Check for overflow.  */
5186         if (residual >= 0x100)
5187           {
5188             (*_bfd_error_handler)
5189               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
5190               input_bfd, input_section,
5191               (long) rel->r_offset, abs (signed_value), howto->name);
5192             return bfd_reloc_overflow;
5193           }
5194
5195         /* Mask out the value and U bit.  */
5196         insn &= 0xff7ff0f0;
5197
5198         /* Set the U bit if the value to go in the place is non-negative.  */
5199         if (signed_value >= 0)
5200           insn |= 1 << 23;
5201
5202         /* Encode the offset.  */
5203         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
5204
5205         bfd_put_32 (input_bfd, insn, hit_data);
5206       }
5207       return bfd_reloc_ok;
5208
5209     case R_ARM_LDC_PC_G0:
5210     case R_ARM_LDC_PC_G1:
5211     case R_ARM_LDC_PC_G2:
5212     case R_ARM_LDC_SB_G0:
5213     case R_ARM_LDC_SB_G1:
5214     case R_ARM_LDC_SB_G2:
5215       {
5216         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
5217         bfd_vma pc = input_section->output_section->vma
5218                      + input_section->output_offset + rel->r_offset;
5219         bfd_vma sb = 0; /* See note above.  */
5220         bfd_vma residual;
5221         bfd_signed_vma signed_value;
5222         int group = 0;
5223
5224         /* Determine which groups of bits to calculate.  */
5225         switch (r_type)
5226           {
5227           case R_ARM_LDC_PC_G0:
5228           case R_ARM_LDC_SB_G0:
5229             group = 0;
5230             break;
5231
5232           case R_ARM_LDC_PC_G1:
5233           case R_ARM_LDC_SB_G1:
5234             group = 1;
5235             break;
5236
5237           case R_ARM_LDC_PC_G2:
5238           case R_ARM_LDC_SB_G2:
5239             group = 2;
5240             break;
5241
5242           default:
5243             abort();
5244           }
5245
5246         /* If REL, extract the addend from the insn.  If RELA, it will
5247            have already been fetched for us.  */
5248         if (globals->use_rel)
5249           {
5250             int negative = (insn & (1 << 23)) ? 1 : -1;
5251             signed_addend = negative * ((insn & 0xff) << 2);
5252           }
5253
5254         /* Compute the value (X) to go in the place.  */
5255         if (r_type == R_ARM_LDC_PC_G0
5256             || r_type == R_ARM_LDC_PC_G1
5257             || r_type == R_ARM_LDC_PC_G2)
5258           /* PC relative.  */
5259           signed_value = value - pc + signed_addend;
5260         else
5261           /* Section base relative.  */
5262           signed_value = value - sb + signed_addend;
5263
5264         /* Calculate the value of the relevant G_{n-1} to obtain
5265            the residual at that stage.  */
5266         calculate_group_reloc_mask (abs (signed_value), group - 1, &residual);
5267
5268         /* Check for overflow.  (The absolute value to go in the place must be
5269            divisible by four and, after having been divided by four, must
5270            fit in eight bits.)  */
5271         if ((residual & 0x3) != 0 || residual >= 0x400)
5272           {
5273             (*_bfd_error_handler)
5274               (_("%B(%A+0x%lx): Overflow whilst splitting 0x%lx for group relocation %s"),
5275               input_bfd, input_section,
5276               (long) rel->r_offset, abs (signed_value), howto->name);
5277             return bfd_reloc_overflow;
5278           }
5279
5280         /* Mask out the value and U bit.  */
5281         insn &= 0xff7fff00;
5282
5283         /* Set the U bit if the value to go in the place is non-negative.  */
5284         if (signed_value >= 0)
5285           insn |= 1 << 23;
5286
5287         /* Encode the offset.  */
5288         insn |= residual >> 2;
5289
5290         bfd_put_32 (input_bfd, insn, hit_data);
5291       }
5292       return bfd_reloc_ok;
5293
5294     default:
5295       return bfd_reloc_notsupported;
5296     }
5297 }
5298
5299
5300 static int
5301 uleb128_size (unsigned int i)
5302 {
5303   int size;
5304   size = 1;
5305   while (i >= 0x80)
5306     {
5307       i >>= 7;
5308       size++;
5309     }
5310   return size;
5311 }
5312
5313 /* Return TRUE if the attribute has the default value (0/"").  */
5314 static bfd_boolean
5315 is_default_attr (aeabi_attribute *attr)
5316 {
5317   if ((attr->type & 1) && attr->i != 0)
5318     return FALSE;
5319   if ((attr->type & 2) && attr->s && *attr->s)
5320     return FALSE;
5321
5322   return TRUE;
5323 }
5324
5325 /* Return the size of a single attribute.  */
5326 static bfd_vma
5327 eabi_attr_size(int tag, aeabi_attribute *attr)
5328 {
5329   bfd_vma size;
5330
5331   if (is_default_attr (attr))
5332     return 0;
5333
5334   size = uleb128_size (tag);
5335   if (attr->type & 1)
5336     size += uleb128_size (attr->i);
5337   if (attr->type & 2)
5338     size += strlen ((char *)attr->s) + 1;
5339   return size;
5340 }
5341   
5342 /* Returns the size of the eabi object attributess section.  */
5343 bfd_vma
5344 elf32_arm_eabi_attr_size (bfd *abfd)
5345 {
5346   bfd_vma size;
5347   aeabi_attribute *attr;
5348   aeabi_attribute_list *list;
5349   int i;
5350
5351   attr = elf32_arm_tdata (abfd)->known_eabi_attributes;
5352   size = 16; /* 'A' <size> "aeabi" 0x1 <size>.  */
5353   for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
5354     size += eabi_attr_size (i, &attr[i]);
5355
5356   for (list = elf32_arm_tdata (abfd)->other_eabi_attributes;
5357        list;
5358        list = list->next)
5359     size += eabi_attr_size (list->tag, &list->attr);
5360
5361   return size;
5362 }
5363
5364 static bfd_byte *
5365 write_uleb128 (bfd_byte *p, unsigned int val)
5366 {
5367   bfd_byte c;
5368   do
5369     {
5370       c = val & 0x7f;
5371       val >>= 7;
5372       if (val)
5373         c |= 0x80;
5374       *(p++) = c;
5375     }
5376   while (val);
5377   return p;
5378 }
5379
5380 /* Write attribute ATTR to butter P, and return a pointer to the following
5381    byte.  */
5382 static bfd_byte *
5383 write_eabi_attribute (bfd_byte *p, int tag, aeabi_attribute *attr)
5384 {
5385   /* Suppress default entries.  */
5386   if (is_default_attr(attr))
5387     return p;
5388
5389   p = write_uleb128 (p, tag);
5390   if (attr->type & 1)
5391     p = write_uleb128 (p, attr->i);
5392   if (attr->type & 2)
5393     {
5394       int len;
5395
5396       len = strlen (attr->s) + 1;
5397       memcpy (p, attr->s, len);
5398       p += len;
5399     }
5400
5401   return p;
5402 }
5403
5404 /* Write the contents of the eabi attributes section to p.  */
5405 void
5406 elf32_arm_set_eabi_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
5407 {
5408   bfd_byte *p;
5409   aeabi_attribute *attr;
5410   aeabi_attribute_list *list;
5411   int i;
5412
5413   p = contents;
5414   *(p++) = 'A';
5415   bfd_put_32 (abfd, size - 1, p);
5416   p += 4;
5417   memcpy (p, "aeabi", 6);
5418   p += 6;
5419   *(p++) = Tag_File;
5420   bfd_put_32 (abfd, size - 11, p);
5421   p += 4;
5422
5423   attr = elf32_arm_tdata (abfd)->known_eabi_attributes;
5424   for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
5425     p = write_eabi_attribute (p, i, &attr[i]);
5426
5427   for (list = elf32_arm_tdata (abfd)->other_eabi_attributes;
5428        list;
5429        list = list->next)
5430     p = write_eabi_attribute (p, list->tag, &list->attr);
5431 }
5432
5433 /* Override final_link to handle EABI object attribute sections.  */
5434
5435 static bfd_boolean
5436 elf32_arm_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5437 {
5438   asection *o;
5439   struct bfd_link_order *p;
5440   asection *attr_section = NULL;
5441   bfd_byte *contents;
5442   bfd_vma size = 0;
5443
5444   /* elf32_arm_merge_private_bfd_data will already have merged the
5445      object attributes.  Remove the input sections from the link, and set
5446      the contents of the output secton.  */
5447   for (o = abfd->sections; o != NULL; o = o->next)
5448     {
5449       if (strcmp (o->name, ".ARM.attributes") == 0)
5450         {
5451           for (p = o->map_head.link_order; p != NULL; p = p->next)
5452             {
5453               asection *input_section;
5454
5455               if (p->type != bfd_indirect_link_order)
5456                 continue;
5457               input_section = p->u.indirect.section;
5458               /* Hack: reset the SEC_HAS_CONTENTS flag so that
5459                  elf_link_input_bfd ignores this section.  */
5460               input_section->flags &= ~SEC_HAS_CONTENTS;
5461             }
5462             
5463           size = elf32_arm_eabi_attr_size (abfd);
5464           bfd_set_section_size (abfd, o, size);
5465           attr_section = o;
5466           /* Skip this section later on.  */
5467           o->map_head.link_order = NULL;
5468         }
5469     }
5470   /* Invoke the ELF linker to do all the work.  */
5471   if (!bfd_elf_final_link (abfd, info))
5472     return FALSE;
5473
5474   if (attr_section)
5475     {
5476       contents = bfd_malloc(size);
5477       if (contents == NULL)
5478         return FALSE;
5479       elf32_arm_set_eabi_attr_contents (abfd, contents, size);
5480       bfd_set_section_contents (abfd, attr_section, contents, 0, size);
5481       free (contents);
5482     }
5483   return TRUE;
5484 }
5485
5486
5487 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
5488 static void
5489 arm_add_to_rel (bfd *              abfd,
5490                 bfd_byte *         address,
5491                 reloc_howto_type * howto,
5492                 bfd_signed_vma     increment)
5493 {
5494   bfd_signed_vma addend;
5495
5496   if (howto->type == R_ARM_THM_CALL)
5497     {
5498       int upper_insn, lower_insn;
5499       int upper, lower;
5500
5501       upper_insn = bfd_get_16 (abfd, address);
5502       lower_insn = bfd_get_16 (abfd, address + 2);
5503       upper = upper_insn & 0x7ff;
5504       lower = lower_insn & 0x7ff;
5505
5506       addend = (upper << 12) | (lower << 1);
5507       addend += increment;
5508       addend >>= 1;
5509
5510       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
5511       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
5512
5513       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
5514       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
5515     }
5516   else
5517     {
5518       bfd_vma        contents;
5519
5520       contents = bfd_get_32 (abfd, address);
5521
5522       /* Get the (signed) value from the instruction.  */
5523       addend = contents & howto->src_mask;
5524       if (addend & ((howto->src_mask + 1) >> 1))
5525         {
5526           bfd_signed_vma mask;
5527
5528           mask = -1;
5529           mask &= ~ howto->src_mask;
5530           addend |= mask;
5531         }
5532
5533       /* Add in the increment, (which is a byte value).  */
5534       switch (howto->type)
5535         {
5536         default:
5537           addend += increment;
5538           break;
5539
5540         case R_ARM_PC24:
5541         case R_ARM_PLT32:
5542         case R_ARM_CALL:
5543         case R_ARM_JUMP24:
5544           addend <<= howto->size;
5545           addend += increment;
5546
5547           /* Should we check for overflow here ?  */
5548
5549           /* Drop any undesired bits.  */
5550           addend >>= howto->rightshift;
5551           break;
5552         }
5553
5554       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
5555
5556       bfd_put_32 (abfd, contents, address);
5557     }
5558 }
5559
5560 #define IS_ARM_TLS_RELOC(R_TYPE)        \
5561   ((R_TYPE) == R_ARM_TLS_GD32           \
5562    || (R_TYPE) == R_ARM_TLS_LDO32       \
5563    || (R_TYPE) == R_ARM_TLS_LDM32       \
5564    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
5565    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
5566    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
5567    || (R_TYPE) == R_ARM_TLS_LE32        \
5568    || (R_TYPE) == R_ARM_TLS_IE32)
5569
5570 /* Relocate an ARM ELF section.  */
5571 static bfd_boolean
5572 elf32_arm_relocate_section (bfd *                  output_bfd,
5573                             struct bfd_link_info * info,
5574                             bfd *                  input_bfd,
5575                             asection *             input_section,
5576                             bfd_byte *             contents,
5577                             Elf_Internal_Rela *    relocs,
5578                             Elf_Internal_Sym *     local_syms,
5579                             asection **            local_sections)
5580 {
5581   Elf_Internal_Shdr *symtab_hdr;
5582   struct elf_link_hash_entry **sym_hashes;
5583   Elf_Internal_Rela *rel;
5584   Elf_Internal_Rela *relend;
5585   const char *name;
5586   struct elf32_arm_link_hash_table * globals;
5587
5588   globals = elf32_arm_hash_table (info);
5589   if (info->relocatable && !globals->use_rel)
5590     return TRUE;
5591
5592   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
5593   sym_hashes = elf_sym_hashes (input_bfd);
5594
5595   rel = relocs;
5596   relend = relocs + input_section->reloc_count;
5597   for (; rel < relend; rel++)
5598     {
5599       int                          r_type;
5600       reloc_howto_type *           howto;
5601       unsigned long                r_symndx;
5602       Elf_Internal_Sym *           sym;
5603       asection *                   sec;
5604       struct elf_link_hash_entry * h;
5605       bfd_vma                      relocation;
5606       bfd_reloc_status_type        r;
5607       arelent                      bfd_reloc;
5608       char                         sym_type;
5609       bfd_boolean                  unresolved_reloc = FALSE;
5610
5611       r_symndx = ELF32_R_SYM (rel->r_info);
5612       r_type   = ELF32_R_TYPE (rel->r_info);
5613       r_type   = arm_real_reloc_type (globals, r_type);
5614
5615       if (   r_type == R_ARM_GNU_VTENTRY
5616           || r_type == R_ARM_GNU_VTINHERIT)
5617         continue;
5618
5619       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
5620       howto = bfd_reloc.howto;
5621
5622       if (info->relocatable && globals->use_rel)
5623         {
5624           /* This is a relocatable link.  We don't have to change
5625              anything, unless the reloc is against a section symbol,
5626              in which case we have to adjust according to where the
5627              section symbol winds up in the output section.  */
5628           if (r_symndx < symtab_hdr->sh_info)
5629             {
5630               sym = local_syms + r_symndx;
5631               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5632                 {
5633                   sec = local_sections[r_symndx];
5634                   arm_add_to_rel (input_bfd, contents + rel->r_offset,
5635                                   howto,
5636                                   (bfd_signed_vma) (sec->output_offset
5637                                                     + sym->st_value));
5638                 }
5639             }
5640
5641           continue;
5642         }
5643
5644       /* This is a final link.  */
5645       h = NULL;
5646       sym = NULL;
5647       sec = NULL;
5648
5649       if (r_symndx < symtab_hdr->sh_info)
5650         {
5651           sym = local_syms + r_symndx;
5652           sym_type = ELF32_ST_TYPE (sym->st_info);
5653           sec = local_sections[r_symndx];
5654           if (globals->use_rel)
5655             {
5656               relocation = (sec->output_section->vma
5657                             + sec->output_offset
5658                             + sym->st_value);
5659               if ((sec->flags & SEC_MERGE)
5660                        && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5661                 {
5662                   asection *msec;
5663                   bfd_vma addend, value;
5664
5665                   if (howto->rightshift)
5666                     {
5667                       (*_bfd_error_handler)
5668                         (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
5669                          input_bfd, input_section,
5670                          (long) rel->r_offset, howto->name);
5671                       return FALSE;
5672                     }
5673
5674                   value = bfd_get_32 (input_bfd, contents + rel->r_offset);
5675
5676                   /* Get the (signed) value from the instruction.  */
5677                   addend = value & howto->src_mask;
5678                   if (addend & ((howto->src_mask + 1) >> 1))
5679                     {
5680                       bfd_signed_vma mask;
5681
5682                       mask = -1;
5683                       mask &= ~ howto->src_mask;
5684                       addend |= mask;
5685                     }
5686                   msec = sec;
5687                   addend =
5688                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
5689                     - relocation;
5690                   addend += msec->output_section->vma + msec->output_offset;
5691                   value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
5692                   bfd_put_32 (input_bfd, value, contents + rel->r_offset);
5693                 }
5694             }
5695           else
5696             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5697         }
5698       else
5699         {
5700           bfd_boolean warned;
5701
5702           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5703                                    r_symndx, symtab_hdr, sym_hashes,
5704                                    h, sec, relocation,
5705                                    unresolved_reloc, warned);
5706
5707           sym_type = h->type;
5708         }
5709
5710       if (h != NULL)
5711         name = h->root.root.string;
5712       else
5713         {
5714           name = (bfd_elf_string_from_elf_section
5715                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
5716           if (name == NULL || *name == '\0')
5717             name = bfd_section_name (input_bfd, sec);
5718         }
5719
5720       if (r_symndx != 0
5721           && r_type != R_ARM_NONE
5722           && (h == NULL
5723               || h->root.type == bfd_link_hash_defined
5724               || h->root.type == bfd_link_hash_defweak)
5725           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
5726         {
5727           (*_bfd_error_handler)
5728             ((sym_type == STT_TLS
5729               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
5730               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
5731              input_bfd,
5732              input_section,
5733              (long) rel->r_offset,
5734              howto->name,
5735              name);
5736         }
5737
5738       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
5739                                          input_section, contents, rel,
5740                                          relocation, info, sec, name,
5741                                          (h ? ELF_ST_TYPE (h->type) :
5742                                           ELF_ST_TYPE (sym->st_info)), h,
5743                                          &unresolved_reloc);
5744
5745       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5746          because such sections are not SEC_ALLOC and thus ld.so will
5747          not process them.  */
5748       if (unresolved_reloc
5749           && !((input_section->flags & SEC_DEBUGGING) != 0
5750                && h->def_dynamic))
5751         {
5752           (*_bfd_error_handler)
5753             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5754              input_bfd,
5755              input_section,
5756              (long) rel->r_offset,
5757              howto->name,
5758              h->root.root.string);
5759           return FALSE;
5760         }
5761
5762       if (r != bfd_reloc_ok)
5763         {
5764           const char * msg = (const char *) 0;
5765
5766           switch (r)
5767             {
5768             case bfd_reloc_overflow:
5769               /* If the overflowing reloc was to an undefined symbol,
5770                  we have already printed one error message and there
5771                  is no point complaining again.  */
5772               if ((! h ||
5773                    h->root.type != bfd_link_hash_undefined)
5774                   && (!((*info->callbacks->reloc_overflow)
5775                         (info, (h ? &h->root : NULL), name, howto->name,
5776                          (bfd_vma) 0, input_bfd, input_section,
5777                          rel->r_offset))))
5778                   return FALSE;
5779               break;
5780
5781             case bfd_reloc_undefined:
5782               if (!((*info->callbacks->undefined_symbol)
5783                     (info, name, input_bfd, input_section,
5784                      rel->r_offset, TRUE)))
5785                 return FALSE;
5786               break;
5787
5788             case bfd_reloc_outofrange:
5789               msg = _("internal error: out of range error");
5790               goto common_error;
5791
5792             case bfd_reloc_notsupported:
5793               msg = _("internal error: unsupported relocation error");
5794               goto common_error;
5795
5796             case bfd_reloc_dangerous:
5797               msg = _("internal error: dangerous error");
5798               goto common_error;
5799
5800             default:
5801               msg = _("internal error: unknown error");
5802               /* fall through */
5803
5804             common_error:
5805               if (!((*info->callbacks->warning)
5806                     (info, msg, name, input_bfd, input_section,
5807                      rel->r_offset)))
5808                 return FALSE;
5809               break;
5810             }
5811         }
5812     }
5813
5814   return TRUE;
5815 }
5816
5817 /* Allocate/find an object attribute.  */
5818 static aeabi_attribute *
5819 elf32_arm_new_eabi_attr (bfd *abfd, int tag)
5820 {
5821   aeabi_attribute *attr;
5822   aeabi_attribute_list *list;
5823   aeabi_attribute_list *p;
5824   aeabi_attribute_list **lastp;
5825
5826
5827   if (tag < NUM_KNOWN_ATTRIBUTES)
5828     {
5829       /* Knwon tags are preallocated.  */
5830       attr = &elf32_arm_tdata (abfd)->known_eabi_attributes[tag];
5831     }
5832   else
5833     {
5834       /* Create a new tag.  */
5835       list = (aeabi_attribute_list *)
5836         bfd_alloc (abfd, sizeof (aeabi_attribute_list));
5837       memset (list, 0, sizeof (aeabi_attribute_list));
5838       list->tag = tag;
5839       /* Keep the tag list in order.  */
5840       lastp = &elf32_arm_tdata (abfd)->other_eabi_attributes;
5841       for (p = *lastp; p; p = p->next)
5842         {
5843           if (tag < p->tag)
5844             break;
5845           lastp = &p->next;
5846         }
5847       list->next = *lastp;
5848       *lastp = list;
5849       attr = &list->attr;
5850     }
5851
5852   return attr;
5853 }
5854
5855 int
5856 elf32_arm_get_eabi_attr_int (bfd *abfd, int tag)
5857 {
5858   aeabi_attribute_list *p;
5859
5860   if (tag < NUM_KNOWN_ATTRIBUTES)
5861     {
5862       /* Knwon tags are preallocated.  */
5863       return elf32_arm_tdata (abfd)->known_eabi_attributes[tag].i;
5864     }
5865   else
5866     {
5867       for (p = elf32_arm_tdata (abfd)->other_eabi_attributes;
5868            p;
5869            p = p->next)
5870         {
5871           if (tag == p->tag)
5872             return p->attr.i;
5873           if (tag < p->tag)
5874             break;
5875         }
5876       return 0;
5877     }
5878 }
5879
5880 void
5881 elf32_arm_add_eabi_attr_int (bfd *abfd, int tag, unsigned int i)
5882 {
5883   aeabi_attribute *attr;
5884
5885   attr = elf32_arm_new_eabi_attr (abfd, tag);
5886   attr->type = 1;
5887   attr->i = i;
5888 }
5889
5890 static char *
5891 attr_strdup (bfd *abfd, const char * s)
5892 {
5893   char * p;
5894   int len;
5895   
5896   len = strlen (s) + 1;
5897   p = (char *)bfd_alloc(abfd, len);
5898   return memcpy (p, s, len);
5899 }
5900
5901 void
5902 elf32_arm_add_eabi_attr_string (bfd *abfd, int tag, const char *s)
5903 {
5904   aeabi_attribute *attr;
5905
5906   attr = elf32_arm_new_eabi_attr (abfd, tag);
5907   attr->type = 2;
5908   attr->s = attr_strdup (abfd, s);
5909 }
5910
5911 void
5912 elf32_arm_add_eabi_attr_compat (bfd *abfd, unsigned int i, const char *s)
5913 {
5914   aeabi_attribute_list *list;
5915   aeabi_attribute_list *p;
5916   aeabi_attribute_list **lastp;
5917
5918   list = (aeabi_attribute_list *)
5919     bfd_alloc (abfd, sizeof (aeabi_attribute_list));
5920   memset (list, 0, sizeof (aeabi_attribute_list));
5921   list->tag = Tag_compatibility;
5922   list->attr.type = 3;
5923   list->attr.i = i;
5924   list->attr.s = attr_strdup (abfd, s);
5925
5926   lastp = &elf32_arm_tdata (abfd)->other_eabi_attributes;
5927   for (p = *lastp; p; p = p->next)
5928     {
5929       int cmp;
5930       if (p->tag != Tag_compatibility)
5931         break;
5932       cmp = strcmp(s, p->attr.s);
5933       if (cmp < 0 || (cmp == 0 && i < p->attr.i))
5934         break;
5935       lastp = &p->next;
5936     }
5937   list->next = *lastp;
5938   *lastp = list;
5939 }
5940
5941 /* Set the right machine number.  */
5942
5943 static bfd_boolean
5944 elf32_arm_object_p (bfd *abfd)
5945 {
5946   unsigned int mach;
5947
5948   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
5949
5950   if (mach != bfd_mach_arm_unknown)
5951     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
5952
5953   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
5954     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
5955
5956   else
5957     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
5958
5959   return TRUE;
5960 }
5961
5962 /* Function to keep ARM specific flags in the ELF header.  */
5963
5964 static bfd_boolean
5965 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
5966 {
5967   if (elf_flags_init (abfd)
5968       && elf_elfheader (abfd)->e_flags != flags)
5969     {
5970       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
5971         {
5972           if (flags & EF_ARM_INTERWORK)
5973             (*_bfd_error_handler)
5974               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
5975                abfd);
5976           else
5977             _bfd_error_handler
5978               (_("Warning: Clearing the interworking flag of %B due to outside request"),
5979                abfd);
5980         }
5981     }
5982   else
5983     {
5984       elf_elfheader (abfd)->e_flags = flags;
5985       elf_flags_init (abfd) = TRUE;
5986     }
5987
5988   return TRUE;
5989 }
5990
5991 /* Copy the eabi object attribute from IBFD to OBFD.  */
5992 static void
5993 copy_eabi_attributes (bfd *ibfd, bfd *obfd)
5994 {
5995   aeabi_attribute *in_attr;
5996   aeabi_attribute *out_attr;
5997   aeabi_attribute_list *list;
5998   int i;
5999
6000   in_attr = elf32_arm_tdata (ibfd)->known_eabi_attributes;
6001   out_attr = elf32_arm_tdata (obfd)->known_eabi_attributes;
6002   for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
6003     {
6004       out_attr->i = in_attr->i;
6005       if (in_attr->s && *in_attr->s)
6006         out_attr->s = attr_strdup (obfd, in_attr->s);
6007       in_attr++;
6008       out_attr++;
6009     }
6010
6011   for (list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
6012        list;
6013        list = list->next)
6014     {
6015       in_attr = &list->attr;
6016       switch (in_attr->type)
6017         {
6018         case 1:
6019           elf32_arm_add_eabi_attr_int (obfd, list->tag, in_attr->i);
6020           break;
6021         case 2:
6022           elf32_arm_add_eabi_attr_string (obfd, list->tag, in_attr->s);
6023           break;
6024         case 3:
6025           elf32_arm_add_eabi_attr_compat (obfd, in_attr->i, in_attr->s);
6026           break;
6027         default:
6028           abort();
6029         }
6030     }
6031 }
6032
6033
6034 /* Copy backend specific data from one object module to another.  */
6035
6036 static bfd_boolean
6037 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6038 {
6039   flagword in_flags;
6040   flagword out_flags;
6041
6042   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6043       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6044     return TRUE;
6045
6046   in_flags  = elf_elfheader (ibfd)->e_flags;
6047   out_flags = elf_elfheader (obfd)->e_flags;
6048
6049   if (elf_flags_init (obfd)
6050       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
6051       && in_flags != out_flags)
6052     {
6053       /* Cannot mix APCS26 and APCS32 code.  */
6054       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
6055         return FALSE;
6056
6057       /* Cannot mix float APCS and non-float APCS code.  */
6058       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
6059         return FALSE;
6060
6061       /* If the src and dest have different interworking flags
6062          then turn off the interworking bit.  */
6063       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
6064         {
6065           if (out_flags & EF_ARM_INTERWORK)
6066             _bfd_error_handler
6067               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
6068                obfd, ibfd);
6069
6070           in_flags &= ~EF_ARM_INTERWORK;
6071         }
6072
6073       /* Likewise for PIC, though don't warn for this case.  */
6074       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
6075         in_flags &= ~EF_ARM_PIC;
6076     }
6077
6078   elf_elfheader (obfd)->e_flags = in_flags;
6079   elf_flags_init (obfd) = TRUE;
6080
6081   /* Also copy the EI_OSABI field.  */
6082   elf_elfheader (obfd)->e_ident[EI_OSABI] =
6083     elf_elfheader (ibfd)->e_ident[EI_OSABI];
6084
6085   /* Copy EABI object attributes.  */
6086   copy_eabi_attributes (ibfd, obfd);
6087
6088   return TRUE;
6089 }
6090
6091 /* Values for Tag_ABI_PCS_R9_use.  */
6092 enum
6093 {
6094   AEABI_R9_V6,
6095   AEABI_R9_SB,
6096   AEABI_R9_TLS,
6097   AEABI_R9_unused
6098 };
6099
6100 /* Values for Tag_ABI_PCS_RW_data.  */
6101 enum
6102 {
6103   AEABI_PCS_RW_data_absolute,
6104   AEABI_PCS_RW_data_PCrel,
6105   AEABI_PCS_RW_data_SBrel,
6106   AEABI_PCS_RW_data_unused
6107 };
6108
6109 /* Values for Tag_ABI_enum_size.  */
6110 enum
6111 {
6112   AEABI_enum_unused,
6113   AEABI_enum_short,
6114   AEABI_enum_wide,
6115   AEABI_enum_forced_wide
6116 };
6117
6118 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
6119    are conflicting attributes.  */
6120 static bfd_boolean
6121 elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
6122 {
6123   aeabi_attribute *in_attr;
6124   aeabi_attribute *out_attr;
6125   aeabi_attribute_list *in_list;
6126   aeabi_attribute_list *out_list;
6127   /* Some tags have 0 = don't care, 1 = strong requirement,
6128      2 = weak requirement.  */
6129   static const int order_312[3] = {3, 1, 2};
6130   int i;
6131
6132   if (!elf32_arm_tdata (ibfd)->known_eabi_attributes[0].i)
6133     {
6134       /* This is the first object.  Copy the attributes.  */
6135       copy_eabi_attributes (ibfd, obfd);
6136       return TRUE;
6137     }
6138
6139   /* Use the Tag_null value to indicate the attributes have been
6140      initialized.  */
6141   elf32_arm_tdata (ibfd)->known_eabi_attributes[0].i = 1;
6142
6143   in_attr = elf32_arm_tdata (ibfd)->known_eabi_attributes;
6144   out_attr = elf32_arm_tdata (obfd)->known_eabi_attributes;
6145   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
6146   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
6147     {
6148       /* Ignore mismatches if teh object doesn't use floating point.  */
6149       if (out_attr[Tag_ABI_FP_number_model].i == 0)
6150         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
6151       else if (in_attr[Tag_ABI_FP_number_model].i != 0)
6152         {
6153           _bfd_error_handler
6154             (_("ERROR: %B uses VFP register arguments, %B does not"),
6155              ibfd, obfd);
6156           return FALSE;
6157         }
6158     }
6159
6160   for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
6161     {
6162       /* Merge this attribute with existing attributes.  */
6163       switch (i)
6164         {
6165         case Tag_CPU_raw_name:
6166         case Tag_CPU_name:
6167           /* Use whichever has the greatest architecture requirements.  */
6168           if (in_attr[Tag_CPU_arch].i > out_attr[Tag_CPU_arch].i)
6169             out_attr[i].s = attr_strdup(obfd, in_attr[i].s);
6170           break;
6171
6172         case Tag_ABI_optimization_goals:
6173         case Tag_ABI_FP_optimization_goals:
6174           /* Use the first value seen.  */
6175           break;
6176
6177         case Tag_CPU_arch:
6178         case Tag_ARM_ISA_use:
6179         case Tag_THUMB_ISA_use:
6180         case Tag_VFP_arch:
6181         case Tag_WMMX_arch:
6182         case Tag_NEON_arch:
6183           /* ??? Do NEON and WMMX conflict?  */
6184         case Tag_ABI_FP_rounding:
6185         case Tag_ABI_FP_denormal:
6186         case Tag_ABI_FP_exceptions:
6187         case Tag_ABI_FP_user_exceptions:
6188         case Tag_ABI_FP_number_model:
6189         case Tag_ABI_align8_preserved:
6190         case Tag_ABI_HardFP_use:
6191           /* Use the largest value specified.  */
6192           if (in_attr[i].i > out_attr[i].i)
6193             out_attr[i].i = in_attr[i].i;
6194           break;
6195
6196         case Tag_CPU_arch_profile:
6197           /* Warn if conflicting architecture profiles used.  */
6198           if (out_attr[i].i && in_attr[i].i && in_attr[i].i != out_attr[i].i)
6199             {
6200               _bfd_error_handler
6201                 (_("ERROR: %B: Conflicting architecture profiles %c/%c"),
6202                  ibfd, in_attr[i].i, out_attr[i].i);
6203               return FALSE;
6204             }
6205           if (in_attr[i].i)
6206             out_attr[i].i = in_attr[i].i;
6207           break;
6208         case Tag_PCS_config:
6209           if (out_attr[i].i == 0)
6210             out_attr[i].i = in_attr[i].i;
6211           else if (in_attr[i].i != 0 && out_attr[i].i != 0)
6212             {
6213               /* It's sometimes ok to mix different configs, so this is only
6214                  a warning.  */
6215               _bfd_error_handler
6216                 (_("Warning: %B: Conflicting platform configuration"), ibfd);
6217             }
6218           break;
6219         case Tag_ABI_PCS_R9_use:
6220           if (out_attr[i].i != AEABI_R9_unused
6221               && in_attr[i].i != AEABI_R9_unused)
6222             {
6223               _bfd_error_handler
6224                 (_("ERROR: %B: Conflicting use of R9"), ibfd);
6225               return FALSE;
6226             }
6227           if (out_attr[i].i == AEABI_R9_unused)
6228             out_attr[i].i = in_attr[i].i;
6229           break;
6230         case Tag_ABI_PCS_RW_data:
6231           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
6232               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
6233               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
6234             {
6235               _bfd_error_handler
6236                 (_("ERROR: %B: SB relative addressing conflicts with use of R9"),
6237                  ibfd);
6238               return FALSE;
6239             }
6240           /* Use the smallest value specified.  */
6241           if (in_attr[i].i < out_attr[i].i)
6242             out_attr[i].i = in_attr[i].i;
6243           break;
6244         case Tag_ABI_PCS_RO_data:
6245           /* Use the smallest value specified.  */
6246           if (in_attr[i].i < out_attr[i].i)
6247             out_attr[i].i = in_attr[i].i;
6248           break;
6249         case Tag_ABI_PCS_GOT_use:
6250           if (in_attr[i].i > 2 || out_attr[i].i > 2
6251               || order_312[in_attr[i].i] < order_312[out_attr[i].i])
6252             out_attr[i].i = in_attr[i].i;
6253           break;
6254         case Tag_ABI_PCS_wchar_t:
6255           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i)
6256             {
6257               _bfd_error_handler
6258                 (_("ERROR: %B: Conflicting definitions of wchar_t"), ibfd);
6259               return FALSE;
6260             }
6261           if (in_attr[i].i)
6262             out_attr[i].i = in_attr[i].i;
6263           break;
6264         case Tag_ABI_align8_needed:
6265           /* ??? Check against Tag_ABI_align8_preserved.  */
6266           if (in_attr[i].i > 2 || out_attr[i].i > 2
6267               || order_312[in_attr[i].i] < order_312[out_attr[i].i])
6268             out_attr[i].i = in_attr[i].i;
6269           break;
6270         case Tag_ABI_enum_size:
6271           if (in_attr[i].i != AEABI_enum_unused)
6272             {
6273               if (out_attr[i].i == AEABI_enum_unused
6274                   || out_attr[i].i == AEABI_enum_forced_wide)
6275                 {
6276                   /* The existing object is compatible with anything.
6277                      Use whatever requirements the new object has.  */
6278                   out_attr[i].i = in_attr[i].i;
6279                 }
6280               else if (in_attr[i].i != AEABI_enum_forced_wide
6281                        && out_attr[i].i != in_attr[i].i)
6282                 {
6283                   _bfd_error_handler
6284                     (_("ERROR: %B: Conflicting enum sizes"), ibfd);
6285                 }
6286             }
6287           break;
6288         case Tag_ABI_VFP_args:
6289           /* Aready done.  */
6290           break;
6291         case Tag_ABI_WMMX_args:
6292           if (in_attr[i].i != out_attr[i].i)
6293             {
6294               _bfd_error_handler
6295                 (_("ERROR: %B uses iWMMXt register arguments, %B does not"),
6296                  ibfd, obfd);
6297               return FALSE;
6298             }
6299           break;
6300         default: /* All known attributes should be explicitly covered.   */
6301           abort ();
6302         }
6303     }
6304
6305   in_list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
6306   out_list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
6307   while (in_list && in_list->tag == Tag_compatibility)
6308     {
6309       in_attr = &in_list->attr;
6310       if (in_attr->i == 0)
6311         continue;
6312       if (in_attr->i == 1)
6313         {
6314           _bfd_error_handler
6315             (_("ERROR: %B: Must be processed by '%s' toolchain"),
6316              ibfd, in_attr->s);
6317           return FALSE;
6318         }
6319       if (!out_list || out_list->tag != Tag_compatibility
6320           || strcmp (in_attr->s, out_list->attr.s) != 0)
6321         {
6322           /* Add this compatibility tag to the output.  */
6323           elf32_arm_add_eabi_attr_compat (obfd, in_attr->i, in_attr->s);
6324           continue;
6325         }
6326       out_attr = &out_list->attr;
6327       /* Check all the input tags with the same identifier.  */
6328       for (;;)
6329         {
6330           if (out_list->tag != Tag_compatibility
6331               || in_attr->i != out_attr->i
6332               || strcmp (in_attr->s, out_attr->s) != 0)
6333             {
6334               _bfd_error_handler
6335                 (_("ERROR: %B: Incompatible object tag '%s':%d"),
6336                  ibfd, in_attr->s, in_attr->i);
6337               return FALSE;
6338             }
6339           in_list = in_list->next;
6340           if (in_list->tag != Tag_compatibility
6341               || strcmp (in_attr->s, in_list->attr.s) != 0)
6342             break;
6343           in_attr = &in_list->attr;
6344           out_list = out_list->next;
6345           if (out_list)
6346             out_attr = &out_list->attr;
6347         }
6348
6349       /* Check the output doesn't have extra tags with this identifier.  */
6350       if (out_list && out_list->tag == Tag_compatibility
6351           && strcmp (in_attr->s, out_list->attr.s) == 0)
6352         {
6353           _bfd_error_handler
6354             (_("ERROR: %B: Incompatible object tag '%s':%d"),
6355              ibfd, in_attr->s, out_list->attr.i);
6356           return FALSE;
6357         }
6358     }
6359
6360   for (; in_list; in_list = in_list->next)
6361     {
6362       if ((in_list->tag & 128) < 64)
6363         {
6364           _bfd_error_handler
6365             (_("Warning: %B: Unknown EABI object attribute %d"),
6366              ibfd, in_list->tag);
6367           break;
6368         }
6369     }
6370   return TRUE;
6371 }
6372
6373
6374 /* Return TRUE if the two EABI versions are incompatible.  */
6375
6376 static bfd_boolean
6377 elf32_arm_versions_compatible (unsigned iver, unsigned over)
6378 {
6379   /* v4 and v5 are the same spec before and after it was released,
6380      so allow mixing them.  */
6381   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
6382       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
6383     return TRUE;
6384
6385   return (iver == over);
6386 }
6387
6388 /* Merge backend specific data from an object file to the output
6389    object file when linking.  */
6390
6391 static bfd_boolean
6392 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
6393 {
6394   flagword out_flags;
6395   flagword in_flags;
6396   bfd_boolean flags_compatible = TRUE;
6397   asection *sec;
6398
6399   /* Check if we have the same endianess.  */
6400   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
6401     return FALSE;
6402
6403   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6404       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6405     return TRUE;
6406
6407   if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
6408     return FALSE;
6409
6410   /* The input BFD must have had its flags initialised.  */
6411   /* The following seems bogus to me -- The flags are initialized in
6412      the assembler but I don't think an elf_flags_init field is
6413      written into the object.  */
6414   /* BFD_ASSERT (elf_flags_init (ibfd)); */
6415
6416   in_flags  = elf_elfheader (ibfd)->e_flags;
6417   out_flags = elf_elfheader (obfd)->e_flags;
6418
6419   if (!elf_flags_init (obfd))
6420     {
6421       /* If the input is the default architecture and had the default
6422          flags then do not bother setting the flags for the output
6423          architecture, instead allow future merges to do this.  If no
6424          future merges ever set these flags then they will retain their
6425          uninitialised values, which surprise surprise, correspond
6426          to the default values.  */
6427       if (bfd_get_arch_info (ibfd)->the_default
6428           && elf_elfheader (ibfd)->e_flags == 0)
6429         return TRUE;
6430
6431       elf_flags_init (obfd) = TRUE;
6432       elf_elfheader (obfd)->e_flags = in_flags;
6433
6434       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6435           && bfd_get_arch_info (obfd)->the_default)
6436         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
6437
6438       return TRUE;
6439     }
6440
6441   /* Determine what should happen if the input ARM architecture
6442      does not match the output ARM architecture.  */
6443   if (! bfd_arm_merge_machines (ibfd, obfd))
6444     return FALSE;
6445
6446   /* Identical flags must be compatible.  */
6447   if (in_flags == out_flags)
6448     return TRUE;
6449
6450   /* Check to see if the input BFD actually contains any sections.  If
6451      not, its flags may not have been initialised either, but it
6452      cannot actually cause any incompatiblity.  Do not short-circuit
6453      dynamic objects; their section list may be emptied by
6454     elf_link_add_object_symbols.
6455
6456     Also check to see if there are no code sections in the input.
6457     In this case there is no need to check for code specific flags.
6458     XXX - do we need to worry about floating-point format compatability
6459     in data sections ?  */
6460   if (!(ibfd->flags & DYNAMIC))
6461     {
6462       bfd_boolean null_input_bfd = TRUE;
6463       bfd_boolean only_data_sections = TRUE;
6464
6465       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6466         {
6467           /* Ignore synthetic glue sections.  */
6468           if (strcmp (sec->name, ".glue_7")
6469               && strcmp (sec->name, ".glue_7t"))
6470             {
6471               if ((bfd_get_section_flags (ibfd, sec)
6472                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6473                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6474                 only_data_sections = FALSE;
6475
6476               null_input_bfd = FALSE;
6477               break;
6478             }
6479         }
6480
6481       if (null_input_bfd || only_data_sections)
6482         return TRUE;
6483     }
6484
6485   /* Complain about various flag mismatches.  */
6486   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
6487                                       EF_ARM_EABI_VERSION (out_flags)))
6488     {
6489       _bfd_error_handler
6490         (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
6491          ibfd, obfd,
6492          (in_flags & EF_ARM_EABIMASK) >> 24,
6493          (out_flags & EF_ARM_EABIMASK) >> 24);
6494       return FALSE;
6495     }
6496
6497   /* Not sure what needs to be checked for EABI versions >= 1.  */
6498   /* VxWorks libraries do not use these flags.  */
6499   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
6500       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
6501       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
6502     {
6503       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
6504         {
6505           _bfd_error_handler
6506             (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
6507              ibfd, obfd,
6508              in_flags & EF_ARM_APCS_26 ? 26 : 32,
6509              out_flags & EF_ARM_APCS_26 ? 26 : 32);
6510           flags_compatible = FALSE;
6511         }
6512
6513       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
6514         {
6515           if (in_flags & EF_ARM_APCS_FLOAT)
6516             _bfd_error_handler
6517               (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
6518                ibfd, obfd);
6519           else
6520             _bfd_error_handler
6521               (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
6522                ibfd, obfd);
6523
6524           flags_compatible = FALSE;
6525         }
6526
6527       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
6528         {
6529           if (in_flags & EF_ARM_VFP_FLOAT)
6530             _bfd_error_handler
6531               (_("ERROR: %B uses VFP instructions, whereas %B does not"),
6532                ibfd, obfd);
6533           else
6534             _bfd_error_handler
6535               (_("ERROR: %B uses FPA instructions, whereas %B does not"),
6536                ibfd, obfd);
6537
6538           flags_compatible = FALSE;
6539         }
6540
6541       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
6542         {
6543           if (in_flags & EF_ARM_MAVERICK_FLOAT)
6544             _bfd_error_handler
6545               (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
6546                ibfd, obfd);
6547           else
6548             _bfd_error_handler
6549               (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
6550                ibfd, obfd);
6551
6552           flags_compatible = FALSE;
6553         }
6554
6555 #ifdef EF_ARM_SOFT_FLOAT
6556       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
6557         {
6558           /* We can allow interworking between code that is VFP format
6559              layout, and uses either soft float or integer regs for
6560              passing floating point arguments and results.  We already
6561              know that the APCS_FLOAT flags match; similarly for VFP
6562              flags.  */
6563           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
6564               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
6565             {
6566               if (in_flags & EF_ARM_SOFT_FLOAT)
6567                 _bfd_error_handler
6568                   (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
6569                    ibfd, obfd);
6570               else
6571                 _bfd_error_handler
6572                   (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
6573                    ibfd, obfd);
6574
6575               flags_compatible = FALSE;
6576             }
6577         }
6578 #endif
6579
6580       /* Interworking mismatch is only a warning.  */
6581       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
6582         {
6583           if (in_flags & EF_ARM_INTERWORK)
6584             {
6585               _bfd_error_handler
6586                 (_("Warning: %B supports interworking, whereas %B does not"),
6587                  ibfd, obfd);
6588             }
6589           else
6590             {
6591               _bfd_error_handler
6592                 (_("Warning: %B does not support interworking, whereas %B does"),
6593                  ibfd, obfd);
6594             }
6595         }
6596     }
6597
6598   return flags_compatible;
6599 }
6600
6601 /* Display the flags field.  */
6602
6603 static bfd_boolean
6604 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
6605 {
6606   FILE * file = (FILE *) ptr;
6607   unsigned long flags;
6608
6609   BFD_ASSERT (abfd != NULL && ptr != NULL);
6610
6611   /* Print normal ELF private data.  */
6612   _bfd_elf_print_private_bfd_data (abfd, ptr);
6613
6614   flags = elf_elfheader (abfd)->e_flags;
6615   /* Ignore init flag - it may not be set, despite the flags field
6616      containing valid data.  */
6617
6618   /* xgettext:c-format */
6619   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
6620
6621   switch (EF_ARM_EABI_VERSION (flags))
6622     {
6623     case EF_ARM_EABI_UNKNOWN:
6624       /* The following flag bits are GNU extensions and not part of the
6625          official ARM ELF extended ABI.  Hence they are only decoded if
6626          the EABI version is not set.  */
6627       if (flags & EF_ARM_INTERWORK)
6628         fprintf (file, _(" [interworking enabled]"));
6629
6630       if (flags & EF_ARM_APCS_26)
6631         fprintf (file, " [APCS-26]");
6632       else
6633         fprintf (file, " [APCS-32]");
6634
6635       if (flags & EF_ARM_VFP_FLOAT)
6636         fprintf (file, _(" [VFP float format]"));
6637       else if (flags & EF_ARM_MAVERICK_FLOAT)
6638         fprintf (file, _(" [Maverick float format]"));
6639       else
6640         fprintf (file, _(" [FPA float format]"));
6641
6642       if (flags & EF_ARM_APCS_FLOAT)
6643         fprintf (file, _(" [floats passed in float registers]"));
6644
6645       if (flags & EF_ARM_PIC)
6646         fprintf (file, _(" [position independent]"));
6647
6648       if (flags & EF_ARM_NEW_ABI)
6649         fprintf (file, _(" [new ABI]"));
6650
6651       if (flags & EF_ARM_OLD_ABI)
6652         fprintf (file, _(" [old ABI]"));
6653
6654       if (flags & EF_ARM_SOFT_FLOAT)
6655         fprintf (file, _(" [software FP]"));
6656
6657       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
6658                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
6659                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
6660                  | EF_ARM_MAVERICK_FLOAT);
6661       break;
6662
6663     case EF_ARM_EABI_VER1:
6664       fprintf (file, _(" [Version1 EABI]"));
6665
6666       if (flags & EF_ARM_SYMSARESORTED)
6667         fprintf (file, _(" [sorted symbol table]"));
6668       else
6669         fprintf (file, _(" [unsorted symbol table]"));
6670
6671       flags &= ~ EF_ARM_SYMSARESORTED;
6672       break;
6673
6674     case EF_ARM_EABI_VER2:
6675       fprintf (file, _(" [Version2 EABI]"));
6676
6677       if (flags & EF_ARM_SYMSARESORTED)
6678         fprintf (file, _(" [sorted symbol table]"));
6679       else
6680         fprintf (file, _(" [unsorted symbol table]"));
6681
6682       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
6683         fprintf (file, _(" [dynamic symbols use segment index]"));
6684
6685       if (flags & EF_ARM_MAPSYMSFIRST)
6686         fprintf (file, _(" [mapping symbols precede others]"));
6687
6688       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
6689                  | EF_ARM_MAPSYMSFIRST);
6690       break;
6691
6692     case EF_ARM_EABI_VER3:
6693       fprintf (file, _(" [Version3 EABI]"));
6694       break;
6695
6696     case EF_ARM_EABI_VER4:
6697       fprintf (file, _(" [Version4 EABI]"));
6698       goto eabi;
6699
6700     case EF_ARM_EABI_VER5:
6701       fprintf (file, _(" [Version5 EABI]"));
6702     eabi:
6703       if (flags & EF_ARM_BE8)
6704         fprintf (file, _(" [BE8]"));
6705
6706       if (flags & EF_ARM_LE8)
6707         fprintf (file, _(" [LE8]"));
6708
6709       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
6710       break;
6711
6712     default:
6713       fprintf (file, _(" <EABI version unrecognised>"));
6714       break;
6715     }
6716
6717   flags &= ~ EF_ARM_EABIMASK;
6718
6719   if (flags & EF_ARM_RELEXEC)
6720     fprintf (file, _(" [relocatable executable]"));
6721
6722   if (flags & EF_ARM_HASENTRY)
6723     fprintf (file, _(" [has entry point]"));
6724
6725   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
6726
6727   if (flags)
6728     fprintf (file, _("<Unrecognised flag bits set>"));
6729
6730   fputc ('\n', file);
6731
6732   return TRUE;
6733 }
6734
6735 static int
6736 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
6737 {
6738   switch (ELF_ST_TYPE (elf_sym->st_info))
6739     {
6740     case STT_ARM_TFUNC:
6741       return ELF_ST_TYPE (elf_sym->st_info);
6742
6743     case STT_ARM_16BIT:
6744       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
6745          This allows us to distinguish between data used by Thumb instructions
6746          and non-data (which is probably code) inside Thumb regions of an
6747          executable.  */
6748       if (type != STT_OBJECT && type != STT_TLS)
6749         return ELF_ST_TYPE (elf_sym->st_info);
6750       break;
6751
6752     default:
6753       break;
6754     }
6755
6756   return type;
6757 }
6758
6759 static asection *
6760 elf32_arm_gc_mark_hook (asection *                   sec,
6761                         struct bfd_link_info *       info ATTRIBUTE_UNUSED,
6762                         Elf_Internal_Rela *          rel,
6763                         struct elf_link_hash_entry * h,
6764                         Elf_Internal_Sym *           sym)
6765 {
6766   if (h != NULL)
6767     {
6768       switch (ELF32_R_TYPE (rel->r_info))
6769       {
6770       case R_ARM_GNU_VTINHERIT:
6771       case R_ARM_GNU_VTENTRY:
6772         break;
6773
6774       default:
6775         switch (h->root.type)
6776           {
6777           case bfd_link_hash_defined:
6778           case bfd_link_hash_defweak:
6779             return h->root.u.def.section;
6780
6781           case bfd_link_hash_common:
6782             return h->root.u.c.p->section;
6783
6784           default:
6785             break;
6786           }
6787        }
6788      }
6789    else
6790      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6791
6792   return NULL;
6793 }
6794
6795 /* Update the got entry reference counts for the section being removed.  */
6796
6797 static bfd_boolean
6798 elf32_arm_gc_sweep_hook (bfd *                     abfd,
6799                          struct bfd_link_info *    info,
6800                          asection *                sec,
6801                          const Elf_Internal_Rela * relocs)
6802 {
6803   Elf_Internal_Shdr *symtab_hdr;
6804   struct elf_link_hash_entry **sym_hashes;
6805   bfd_signed_vma *local_got_refcounts;
6806   const Elf_Internal_Rela *rel, *relend;
6807   struct elf32_arm_link_hash_table * globals;
6808
6809   globals = elf32_arm_hash_table (info);
6810
6811   elf_section_data (sec)->local_dynrel = NULL;
6812
6813   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6814   sym_hashes = elf_sym_hashes (abfd);
6815   local_got_refcounts = elf_local_got_refcounts (abfd);
6816
6817   relend = relocs + sec->reloc_count;
6818   for (rel = relocs; rel < relend; rel++)
6819     {
6820       unsigned long r_symndx;
6821       struct elf_link_hash_entry *h = NULL;
6822       int r_type;
6823
6824       r_symndx = ELF32_R_SYM (rel->r_info);
6825       if (r_symndx >= symtab_hdr->sh_info)
6826         {
6827           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6828           while (h->root.type == bfd_link_hash_indirect
6829                  || h->root.type == bfd_link_hash_warning)
6830             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6831         }
6832
6833       r_type = ELF32_R_TYPE (rel->r_info);
6834       r_type = arm_real_reloc_type (globals, r_type);
6835       switch (r_type)
6836         {
6837         case R_ARM_GOT32:
6838         case R_ARM_GOT_PREL:
6839         case R_ARM_TLS_GD32:
6840         case R_ARM_TLS_IE32:
6841           if (h != NULL)
6842             {
6843               if (h->got.refcount > 0)
6844                 h->got.refcount -= 1;
6845             }
6846           else if (local_got_refcounts != NULL)
6847             {
6848               if (local_got_refcounts[r_symndx] > 0)
6849                 local_got_refcounts[r_symndx] -= 1;
6850             }
6851           break;
6852
6853         case R_ARM_TLS_LDM32:
6854           elf32_arm_hash_table (info)->tls_ldm_got.refcount -= 1;
6855           break;
6856
6857         case R_ARM_ABS32:
6858         case R_ARM_ABS32_NOI:
6859         case R_ARM_REL32:
6860         case R_ARM_REL32_NOI:
6861         case R_ARM_PC24:
6862         case R_ARM_PLT32:
6863         case R_ARM_CALL:
6864         case R_ARM_JUMP24:
6865         case R_ARM_PREL31:
6866         case R_ARM_THM_CALL:
6867         case R_ARM_MOVW_ABS_NC:
6868         case R_ARM_MOVT_ABS:
6869         case R_ARM_MOVW_PREL_NC:
6870         case R_ARM_MOVT_PREL:
6871         case R_ARM_THM_MOVW_ABS_NC:
6872         case R_ARM_THM_MOVT_ABS:
6873         case R_ARM_THM_MOVW_PREL_NC:
6874         case R_ARM_THM_MOVT_PREL:
6875           /* Should the interworking branches be here also?  */
6876
6877           if (h != NULL)
6878             {
6879               struct elf32_arm_link_hash_entry *eh;
6880               struct elf32_arm_relocs_copied **pp;
6881               struct elf32_arm_relocs_copied *p;
6882
6883               eh = (struct elf32_arm_link_hash_entry *) h;
6884
6885               if (h->plt.refcount > 0)
6886                 {
6887                   h->plt.refcount -= 1;
6888                   if (ELF32_R_TYPE (rel->r_info) == R_ARM_THM_CALL)
6889                     eh->plt_thumb_refcount--;
6890                 }
6891
6892               if (r_type == R_ARM_ABS32
6893                   || r_type == R_ARM_REL32
6894                   || r_type == R_ARM_ABS32_NOI
6895                   || r_type == R_ARM_REL32_NOI)
6896                 {
6897                   for (pp = &eh->relocs_copied; (p = *pp) != NULL;
6898                        pp = &p->next)
6899                   if (p->section == sec)
6900                     {
6901                       p->count -= 1;
6902                       if (ELF32_R_TYPE (rel->r_info) == R_ARM_REL32
6903                           || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32_NOI)
6904                         p->pc_count -= 1;
6905                       if (p->count == 0)
6906                         *pp = p->next;
6907                       break;
6908                     }
6909                 }
6910             }
6911           break;
6912
6913         default:
6914           break;
6915         }
6916     }
6917
6918   return TRUE;
6919 }
6920
6921 /* Look through the relocs for a section during the first phase.  */
6922
6923 static bfd_boolean
6924 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
6925                         asection *sec, const Elf_Internal_Rela *relocs)
6926 {
6927   Elf_Internal_Shdr *symtab_hdr;
6928   struct elf_link_hash_entry **sym_hashes;
6929   struct elf_link_hash_entry **sym_hashes_end;
6930   const Elf_Internal_Rela *rel;
6931   const Elf_Internal_Rela *rel_end;
6932   bfd *dynobj;
6933   asection *sreloc;
6934   bfd_vma *local_got_offsets;
6935   struct elf32_arm_link_hash_table *htab;
6936
6937   if (info->relocatable)
6938     return TRUE;
6939
6940   htab = elf32_arm_hash_table (info);
6941   sreloc = NULL;
6942
6943   /* Create dynamic sections for relocatable executables so that we can
6944      copy relocations.  */
6945   if (htab->root.is_relocatable_executable
6946       && ! htab->root.dynamic_sections_created)
6947     {
6948       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
6949         return FALSE;
6950     }
6951
6952   dynobj = elf_hash_table (info)->dynobj;
6953   local_got_offsets = elf_local_got_offsets (abfd);
6954
6955   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6956   sym_hashes = elf_sym_hashes (abfd);
6957   sym_hashes_end = sym_hashes
6958     + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6959
6960   if (!elf_bad_symtab (abfd))
6961     sym_hashes_end -= symtab_hdr->sh_info;
6962
6963   rel_end = relocs + sec->reloc_count;
6964   for (rel = relocs; rel < rel_end; rel++)
6965     {
6966       struct elf_link_hash_entry *h;
6967       struct elf32_arm_link_hash_entry *eh;
6968       unsigned long r_symndx;
6969       int r_type;
6970
6971       r_symndx = ELF32_R_SYM (rel->r_info);
6972       r_type = ELF32_R_TYPE (rel->r_info);
6973       r_type = arm_real_reloc_type (htab, r_type);
6974
6975       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
6976         {
6977           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
6978                                  r_symndx);
6979           return FALSE;
6980         }
6981
6982       if (r_symndx < symtab_hdr->sh_info)
6983         h = NULL;
6984       else
6985         {
6986           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6987           while (h->root.type == bfd_link_hash_indirect
6988                  || h->root.type == bfd_link_hash_warning)
6989             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6990         }
6991
6992       eh = (struct elf32_arm_link_hash_entry *) h;
6993
6994       switch (r_type)
6995         {
6996           case R_ARM_GOT32:
6997           case R_ARM_GOT_PREL:
6998           case R_ARM_TLS_GD32:
6999           case R_ARM_TLS_IE32:
7000             /* This symbol requires a global offset table entry.  */
7001             {
7002               int tls_type, old_tls_type;
7003
7004               switch (r_type)
7005                 {
7006                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
7007                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
7008                 default: tls_type = GOT_NORMAL; break;
7009                 }
7010
7011               if (h != NULL)
7012                 {
7013                   h->got.refcount++;
7014                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
7015                 }
7016               else
7017                 {
7018                   bfd_signed_vma *local_got_refcounts;
7019
7020                   /* This is a global offset table entry for a local symbol.  */
7021                   local_got_refcounts = elf_local_got_refcounts (abfd);
7022                   if (local_got_refcounts == NULL)
7023                     {
7024                       bfd_size_type size;
7025                       
7026                       size = symtab_hdr->sh_info;
7027                       size *= (sizeof (bfd_signed_vma) + sizeof(char));
7028                       local_got_refcounts = bfd_zalloc (abfd, size);
7029                       if (local_got_refcounts == NULL)
7030                         return FALSE;
7031                       elf_local_got_refcounts (abfd) = local_got_refcounts;
7032                       elf32_arm_local_got_tls_type (abfd)
7033                         = (char *) (local_got_refcounts + symtab_hdr->sh_info);
7034                     }
7035                   local_got_refcounts[r_symndx] += 1;
7036                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
7037                 }
7038
7039               /* We will already have issued an error message if there is a
7040                  TLS / non-TLS mismatch, based on the symbol type.  We don't
7041                  support any linker relaxations.  So just combine any TLS
7042                  types needed.  */
7043               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7044                   && tls_type != GOT_NORMAL)
7045                 tls_type |= old_tls_type;
7046
7047               if (old_tls_type != tls_type)
7048                 {
7049                   if (h != NULL)
7050                     elf32_arm_hash_entry (h)->tls_type = tls_type;
7051                   else
7052                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
7053                 }
7054             }
7055             /* Fall through */
7056
7057           case R_ARM_TLS_LDM32:
7058             if (r_type == R_ARM_TLS_LDM32)
7059                 htab->tls_ldm_got.refcount++;
7060             /* Fall through */
7061
7062           case R_ARM_GOTOFF32:
7063           case R_ARM_GOTPC:
7064             if (htab->sgot == NULL)
7065               {
7066                 if (htab->root.dynobj == NULL)
7067                   htab->root.dynobj = abfd;
7068                 if (!create_got_section (htab->root.dynobj, info))
7069                   return FALSE;
7070               }
7071             break;
7072
7073           case R_ARM_ABS12:
7074             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
7075                ldr __GOTT_INDEX__ offsets.  */
7076             if (!htab->vxworks_p)
7077               break;
7078             /* Fall through */
7079
7080           case R_ARM_ABS32:
7081           case R_ARM_ABS32_NOI:
7082           case R_ARM_REL32:
7083           case R_ARM_REL32_NOI:
7084           case R_ARM_PC24:
7085           case R_ARM_PLT32:
7086           case R_ARM_CALL:
7087           case R_ARM_JUMP24:
7088           case R_ARM_PREL31:
7089           case R_ARM_THM_CALL:
7090           case R_ARM_MOVW_ABS_NC:
7091           case R_ARM_MOVT_ABS:
7092           case R_ARM_MOVW_PREL_NC:
7093           case R_ARM_MOVT_PREL:
7094           case R_ARM_THM_MOVW_ABS_NC:
7095           case R_ARM_THM_MOVT_ABS:
7096           case R_ARM_THM_MOVW_PREL_NC:
7097           case R_ARM_THM_MOVT_PREL:
7098             /* Should the interworking branches be listed here?  */
7099             if (h != NULL)
7100               {
7101                 /* If this reloc is in a read-only section, we might
7102                    need a copy reloc.  We can't check reliably at this
7103                    stage whether the section is read-only, as input
7104                    sections have not yet been mapped to output sections.
7105                    Tentatively set the flag for now, and correct in
7106                    adjust_dynamic_symbol.  */
7107                 if (!info->shared)
7108                   h->non_got_ref = 1;
7109
7110                 /* We may need a .plt entry if the function this reloc
7111                    refers to is in a different object.  We can't tell for
7112                    sure yet, because something later might force the
7113                    symbol local.  */
7114                 if (r_type != R_ARM_ABS32
7115                     && r_type != R_ARM_REL32
7116                     && r_type != R_ARM_ABS32_NOI
7117                     && r_type != R_ARM_REL32_NOI)
7118                   h->needs_plt = 1;
7119
7120                 /* If we create a PLT entry, this relocation will reference
7121                    it, even if it's an ABS32 relocation.  */
7122                 h->plt.refcount += 1;
7123
7124                 if (r_type == R_ARM_THM_CALL)
7125                   eh->plt_thumb_refcount += 1;
7126               }
7127
7128             /* If we are creating a shared library or relocatable executable,
7129                and this is a reloc against a global symbol, or a non PC
7130                relative reloc against a local symbol, then we need to copy
7131                the reloc into the shared library.  However, if we are linking
7132                with -Bsymbolic, we do not need to copy a reloc against a
7133                global symbol which is defined in an object we are
7134                including in the link (i.e., DEF_REGULAR is set).  At
7135                this point we have not seen all the input files, so it is
7136                possible that DEF_REGULAR is not set now but will be set
7137                later (it is never cleared).  We account for that
7138                possibility below by storing information in the
7139                relocs_copied field of the hash table entry.  */
7140             if ((info->shared || htab->root.is_relocatable_executable)
7141                 && (sec->flags & SEC_ALLOC) != 0
7142                 && ((r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI)
7143                     || (h != NULL && ! h->needs_plt
7144                         && (! info->symbolic || ! h->def_regular))))
7145               {
7146                 struct elf32_arm_relocs_copied *p, **head;
7147
7148                 /* When creating a shared object, we must copy these
7149                    reloc types into the output file.  We create a reloc
7150                    section in dynobj and make room for this reloc.  */
7151                 if (sreloc == NULL)
7152                   {
7153                     const char * name;
7154
7155                     name = (bfd_elf_string_from_elf_section
7156                             (abfd,
7157                              elf_elfheader (abfd)->e_shstrndx,
7158                              elf_section_data (sec)->rel_hdr.sh_name));
7159                     if (name == NULL)
7160                       return FALSE;
7161
7162                     BFD_ASSERT (reloc_section_p (htab, name, sec));
7163
7164                     sreloc = bfd_get_section_by_name (dynobj, name);
7165                     if (sreloc == NULL)
7166                       {
7167                         flagword flags;
7168
7169                         flags = (SEC_HAS_CONTENTS | SEC_READONLY
7170                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7171                         if ((sec->flags & SEC_ALLOC) != 0
7172                             /* BPABI objects never have dynamic
7173                                relocations mapped.  */
7174                             && !htab->symbian_p)
7175                           flags |= SEC_ALLOC | SEC_LOAD;
7176                         sreloc = bfd_make_section_with_flags (dynobj,
7177                                                               name,
7178                                                               flags);
7179                         if (sreloc == NULL
7180                             || ! bfd_set_section_alignment (dynobj, sreloc, 2))
7181                           return FALSE;
7182                       }
7183
7184                     elf_section_data (sec)->sreloc = sreloc;
7185                   }
7186
7187                 /* If this is a global symbol, we count the number of
7188                    relocations we need for this symbol.  */
7189                 if (h != NULL)
7190                   {
7191                     head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
7192                   }
7193                 else
7194                   {
7195                     /* Track dynamic relocs needed for local syms too.
7196                        We really need local syms available to do this
7197                        easily.  Oh well.  */
7198
7199                     asection *s;
7200                     void *vpp;
7201
7202                     s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
7203                                                    sec, r_symndx);
7204                     if (s == NULL)
7205                       return FALSE;
7206
7207                     vpp = &elf_section_data (s)->local_dynrel;
7208                     head = (struct elf32_arm_relocs_copied **) vpp;
7209                   }
7210
7211                 p = *head;
7212                 if (p == NULL || p->section != sec)
7213                   {
7214                     bfd_size_type amt = sizeof *p;
7215
7216                     p = bfd_alloc (htab->root.dynobj, amt);
7217                     if (p == NULL)
7218                       return FALSE;
7219                     p->next = *head;
7220                     *head = p;
7221                     p->section = sec;
7222                     p->count = 0;
7223                     p->pc_count = 0;
7224                   }
7225
7226                 if (r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
7227                   p->pc_count += 1;
7228                 p->count += 1;
7229               }
7230             break;
7231
7232         /* This relocation describes the C++ object vtable hierarchy.
7233            Reconstruct it for later use during GC.  */
7234         case R_ARM_GNU_VTINHERIT:
7235           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7236             return FALSE;
7237           break;
7238
7239         /* This relocation describes which C++ vtable entries are actually
7240            used.  Record for later use during GC.  */
7241         case R_ARM_GNU_VTENTRY:
7242           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7243             return FALSE;
7244           break;
7245         }
7246     }
7247
7248   return TRUE;
7249 }
7250
7251 /* Treat mapping symbols as special target symbols.  */
7252
7253 static bfd_boolean
7254 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
7255 {
7256   return bfd_is_arm_special_symbol_name (sym->name,
7257                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
7258 }
7259
7260 /* This is a copy of elf_find_function() from elf.c except that
7261    ARM mapping symbols are ignored when looking for function names
7262    and STT_ARM_TFUNC is considered to a function type.  */
7263
7264 static bfd_boolean
7265 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
7266                        asection *    section,
7267                        asymbol **    symbols,
7268                        bfd_vma       offset,
7269                        const char ** filename_ptr,
7270                        const char ** functionname_ptr)
7271 {
7272   const char * filename = NULL;
7273   asymbol * func = NULL;
7274   bfd_vma low_func = 0;
7275   asymbol ** p;
7276
7277   for (p = symbols; *p != NULL; p++)
7278     {
7279       elf_symbol_type *q;
7280
7281       q = (elf_symbol_type *) *p;
7282
7283       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7284         {
7285         default:
7286           break;
7287         case STT_FILE:
7288           filename = bfd_asymbol_name (&q->symbol);
7289           break;
7290         case STT_FUNC:
7291         case STT_ARM_TFUNC:
7292         case STT_NOTYPE:
7293           /* Skip mapping symbols.  */
7294           if ((q->symbol.flags & BSF_LOCAL)
7295               && bfd_is_arm_special_symbol_name (q->symbol.name,
7296                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
7297             continue;
7298           /* Fall through.  */
7299           if (bfd_get_section (&q->symbol) == section
7300               && q->symbol.value >= low_func
7301               && q->symbol.value <= offset)
7302             {
7303               func = (asymbol *) q;
7304               low_func = q->symbol.value;
7305             }
7306           break;
7307         }
7308     }
7309
7310   if (func == NULL)
7311     return FALSE;
7312
7313   if (filename_ptr)
7314     *filename_ptr = filename;
7315   if (functionname_ptr)
7316     *functionname_ptr = bfd_asymbol_name (func);
7317
7318   return TRUE;
7319 }  
7320
7321
7322 /* Find the nearest line to a particular section and offset, for error
7323    reporting.   This code is a duplicate of the code in elf.c, except
7324    that it uses arm_elf_find_function.  */
7325
7326 static bfd_boolean
7327 elf32_arm_find_nearest_line (bfd *          abfd,
7328                              asection *     section,
7329                              asymbol **     symbols,
7330                              bfd_vma        offset,
7331                              const char **  filename_ptr,
7332                              const char **  functionname_ptr,
7333                              unsigned int * line_ptr)
7334 {
7335   bfd_boolean found = FALSE;
7336
7337   /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
7338
7339   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7340                                      filename_ptr, functionname_ptr,
7341                                      line_ptr, 0,
7342                                      & elf_tdata (abfd)->dwarf2_find_line_info))
7343     {
7344       if (!*functionname_ptr)
7345         arm_elf_find_function (abfd, section, symbols, offset,
7346                                *filename_ptr ? NULL : filename_ptr,
7347                                functionname_ptr);
7348
7349       return TRUE;
7350     }
7351
7352   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7353                                              & found, filename_ptr,
7354                                              functionname_ptr, line_ptr,
7355                                              & elf_tdata (abfd)->line_info))
7356     return FALSE;
7357
7358   if (found && (*functionname_ptr || *line_ptr))
7359     return TRUE;
7360
7361   if (symbols == NULL)
7362     return FALSE;
7363
7364   if (! arm_elf_find_function (abfd, section, symbols, offset,
7365                                filename_ptr, functionname_ptr))
7366     return FALSE;
7367
7368   *line_ptr = 0;
7369   return TRUE;
7370 }
7371
7372 static bfd_boolean
7373 elf32_arm_find_inliner_info (bfd *          abfd,
7374                              const char **  filename_ptr,
7375                              const char **  functionname_ptr,
7376                              unsigned int * line_ptr)
7377 {
7378   bfd_boolean found;
7379   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7380                                          functionname_ptr, line_ptr,
7381                                          & elf_tdata (abfd)->dwarf2_find_line_info);
7382   return found;
7383 }
7384
7385 /* Adjust a symbol defined by a dynamic object and referenced by a
7386    regular object.  The current definition is in some section of the
7387    dynamic object, but we're not including those sections.  We have to
7388    change the definition to something the rest of the link can
7389    understand.  */
7390
7391 static bfd_boolean
7392 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
7393                                  struct elf_link_hash_entry * h)
7394 {
7395   bfd * dynobj;
7396   asection * s;
7397   unsigned int power_of_two;
7398   struct elf32_arm_link_hash_entry * eh;
7399   struct elf32_arm_link_hash_table *globals;
7400
7401   globals = elf32_arm_hash_table (info);
7402   dynobj = elf_hash_table (info)->dynobj;
7403
7404   /* Make sure we know what is going on here.  */
7405   BFD_ASSERT (dynobj != NULL
7406               && (h->needs_plt
7407                   || h->u.weakdef != NULL
7408                   || (h->def_dynamic
7409                       && h->ref_regular
7410                       && !h->def_regular)));
7411
7412   eh = (struct elf32_arm_link_hash_entry *) h;
7413
7414   /* If this is a function, put it in the procedure linkage table.  We
7415      will fill in the contents of the procedure linkage table later,
7416      when we know the address of the .got section.  */
7417   if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
7418       || h->needs_plt)
7419     {
7420       if (h->plt.refcount <= 0
7421           || SYMBOL_CALLS_LOCAL (info, h)
7422           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7423               && h->root.type == bfd_link_hash_undefweak))
7424         {
7425           /* This case can occur if we saw a PLT32 reloc in an input
7426              file, but the symbol was never referred to by a dynamic
7427              object, or if all references were garbage collected.  In
7428              such a case, we don't actually need to build a procedure
7429              linkage table, and we can just do a PC24 reloc instead.  */
7430           h->plt.offset = (bfd_vma) -1;
7431           eh->plt_thumb_refcount = 0;
7432           h->needs_plt = 0;
7433         }
7434
7435       return TRUE;
7436     }
7437   else
7438     {
7439       /* It's possible that we incorrectly decided a .plt reloc was
7440          needed for an R_ARM_PC24 or similar reloc to a non-function sym
7441          in check_relocs.  We can't decide accurately between function
7442          and non-function syms in check-relocs; Objects loaded later in
7443          the link may change h->type.  So fix it now.  */
7444       h->plt.offset = (bfd_vma) -1;
7445       eh->plt_thumb_refcount = 0;
7446     }
7447
7448   /* If this is a weak symbol, and there is a real definition, the
7449      processor independent code will have arranged for us to see the
7450      real definition first, and we can just use the same value.  */
7451   if (h->u.weakdef != NULL)
7452     {
7453       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7454                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7455       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7456       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7457       return TRUE;
7458     }
7459
7460   /* If there are no non-GOT references, we do not need a copy
7461      relocation.  */
7462   if (!h->non_got_ref)
7463     return TRUE;
7464
7465   /* This is a reference to a symbol defined by a dynamic object which
7466      is not a function.  */
7467
7468   /* If we are creating a shared library, we must presume that the
7469      only references to the symbol are via the global offset table.
7470      For such cases we need not do anything here; the relocations will
7471      be handled correctly by relocate_section.  Relocatable executables
7472      can reference data in shared objects directly, so we don't need to
7473      do anything here.  */
7474   if (info->shared || globals->root.is_relocatable_executable)
7475     return TRUE;
7476
7477   if (h->size == 0)
7478     {
7479       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
7480                              h->root.root.string);
7481       return TRUE;
7482     }
7483
7484   /* We must allocate the symbol in our .dynbss section, which will
7485      become part of the .bss section of the executable.  There will be
7486      an entry for this symbol in the .dynsym section.  The dynamic
7487      object will contain position independent code, so all references
7488      from the dynamic object to this symbol will go through the global
7489      offset table.  The dynamic linker will use the .dynsym entry to
7490      determine the address it must put in the global offset table, so
7491      both the dynamic object and the regular object will refer to the
7492      same memory location for the variable.  */
7493   s = bfd_get_section_by_name (dynobj, ".dynbss");
7494   BFD_ASSERT (s != NULL);
7495
7496   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
7497      copy the initial value out of the dynamic object and into the
7498      runtime process image.  We need to remember the offset into the
7499      .rel(a).bss section we are going to use.  */
7500   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
7501     {
7502       asection *srel;
7503
7504       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (globals, ".bss"));
7505       BFD_ASSERT (srel != NULL);
7506       srel->size += RELOC_SIZE (globals);
7507       h->needs_copy = 1;
7508     }
7509
7510   /* We need to figure out the alignment required for this symbol.  I
7511      have no idea how ELF linkers handle this.  */
7512   power_of_two = bfd_log2 (h->size);
7513   if (power_of_two > 3)
7514     power_of_two = 3;
7515
7516   /* Apply the required alignment.  */
7517   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
7518   if (power_of_two > bfd_get_section_alignment (dynobj, s))
7519     {
7520       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
7521         return FALSE;
7522     }
7523
7524   /* Define the symbol as being at this point in the section.  */
7525   h->root.u.def.section = s;
7526   h->root.u.def.value = s->size;
7527
7528   /* Increment the section size to make room for the symbol.  */
7529   s->size += h->size;
7530
7531   return TRUE;
7532 }
7533
7534 /* Allocate space in .plt, .got and associated reloc sections for
7535    dynamic relocs.  */
7536
7537 static bfd_boolean
7538 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
7539 {
7540   struct bfd_link_info *info;
7541   struct elf32_arm_link_hash_table *htab;
7542   struct elf32_arm_link_hash_entry *eh;
7543   struct elf32_arm_relocs_copied *p;
7544
7545   eh = (struct elf32_arm_link_hash_entry *) h;
7546
7547   if (h->root.type == bfd_link_hash_indirect)
7548     return TRUE;
7549
7550   if (h->root.type == bfd_link_hash_warning)
7551     /* When warning symbols are created, they **replace** the "real"
7552        entry in the hash table, thus we never get to see the real
7553        symbol in a hash traversal.  So look at it now.  */
7554     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7555
7556   info = (struct bfd_link_info *) inf;
7557   htab = elf32_arm_hash_table (info);
7558
7559   if (htab->root.dynamic_sections_created
7560       && h->plt.refcount > 0)
7561     {
7562       /* Make sure this symbol is output as a dynamic symbol.
7563          Undefined weak syms won't yet be marked as dynamic.  */
7564       if (h->dynindx == -1
7565           && !h->forced_local)
7566         {
7567           if (! bfd_elf_link_record_dynamic_symbol (info, h))
7568             return FALSE;
7569         }
7570
7571       if (info->shared
7572           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
7573         {
7574           asection *s = htab->splt;
7575
7576           /* If this is the first .plt entry, make room for the special
7577              first entry.  */
7578           if (s->size == 0)
7579             s->size += htab->plt_header_size;
7580
7581           h->plt.offset = s->size;
7582
7583           /* If we will insert a Thumb trampoline before this PLT, leave room
7584              for it.  */
7585           if (!htab->use_blx && eh->plt_thumb_refcount > 0)
7586             {
7587               h->plt.offset += PLT_THUMB_STUB_SIZE;
7588               s->size += PLT_THUMB_STUB_SIZE;
7589             }
7590
7591           /* If this symbol is not defined in a regular file, and we are
7592              not generating a shared library, then set the symbol to this
7593              location in the .plt.  This is required to make function
7594              pointers compare as equal between the normal executable and
7595              the shared library.  */
7596           if (! info->shared
7597               && !h->def_regular)
7598             {
7599               h->root.u.def.section = s;
7600               h->root.u.def.value = h->plt.offset;
7601
7602               /* Make sure the function is not marked as Thumb, in case
7603                  it is the target of an ABS32 relocation, which will
7604                  point to the PLT entry.  */
7605               if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
7606                 h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
7607             }
7608
7609           /* Make room for this entry.  */
7610           s->size += htab->plt_entry_size;
7611
7612           if (!htab->symbian_p)
7613             {
7614               /* We also need to make an entry in the .got.plt section, which
7615                  will be placed in the .got section by the linker script.  */
7616               eh->plt_got_offset = htab->sgotplt->size;
7617               htab->sgotplt->size += 4;
7618             }
7619
7620           /* We also need to make an entry in the .rel(a).plt section.  */
7621           htab->srelplt->size += RELOC_SIZE (htab);
7622
7623           /* VxWorks executables have a second set of relocations for
7624              each PLT entry.  They go in a separate relocation section,
7625              which is processed by the kernel loader.  */
7626           if (htab->vxworks_p && !info->shared)
7627             {
7628               /* There is a relocation for the initial PLT entry:
7629                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
7630               if (h->plt.offset == htab->plt_header_size)
7631                 htab->srelplt2->size += RELOC_SIZE (htab);
7632
7633               /* There are two extra relocations for each subsequent
7634                  PLT entry: an R_ARM_32 relocation for the GOT entry,
7635                  and an R_ARM_32 relocation for the PLT entry.  */
7636               htab->srelplt2->size += RELOC_SIZE (htab) * 2;
7637             }
7638         }
7639       else
7640         {
7641           h->plt.offset = (bfd_vma) -1;
7642           h->needs_plt = 0;
7643         }
7644     }
7645   else
7646     {
7647       h->plt.offset = (bfd_vma) -1;
7648       h->needs_plt = 0;
7649     }
7650
7651   if (h->got.refcount > 0)
7652     {
7653       asection *s;
7654       bfd_boolean dyn;
7655       int tls_type = elf32_arm_hash_entry (h)->tls_type;
7656       int indx;
7657
7658       /* Make sure this symbol is output as a dynamic symbol.
7659          Undefined weak syms won't yet be marked as dynamic.  */
7660       if (h->dynindx == -1
7661           && !h->forced_local)
7662         {
7663           if (! bfd_elf_link_record_dynamic_symbol (info, h))
7664             return FALSE;
7665         }
7666
7667       if (!htab->symbian_p)
7668         {
7669           s = htab->sgot;
7670           h->got.offset = s->size;
7671
7672           if (tls_type == GOT_UNKNOWN)
7673             abort ();
7674
7675           if (tls_type == GOT_NORMAL)
7676             /* Non-TLS symbols need one GOT slot.  */
7677             s->size += 4;
7678           else
7679             {
7680               if (tls_type & GOT_TLS_GD)
7681                 /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  */
7682                 s->size += 8;
7683               if (tls_type & GOT_TLS_IE)
7684                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
7685                 s->size += 4;
7686             }
7687
7688           dyn = htab->root.dynamic_sections_created;
7689
7690           indx = 0;
7691           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7692               && (!info->shared
7693                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
7694             indx = h->dynindx;
7695
7696           if (tls_type != GOT_NORMAL
7697               && (info->shared || indx != 0)
7698               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7699                   || h->root.type != bfd_link_hash_undefweak))
7700             {
7701               if (tls_type & GOT_TLS_IE)
7702                 htab->srelgot->size += RELOC_SIZE (htab);
7703
7704               if (tls_type & GOT_TLS_GD)
7705                 htab->srelgot->size += RELOC_SIZE (htab);
7706
7707               if ((tls_type & GOT_TLS_GD) && indx != 0)
7708                 htab->srelgot->size += RELOC_SIZE (htab);
7709             }
7710           else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7711                     || h->root.type != bfd_link_hash_undefweak)
7712                    && (info->shared
7713                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
7714             htab->srelgot->size += RELOC_SIZE (htab);
7715         }
7716     }
7717   else
7718     h->got.offset = (bfd_vma) -1;
7719
7720   /* Allocate stubs for exported Thumb functions on v4t.  */
7721   if (!htab->use_blx && h->dynindx != -1
7722       && ELF_ST_TYPE (h->type) == STT_ARM_TFUNC
7723       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7724     {
7725       struct elf_link_hash_entry * th;
7726       struct bfd_link_hash_entry * bh;
7727       struct elf_link_hash_entry * myh;
7728       char name[1024];
7729       asection *s;
7730       bh = NULL;
7731       /* Create a new symbol to regist the real location of the function.  */
7732       s = h->root.u.def.section;
7733       sprintf(name, "__real_%s", h->root.root.string);
7734       _bfd_generic_link_add_one_symbol (info, s->owner,
7735                                         name, BSF_GLOBAL, s,
7736                                         h->root.u.def.value,
7737                                         NULL, TRUE, FALSE, &bh);
7738
7739       myh = (struct elf_link_hash_entry *) bh;
7740       myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
7741       myh->forced_local = 1;
7742       eh->export_glue = myh;
7743       th = record_arm_to_thumb_glue (info, h);
7744       /* Point the symbol at the stub.  */
7745       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
7746       h->root.u.def.section = th->root.u.def.section;
7747       h->root.u.def.value = th->root.u.def.value & ~1;
7748     }
7749
7750   if (eh->relocs_copied == NULL)
7751     return TRUE;
7752
7753   /* In the shared -Bsymbolic case, discard space allocated for
7754      dynamic pc-relative relocs against symbols which turn out to be
7755      defined in regular objects.  For the normal shared case, discard
7756      space for pc-relative relocs that have become local due to symbol
7757      visibility changes.  */
7758
7759   if (info->shared || htab->root.is_relocatable_executable)
7760     {
7761       /* The only relocs that use pc_count are R_ARM_REL32 and
7762          R_ARM_REL32_NOI, which will appear on something like
7763          ".long foo - .".  We want calls to protected symbols to resolve
7764          directly to the function rather than going via the plt.  If people
7765          want function pointer comparisons to work as expected then they
7766          should avoid writing assembly like ".long foo - .".  */
7767       if (SYMBOL_CALLS_LOCAL (info, h))
7768         {
7769           struct elf32_arm_relocs_copied **pp;
7770
7771           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
7772             {
7773               p->count -= p->pc_count;
7774               p->pc_count = 0;
7775               if (p->count == 0)
7776                 *pp = p->next;
7777               else
7778                 pp = &p->next;
7779             }
7780         }
7781
7782       /* Also discard relocs on undefined weak syms with non-default
7783          visibility.  */
7784       if (eh->relocs_copied != NULL
7785           && h->root.type == bfd_link_hash_undefweak)
7786         {
7787           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7788             eh->relocs_copied = NULL;
7789
7790           /* Make sure undefined weak symbols are output as a dynamic
7791              symbol in PIEs.  */
7792           else if (h->dynindx == -1
7793                    && !h->forced_local)
7794             {
7795               if (! bfd_elf_link_record_dynamic_symbol (info, h))
7796                 return FALSE;
7797             }
7798         }
7799
7800       else if (htab->root.is_relocatable_executable && h->dynindx == -1
7801                && h->root.type == bfd_link_hash_new)
7802         {
7803           /* Output absolute symbols so that we can create relocations
7804              against them.  For normal symbols we output a relocation
7805              against the section that contains them.  */
7806           if (! bfd_elf_link_record_dynamic_symbol (info, h))
7807             return FALSE;
7808         }
7809
7810     }
7811   else
7812     {
7813       /* For the non-shared case, discard space for relocs against
7814          symbols which turn out to need copy relocs or are not
7815          dynamic.  */
7816
7817       if (!h->non_got_ref
7818           && ((h->def_dynamic
7819                && !h->def_regular)
7820               || (htab->root.dynamic_sections_created
7821                   && (h->root.type == bfd_link_hash_undefweak
7822                       || h->root.type == bfd_link_hash_undefined))))
7823         {
7824           /* Make sure this symbol is output as a dynamic symbol.
7825              Undefined weak syms won't yet be marked as dynamic.  */
7826           if (h->dynindx == -1
7827               && !h->forced_local)
7828             {
7829               if (! bfd_elf_link_record_dynamic_symbol (info, h))
7830                 return FALSE;
7831             }
7832
7833           /* If that succeeded, we know we'll be keeping all the
7834              relocs.  */
7835           if (h->dynindx != -1)
7836             goto keep;
7837         }
7838
7839       eh->relocs_copied = NULL;
7840
7841     keep: ;
7842     }
7843
7844   /* Finally, allocate space.  */
7845   for (p = eh->relocs_copied; p != NULL; p = p->next)
7846     {
7847       asection *sreloc = elf_section_data (p->section)->sreloc;
7848       sreloc->size += p->count * RELOC_SIZE (htab);
7849     }
7850
7851   return TRUE;
7852 }
7853
7854 /* Find any dynamic relocs that apply to read-only sections.  */
7855
7856 static bfd_boolean
7857 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
7858 {
7859   struct elf32_arm_link_hash_entry *eh;
7860   struct elf32_arm_relocs_copied *p;
7861
7862   if (h->root.type == bfd_link_hash_warning)
7863     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7864
7865   eh = (struct elf32_arm_link_hash_entry *) h;
7866   for (p = eh->relocs_copied; p != NULL; p = p->next)
7867     {
7868       asection *s = p->section;
7869
7870       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7871         {
7872           struct bfd_link_info *info = (struct bfd_link_info *) inf;
7873
7874           info->flags |= DF_TEXTREL;
7875
7876           /* Not an error, just cut short the traversal.  */
7877           return FALSE;
7878         }
7879     }
7880   return TRUE;
7881 }
7882
7883 /* Set the sizes of the dynamic sections.  */
7884
7885 static bfd_boolean
7886 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
7887                                  struct bfd_link_info * info)
7888 {
7889   bfd * dynobj;
7890   asection * s;
7891   bfd_boolean plt;
7892   bfd_boolean relocs;
7893   bfd *ibfd;
7894   struct elf32_arm_link_hash_table *htab;
7895
7896   htab = elf32_arm_hash_table (info);
7897   dynobj = elf_hash_table (info)->dynobj;
7898   BFD_ASSERT (dynobj != NULL);
7899   check_use_blx (htab);
7900
7901   if (elf_hash_table (info)->dynamic_sections_created)
7902     {
7903       /* Set the contents of the .interp section to the interpreter.  */
7904       if (info->executable)
7905         {
7906           s = bfd_get_section_by_name (dynobj, ".interp");
7907           BFD_ASSERT (s != NULL);
7908           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7909           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7910         }
7911     }
7912
7913   /* Set up .got offsets for local syms, and space for local dynamic
7914      relocs.  */
7915   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7916     {
7917       bfd_signed_vma *local_got;
7918       bfd_signed_vma *end_local_got;
7919       char *local_tls_type;
7920       bfd_size_type locsymcount;
7921       Elf_Internal_Shdr *symtab_hdr;
7922       asection *srel;
7923
7924       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
7925         continue;
7926
7927       for (s = ibfd->sections; s != NULL; s = s->next)
7928         {
7929           struct elf32_arm_relocs_copied *p;
7930
7931           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7932             {
7933               if (!bfd_is_abs_section (p->section)
7934                   && bfd_is_abs_section (p->section->output_section))
7935                 {
7936                   /* Input section has been discarded, either because
7937                      it is a copy of a linkonce section or due to
7938                      linker script /DISCARD/, so we'll be discarding
7939                      the relocs too.  */
7940                 }
7941               else if (p->count != 0)
7942                 {
7943                   srel = elf_section_data (p->section)->sreloc;
7944                   srel->size += p->count * RELOC_SIZE (htab);
7945                   if ((p->section->output_section->flags & SEC_READONLY) != 0)
7946                     info->flags |= DF_TEXTREL;
7947                 }
7948             }
7949         }
7950
7951       local_got = elf_local_got_refcounts (ibfd);
7952       if (!local_got)
7953         continue;
7954
7955       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7956       locsymcount = symtab_hdr->sh_info;
7957       end_local_got = local_got + locsymcount;
7958       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
7959       s = htab->sgot;
7960       srel = htab->srelgot;
7961       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
7962         {
7963           if (*local_got > 0)
7964             {
7965               *local_got = s->size;
7966               if (*local_tls_type & GOT_TLS_GD)
7967                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
7968                 s->size += 8;
7969               if (*local_tls_type & GOT_TLS_IE)
7970                 s->size += 4;
7971               if (*local_tls_type == GOT_NORMAL)
7972                 s->size += 4;
7973
7974               if (info->shared || *local_tls_type == GOT_TLS_GD)
7975                 srel->size += RELOC_SIZE (htab);
7976             }
7977           else
7978             *local_got = (bfd_vma) -1;
7979         }
7980     }
7981
7982   if (htab->tls_ldm_got.refcount > 0)
7983     {
7984       /* Allocate two GOT entries and one dynamic relocation (if necessary)
7985          for R_ARM_TLS_LDM32 relocations.  */
7986       htab->tls_ldm_got.offset = htab->sgot->size;
7987       htab->sgot->size += 8;
7988       if (info->shared)
7989         htab->srelgot->size += RELOC_SIZE (htab);
7990     }
7991   else
7992     htab->tls_ldm_got.offset = -1;
7993
7994   /* Allocate global sym .plt and .got entries, and space for global
7995      sym dynamic relocs.  */
7996   elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
7997
7998   /* The check_relocs and adjust_dynamic_symbol entry points have
7999      determined the sizes of the various dynamic sections.  Allocate
8000      memory for them.  */
8001   plt = FALSE;
8002   relocs = FALSE;
8003   for (s = dynobj->sections; s != NULL; s = s->next)
8004     {
8005       const char * name;
8006
8007       if ((s->flags & SEC_LINKER_CREATED) == 0)
8008         continue;
8009
8010       /* It's OK to base decisions on the section name, because none
8011          of the dynobj section names depend upon the input files.  */
8012       name = bfd_get_section_name (dynobj, s);
8013
8014       if (strcmp (name, ".plt") == 0)
8015         {
8016           /* Remember whether there is a PLT.  */
8017           plt = s->size != 0;
8018         }
8019       else if (strncmp (name, ".rel", 4) == 0)
8020         {
8021           if (s->size != 0)
8022             {
8023               /* Remember whether there are any reloc sections other
8024                  than .rel(a).plt and .rela.plt.unloaded.  */
8025               if (s != htab->srelplt && s != htab->srelplt2)
8026                 relocs = TRUE;
8027
8028               /* We use the reloc_count field as a counter if we need
8029                  to copy relocs into the output file.  */
8030               s->reloc_count = 0;
8031             }
8032         }
8033       else if (strncmp (name, ".got", 4) != 0
8034                && strcmp (name, ".dynbss") != 0)
8035         {
8036           /* It's not one of our sections, so don't allocate space.  */
8037           continue;
8038         }
8039
8040       if (s->size == 0)
8041         {
8042           /* If we don't need this section, strip it from the
8043              output file.  This is mostly to handle .rel(a).bss and
8044              .rel(a).plt.  We must create both sections in
8045              create_dynamic_sections, because they must be created
8046              before the linker maps input sections to output
8047              sections.  The linker does that before
8048              adjust_dynamic_symbol is called, and it is that
8049              function which decides whether anything needs to go
8050              into these sections.  */
8051           s->flags |= SEC_EXCLUDE;
8052           continue;
8053         }
8054
8055       if ((s->flags & SEC_HAS_CONTENTS) == 0)
8056         continue;
8057
8058       /* Allocate memory for the section contents.  */
8059       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
8060       if (s->contents == NULL)
8061         return FALSE;
8062     }
8063
8064   if (elf_hash_table (info)->dynamic_sections_created)
8065     {
8066       /* Add some entries to the .dynamic section.  We fill in the
8067          values later, in elf32_arm_finish_dynamic_sections, but we
8068          must add the entries now so that we get the correct size for
8069          the .dynamic section.  The DT_DEBUG entry is filled in by the
8070          dynamic linker and used by the debugger.  */
8071 #define add_dynamic_entry(TAG, VAL) \
8072   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8073
8074      if (info->executable)
8075         {
8076           if (!add_dynamic_entry (DT_DEBUG, 0))
8077             return FALSE;
8078         }
8079
8080       if (plt)
8081         {
8082           if (   !add_dynamic_entry (DT_PLTGOT, 0)
8083               || !add_dynamic_entry (DT_PLTRELSZ, 0)
8084               || !add_dynamic_entry (DT_PLTREL,
8085                                      htab->use_rel ? DT_REL : DT_RELA)
8086               || !add_dynamic_entry (DT_JMPREL, 0))
8087             return FALSE;
8088         }
8089
8090       if (relocs)
8091         {
8092           if (htab->use_rel)
8093             {
8094               if (!add_dynamic_entry (DT_REL, 0)
8095                   || !add_dynamic_entry (DT_RELSZ, 0)
8096                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
8097                 return FALSE;
8098             }
8099           else
8100             {
8101               if (!add_dynamic_entry (DT_RELA, 0)
8102                   || !add_dynamic_entry (DT_RELASZ, 0)
8103                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
8104                 return FALSE;
8105             }
8106         }
8107
8108       /* If any dynamic relocs apply to a read-only section,
8109          then we need a DT_TEXTREL entry.  */
8110       if ((info->flags & DF_TEXTREL) == 0)
8111         elf_link_hash_traverse (&htab->root, elf32_arm_readonly_dynrelocs,
8112                                 (PTR) info);
8113
8114       if ((info->flags & DF_TEXTREL) != 0)
8115         {
8116           if (!add_dynamic_entry (DT_TEXTREL, 0))
8117             return FALSE;
8118         }
8119     }
8120 #undef add_dynamic_entry
8121
8122   return TRUE;
8123 }
8124
8125 /* Finish up dynamic symbol handling.  We set the contents of various
8126    dynamic sections here.  */
8127
8128 static bfd_boolean
8129 elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
8130                                  struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
8131 {
8132   bfd * dynobj;
8133   struct elf32_arm_link_hash_table *htab;
8134   struct elf32_arm_link_hash_entry *eh;
8135
8136   dynobj = elf_hash_table (info)->dynobj;
8137   htab = elf32_arm_hash_table (info);
8138   eh = (struct elf32_arm_link_hash_entry *) h;
8139
8140   if (h->plt.offset != (bfd_vma) -1)
8141     {
8142       asection * splt;
8143       asection * srel;
8144       bfd_byte *loc;
8145       bfd_vma plt_index;
8146       Elf_Internal_Rela rel;
8147
8148       /* This symbol has an entry in the procedure linkage table.  Set
8149          it up.  */
8150
8151       BFD_ASSERT (h->dynindx != -1);
8152
8153       splt = bfd_get_section_by_name (dynobj, ".plt");
8154       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".plt"));
8155       BFD_ASSERT (splt != NULL && srel != NULL);
8156
8157       /* Fill in the entry in the procedure linkage table.  */
8158       if (htab->symbian_p)
8159         {
8160           put_arm_insn (htab, output_bfd, 
8161                       elf32_arm_symbian_plt_entry[0],
8162                       splt->contents + h->plt.offset);
8163           bfd_put_32 (output_bfd, 
8164                       elf32_arm_symbian_plt_entry[1],
8165                       splt->contents + h->plt.offset + 4);
8166           
8167           /* Fill in the entry in the .rel.plt section.  */
8168           rel.r_offset = (splt->output_section->vma
8169                           + splt->output_offset
8170                           + h->plt.offset + 4);
8171           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
8172
8173           /* Get the index in the procedure linkage table which
8174              corresponds to this symbol.  This is the index of this symbol
8175              in all the symbols for which we are making plt entries.  The
8176              first entry in the procedure linkage table is reserved.  */
8177           plt_index = ((h->plt.offset - htab->plt_header_size) 
8178                        / htab->plt_entry_size);
8179         }
8180       else
8181         {
8182           bfd_vma got_offset, got_address, plt_address;
8183           bfd_vma got_displacement;
8184           asection * sgot;
8185           bfd_byte * ptr;
8186           
8187           sgot = bfd_get_section_by_name (dynobj, ".got.plt");
8188           BFD_ASSERT (sgot != NULL);
8189
8190           /* Get the offset into the .got.plt table of the entry that
8191              corresponds to this function.  */
8192           got_offset = eh->plt_got_offset;
8193
8194           /* Get the index in the procedure linkage table which
8195              corresponds to this symbol.  This is the index of this symbol
8196              in all the symbols for which we are making plt entries.  The
8197              first three entries in .got.plt are reserved; after that
8198              symbols appear in the same order as in .plt.  */
8199           plt_index = (got_offset - 12) / 4;
8200
8201           /* Calculate the address of the GOT entry.  */
8202           got_address = (sgot->output_section->vma
8203                          + sgot->output_offset
8204                          + got_offset);
8205
8206           /* ...and the address of the PLT entry.  */
8207           plt_address = (splt->output_section->vma
8208                          + splt->output_offset
8209                          + h->plt.offset);
8210
8211           ptr = htab->splt->contents + h->plt.offset;
8212           if (htab->vxworks_p && info->shared)
8213             {
8214               unsigned int i;
8215               bfd_vma val;
8216
8217               for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
8218                 {
8219                   val = elf32_arm_vxworks_shared_plt_entry[i];
8220                   if (i == 2)
8221                     val |= got_address - sgot->output_section->vma;
8222                   if (i == 5)
8223                     val |= plt_index * RELOC_SIZE (htab);
8224                   if (i == 2 || i == 5)
8225                     bfd_put_32 (output_bfd, val, ptr);
8226                   else
8227                     put_arm_insn (htab, output_bfd, val, ptr);
8228                 }
8229             }
8230           else if (htab->vxworks_p)
8231             {
8232               unsigned int i;
8233               bfd_vma val;
8234
8235               for (i = 0; i != htab->plt_entry_size / 4; i++)
8236                 {
8237                   val = elf32_arm_vxworks_exec_plt_entry[i];
8238                   if (i == 2)
8239                     val |= got_address;
8240                   if (i == 4)
8241                     val |= 0xffffff & -((h->plt.offset + i * 4 + 8) >> 2);
8242                   if (i == 5)
8243                     val |= plt_index * RELOC_SIZE (htab);
8244                   if (i == 2 || i == 5)
8245                     bfd_put_32 (output_bfd, val, ptr);
8246                   else
8247                     put_arm_insn (htab, output_bfd, val, ptr);
8248                 }
8249
8250               loc = (htab->srelplt2->contents
8251                      + (plt_index * 2 + 1) * RELOC_SIZE (htab));
8252
8253               /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
8254                  referencing the GOT for this PLT entry.  */
8255               rel.r_offset = plt_address + 8;
8256               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
8257               rel.r_addend = got_offset;
8258               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8259               loc += RELOC_SIZE (htab);
8260
8261               /* Create the R_ARM_ABS32 relocation referencing the
8262                  beginning of the PLT for this GOT entry.  */
8263               rel.r_offset = got_address;
8264               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
8265               rel.r_addend = 0;
8266               SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8267             }
8268           else
8269             {
8270               /* Calculate the displacement between the PLT slot and the
8271                  entry in the GOT.  The eight-byte offset accounts for the
8272                  value produced by adding to pc in the first instruction
8273                  of the PLT stub.  */
8274               got_displacement = got_address - (plt_address + 8);
8275
8276               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
8277
8278               if (!htab->use_blx && eh->plt_thumb_refcount > 0)
8279                 {
8280                   put_thumb_insn (htab, output_bfd,
8281                                   elf32_arm_plt_thumb_stub[0], ptr - 4);
8282                   put_thumb_insn (htab, output_bfd,
8283                                   elf32_arm_plt_thumb_stub[1], ptr - 2);
8284                 }
8285
8286               put_arm_insn (htab, output_bfd,
8287                             elf32_arm_plt_entry[0]
8288                             | ((got_displacement & 0x0ff00000) >> 20),
8289                             ptr + 0);
8290               put_arm_insn (htab, output_bfd,
8291                             elf32_arm_plt_entry[1]
8292                             | ((got_displacement & 0x000ff000) >> 12),
8293                             ptr+ 4);
8294               put_arm_insn (htab, output_bfd,
8295                             elf32_arm_plt_entry[2]
8296                             | (got_displacement & 0x00000fff),
8297                             ptr + 8);
8298 #ifdef FOUR_WORD_PLT
8299               bfd_put_32 (output_bfd, elf32_arm_plt_entry[3], ptr + 12);
8300 #endif
8301             }
8302
8303           /* Fill in the entry in the global offset table.  */
8304           bfd_put_32 (output_bfd,
8305                       (splt->output_section->vma
8306                        + splt->output_offset),
8307                       sgot->contents + got_offset);
8308           
8309           /* Fill in the entry in the .rel(a).plt section.  */
8310           rel.r_addend = 0;
8311           rel.r_offset = got_address;
8312           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
8313         }
8314
8315       loc = srel->contents + plt_index * RELOC_SIZE (htab);
8316       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8317
8318       if (!h->def_regular)
8319         {
8320           /* Mark the symbol as undefined, rather than as defined in
8321              the .plt section.  Leave the value alone.  */
8322           sym->st_shndx = SHN_UNDEF;
8323           /* If the symbol is weak, we do need to clear the value.
8324              Otherwise, the PLT entry would provide a definition for
8325              the symbol even if the symbol wasn't defined anywhere,
8326              and so the symbol would never be NULL.  */
8327           if (!h->ref_regular_nonweak)
8328             sym->st_value = 0;
8329         }
8330     }
8331
8332   if (h->got.offset != (bfd_vma) -1
8333       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
8334       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
8335     {
8336       asection * sgot;
8337       asection * srel;
8338       Elf_Internal_Rela rel;
8339       bfd_byte *loc;
8340       bfd_vma offset;
8341
8342       /* This symbol has an entry in the global offset table.  Set it
8343          up.  */
8344       sgot = bfd_get_section_by_name (dynobj, ".got");
8345       srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".got"));
8346       BFD_ASSERT (sgot != NULL && srel != NULL);
8347
8348       offset = (h->got.offset & ~(bfd_vma) 1);
8349       rel.r_addend = 0;
8350       rel.r_offset = (sgot->output_section->vma
8351                       + sgot->output_offset
8352                       + offset);
8353
8354       /* If this is a static link, or it is a -Bsymbolic link and the
8355          symbol is defined locally or was forced to be local because
8356          of a version file, we just want to emit a RELATIVE reloc.
8357          The entry in the global offset table will already have been
8358          initialized in the relocate_section function.  */
8359       if (info->shared
8360           && SYMBOL_REFERENCES_LOCAL (info, h))
8361         {
8362           BFD_ASSERT((h->got.offset & 1) != 0);
8363           rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
8364           if (!htab->use_rel)
8365             {
8366               rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + offset);
8367               bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
8368             }
8369         }
8370       else
8371         {
8372           BFD_ASSERT((h->got.offset & 1) == 0);
8373           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
8374           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
8375         }
8376
8377       loc = srel->contents + srel->reloc_count++ * RELOC_SIZE (htab);
8378       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8379     }
8380
8381   if (h->needs_copy)
8382     {
8383       asection * s;
8384       Elf_Internal_Rela rel;
8385       bfd_byte *loc;
8386
8387       /* This symbol needs a copy reloc.  Set it up.  */
8388       BFD_ASSERT (h->dynindx != -1
8389                   && (h->root.type == bfd_link_hash_defined
8390                       || h->root.type == bfd_link_hash_defweak));
8391
8392       s = bfd_get_section_by_name (h->root.u.def.section->owner,
8393                                    RELOC_SECTION (htab, ".bss"));
8394       BFD_ASSERT (s != NULL);
8395
8396       rel.r_addend = 0;
8397       rel.r_offset = (h->root.u.def.value
8398                       + h->root.u.def.section->output_section->vma
8399                       + h->root.u.def.section->output_offset);
8400       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
8401       loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
8402       SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
8403     }
8404
8405   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
8406      the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
8407      to the ".got" section.  */
8408   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8409       || (!htab->vxworks_p && h == htab->root.hgot))
8410     sym->st_shndx = SHN_ABS;
8411
8412   return TRUE;
8413 }
8414
8415 /* Finish up the dynamic sections.  */
8416
8417 static bfd_boolean
8418 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
8419 {
8420   bfd * dynobj;
8421   asection * sgot;
8422   asection * sdyn;
8423
8424   dynobj = elf_hash_table (info)->dynobj;
8425
8426   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
8427   BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
8428   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8429
8430   if (elf_hash_table (info)->dynamic_sections_created)
8431     {
8432       asection *splt;
8433       Elf32_External_Dyn *dyncon, *dynconend;
8434       struct elf32_arm_link_hash_table *htab;
8435
8436       htab = elf32_arm_hash_table (info);
8437       splt = bfd_get_section_by_name (dynobj, ".plt");
8438       BFD_ASSERT (splt != NULL && sdyn != NULL);
8439
8440       dyncon = (Elf32_External_Dyn *) sdyn->contents;
8441       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8442
8443       for (; dyncon < dynconend; dyncon++)
8444         {
8445           Elf_Internal_Dyn dyn;
8446           const char * name;
8447           asection * s;
8448
8449           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
8450
8451           switch (dyn.d_tag)
8452             {
8453               unsigned int type;
8454
8455             default:
8456               break;
8457
8458             case DT_HASH:
8459               name = ".hash";
8460               goto get_vma_if_bpabi;
8461             case DT_STRTAB:
8462               name = ".dynstr";
8463               goto get_vma_if_bpabi;
8464             case DT_SYMTAB:
8465               name = ".dynsym";
8466               goto get_vma_if_bpabi;
8467             case DT_VERSYM:
8468               name = ".gnu.version";
8469               goto get_vma_if_bpabi;
8470             case DT_VERDEF:
8471               name = ".gnu.version_d";
8472               goto get_vma_if_bpabi;
8473             case DT_VERNEED:
8474               name = ".gnu.version_r";
8475               goto get_vma_if_bpabi;
8476
8477             case DT_PLTGOT:
8478               name = ".got";
8479               goto get_vma;
8480             case DT_JMPREL:
8481               name = RELOC_SECTION (htab, ".plt");
8482             get_vma:
8483               s = bfd_get_section_by_name (output_bfd, name);
8484               BFD_ASSERT (s != NULL);
8485               if (!htab->symbian_p)
8486                 dyn.d_un.d_ptr = s->vma;
8487               else
8488                 /* In the BPABI, tags in the PT_DYNAMIC section point
8489                    at the file offset, not the memory address, for the
8490                    convenience of the post linker.  */
8491                 dyn.d_un.d_ptr = s->filepos;
8492               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8493               break;
8494
8495             get_vma_if_bpabi:
8496               if (htab->symbian_p)
8497                 goto get_vma;
8498               break;
8499
8500             case DT_PLTRELSZ:
8501               s = bfd_get_section_by_name (output_bfd,
8502                                            RELOC_SECTION (htab, ".plt"));
8503               BFD_ASSERT (s != NULL);
8504               dyn.d_un.d_val = s->size;
8505               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8506               break;
8507               
8508             case DT_RELSZ:
8509             case DT_RELASZ:
8510               if (!htab->symbian_p)
8511                 {
8512                   /* My reading of the SVR4 ABI indicates that the
8513                      procedure linkage table relocs (DT_JMPREL) should be
8514                      included in the overall relocs (DT_REL).  This is
8515                      what Solaris does.  However, UnixWare can not handle
8516                      that case.  Therefore, we override the DT_RELSZ entry
8517                      here to make it not include the JMPREL relocs.  Since
8518                      the linker script arranges for .rel(a).plt to follow all
8519                      other relocation sections, we don't have to worry
8520                      about changing the DT_REL entry.  */
8521                   s = bfd_get_section_by_name (output_bfd,
8522                                                RELOC_SECTION (htab, ".plt"));
8523                   if (s != NULL)
8524                     dyn.d_un.d_val -= s->size;
8525                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8526                   break;
8527                 }
8528               /* Fall through */
8529
8530             case DT_REL:
8531             case DT_RELA:
8532               /* In the BPABI, the DT_REL tag must point at the file
8533                  offset, not the VMA, of the first relocation
8534                  section.  So, we use code similar to that in
8535                  elflink.c, but do not check for SHF_ALLOC on the
8536                  relcoation section, since relocations sections are
8537                  never allocated under the BPABI.  The comments above
8538                  about Unixware notwithstanding, we include all of the
8539                  relocations here.  */
8540               if (htab->symbian_p)
8541                 {
8542                   unsigned int i;
8543                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8544                           ? SHT_REL : SHT_RELA);
8545                   dyn.d_un.d_val = 0;
8546                   for (i = 1; i < elf_numsections (output_bfd); i++)
8547                     {
8548                       Elf_Internal_Shdr *hdr 
8549                         = elf_elfsections (output_bfd)[i];
8550                       if (hdr->sh_type == type)
8551                         {
8552                           if (dyn.d_tag == DT_RELSZ 
8553                               || dyn.d_tag == DT_RELASZ)
8554                             dyn.d_un.d_val += hdr->sh_size;
8555                           else if ((ufile_ptr) hdr->sh_offset
8556                                    <= dyn.d_un.d_val - 1)
8557                             dyn.d_un.d_val = hdr->sh_offset;
8558                         }
8559                     }
8560                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8561                 }
8562               break;
8563
8564               /* Set the bottom bit of DT_INIT/FINI if the
8565                  corresponding function is Thumb.  */
8566             case DT_INIT:
8567               name = info->init_function;
8568               goto get_sym;
8569             case DT_FINI:
8570               name = info->fini_function;
8571             get_sym:
8572               /* If it wasn't set by elf_bfd_final_link
8573                  then there is nothing to adjust.  */
8574               if (dyn.d_un.d_val != 0)
8575                 {
8576                   struct elf_link_hash_entry * eh;
8577
8578                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
8579                                              FALSE, FALSE, TRUE);
8580                   if (eh != (struct elf_link_hash_entry *) NULL
8581                       && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
8582                     {
8583                       dyn.d_un.d_val |= 1;
8584                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8585                     }
8586                 }
8587               break;
8588             }
8589         }
8590
8591       /* Fill in the first entry in the procedure linkage table.  */
8592       if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
8593         {
8594           const bfd_vma *plt0_entry;
8595           bfd_vma got_address, plt_address, got_displacement;
8596
8597           /* Calculate the addresses of the GOT and PLT.  */
8598           got_address = sgot->output_section->vma + sgot->output_offset;
8599           plt_address = splt->output_section->vma + splt->output_offset;
8600
8601           if (htab->vxworks_p)
8602             {
8603               /* The VxWorks GOT is relocated by the dynamic linker.
8604                  Therefore, we must emit relocations rather than simply
8605                  computing the values now.  */
8606               Elf_Internal_Rela rel;
8607
8608               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
8609               put_arm_insn (htab, output_bfd, plt0_entry[0],
8610                             splt->contents + 0);
8611               put_arm_insn (htab, output_bfd, plt0_entry[1],
8612                             splt->contents + 4);
8613               put_arm_insn (htab, output_bfd, plt0_entry[2],
8614                             splt->contents + 8);
8615               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
8616
8617               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_. */
8618               rel.r_offset = plt_address + 12;
8619               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
8620               rel.r_addend = 0;
8621               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
8622                                      htab->srelplt2->contents);
8623             }
8624           else
8625             {
8626               got_displacement = got_address - (plt_address + 16);
8627
8628               plt0_entry = elf32_arm_plt0_entry;
8629               put_arm_insn (htab, output_bfd, plt0_entry[0],
8630                             splt->contents + 0);
8631               put_arm_insn (htab, output_bfd, plt0_entry[1],
8632                             splt->contents + 4);
8633               put_arm_insn (htab, output_bfd, plt0_entry[2],
8634                             splt->contents + 8);
8635               put_arm_insn (htab, output_bfd, plt0_entry[3],
8636                             splt->contents + 12);
8637
8638 #ifdef FOUR_WORD_PLT
8639               /* The displacement value goes in the otherwise-unused
8640                  last word of the second entry.  */
8641               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
8642 #else
8643               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
8644 #endif
8645             }
8646         }
8647
8648       /* UnixWare sets the entsize of .plt to 4, although that doesn't
8649          really seem like the right value.  */
8650       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
8651
8652       if (htab->vxworks_p && !info->shared && htab->splt->size > 0)
8653         {
8654           /* Correct the .rel(a).plt.unloaded relocations.  They will have
8655              incorrect symbol indexes.  */
8656           int num_plts;
8657           unsigned char *p;
8658
8659           num_plts = ((htab->splt->size - htab->plt_header_size)
8660                       / htab->plt_entry_size);
8661           p = htab->srelplt2->contents + RELOC_SIZE (htab);
8662
8663           for (; num_plts; num_plts--)
8664             {
8665               Elf_Internal_Rela rel;
8666
8667               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
8668               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
8669               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
8670               p += RELOC_SIZE (htab);
8671
8672               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
8673               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
8674               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
8675               p += RELOC_SIZE (htab);
8676             }
8677         }
8678     }
8679
8680   /* Fill in the first three entries in the global offset table.  */
8681   if (sgot)
8682     {
8683       if (sgot->size > 0)
8684         {
8685           if (sdyn == NULL)
8686             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
8687           else
8688             bfd_put_32 (output_bfd,
8689                         sdyn->output_section->vma + sdyn->output_offset,
8690                         sgot->contents);
8691           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
8692           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
8693         }
8694
8695       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
8696     }
8697
8698   return TRUE;
8699 }
8700
8701 static void
8702 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8703 {
8704   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
8705   struct elf32_arm_link_hash_table *globals;
8706
8707   i_ehdrp = elf_elfheader (abfd);
8708
8709   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
8710     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
8711   else
8712     i_ehdrp->e_ident[EI_OSABI] = 0;
8713   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
8714
8715   if (link_info)
8716     {
8717       globals = elf32_arm_hash_table (link_info);
8718       if (globals->byteswap_code)
8719         i_ehdrp->e_flags |= EF_ARM_BE8;
8720     }
8721 }
8722
8723 static enum elf_reloc_type_class
8724 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
8725 {
8726   switch ((int) ELF32_R_TYPE (rela->r_info))
8727     {
8728     case R_ARM_RELATIVE:
8729       return reloc_class_relative;
8730     case R_ARM_JUMP_SLOT:
8731       return reloc_class_plt;
8732     case R_ARM_COPY:
8733       return reloc_class_copy;
8734     default:
8735       return reloc_class_normal;
8736     }
8737 }
8738
8739 /* Set the right machine number for an Arm ELF file.  */
8740
8741 static bfd_boolean
8742 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
8743 {
8744   if (hdr->sh_type == SHT_NOTE)
8745     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
8746
8747   return TRUE;
8748 }
8749
8750 static void
8751 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
8752 {
8753   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
8754 }
8755
8756 /* Return TRUE if this is an unwinding table entry.  */
8757
8758 static bfd_boolean
8759 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
8760 {
8761   size_t len1, len2;
8762
8763   len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
8764   len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
8765   return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
8766           || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
8767 }
8768
8769
8770 /* Set the type and flags for an ARM section.  We do this by
8771    the section name, which is a hack, but ought to work.  */
8772
8773 static bfd_boolean
8774 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
8775 {
8776   const char * name;
8777
8778   name = bfd_get_section_name (abfd, sec);
8779
8780   if (is_arm_elf_unwind_section_name (abfd, name))
8781     {
8782       hdr->sh_type = SHT_ARM_EXIDX;
8783       hdr->sh_flags |= SHF_LINK_ORDER;
8784     }
8785   else if (strcmp(name, ".ARM.attributes") == 0)
8786     {
8787       hdr->sh_type = SHT_ARM_ATTRIBUTES;
8788     }
8789   return TRUE;
8790 }
8791
8792 /* Parse an Arm EABI attributes section.  */
8793 static void
8794 elf32_arm_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
8795 {
8796   bfd_byte *contents;
8797   bfd_byte *p;
8798   bfd_vma len;
8799
8800   contents = bfd_malloc (hdr->sh_size);
8801   if (!contents)
8802     return;
8803   if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
8804                                  hdr->sh_size))
8805     {
8806       free (contents);
8807       return;
8808     }
8809   p = contents;
8810   if (*(p++) == 'A')
8811     {
8812       len = hdr->sh_size - 1;
8813       while (len > 0)
8814         {
8815           int namelen;
8816           bfd_vma section_len;
8817
8818           section_len = bfd_get_32 (abfd, p);
8819           p += 4;
8820           if (section_len > len)
8821             section_len = len;
8822           len -= section_len;
8823           namelen = strlen ((char *)p) + 1;
8824           section_len -= namelen + 4;
8825           if (strcmp((char *)p, "aeabi") != 0)
8826             {
8827               /* Vendor section.  Ignore it.  */
8828               p += namelen + section_len;
8829             }
8830           else
8831             {
8832               p += namelen;
8833               while (section_len > 0)
8834                 {
8835                   int tag;
8836                   unsigned int n;
8837                   unsigned int val;
8838                   bfd_vma subsection_len;
8839                   bfd_byte *end;
8840
8841                   tag = read_unsigned_leb128 (abfd, p, &n);
8842                   p += n;
8843                   subsection_len = bfd_get_32 (abfd, p);
8844                   p += 4;
8845                   if (subsection_len > section_len)
8846                     subsection_len = section_len;
8847                   section_len -= subsection_len;
8848                   subsection_len -= n + 4;
8849                   end = p + subsection_len;
8850                   switch (tag)
8851                     {
8852                     case Tag_File:
8853                       while (p < end)
8854                         {
8855                           bfd_boolean is_string;
8856
8857                           tag = read_unsigned_leb128 (abfd, p, &n);
8858                           p += n;
8859                           if (tag == 4 || tag == 5)
8860                             is_string = 1;
8861                           else if (tag < 32)
8862                             is_string = 0;
8863                           else
8864                             is_string = (tag & 1) != 0;
8865                           if (tag == Tag_compatibility)
8866                             {
8867                               val = read_unsigned_leb128 (abfd, p, &n);
8868                               p += n;
8869                               elf32_arm_add_eabi_attr_compat (abfd, val,
8870                                                               (char *)p);
8871                               p += strlen ((char *)p) + 1;
8872                             }
8873                           else if (is_string)
8874                             {
8875                               elf32_arm_add_eabi_attr_string (abfd, tag,
8876                                                               (char *)p);
8877                               p += strlen ((char *)p) + 1;
8878                             }
8879                           else
8880                             {
8881                               val = read_unsigned_leb128 (abfd, p, &n);
8882                               p += n;
8883                               elf32_arm_add_eabi_attr_int (abfd, tag, val);
8884                             }
8885                         }
8886                       break;
8887                     case Tag_Section:
8888                     case Tag_Symbol:
8889                       /* Don't have anywhere convenient to attach these.
8890                          Fall through for now.  */
8891                     default:
8892                       /* Ignore things we don't kow about.  */
8893                       p += subsection_len;
8894                       subsection_len = 0;
8895                       break;
8896                     }
8897                 }
8898             }
8899         }
8900     }
8901   free (contents);
8902 }
8903
8904 /* Handle an ARM specific section when reading an object file.  This is
8905    called when bfd_section_from_shdr finds a section with an unknown
8906    type.  */
8907
8908 static bfd_boolean
8909 elf32_arm_section_from_shdr (bfd *abfd,
8910                              Elf_Internal_Shdr * hdr,
8911                              const char *name,
8912                              int shindex)
8913 {
8914   /* There ought to be a place to keep ELF backend specific flags, but
8915      at the moment there isn't one.  We just keep track of the
8916      sections by their name, instead.  Fortunately, the ABI gives
8917      names for all the ARM specific sections, so we will probably get
8918      away with this.  */
8919   switch (hdr->sh_type)
8920     {
8921     case SHT_ARM_EXIDX:
8922     case SHT_ARM_PREEMPTMAP:
8923     case SHT_ARM_ATTRIBUTES:
8924       break;
8925
8926     default:
8927       return FALSE;
8928     }
8929
8930   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
8931     return FALSE;
8932
8933   if (hdr->sh_type == SHT_ARM_ATTRIBUTES)
8934     elf32_arm_parse_attributes(abfd, hdr);
8935   return TRUE;
8936 }
8937
8938 /* A structure used to record a list of sections, independently
8939    of the next and prev fields in the asection structure.  */
8940 typedef struct section_list
8941 {
8942   asection * sec;
8943   struct section_list * next;
8944   struct section_list * prev;
8945 }
8946 section_list;
8947
8948 /* Unfortunately we need to keep a list of sections for which
8949    an _arm_elf_section_data structure has been allocated.  This
8950    is because it is possible for functions like elf32_arm_write_section
8951    to be called on a section which has had an elf_data_structure
8952    allocated for it (and so the used_by_bfd field is valid) but
8953    for which the ARM extended version of this structure - the
8954    _arm_elf_section_data structure - has not been allocated.  */
8955 static section_list * sections_with_arm_elf_section_data = NULL;
8956
8957 static void
8958 record_section_with_arm_elf_section_data (asection * sec)
8959 {
8960   struct section_list * entry;
8961
8962   entry = bfd_malloc (sizeof (* entry));
8963   if (entry == NULL)
8964     return;
8965   entry->sec = sec;
8966   entry->next = sections_with_arm_elf_section_data;
8967   entry->prev = NULL;
8968   if (entry->next != NULL)
8969     entry->next->prev = entry;
8970   sections_with_arm_elf_section_data = entry;
8971 }
8972
8973 static struct section_list *
8974 find_arm_elf_section_entry (asection * sec)
8975 {
8976   struct section_list * entry;
8977   static struct section_list * last_entry = NULL;
8978
8979   /* This is a short cut for the typical case where the sections are added
8980      to the sections_with_arm_elf_section_data list in forward order and
8981      then looked up here in backwards order.  This makes a real difference
8982      to the ld-srec/sec64k.exp linker test.  */
8983   entry = sections_with_arm_elf_section_data;
8984   if (last_entry != NULL)
8985     {
8986       if (last_entry->sec == sec)
8987         entry = last_entry;
8988       else if (last_entry->next != NULL
8989                && last_entry->next->sec == sec)
8990         entry = last_entry->next;
8991     }
8992
8993   for (; entry; entry = entry->next)
8994     if (entry->sec == sec)
8995       break;
8996
8997   if (entry)
8998     /* Record the entry prior to this one - it is the entry we are most
8999        likely to want to locate next time.  Also this way if we have been
9000        called from unrecord_section_with_arm_elf_section_data() we will not
9001        be caching a pointer that is about to be freed.  */
9002     last_entry = entry->prev;
9003
9004   return entry;
9005 }
9006
9007 static _arm_elf_section_data *
9008 get_arm_elf_section_data (asection * sec)
9009 {
9010   struct section_list * entry;
9011
9012   entry = find_arm_elf_section_entry (sec);
9013
9014   if (entry)
9015     return elf32_arm_section_data (entry->sec);
9016   else
9017     return NULL;
9018 }
9019
9020 static void
9021 unrecord_section_with_arm_elf_section_data (asection * sec)
9022 {
9023   struct section_list * entry;
9024
9025   entry = find_arm_elf_section_entry (sec);
9026
9027   if (entry)
9028     {
9029       if (entry->prev != NULL)
9030         entry->prev->next = entry->next;
9031       if (entry->next != NULL)
9032         entry->next->prev = entry->prev;
9033       if (entry == sections_with_arm_elf_section_data)
9034         sections_with_arm_elf_section_data = entry->next;
9035       free (entry);
9036     }
9037 }
9038
9039 /* Called for each symbol.  Builds a section map based on mapping symbols.
9040    Does not alter any of the symbols.  */
9041
9042 static bfd_boolean
9043 elf32_arm_output_symbol_hook (struct bfd_link_info *info,
9044                               const char *name,
9045                               Elf_Internal_Sym *elfsym,
9046                               asection *input_sec,
9047                               struct elf_link_hash_entry *h)
9048 {
9049   int mapcount;
9050   elf32_arm_section_map *map;
9051   elf32_arm_section_map *newmap;
9052   _arm_elf_section_data *arm_data;
9053   struct elf32_arm_link_hash_table *globals;
9054
9055   globals = elf32_arm_hash_table (info);
9056   if (globals->vxworks_p
9057       && !elf_vxworks_link_output_symbol_hook (info, name, elfsym,
9058                                                input_sec, h))
9059     return FALSE;
9060
9061   /* Only do this on final link.  */
9062   if (info->relocatable)
9063     return TRUE;
9064
9065   /* Only build a map if we need to byteswap code.  */
9066   if (!globals->byteswap_code)
9067     return TRUE;
9068
9069   /* We only want mapping symbols.  */
9070   if (!bfd_is_arm_special_symbol_name (name, BFD_ARM_SPECIAL_SYM_TYPE_MAP))
9071     return TRUE;
9072
9073   /* If this section has not been allocated an _arm_elf_section_data
9074      structure then we cannot record anything.  */
9075   arm_data = get_arm_elf_section_data (input_sec);
9076   if (arm_data == NULL)
9077     return TRUE;
9078
9079   mapcount = arm_data->mapcount + 1;
9080   map = arm_data->map;
9081
9082   /* TODO: This may be inefficient, but we probably don't usually have many
9083      mapping symbols per section.  */
9084   newmap = bfd_realloc (map, mapcount * sizeof (* map));
9085   if (newmap != NULL)
9086     {
9087       arm_data->map = newmap;
9088       arm_data->mapcount = mapcount;
9089
9090       newmap[mapcount - 1].vma = elfsym->st_value;
9091       newmap[mapcount - 1].type = name[1];
9092     }
9093
9094   return TRUE;
9095 }
9096
9097 typedef struct
9098 {
9099   void *finfo;
9100   struct bfd_link_info *info;
9101   int plt_shndx;
9102   bfd_vma plt_offset;
9103   bfd_boolean (*func) (void *, const char *, Elf_Internal_Sym *,
9104                        asection *, struct elf_link_hash_entry *);
9105 } output_arch_syminfo;
9106
9107 enum map_symbol_type
9108 {
9109   ARM_MAP_ARM,
9110   ARM_MAP_THUMB,
9111   ARM_MAP_DATA
9112 };
9113
9114
9115 /* Output a single PLT mapping symbol.  */
9116
9117 static bfd_boolean
9118 elf32_arm_ouput_plt_map_sym (output_arch_syminfo *osi,
9119                              enum map_symbol_type type,
9120                              bfd_vma offset)
9121 {
9122   static const char *names[3] = {"$a", "$t", "$d"};
9123   struct elf32_arm_link_hash_table *htab;
9124   Elf_Internal_Sym sym;
9125
9126   htab = elf32_arm_hash_table (osi->info);
9127   sym.st_value = osi->plt_offset + offset;
9128   sym.st_size = 0;
9129   sym.st_other = 0;
9130   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
9131   sym.st_shndx = osi->plt_shndx;
9132   if (!osi->func (osi->finfo, names[type], &sym, htab->splt, NULL))
9133     return FALSE;
9134   return TRUE;
9135 }
9136
9137
9138 /* Output mapping symbols for PLT entries associated with H.  */
9139
9140 static bfd_boolean
9141 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
9142 {
9143   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
9144   struct elf32_arm_link_hash_table *htab;
9145   struct elf32_arm_link_hash_entry *eh;
9146   bfd_vma addr;
9147
9148   htab = elf32_arm_hash_table (osi->info);
9149
9150   if (h->root.type == bfd_link_hash_indirect)
9151     return TRUE;
9152
9153   if (h->root.type == bfd_link_hash_warning)
9154     /* When warning symbols are created, they **replace** the "real"
9155        entry in the hash table, thus we never get to see the real
9156        symbol in a hash traversal.  So look at it now.  */
9157     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9158
9159   if (h->plt.offset == (bfd_vma) -1)
9160     return TRUE;
9161
9162   eh = (struct elf32_arm_link_hash_entry *) h;
9163   addr = h->plt.offset;
9164   if (htab->symbian_p)
9165     {
9166       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_ARM, addr))
9167         return FALSE;
9168       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_DATA, addr + 4))
9169         return FALSE;
9170     }
9171   else if (htab->vxworks_p)
9172     {
9173       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_ARM, addr))
9174         return FALSE;
9175       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_DATA, addr + 8))
9176         return FALSE;
9177       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_ARM, addr + 12))
9178         return FALSE;
9179       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_DATA, addr + 20))
9180         return FALSE;
9181     }
9182   else
9183     {
9184       bfd_boolean thumb_stub;
9185
9186       thumb_stub = eh->plt_thumb_refcount > 0 && !htab->use_blx;
9187       if (thumb_stub)
9188         {
9189           if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_THUMB, addr - 4))
9190             return FALSE;
9191         }
9192 #ifdef FOUR_WORD_PLT
9193       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_ARM, addr))
9194         return FALSE;
9195       if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_DATA, addr + 12))
9196         return FALSE;
9197 #else
9198       /* A three-word PLT with no Thumb thunk contains only Arm code, 
9199          so only need to output a mapping symbol for the first PLT entry and
9200          entries with thumb thunks.  */
9201       if (thumb_stub || addr == 20)
9202         {
9203           if (!elf32_arm_ouput_plt_map_sym (osi, ARM_MAP_ARM, addr))
9204             return FALSE;
9205         }
9206 #endif
9207     }
9208
9209   return TRUE;
9210 }
9211
9212
9213 /* Output mapping symbols for the PLT.  */
9214
9215 static bfd_boolean
9216 elf32_arm_output_arch_local_syms (bfd *output_bfd,
9217     struct bfd_link_info *info,
9218     void *finfo, bfd_boolean (*func) (void *, const char *,
9219                                     Elf_Internal_Sym *,
9220                                     asection *,
9221                                     struct elf_link_hash_entry *))
9222 {
9223   output_arch_syminfo osi;
9224   struct elf32_arm_link_hash_table *htab;
9225
9226   htab = elf32_arm_hash_table (info);
9227   if (!htab->splt || htab->splt->size == 0)
9228     return TRUE;
9229
9230   check_use_blx(htab);
9231   osi.finfo = finfo;
9232   osi.info = info;
9233   osi.func = func;
9234   osi.plt_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9235       htab->splt->output_section);
9236   osi.plt_offset = htab->splt->output_section->vma;
9237
9238   /* Output mapping symbols for the plt header.  SymbianOS does not have a
9239      plt header.  */
9240   if (htab->vxworks_p)
9241     {
9242       /* VxWorks shared libraries have no PLT header.  */
9243       if (!info->shared)
9244         {
9245           if (!elf32_arm_ouput_plt_map_sym (&osi, ARM_MAP_ARM, 0))
9246             return FALSE;
9247           if (!elf32_arm_ouput_plt_map_sym (&osi, ARM_MAP_DATA, 12))
9248             return FALSE;
9249         }
9250     }
9251   else if (!htab->symbian_p)
9252     {
9253       if (!elf32_arm_ouput_plt_map_sym (&osi, ARM_MAP_ARM, 0))
9254         return FALSE;
9255 #ifndef FOUR_WORD_PLT
9256       if (!elf32_arm_ouput_plt_map_sym (&osi, ARM_MAP_DATA, 16))
9257         return FALSE;
9258 #endif
9259     }
9260
9261   elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, (void *) &osi);
9262   return TRUE;
9263 }
9264
9265 /* Allocate target specific section data.  */
9266
9267 static bfd_boolean
9268 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
9269 {
9270   _arm_elf_section_data *sdata;
9271   bfd_size_type amt = sizeof (*sdata);
9272
9273   sdata = bfd_zalloc (abfd, amt);
9274   if (sdata == NULL)
9275     return FALSE;
9276   sec->used_by_bfd = sdata;
9277
9278   record_section_with_arm_elf_section_data (sec);
9279
9280   return _bfd_elf_new_section_hook (abfd, sec);
9281 }
9282
9283
9284 /* Used to order a list of mapping symbols by address.  */
9285
9286 static int
9287 elf32_arm_compare_mapping (const void * a, const void * b)
9288 {
9289   return ((const elf32_arm_section_map *) a)->vma
9290          > ((const elf32_arm_section_map *) b)->vma;
9291 }
9292
9293
9294 /* Do code byteswapping.  Return FALSE afterwards so that the section is
9295    written out as normal.  */
9296
9297 static bfd_boolean
9298 elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
9299                          bfd_byte *contents)
9300 {
9301   int mapcount;
9302   _arm_elf_section_data *arm_data;
9303   elf32_arm_section_map *map;
9304   bfd_vma ptr;
9305   bfd_vma end;
9306   bfd_vma offset;
9307   bfd_byte tmp;
9308   int i;
9309
9310   /* If this section has not been allocated an _arm_elf_section_data
9311      structure then we cannot record anything.  */
9312   arm_data = get_arm_elf_section_data (sec);
9313   if (arm_data == NULL)
9314     return FALSE;
9315
9316   mapcount = arm_data->mapcount;
9317   map = arm_data->map;
9318
9319   if (mapcount == 0)
9320     return FALSE;
9321
9322   qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
9323
9324   offset = sec->output_section->vma + sec->output_offset;
9325   ptr = map[0].vma - offset;
9326   for (i = 0; i < mapcount; i++)
9327     {
9328       if (i == mapcount - 1)
9329         end = sec->size;
9330       else
9331         end = map[i + 1].vma - offset;
9332
9333       switch (map[i].type)
9334         {
9335         case 'a':
9336           /* Byte swap code words.  */
9337           while (ptr + 3 < end)
9338             {
9339               tmp = contents[ptr];
9340               contents[ptr] = contents[ptr + 3];
9341               contents[ptr + 3] = tmp;
9342               tmp = contents[ptr + 1];
9343               contents[ptr + 1] = contents[ptr + 2];
9344               contents[ptr + 2] = tmp;
9345               ptr += 4;
9346             }
9347           break;
9348
9349         case 't':
9350           /* Byte swap code halfwords.  */
9351           while (ptr + 1 < end)
9352             {
9353               tmp = contents[ptr];
9354               contents[ptr] = contents[ptr + 1];
9355               contents[ptr + 1] = tmp;
9356               ptr += 2;
9357             }
9358           break;
9359
9360         case 'd':
9361           /* Leave data alone.  */
9362           break;
9363         }
9364       ptr = end;
9365     }
9366
9367   free (map);
9368   arm_data->mapcount = 0;
9369   arm_data->map = NULL;
9370   unrecord_section_with_arm_elf_section_data (sec);
9371
9372   return FALSE;
9373 }
9374
9375 static void
9376 unrecord_section_via_map_over_sections (bfd * abfd ATTRIBUTE_UNUSED,
9377                                         asection * sec,
9378                                         void * ignore ATTRIBUTE_UNUSED)
9379 {
9380   unrecord_section_with_arm_elf_section_data (sec);
9381 }
9382
9383 static bfd_boolean
9384 elf32_arm_close_and_cleanup (bfd * abfd)
9385 {
9386   bfd_map_over_sections (abfd, unrecord_section_via_map_over_sections, NULL);
9387
9388   return _bfd_elf_close_and_cleanup (abfd);
9389 }
9390
9391 /* Display STT_ARM_TFUNC symbols as functions.  */
9392
9393 static void
9394 elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
9395                              asymbol *asym)
9396 {
9397   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
9398
9399   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
9400     elfsym->symbol.flags |= BSF_FUNCTION;
9401 }
9402
9403
9404 /* Mangle thumb function symbols as we read them in.  */
9405
9406 static void
9407 elf32_arm_swap_symbol_in (bfd * abfd,
9408                           const void *psrc,
9409                           const void *pshn,
9410                           Elf_Internal_Sym *dst)
9411 {
9412   bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst);
9413
9414   /* New EABI objects mark thumb function symbols by setting the low bit of
9415      the address.  Turn these into STT_ARM_TFUNC.  */
9416   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
9417       && (dst->st_value & 1))
9418     {
9419       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
9420       dst->st_value &= ~(bfd_vma) 1;
9421     }
9422 }
9423
9424
9425 /* Mangle thumb function symbols as we write them out.  */
9426
9427 static void
9428 elf32_arm_swap_symbol_out (bfd *abfd,
9429                            const Elf_Internal_Sym *src,
9430                            void *cdst,
9431                            void *shndx)
9432 {
9433   Elf_Internal_Sym newsym;
9434
9435   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
9436      of the address set, as per the new EABI.  We do this unconditionally
9437      because objcopy does not set the elf header flags until after
9438      it writes out the symbol table.  */
9439   if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
9440     {
9441       newsym = *src;
9442       newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
9443       if (newsym.st_shndx != SHN_UNDEF)
9444         {
9445           /* Do this only for defined symbols. At link type, the static
9446              linker will simulate the work of dynamic linker of resolving
9447              symbols and will carry over the thumbness of found symbols to
9448              the output symbol table. It's not clear how it happens, but
9449              the thumbness of underfined symbols can well be different at
9450              runtime, and writing '1' for them will be confusing for users
9451              and possibly for dynamic linker itself.
9452           */
9453           newsym.st_value |= 1;
9454         }
9455       
9456       src = &newsym;
9457     }
9458   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
9459 }
9460
9461 /* Add the PT_ARM_EXIDX program header.  */
9462
9463 static bfd_boolean
9464 elf32_arm_modify_segment_map (bfd *abfd, 
9465                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
9466 {
9467   struct elf_segment_map *m;
9468   asection *sec;
9469
9470   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
9471   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
9472     {
9473       /* If there is already a PT_ARM_EXIDX header, then we do not
9474          want to add another one.  This situation arises when running
9475          "strip"; the input binary already has the header.  */
9476       m = elf_tdata (abfd)->segment_map;
9477       while (m && m->p_type != PT_ARM_EXIDX)
9478         m = m->next;
9479       if (!m)
9480         {
9481           m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
9482           if (m == NULL)
9483             return FALSE;
9484           m->p_type = PT_ARM_EXIDX;
9485           m->count = 1;
9486           m->sections[0] = sec;
9487
9488           m->next = elf_tdata (abfd)->segment_map;
9489           elf_tdata (abfd)->segment_map = m;
9490         }
9491     }
9492
9493   return TRUE;
9494 }
9495
9496 /* We may add a PT_ARM_EXIDX program header.  */
9497
9498 static int
9499 elf32_arm_additional_program_headers (bfd *abfd)
9500 {
9501   asection *sec;
9502
9503   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
9504   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
9505     return 1;
9506   else
9507     return 0;
9508 }
9509
9510 /* We use this to override swap_symbol_in and swap_symbol_out.  */
9511 const struct elf_size_info elf32_arm_size_info = {
9512   sizeof (Elf32_External_Ehdr),
9513   sizeof (Elf32_External_Phdr),
9514   sizeof (Elf32_External_Shdr),
9515   sizeof (Elf32_External_Rel),
9516   sizeof (Elf32_External_Rela),
9517   sizeof (Elf32_External_Sym),
9518   sizeof (Elf32_External_Dyn),
9519   sizeof (Elf_External_Note),
9520   4,
9521   1,
9522   32, 2,
9523   ELFCLASS32, EV_CURRENT,
9524   bfd_elf32_write_out_phdrs,
9525   bfd_elf32_write_shdrs_and_ehdr,
9526   bfd_elf32_write_relocs,
9527   elf32_arm_swap_symbol_in,
9528   elf32_arm_swap_symbol_out,
9529   bfd_elf32_slurp_reloc_table,
9530   bfd_elf32_slurp_symbol_table,
9531   bfd_elf32_swap_dyn_in,
9532   bfd_elf32_swap_dyn_out,
9533   bfd_elf32_swap_reloc_in,
9534   bfd_elf32_swap_reloc_out,
9535   bfd_elf32_swap_reloca_in,
9536   bfd_elf32_swap_reloca_out
9537 };
9538
9539 #define ELF_ARCH                        bfd_arch_arm
9540 #define ELF_MACHINE_CODE                EM_ARM
9541 #ifdef __QNXTARGET__
9542 #define ELF_MAXPAGESIZE                 0x1000
9543 #else
9544 #define ELF_MAXPAGESIZE                 0x8000
9545 #endif
9546 #define ELF_MINPAGESIZE                 0x1000
9547
9548 #define bfd_elf32_mkobject                      elf32_arm_mkobject
9549
9550 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
9551 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
9552 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
9553 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
9554 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
9555 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
9556 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
9557 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
9558 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
9559 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
9560 #define bfd_elf32_close_and_cleanup             elf32_arm_close_and_cleanup
9561 #define bfd_elf32_bfd_final_link                elf32_arm_bfd_final_link
9562
9563 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
9564 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
9565 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
9566 #define elf_backend_check_relocs                elf32_arm_check_relocs
9567 #define elf_backend_relocate_section            elf32_arm_relocate_section
9568 #define elf_backend_write_section               elf32_arm_write_section
9569 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
9570 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
9571 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
9572 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
9573 #define elf_backend_link_output_symbol_hook     elf32_arm_output_symbol_hook
9574 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
9575 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
9576 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
9577 #define elf_backend_object_p                    elf32_arm_object_p
9578 #define elf_backend_section_flags               elf32_arm_section_flags
9579 #define elf_backend_fake_sections               elf32_arm_fake_sections
9580 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
9581 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
9582 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
9583 #define elf_backend_symbol_processing           elf32_arm_symbol_processing
9584 #define elf_backend_size_info                   elf32_arm_size_info
9585 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
9586 #define elf_backend_additional_program_headers \
9587   elf32_arm_additional_program_headers
9588 #define elf_backend_output_arch_local_syms \
9589   elf32_arm_output_arch_local_syms
9590 #define elf_backend_begin_write_processing \
9591     elf32_arm_begin_write_processing
9592
9593 #define elf_backend_can_refcount    1
9594 #define elf_backend_can_gc_sections 1
9595 #define elf_backend_plt_readonly    1
9596 #define elf_backend_want_got_plt    1
9597 #define elf_backend_want_plt_sym    0
9598 #define elf_backend_may_use_rel_p   1
9599 #define elf_backend_may_use_rela_p  0
9600 #define elf_backend_default_use_rela_p 0
9601 #define elf_backend_rela_normal     0
9602
9603 #define elf_backend_got_header_size     12
9604
9605 #include "elf32-target.h"
9606
9607 /* VxWorks Targets */
9608
9609 #undef TARGET_LITTLE_SYM
9610 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
9611 #undef TARGET_LITTLE_NAME
9612 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
9613 #undef TARGET_BIG_SYM
9614 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
9615 #undef TARGET_BIG_NAME
9616 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
9617
9618 /* Like elf32_arm_link_hash_table_create -- but overrides
9619    appropriately for VxWorks.  */
9620 static struct bfd_link_hash_table *
9621 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
9622 {
9623   struct bfd_link_hash_table *ret;
9624
9625   ret = elf32_arm_link_hash_table_create (abfd);
9626   if (ret)
9627     {
9628       struct elf32_arm_link_hash_table *htab
9629         = (struct elf32_arm_link_hash_table *) ret;
9630       htab->use_rel = 0;
9631       htab->vxworks_p = 1;
9632     }
9633   return ret;
9634 }     
9635
9636 static void
9637 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9638 {
9639   elf32_arm_final_write_processing (abfd, linker);
9640   elf_vxworks_final_write_processing (abfd, linker);
9641 }
9642
9643 #undef elf32_bed
9644 #define elf32_bed elf32_arm_vxworks_bed
9645
9646 #undef bfd_elf32_bfd_link_hash_table_create
9647 #define bfd_elf32_bfd_link_hash_table_create \
9648   elf32_arm_vxworks_link_hash_table_create
9649 #undef elf_backend_add_symbol_hook
9650 #define elf_backend_add_symbol_hook \
9651   elf_vxworks_add_symbol_hook
9652 #undef elf_backend_final_write_processing
9653 #define elf_backend_final_write_processing \
9654   elf32_arm_vxworks_final_write_processing
9655 #undef elf_backend_emit_relocs
9656 #define elf_backend_emit_relocs \
9657   elf_vxworks_emit_relocs
9658
9659 #undef elf_backend_may_use_rel_p
9660 #define elf_backend_may_use_rel_p       0
9661 #undef elf_backend_may_use_rela_p
9662 #define elf_backend_may_use_rela_p      1
9663 #undef elf_backend_default_use_rela_p
9664 #define elf_backend_default_use_rela_p  1
9665 #undef elf_backend_rela_normal
9666 #define elf_backend_rela_normal         1
9667 #undef elf_backend_want_plt_sym
9668 #define elf_backend_want_plt_sym        1
9669 #undef ELF_MAXPAGESIZE
9670 #define ELF_MAXPAGESIZE                 0x1000
9671
9672 #include "elf32-target.h"
9673
9674
9675 /* Symbian OS Targets */
9676
9677 #undef TARGET_LITTLE_SYM
9678 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
9679 #undef TARGET_LITTLE_NAME
9680 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
9681 #undef TARGET_BIG_SYM
9682 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
9683 #undef TARGET_BIG_NAME
9684 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
9685
9686 /* Like elf32_arm_link_hash_table_create -- but overrides
9687    appropriately for Symbian OS.  */
9688 static struct bfd_link_hash_table *
9689 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
9690 {
9691   struct bfd_link_hash_table *ret;
9692
9693   ret = elf32_arm_link_hash_table_create (abfd);
9694   if (ret)
9695     {
9696       struct elf32_arm_link_hash_table *htab
9697         = (struct elf32_arm_link_hash_table *)ret;
9698       /* There is no PLT header for Symbian OS.  */
9699       htab->plt_header_size = 0;
9700       /* The PLT entries are each three instructions.  */
9701       htab->plt_entry_size = 4 * NUM_ELEM (elf32_arm_symbian_plt_entry);
9702       htab->symbian_p = 1;
9703       /* Symbian uses armv5t or above, so use_blx is always true.  */
9704       htab->use_blx = 1;
9705       htab->root.is_relocatable_executable = 1;
9706     }
9707   return ret;
9708 }     
9709
9710 static const struct bfd_elf_special_section
9711 elf32_arm_symbian_special_sections[] =
9712 {
9713   /* In a BPABI executable, the dynamic linking sections do not go in
9714      the loadable read-only segment.  The post-linker may wish to
9715      refer to these sections, but they are not part of the final
9716      program image.  */
9717   { ".dynamic",        8,  0, SHT_DYNAMIC,  0 },
9718   { ".dynstr",         7,  0, SHT_STRTAB,   0 },
9719   { ".dynsym",         7,  0, SHT_DYNSYM,   0 },
9720   { ".got",            4,  0, SHT_PROGBITS, 0 },
9721   { ".hash",           5,  0, SHT_HASH,     0 },
9722   /* These sections do not need to be writable as the SymbianOS
9723      postlinker will arrange things so that no dynamic relocation is
9724      required.  */
9725   { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC },
9726   { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC },
9727   { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC },
9728   { NULL,              0,  0, 0,            0 }
9729 };
9730
9731 static void
9732 elf32_arm_symbian_begin_write_processing (bfd *abfd, 
9733                                           struct bfd_link_info *link_info)
9734 {
9735   /* BPABI objects are never loaded directly by an OS kernel; they are
9736      processed by a postlinker first, into an OS-specific format.  If
9737      the D_PAGED bit is set on the file, BFD will align segments on
9738      page boundaries, so that an OS can directly map the file.  With
9739      BPABI objects, that just results in wasted space.  In addition,
9740      because we clear the D_PAGED bit, map_sections_to_segments will
9741      recognize that the program headers should not be mapped into any
9742      loadable segment.  */
9743   abfd->flags &= ~D_PAGED;
9744   elf32_arm_begin_write_processing(abfd, link_info);
9745 }
9746
9747 static bfd_boolean
9748 elf32_arm_symbian_modify_segment_map (bfd *abfd, 
9749                                       struct bfd_link_info *info)
9750 {
9751   struct elf_segment_map *m;
9752   asection *dynsec;
9753
9754   /* BPABI shared libraries and executables should have a PT_DYNAMIC
9755      segment.  However, because the .dynamic section is not marked
9756      with SEC_LOAD, the generic ELF code will not create such a
9757      segment.  */
9758   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
9759   if (dynsec)
9760     {
9761       m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
9762       m->next = elf_tdata (abfd)->segment_map;
9763       elf_tdata (abfd)->segment_map = m;
9764     }
9765
9766   /* Also call the generic arm routine.  */
9767   return elf32_arm_modify_segment_map (abfd, info);
9768 }
9769
9770 #undef elf32_bed
9771 #define elf32_bed elf32_arm_symbian_bed
9772
9773 /* The dynamic sections are not allocated on SymbianOS; the postlinker
9774    will process them and then discard them.  */
9775 #undef ELF_DYNAMIC_SEC_FLAGS
9776 #define ELF_DYNAMIC_SEC_FLAGS \
9777   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
9778
9779 #undef bfd_elf32_bfd_link_hash_table_create
9780 #define bfd_elf32_bfd_link_hash_table_create \
9781   elf32_arm_symbian_link_hash_table_create
9782 #undef elf_backend_add_symbol_hook
9783
9784 #undef elf_backend_special_sections
9785 #define elf_backend_special_sections elf32_arm_symbian_special_sections
9786
9787 #undef elf_backend_begin_write_processing
9788 #define elf_backend_begin_write_processing \
9789     elf32_arm_symbian_begin_write_processing
9790 #undef elf_backend_final_write_processing
9791 #define elf_backend_final_write_processing \
9792   elf32_arm_final_write_processing
9793 #undef elf_backend_emit_relocs
9794
9795 #undef elf_backend_modify_segment_map
9796 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
9797
9798 /* There is no .got section for BPABI objects, and hence no header.  */
9799 #undef elf_backend_got_header_size
9800 #define elf_backend_got_header_size 0
9801
9802 /* Similarly, there is no .got.plt section.  */
9803 #undef elf_backend_want_got_plt
9804 #define elf_backend_want_got_plt 0
9805
9806 #undef elf_backend_may_use_rel_p
9807 #define elf_backend_may_use_rel_p       1
9808 #undef elf_backend_may_use_rela_p
9809 #define elf_backend_may_use_rela_p      0
9810 #undef elf_backend_default_use_rela_p
9811 #define elf_backend_default_use_rela_p  0
9812 #undef elf_backend_rela_normal
9813 #define elf_backend_rela_normal         0
9814 #undef elf_backend_want_plt_sym
9815 #define elf_backend_want_plt_sym        0
9816 #undef ELF_MAXPAGESIZE
9817 #define ELF_MAXPAGESIZE                 0x8000
9818
9819 #include "elf32-target.h"