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