2005-04-08 Paul Brook <paul@codesourcery.com>
[external/binutils.git] / bfd / elf32-arm.c
1 /* 32-bit ELF support for ARM
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "elf/arm.h"
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26
27 #ifndef NUM_ELEM
28 #define NUM_ELEM(a)  (sizeof (a) / (sizeof (a)[0]))
29 #endif
30
31 #define elf_info_to_howto               0
32 #define elf_info_to_howto_rel           elf32_arm_info_to_howto
33
34 #define ARM_ELF_ABI_VERSION             0
35 #define ARM_ELF_OS_ABI_VERSION          ELFOSABI_ARM
36
37 static reloc_howto_type * elf32_arm_reloc_type_lookup
38   PARAMS ((bfd * abfd, bfd_reloc_code_real_type code));
39 static bfd_boolean elf32_arm_nabi_grok_prstatus
40   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
41 static bfd_boolean elf32_arm_nabi_grok_psinfo
42   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
43
44 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
45    R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
46    in that slot.  */
47
48 static reloc_howto_type elf32_arm_howto_table[] =
49 {
50   /* No relocation */
51   HOWTO (R_ARM_NONE,            /* type */
52          0,                     /* rightshift */
53          0,                     /* size (0 = byte, 1 = short, 2 = long) */
54          0,                     /* bitsize */
55          FALSE,                 /* pc_relative */
56          0,                     /* bitpos */
57          complain_overflow_dont,/* complain_on_overflow */
58          bfd_elf_generic_reloc, /* special_function */
59          "R_ARM_NONE",          /* name */
60          FALSE,                 /* partial_inplace */
61          0,                     /* src_mask */
62          0,                     /* dst_mask */
63          FALSE),                /* pcrel_offset */
64
65   HOWTO (R_ARM_PC24,            /* type */
66          2,                     /* rightshift */
67          2,                     /* size (0 = byte, 1 = short, 2 = long) */
68          24,                    /* bitsize */
69          TRUE,                  /* pc_relative */
70          0,                     /* bitpos */
71          complain_overflow_signed,/* complain_on_overflow */
72          bfd_elf_generic_reloc, /* special_function */
73          "R_ARM_PC24",          /* name */
74          FALSE,                 /* partial_inplace */
75          0x00ffffff,            /* src_mask */
76          0x00ffffff,            /* dst_mask */
77          TRUE),                 /* pcrel_offset */
78
79   /* 32 bit absolute */
80   HOWTO (R_ARM_ABS32,           /* type */
81          0,                     /* rightshift */
82          2,                     /* size (0 = byte, 1 = short, 2 = long) */
83          32,                    /* bitsize */
84          FALSE,                 /* pc_relative */
85          0,                     /* bitpos */
86          complain_overflow_bitfield,/* complain_on_overflow */
87          bfd_elf_generic_reloc, /* special_function */
88          "R_ARM_ABS32",         /* name */
89          FALSE,                 /* partial_inplace */
90          0xffffffff,            /* src_mask */
91          0xffffffff,            /* dst_mask */
92          FALSE),                /* pcrel_offset */
93
94   /* standard 32bit pc-relative reloc */
95   HOWTO (R_ARM_REL32,           /* type */
96          0,                     /* rightshift */
97          2,                     /* size (0 = byte, 1 = short, 2 = long) */
98          32,                    /* bitsize */
99          TRUE,                  /* pc_relative */
100          0,                     /* bitpos */
101          complain_overflow_bitfield,/* complain_on_overflow */
102          bfd_elf_generic_reloc, /* special_function */
103          "R_ARM_REL32",         /* name */
104          FALSE,                 /* partial_inplace */
105          0xffffffff,            /* src_mask */
106          0xffffffff,            /* dst_mask */
107          TRUE),                 /* pcrel_offset */
108
109   /* 8 bit absolute */
110   HOWTO (R_ARM_PC13,            /* type */
111          0,                     /* rightshift */
112          0,                     /* size (0 = byte, 1 = short, 2 = long) */
113          8,                     /* bitsize */
114          FALSE,                 /* pc_relative */
115          0,                     /* bitpos */
116          complain_overflow_bitfield,/* complain_on_overflow */
117          bfd_elf_generic_reloc, /* special_function */
118          "R_ARM_PC13",          /* name */
119          FALSE,                 /* partial_inplace */
120          0x000000ff,            /* src_mask */
121          0x000000ff,            /* dst_mask */
122          FALSE),                /* pcrel_offset */
123
124    /* 16 bit absolute */
125   HOWTO (R_ARM_ABS16,           /* type */
126          0,                     /* rightshift */
127          1,                     /* size (0 = byte, 1 = short, 2 = long) */
128          16,                    /* bitsize */
129          FALSE,                 /* pc_relative */
130          0,                     /* bitpos */
131          complain_overflow_bitfield,/* complain_on_overflow */
132          bfd_elf_generic_reloc, /* special_function */
133          "R_ARM_ABS16",         /* name */
134          FALSE,                 /* partial_inplace */
135          0x0000ffff,            /* src_mask */
136          0x0000ffff,            /* dst_mask */
137          FALSE),                /* pcrel_offset */
138
139   /* 12 bit absolute */
140   HOWTO (R_ARM_ABS12,           /* type */
141          0,                     /* rightshift */
142          2,                     /* size (0 = byte, 1 = short, 2 = long) */
143          12,                    /* bitsize */
144          FALSE,                 /* pc_relative */
145          0,                     /* bitpos */
146          complain_overflow_bitfield,/* complain_on_overflow */
147          bfd_elf_generic_reloc, /* special_function */
148          "R_ARM_ABS12",         /* name */
149          FALSE,                 /* partial_inplace */
150          0x000008ff,            /* src_mask */
151          0x000008ff,            /* dst_mask */
152          FALSE),                /* pcrel_offset */
153
154   HOWTO (R_ARM_THM_ABS5,        /* type */
155          6,                     /* rightshift */
156          1,                     /* size (0 = byte, 1 = short, 2 = long) */
157          5,                     /* bitsize */
158          FALSE,                 /* pc_relative */
159          0,                     /* bitpos */
160          complain_overflow_bitfield,/* complain_on_overflow */
161          bfd_elf_generic_reloc, /* special_function */
162          "R_ARM_THM_ABS5",      /* name */
163          FALSE,                 /* partial_inplace */
164          0x000007e0,            /* src_mask */
165          0x000007e0,            /* dst_mask */
166          FALSE),                /* pcrel_offset */
167
168   /* 8 bit absolute */
169   HOWTO (R_ARM_ABS8,            /* type */
170          0,                     /* rightshift */
171          0,                     /* size (0 = byte, 1 = short, 2 = long) */
172          8,                     /* bitsize */
173          FALSE,                 /* pc_relative */
174          0,                     /* bitpos */
175          complain_overflow_bitfield,/* complain_on_overflow */
176          bfd_elf_generic_reloc, /* special_function */
177          "R_ARM_ABS8",          /* name */
178          FALSE,                 /* partial_inplace */
179          0x000000ff,            /* src_mask */
180          0x000000ff,            /* dst_mask */
181          FALSE),                /* pcrel_offset */
182
183   HOWTO (R_ARM_SBREL32,         /* type */
184          0,                     /* rightshift */
185          2,                     /* size (0 = byte, 1 = short, 2 = long) */
186          32,                    /* bitsize */
187          FALSE,                 /* pc_relative */
188          0,                     /* bitpos */
189          complain_overflow_dont,/* complain_on_overflow */
190          bfd_elf_generic_reloc, /* special_function */
191          "R_ARM_SBREL32",       /* name */
192          FALSE,                 /* partial_inplace */
193          0xffffffff,            /* src_mask */
194          0xffffffff,            /* dst_mask */
195          FALSE),                /* pcrel_offset */
196
197   HOWTO (R_ARM_THM_PC22,        /* type */
198          1,                     /* rightshift */
199          2,                     /* size (0 = byte, 1 = short, 2 = long) */
200          23,                    /* bitsize */
201          TRUE,                  /* pc_relative */
202          0,                     /* bitpos */
203          complain_overflow_signed,/* complain_on_overflow */
204          bfd_elf_generic_reloc, /* special_function */
205          "R_ARM_THM_PC22",      /* name */
206          FALSE,                 /* partial_inplace */
207          0x07ff07ff,            /* src_mask */
208          0x07ff07ff,            /* dst_mask */
209          TRUE),                 /* pcrel_offset */
210
211   HOWTO (R_ARM_THM_PC8,         /* type */
212          1,                     /* rightshift */
213          1,                     /* size (0 = byte, 1 = short, 2 = long) */
214          8,                     /* bitsize */
215          TRUE,                  /* pc_relative */
216          0,                     /* bitpos */
217          complain_overflow_signed,/* complain_on_overflow */
218          bfd_elf_generic_reloc, /* special_function */
219          "R_ARM_THM_PC8",       /* name */
220          FALSE,                 /* partial_inplace */
221          0x000000ff,            /* src_mask */
222          0x000000ff,            /* dst_mask */
223          TRUE),                 /* pcrel_offset */
224
225   HOWTO (R_ARM_AMP_VCALL9,      /* type */
226          1,                     /* rightshift */
227          1,                     /* size (0 = byte, 1 = short, 2 = long) */
228          8,                     /* bitsize */
229          TRUE,                  /* pc_relative */
230          0,                     /* bitpos */
231          complain_overflow_signed,/* complain_on_overflow */
232          bfd_elf_generic_reloc, /* special_function */
233          "R_ARM_AMP_VCALL9",    /* name */
234          FALSE,                 /* partial_inplace */
235          0x000000ff,            /* src_mask */
236          0x000000ff,            /* dst_mask */
237          TRUE),                 /* pcrel_offset */
238
239   HOWTO (R_ARM_SWI24,           /* type */
240          0,                     /* rightshift */
241          0,                     /* size (0 = byte, 1 = short, 2 = long) */
242          0,                     /* bitsize */
243          FALSE,                 /* pc_relative */
244          0,                     /* bitpos */
245          complain_overflow_signed,/* complain_on_overflow */
246          bfd_elf_generic_reloc, /* special_function */
247          "R_ARM_SWI24",         /* name */
248          FALSE,                 /* partial_inplace */
249          0x00000000,            /* src_mask */
250          0x00000000,            /* dst_mask */
251          FALSE),                /* pcrel_offset */
252
253   HOWTO (R_ARM_THM_SWI8,        /* type */
254          0,                     /* rightshift */
255          0,                     /* size (0 = byte, 1 = short, 2 = long) */
256          0,                     /* bitsize */
257          FALSE,                 /* pc_relative */
258          0,                     /* bitpos */
259          complain_overflow_signed,/* complain_on_overflow */
260          bfd_elf_generic_reloc, /* special_function */
261          "R_ARM_SWI8",          /* name */
262          FALSE,                 /* partial_inplace */
263          0x00000000,            /* src_mask */
264          0x00000000,            /* dst_mask */
265          FALSE),                /* pcrel_offset */
266
267   /* BLX instruction for the ARM.  */
268   HOWTO (R_ARM_XPC25,           /* type */
269          2,                     /* rightshift */
270          2,                     /* size (0 = byte, 1 = short, 2 = long) */
271          25,                    /* bitsize */
272          TRUE,                  /* pc_relative */
273          0,                     /* bitpos */
274          complain_overflow_signed,/* complain_on_overflow */
275          bfd_elf_generic_reloc, /* special_function */
276          "R_ARM_XPC25",         /* name */
277          FALSE,                 /* partial_inplace */
278          0x00ffffff,            /* src_mask */
279          0x00ffffff,            /* dst_mask */
280          TRUE),                 /* pcrel_offset */
281
282   /* BLX instruction for the Thumb.  */
283   HOWTO (R_ARM_THM_XPC22,       /* type */
284          2,                     /* rightshift */
285          2,                     /* size (0 = byte, 1 = short, 2 = long) */
286          22,                    /* bitsize */
287          TRUE,                  /* pc_relative */
288          0,                     /* bitpos */
289          complain_overflow_signed,/* complain_on_overflow */
290          bfd_elf_generic_reloc, /* special_function */
291          "R_ARM_THM_XPC22",     /* name */
292          FALSE,                 /* partial_inplace */
293          0x07ff07ff,            /* src_mask */
294          0x07ff07ff,            /* dst_mask */
295          TRUE),                 /* pcrel_offset */
296
297   /* Dynamic TLS relocations.  */
298
299   HOWTO (R_ARM_TLS_DTPMOD32,    /* type */
300          0,                     /* rightshift */
301          2,                     /* size (0 = byte, 1 = short, 2 = long) */
302          32,                    /* bitsize */
303          FALSE,                 /* pc_relative */
304          0,                     /* bitpos */
305          complain_overflow_bitfield,/* complain_on_overflow */
306          bfd_elf_generic_reloc, /* special_function */
307          "R_ARM_TLS_DTPMOD32",  /* name */
308          TRUE,                  /* partial_inplace */
309          0xffffffff,            /* src_mask */
310          0xffffffff,            /* dst_mask */
311          FALSE),                /* pcrel_offset */
312
313   HOWTO (R_ARM_TLS_DTPOFF32,    /* type */
314          0,                     /* rightshift */
315          2,                     /* size (0 = byte, 1 = short, 2 = long) */
316          32,                    /* bitsize */
317          FALSE,                 /* pc_relative */
318          0,                     /* bitpos */
319          complain_overflow_bitfield,/* complain_on_overflow */
320          bfd_elf_generic_reloc, /* special_function */
321          "R_ARM_TLS_DTPOFF32",  /* name */
322          TRUE,                  /* partial_inplace */
323          0xffffffff,            /* src_mask */
324          0xffffffff,            /* dst_mask */
325          FALSE),                /* pcrel_offset */
326
327   HOWTO (R_ARM_TLS_TPOFF32,     /* type */
328          0,                     /* rightshift */
329          2,                     /* size (0 = byte, 1 = short, 2 = long) */
330          32,                    /* bitsize */
331          FALSE,                 /* pc_relative */
332          0,                     /* bitpos */
333          complain_overflow_bitfield,/* complain_on_overflow */
334          bfd_elf_generic_reloc, /* special_function */
335          "R_ARM_TLS_TPOFF32",   /* name */
336          TRUE,                  /* partial_inplace */
337          0xffffffff,            /* src_mask */
338          0xffffffff,            /* dst_mask */
339          FALSE),                /* pcrel_offset */
340
341   /* Relocs used in ARM Linux */
342
343   HOWTO (R_ARM_COPY,            /* type */
344          0,                     /* rightshift */
345          2,                     /* size (0 = byte, 1 = short, 2 = long) */
346          32,                    /* bitsize */
347          FALSE,                 /* pc_relative */
348          0,                     /* bitpos */
349          complain_overflow_bitfield,/* complain_on_overflow */
350          bfd_elf_generic_reloc, /* special_function */
351          "R_ARM_COPY",          /* name */
352          TRUE,                  /* partial_inplace */
353          0xffffffff,            /* src_mask */
354          0xffffffff,            /* dst_mask */
355          FALSE),                /* pcrel_offset */
356
357   HOWTO (R_ARM_GLOB_DAT,        /* type */
358          0,                     /* rightshift */
359          2,                     /* size (0 = byte, 1 = short, 2 = long) */
360          32,                    /* bitsize */
361          FALSE,                 /* pc_relative */
362          0,                     /* bitpos */
363          complain_overflow_bitfield,/* complain_on_overflow */
364          bfd_elf_generic_reloc, /* special_function */
365          "R_ARM_GLOB_DAT",      /* name */
366          TRUE,                  /* partial_inplace */
367          0xffffffff,            /* src_mask */
368          0xffffffff,            /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
371   HOWTO (R_ARM_JUMP_SLOT,       /* type */
372          0,                     /* rightshift */
373          2,                     /* size (0 = byte, 1 = short, 2 = long) */
374          32,                    /* bitsize */
375          FALSE,                 /* pc_relative */
376          0,                     /* bitpos */
377          complain_overflow_bitfield,/* complain_on_overflow */
378          bfd_elf_generic_reloc, /* special_function */
379          "R_ARM_JUMP_SLOT",     /* name */
380          TRUE,                  /* partial_inplace */
381          0xffffffff,            /* src_mask */
382          0xffffffff,            /* dst_mask */
383          FALSE),                /* pcrel_offset */
384
385   HOWTO (R_ARM_RELATIVE,        /* type */
386          0,                     /* rightshift */
387          2,                     /* size (0 = byte, 1 = short, 2 = long) */
388          32,                    /* bitsize */
389          FALSE,                 /* pc_relative */
390          0,                     /* bitpos */
391          complain_overflow_bitfield,/* complain_on_overflow */
392          bfd_elf_generic_reloc, /* special_function */
393          "R_ARM_RELATIVE",      /* name */
394          TRUE,                  /* partial_inplace */
395          0xffffffff,            /* src_mask */
396          0xffffffff,            /* dst_mask */
397          FALSE),                /* pcrel_offset */
398
399   HOWTO (R_ARM_GOTOFF,          /* type */
400          0,                     /* rightshift */
401          2,                     /* size (0 = byte, 1 = short, 2 = long) */
402          32,                    /* bitsize */
403          FALSE,                 /* pc_relative */
404          0,                     /* bitpos */
405          complain_overflow_bitfield,/* complain_on_overflow */
406          bfd_elf_generic_reloc, /* special_function */
407          "R_ARM_GOTOFF",        /* name */
408          TRUE,                  /* partial_inplace */
409          0xffffffff,            /* src_mask */
410          0xffffffff,            /* dst_mask */
411          FALSE),                /* pcrel_offset */
412
413   HOWTO (R_ARM_GOTPC,           /* type */
414          0,                     /* rightshift */
415          2,                     /* size (0 = byte, 1 = short, 2 = long) */
416          32,                    /* bitsize */
417          TRUE,                  /* pc_relative */
418          0,                     /* bitpos */
419          complain_overflow_bitfield,/* complain_on_overflow */
420          bfd_elf_generic_reloc, /* special_function */
421          "R_ARM_GOTPC",         /* name */
422          TRUE,                  /* partial_inplace */
423          0xffffffff,            /* src_mask */
424          0xffffffff,            /* dst_mask */
425          TRUE),                 /* pcrel_offset */
426
427   HOWTO (R_ARM_GOT32,           /* type */
428          0,                     /* rightshift */
429          2,                     /* size (0 = byte, 1 = short, 2 = long) */
430          32,                    /* bitsize */
431          FALSE,                 /* pc_relative */
432          0,                     /* bitpos */
433          complain_overflow_bitfield,/* complain_on_overflow */
434          bfd_elf_generic_reloc, /* special_function */
435          "R_ARM_GOT32",         /* name */
436          TRUE,                  /* partial_inplace */
437          0xffffffff,            /* src_mask */
438          0xffffffff,            /* dst_mask */
439          FALSE),                /* pcrel_offset */
440
441   HOWTO (R_ARM_PLT32,           /* type */
442          2,                     /* rightshift */
443          2,                     /* size (0 = byte, 1 = short, 2 = long) */
444          26,                    /* bitsize */
445          TRUE,                  /* pc_relative */
446          0,                     /* bitpos */
447          complain_overflow_bitfield,/* complain_on_overflow */
448          bfd_elf_generic_reloc, /* special_function */
449          "R_ARM_PLT32",         /* name */
450          TRUE,                  /* partial_inplace */
451          0x00ffffff,            /* src_mask */
452          0x00ffffff,            /* dst_mask */
453          TRUE),                 /* pcrel_offset */
454
455   HOWTO (R_ARM_CALL,            /* type */
456          2,                     /* rightshift */
457          2,                     /* size (0 = byte, 1 = short, 2 = long) */
458          24,                    /* bitsize */
459          TRUE,                  /* pc_relative */
460          0,                     /* bitpos */
461          complain_overflow_signed,/* complain_on_overflow */
462          bfd_elf_generic_reloc, /* special_function */
463          "R_ARM_CALL",          /* name */
464          FALSE,                 /* partial_inplace */
465          0x00ffffff,            /* src_mask */
466          0x00ffffff,            /* dst_mask */
467          TRUE),                 /* pcrel_offset */
468
469   HOWTO (R_ARM_JUMP24,          /* type */
470          2,                     /* rightshift */
471          2,                     /* size (0 = byte, 1 = short, 2 = long) */
472          24,                    /* bitsize */
473          TRUE,                  /* pc_relative */
474          0,                     /* bitpos */
475          complain_overflow_signed,/* complain_on_overflow */
476          bfd_elf_generic_reloc, /* special_function */
477          "R_ARM_JUMP24",        /* name */
478          FALSE,                 /* partial_inplace */
479          0x00ffffff,            /* src_mask */
480          0x00ffffff,            /* dst_mask */
481          TRUE),                 /* pcrel_offset */
482
483   HOWTO (R_ARM_NONE,            /* type */
484          0,                     /* rightshift */
485          0,                     /* size (0 = byte, 1 = short, 2 = long) */
486          0,                     /* bitsize */
487          FALSE,                 /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_dont,/* complain_on_overflow */
490          bfd_elf_generic_reloc, /* special_function */
491          "R_ARM_unknown_30",    /* name */
492          FALSE,                 /* partial_inplace */
493          0,                     /* src_mask */
494          0,                     /* dst_mask */
495          FALSE),                /* pcrel_offset */
496
497   HOWTO (R_ARM_NONE,            /* type */
498          0,                     /* rightshift */
499          0,                     /* size (0 = byte, 1 = short, 2 = long) */
500          0,                     /* bitsize */
501          FALSE,                 /* pc_relative */
502          0,                     /* bitpos */
503          complain_overflow_dont,/* complain_on_overflow */
504          bfd_elf_generic_reloc, /* special_function */
505          "R_ARM_unknown_31",    /* name */
506          FALSE,                 /* partial_inplace */
507          0,                     /* src_mask */
508          0,                     /* dst_mask */
509          FALSE),                /* pcrel_offset */
510
511   HOWTO (R_ARM_ALU_PCREL7_0,    /* type */
512          0,                     /* rightshift */
513          2,                     /* size (0 = byte, 1 = short, 2 = long) */
514          12,                    /* bitsize */
515          TRUE,                  /* pc_relative */
516          0,                     /* bitpos */
517          complain_overflow_dont,/* complain_on_overflow */
518          bfd_elf_generic_reloc, /* special_function */
519          "R_ARM_ALU_PCREL_7_0", /* name */
520          FALSE,                 /* partial_inplace */
521          0x00000fff,            /* src_mask */
522          0x00000fff,            /* dst_mask */
523          TRUE),                 /* pcrel_offset */
524
525   HOWTO (R_ARM_ALU_PCREL15_8,   /* type */
526          0,                     /* rightshift */
527          2,                     /* size (0 = byte, 1 = short, 2 = long) */
528          12,                    /* bitsize */
529          TRUE,                  /* pc_relative */
530          8,                     /* bitpos */
531          complain_overflow_dont,/* complain_on_overflow */
532          bfd_elf_generic_reloc, /* special_function */
533          "R_ARM_ALU_PCREL_15_8",/* name */
534          FALSE,                 /* partial_inplace */
535          0x00000fff,            /* src_mask */
536          0x00000fff,            /* dst_mask */
537          TRUE),                 /* pcrel_offset */
538
539   HOWTO (R_ARM_ALU_PCREL23_15,  /* type */
540          0,                     /* rightshift */
541          2,                     /* size (0 = byte, 1 = short, 2 = long) */
542          12,                    /* bitsize */
543          TRUE,                  /* pc_relative */
544          16,                    /* bitpos */
545          complain_overflow_dont,/* complain_on_overflow */
546          bfd_elf_generic_reloc, /* special_function */
547          "R_ARM_ALU_PCREL_23_15",/* name */
548          FALSE,                 /* partial_inplace */
549          0x00000fff,            /* src_mask */
550          0x00000fff,            /* dst_mask */
551          TRUE),                 /* pcrel_offset */
552
553   HOWTO (R_ARM_LDR_SBREL_11_0,  /* type */
554          0,                     /* rightshift */
555          2,                     /* size (0 = byte, 1 = short, 2 = long) */
556          12,                    /* bitsize */
557          FALSE,                 /* pc_relative */
558          0,                     /* bitpos */
559          complain_overflow_dont,/* complain_on_overflow */
560          bfd_elf_generic_reloc, /* special_function */
561          "R_ARM_LDR_SBREL_11_0",/* name */
562          FALSE,                 /* partial_inplace */
563          0x00000fff,            /* src_mask */
564          0x00000fff,            /* dst_mask */
565          FALSE),                /* pcrel_offset */
566
567   HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
568          0,                     /* rightshift */
569          2,                     /* size (0 = byte, 1 = short, 2 = long) */
570          8,                     /* bitsize */
571          FALSE,                 /* pc_relative */
572          12,                    /* bitpos */
573          complain_overflow_dont,/* complain_on_overflow */
574          bfd_elf_generic_reloc, /* special_function */
575          "R_ARM_ALU_SBREL_19_12",/* name */
576          FALSE,                 /* partial_inplace */
577          0x000ff000,            /* src_mask */
578          0x000ff000,            /* dst_mask */
579          FALSE),                /* pcrel_offset */
580
581   HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
582          0,                     /* rightshift */
583          2,                     /* size (0 = byte, 1 = short, 2 = long) */
584          8,                     /* bitsize */
585          FALSE,                 /* pc_relative */
586          20,                    /* bitpos */
587          complain_overflow_dont,/* complain_on_overflow */
588          bfd_elf_generic_reloc, /* special_function */
589          "R_ARM_ALU_SBREL_27_20",/* name */
590          FALSE,                 /* partial_inplace */
591          0x0ff00000,            /* src_mask */
592          0x0ff00000,            /* dst_mask */
593          FALSE),                /* pcrel_offset */
594
595   HOWTO (R_ARM_TARGET1,         /* type */
596          0,                     /* rightshift */
597          2,                     /* size (0 = byte, 1 = short, 2 = long) */
598          32,                    /* bitsize */
599          FALSE,                 /* pc_relative */
600          0,                     /* bitpos */
601          complain_overflow_dont,/* complain_on_overflow */
602          bfd_elf_generic_reloc, /* special_function */
603          "R_ARM_TARGET1",       /* name */
604          FALSE,                 /* partial_inplace */
605          0xffffffff,            /* src_mask */
606          0xffffffff,            /* dst_mask */
607          FALSE),                /* pcrel_offset */
608
609   HOWTO (R_ARM_ROSEGREL32,      /* type */
610          0,                     /* rightshift */
611          2,                     /* size (0 = byte, 1 = short, 2 = long) */
612          32,                    /* bitsize */
613          FALSE,                 /* pc_relative */
614          0,                     /* bitpos */
615          complain_overflow_dont,/* complain_on_overflow */
616          bfd_elf_generic_reloc, /* special_function */
617          "R_ARM_ROSEGREL32",    /* name */
618          FALSE,                 /* partial_inplace */
619          0xffffffff,            /* src_mask */
620          0xffffffff,            /* dst_mask */
621          FALSE),                /* pcrel_offset */
622
623   HOWTO (R_ARM_V4BX,            /* type */
624          0,                     /* rightshift */
625          2,                     /* size (0 = byte, 1 = short, 2 = long) */
626          32,                    /* bitsize */
627          FALSE,                 /* pc_relative */
628          0,                     /* bitpos */
629          complain_overflow_dont,/* complain_on_overflow */
630          bfd_elf_generic_reloc, /* special_function */
631          "R_ARM_V4BX",          /* name */
632          FALSE,                 /* partial_inplace */
633          0xffffffff,            /* src_mask */
634          0xffffffff,            /* dst_mask */
635          FALSE),                /* pcrel_offset */
636
637   HOWTO (R_ARM_TARGET2,         /* type */
638          0,                     /* rightshift */
639          2,                     /* size (0 = byte, 1 = short, 2 = long) */
640          32,                    /* bitsize */
641          FALSE,                 /* pc_relative */
642          0,                     /* bitpos */
643          complain_overflow_signed,/* complain_on_overflow */
644          bfd_elf_generic_reloc, /* special_function */
645          "R_ARM_TARGET2",       /* name */
646          FALSE,                 /* partial_inplace */
647          0xffffffff,            /* src_mask */
648          0xffffffff,            /* dst_mask */
649          TRUE),                 /* pcrel_offset */
650
651   HOWTO (R_ARM_PREL31,          /* type */
652          0,                     /* rightshift */
653          2,                     /* size (0 = byte, 1 = short, 2 = long) */
654          31,                    /* bitsize */
655          TRUE,                  /* pc_relative */
656          0,                     /* bitpos */
657          complain_overflow_signed,/* complain_on_overflow */
658          bfd_elf_generic_reloc, /* special_function */
659          "R_ARM_PREL31",        /* name */
660          FALSE,                 /* partial_inplace */
661          0x7fffffff,            /* src_mask */
662          0x7fffffff,            /* dst_mask */
663          TRUE),                 /* pcrel_offset */
664 };
665
666 static reloc_howto_type elf32_arm_tls_gd32_howto = 
667   HOWTO (R_ARM_TLS_GD32,        /* type */
668          0,                     /* rightshift */
669          2,                     /* size (0 = byte, 1 = short, 2 = long) */
670          32,                    /* bitsize */
671          FALSE,                 /* pc_relative */
672          0,                     /* bitpos */
673          complain_overflow_bitfield,/* complain_on_overflow */
674          NULL,                  /* special_function */
675          "R_ARM_TLS_GD32",      /* name */
676          TRUE,                  /* partial_inplace */
677          0xffffffff,            /* src_mask */
678          0xffffffff,            /* dst_mask */
679          FALSE);                /* pcrel_offset */
680
681 static reloc_howto_type elf32_arm_tls_ldo32_howto = 
682   HOWTO (R_ARM_TLS_LDO32,       /* type */
683          0,                     /* rightshift */
684          2,                     /* size (0 = byte, 1 = short, 2 = long) */
685          32,                    /* bitsize */
686          FALSE,                 /* pc_relative */
687          0,                     /* bitpos */
688          complain_overflow_bitfield,/* complain_on_overflow */
689          bfd_elf_generic_reloc, /* special_function */
690          "R_ARM_TLS_LDO32",     /* name */
691          TRUE,                  /* partial_inplace */
692          0xffffffff,            /* src_mask */
693          0xffffffff,            /* dst_mask */
694          FALSE);                /* pcrel_offset */
695
696 static reloc_howto_type elf32_arm_tls_ldm32_howto = 
697   HOWTO (R_ARM_TLS_LDM32,       /* type */
698          0,                     /* rightshift */
699          2,                     /* size (0 = byte, 1 = short, 2 = long) */
700          32,                    /* bitsize */
701          FALSE,                 /* pc_relative */
702          0,                     /* bitpos */
703          complain_overflow_bitfield,/* complain_on_overflow */
704          bfd_elf_generic_reloc, /* special_function */
705          "R_ARM_TLS_LDM32",     /* name */
706          TRUE,                  /* partial_inplace */
707          0xffffffff,            /* src_mask */
708          0xffffffff,            /* dst_mask */
709          FALSE);                /* pcrel_offset */
710
711 static reloc_howto_type elf32_arm_tls_le32_howto = 
712   HOWTO (R_ARM_TLS_LE32,        /* type */
713          0,                     /* rightshift */
714          2,                     /* size (0 = byte, 1 = short, 2 = long) */
715          32,                    /* bitsize */
716          FALSE,                 /* pc_relative */
717          0,                     /* bitpos */
718          complain_overflow_bitfield,/* complain_on_overflow */
719          bfd_elf_generic_reloc, /* special_function */
720          "R_ARM_TLS_LE32",      /* name */
721          TRUE,                  /* partial_inplace */
722          0xffffffff,            /* src_mask */
723          0xffffffff,            /* dst_mask */
724          FALSE);                /* pcrel_offset */
725
726 static reloc_howto_type elf32_arm_tls_ie32_howto = 
727   HOWTO (R_ARM_TLS_IE32,        /* type */
728          0,                     /* rightshift */
729          2,                     /* size (0 = byte, 1 = short, 2 = long) */
730          32,                    /* bitsize */
731          FALSE,                  /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_bitfield,/* complain_on_overflow */
734          NULL,                  /* special_function */
735          "R_ARM_TLS_IE32",      /* name */
736          TRUE,                  /* partial_inplace */
737          0xffffffff,            /* src_mask */
738          0xffffffff,            /* dst_mask */
739          FALSE);                /* pcrel_offset */
740
741   /* GNU extension to record C++ vtable hierarchy */
742 static reloc_howto_type elf32_arm_vtinherit_howto =
743   HOWTO (R_ARM_GNU_VTINHERIT, /* type */
744          0,                     /* rightshift */
745          2,                     /* size (0 = byte, 1 = short, 2 = long) */
746          0,                     /* bitsize */
747          FALSE,                 /* pc_relative */
748          0,                     /* bitpos */
749          complain_overflow_dont, /* complain_on_overflow */
750          NULL,                  /* special_function */
751          "R_ARM_GNU_VTINHERIT", /* name */
752          FALSE,                 /* partial_inplace */
753          0,                     /* src_mask */
754          0,                     /* dst_mask */
755          FALSE);                /* pcrel_offset */
756
757   /* GNU extension to record C++ vtable member usage */
758 static reloc_howto_type elf32_arm_vtentry_howto =
759   HOWTO (R_ARM_GNU_VTENTRY,     /* type */
760          0,                     /* rightshift */
761          2,                     /* size (0 = byte, 1 = short, 2 = long) */
762          0,                     /* bitsize */
763          FALSE,                 /* pc_relative */
764          0,                     /* bitpos */
765          complain_overflow_dont, /* complain_on_overflow */
766          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
767          "R_ARM_GNU_VTENTRY",   /* name */
768          FALSE,                 /* partial_inplace */
769          0,                     /* src_mask */
770          0,                     /* dst_mask */
771          FALSE);                /* pcrel_offset */
772
773   /* 12 bit pc relative */
774 static reloc_howto_type elf32_arm_thm_pc11_howto =
775   HOWTO (R_ARM_THM_PC11,        /* type */
776          1,                     /* rightshift */
777          1,                     /* size (0 = byte, 1 = short, 2 = long) */
778          11,                    /* bitsize */
779          TRUE,                  /* pc_relative */
780          0,                     /* bitpos */
781          complain_overflow_signed,      /* complain_on_overflow */
782          bfd_elf_generic_reloc, /* special_function */
783          "R_ARM_THM_PC11",      /* name */
784          FALSE,                 /* partial_inplace */
785          0x000007ff,            /* src_mask */
786          0x000007ff,            /* dst_mask */
787          TRUE);                 /* pcrel_offset */
788
789   /* 12 bit pc relative */
790 static reloc_howto_type elf32_arm_thm_pc9_howto =
791   HOWTO (R_ARM_THM_PC9,         /* type */
792          1,                     /* rightshift */
793          1,                     /* size (0 = byte, 1 = short, 2 = long) */
794          8,                     /* bitsize */
795          TRUE,                  /* pc_relative */
796          0,                     /* bitpos */
797          complain_overflow_signed,      /* complain_on_overflow */
798          bfd_elf_generic_reloc, /* special_function */
799          "R_ARM_THM_PC9",       /* name */
800          FALSE,                 /* partial_inplace */
801          0x000000ff,            /* src_mask */
802          0x000000ff,            /* dst_mask */
803          TRUE);                 /* pcrel_offset */
804
805 /* Place relative GOT-indirect.  */
806 static reloc_howto_type elf32_arm_got_prel =
807   HOWTO (R_ARM_GOT_PREL,        /* type */
808          0,                     /* rightshift */
809          2,                     /* size (0 = byte, 1 = short, 2 = long) */
810          32,                    /* bitsize */
811          TRUE,                  /* pc_relative */
812          0,                     /* bitpos */
813          complain_overflow_dont,        /* complain_on_overflow */
814          bfd_elf_generic_reloc, /* special_function */
815          "R_ARM_GOT_PREL",      /* name */
816          FALSE,                 /* partial_inplace */
817          0xffffffff,            /* src_mask */
818          0xffffffff,            /* dst_mask */
819          TRUE);                 /* pcrel_offset */
820
821 /* Currently unused relocations.  */
822 static reloc_howto_type elf32_arm_r_howto[4] =
823 {
824   HOWTO (R_ARM_RREL32,          /* type */
825          0,                     /* rightshift */
826          0,                     /* size (0 = byte, 1 = short, 2 = long) */
827          0,                     /* bitsize */
828          FALSE,                 /* pc_relative */
829          0,                     /* bitpos */
830          complain_overflow_dont,/* complain_on_overflow */
831          bfd_elf_generic_reloc, /* special_function */
832          "R_ARM_RREL32",        /* name */
833          FALSE,                 /* partial_inplace */
834          0,                     /* src_mask */
835          0,                     /* dst_mask */
836          FALSE),                /* pcrel_offset */
837
838   HOWTO (R_ARM_RABS32,          /* type */
839          0,                     /* rightshift */
840          0,                     /* size (0 = byte, 1 = short, 2 = long) */
841          0,                     /* bitsize */
842          FALSE,                 /* pc_relative */
843          0,                     /* bitpos */
844          complain_overflow_dont,/* complain_on_overflow */
845          bfd_elf_generic_reloc, /* special_function */
846          "R_ARM_RABS32",        /* name */
847          FALSE,                 /* partial_inplace */
848          0,                     /* src_mask */
849          0,                     /* dst_mask */
850          FALSE),                /* pcrel_offset */
851
852   HOWTO (R_ARM_RPC24,           /* type */
853          0,                     /* rightshift */
854          0,                     /* size (0 = byte, 1 = short, 2 = long) */
855          0,                     /* bitsize */
856          FALSE,                 /* pc_relative */
857          0,                     /* bitpos */
858          complain_overflow_dont,/* complain_on_overflow */
859          bfd_elf_generic_reloc, /* special_function */
860          "R_ARM_RPC24",         /* name */
861          FALSE,                 /* partial_inplace */
862          0,                     /* src_mask */
863          0,                     /* dst_mask */
864          FALSE),                /* pcrel_offset */
865
866   HOWTO (R_ARM_RBASE,           /* type */
867          0,                     /* rightshift */
868          0,                     /* size (0 = byte, 1 = short, 2 = long) */
869          0,                     /* bitsize */
870          FALSE,                 /* pc_relative */
871          0,                     /* bitpos */
872          complain_overflow_dont,/* complain_on_overflow */
873          bfd_elf_generic_reloc, /* special_function */
874          "R_ARM_RBASE",         /* name */
875          FALSE,                 /* partial_inplace */
876          0,                     /* src_mask */
877          0,                     /* dst_mask */
878          FALSE)                 /* pcrel_offset */
879 };
880
881 static reloc_howto_type *
882 elf32_arm_howto_from_type (unsigned int r_type)
883 {
884   if (r_type < NUM_ELEM (elf32_arm_howto_table))
885     return &elf32_arm_howto_table[r_type];
886     
887   switch (r_type)
888     {
889     case R_ARM_GOT_PREL:
890       return &elf32_arm_got_prel;
891
892     case R_ARM_GNU_VTINHERIT:
893       return &elf32_arm_vtinherit_howto;
894
895     case R_ARM_GNU_VTENTRY:
896       return &elf32_arm_vtentry_howto;
897
898     case R_ARM_THM_PC11:
899       return &elf32_arm_thm_pc11_howto;
900
901     case R_ARM_THM_PC9:
902       return &elf32_arm_thm_pc9_howto;
903       
904     case R_ARM_TLS_GD32:
905       return &elf32_arm_tls_gd32_howto;
906       break;
907
908     case R_ARM_TLS_LDO32:
909       return &elf32_arm_tls_ldo32_howto;
910       break;
911
912     case R_ARM_TLS_LDM32:
913       return &elf32_arm_tls_ldm32_howto;
914       break;
915
916     case R_ARM_TLS_IE32:
917       return &elf32_arm_tls_ie32_howto;
918       break;
919
920     case R_ARM_TLS_LE32:
921       return &elf32_arm_tls_le32_howto;
922       break;
923
924     case R_ARM_RREL32:
925     case R_ARM_RABS32:
926     case R_ARM_RPC24:
927     case R_ARM_RBASE:
928       return &elf32_arm_r_howto[r_type - R_ARM_RREL32];
929
930     default:
931       return NULL;
932     }
933 }
934
935 static void
936 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
937                          Elf_Internal_Rela * elf_reloc)
938 {
939   unsigned int r_type;
940
941   r_type = ELF32_R_TYPE (elf_reloc->r_info);
942   bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
943 }
944
945 struct elf32_arm_reloc_map
946   {
947     bfd_reloc_code_real_type  bfd_reloc_val;
948     unsigned char             elf_reloc_val;
949   };
950
951 /* All entries in this list must also be present in elf32_arm_howto_table.  */
952 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
953   {
954     {BFD_RELOC_NONE,                 R_ARM_NONE},
955     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
956     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
957     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
958     {BFD_RELOC_32,                   R_ARM_ABS32},
959     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
960     {BFD_RELOC_8,                    R_ARM_ABS8},
961     {BFD_RELOC_16,                   R_ARM_ABS16},
962     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
963     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
964     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_PC22},
965     {BFD_RELOC_ARM_COPY,             R_ARM_COPY},
966     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
967     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
968     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
969     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF},
970     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
971     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
972     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
973     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
974     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
975     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
976     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
977     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
978     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
979     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
980     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
981     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
982     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
983     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
984     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
985     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
986     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
987   };
988
989 static reloc_howto_type *
990 elf32_arm_reloc_type_lookup (abfd, code)
991      bfd *abfd ATTRIBUTE_UNUSED;
992      bfd_reloc_code_real_type code;
993 {
994   unsigned int i;
995
996   switch (code)
997     {
998     case BFD_RELOC_VTABLE_INHERIT:
999       return & elf32_arm_vtinherit_howto;
1000
1001     case BFD_RELOC_VTABLE_ENTRY:
1002       return & elf32_arm_vtentry_howto;
1003
1004     case BFD_RELOC_THUMB_PCREL_BRANCH12:
1005       return & elf32_arm_thm_pc11_howto;
1006
1007     case BFD_RELOC_THUMB_PCREL_BRANCH9:
1008       return & elf32_arm_thm_pc9_howto;
1009
1010     case BFD_RELOC_ARM_TLS_GD32:
1011       return & elf32_arm_tls_gd32_howto;
1012
1013     case BFD_RELOC_ARM_TLS_LDO32:
1014       return & elf32_arm_tls_ldo32_howto;
1015
1016     case BFD_RELOC_ARM_TLS_LDM32:
1017       return & elf32_arm_tls_ldm32_howto;
1018
1019     case BFD_RELOC_ARM_TLS_IE32:
1020       return & elf32_arm_tls_ie32_howto;
1021
1022     case BFD_RELOC_ARM_TLS_LE32:
1023       return & elf32_arm_tls_le32_howto;
1024
1025     default:
1026       for (i = 0; i < NUM_ELEM (elf32_arm_reloc_map); i ++)
1027         if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1028           return & elf32_arm_howto_table[elf32_arm_reloc_map[i].elf_reloc_val];
1029
1030       return NULL;
1031    }
1032 }
1033
1034 /* Support for core dump NOTE sections */
1035 static bfd_boolean
1036 elf32_arm_nabi_grok_prstatus (abfd, note)
1037      bfd *abfd;
1038      Elf_Internal_Note *note;
1039 {
1040   int offset;
1041   size_t size;
1042
1043   switch (note->descsz)
1044     {
1045       default:
1046         return FALSE;
1047
1048       case 148:         /* Linux/ARM 32-bit*/
1049         /* pr_cursig */
1050         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1051
1052         /* pr_pid */
1053         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1054
1055         /* pr_reg */
1056         offset = 72;
1057         size = 72;
1058
1059         break;
1060     }
1061
1062   /* Make a ".reg/999" section.  */
1063   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1064                                           size, note->descpos + offset);
1065 }
1066
1067 static bfd_boolean
1068 elf32_arm_nabi_grok_psinfo (abfd, note)
1069      bfd *abfd;
1070      Elf_Internal_Note *note;
1071 {
1072   switch (note->descsz)
1073     {
1074       default:
1075         return FALSE;
1076
1077       case 124:         /* Linux/ARM elf_prpsinfo */
1078         elf_tdata (abfd)->core_program
1079          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1080         elf_tdata (abfd)->core_command
1081          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1082     }
1083
1084   /* Note that for some reason, a spurious space is tacked
1085      onto the end of the args in some (at least one anyway)
1086      implementations, so strip it off if it exists.  */
1087
1088   {
1089     char *command = elf_tdata (abfd)->core_command;
1090     int n = strlen (command);
1091
1092     if (0 < n && command[n - 1] == ' ')
1093       command[n - 1] = '\0';
1094   }
1095
1096   return TRUE;
1097 }
1098
1099 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
1100 #define TARGET_LITTLE_NAME              "elf32-littlearm"
1101 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
1102 #define TARGET_BIG_NAME                 "elf32-bigarm"
1103
1104 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
1105 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
1106
1107 typedef unsigned long int insn32;
1108 typedef unsigned short int insn16;
1109
1110 /* In lieu of proper flags, assume all EABIv4 objects are interworkable.  */
1111 #define INTERWORK_FLAG(abfd)  \
1112   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) == EF_ARM_EABI_VER4 \
1113   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
1114
1115 /* The linker script knows the section names for placement.
1116    The entry_names are used to do simple name mangling on the stubs.
1117    Given a function name, and its type, the stub can be found. The
1118    name can be changed. The only requirement is the %s be present.  */
1119 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1120 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
1121
1122 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1123 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
1124
1125 /* The name of the dynamic interpreter.  This is put in the .interp
1126    section.  */
1127 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1128
1129 #ifdef FOUR_WORD_PLT
1130
1131 /* The first entry in a procedure linkage table looks like
1132    this.  It is set up so that any shared library function that is
1133    called before the relocation has been set up calls the dynamic
1134    linker first.  */
1135 static const bfd_vma elf32_arm_plt0_entry [] =
1136   {
1137     0xe52de004,         /* str   lr, [sp, #-4]! */
1138     0xe59fe010,         /* ldr   lr, [pc, #16]  */
1139     0xe08fe00e,         /* add   lr, pc, lr     */
1140     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1141   };
1142
1143 /* Subsequent entries in a procedure linkage table look like
1144    this.  */
1145 static const bfd_vma elf32_arm_plt_entry [] =
1146   {
1147     0xe28fc600,         /* add   ip, pc, #NN    */
1148     0xe28cca00,         /* add   ip, ip, #NN    */
1149     0xe5bcf000,         /* ldr   pc, [ip, #NN]! */
1150     0x00000000,         /* unused               */
1151   };
1152
1153 #else
1154
1155 /* The first entry in a procedure linkage table looks like
1156    this.  It is set up so that any shared library function that is
1157    called before the relocation has been set up calls the dynamic
1158    linker first.  */
1159 static const bfd_vma elf32_arm_plt0_entry [] =
1160   {
1161     0xe52de004,         /* str   lr, [sp, #-4]! */
1162     0xe59fe004,         /* ldr   lr, [pc, #4]   */
1163     0xe08fe00e,         /* add   lr, pc, lr     */
1164     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
1165     0x00000000,         /* &GOT[0] - .          */
1166   };
1167
1168 /* Subsequent entries in a procedure linkage table look like
1169    this.  */
1170 static const bfd_vma elf32_arm_plt_entry [] =
1171   {
1172     0xe28fc600,         /* add   ip, pc, #0xNN00000 */
1173     0xe28cca00,         /* add   ip, ip, #0xNN000   */
1174     0xe5bcf000,         /* ldr   pc, [ip, #0xNNN]!  */
1175   };
1176
1177 #endif
1178
1179 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
1180 #define PLT_THUMB_STUB_SIZE 4
1181 static const bfd_vma elf32_arm_plt_thumb_stub [] =
1182   {
1183     0x4778,             /* bx pc */
1184     0x46c0              /* nop   */
1185   };
1186
1187 /* The entries in a PLT when using a DLL-based target with multiple
1188    address spaces.  */
1189 static const bfd_vma elf32_arm_symbian_plt_entry [] = 
1190   {
1191     0xe51ff004,         /* ldr   pc, [pc, #-4] */
1192     0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
1193   };
1194
1195 /* Used to build a map of a section.  This is required for mixed-endian
1196    code/data.  */
1197
1198 typedef struct elf32_elf_section_map
1199 {
1200   bfd_vma vma;
1201   char type;
1202 }
1203 elf32_arm_section_map;
1204
1205 struct _arm_elf_section_data
1206 {
1207   struct bfd_elf_section_data elf;
1208   int mapcount;
1209   elf32_arm_section_map *map;
1210 };
1211
1212 #define elf32_arm_section_data(sec) \
1213   ((struct _arm_elf_section_data *) elf_section_data (sec))
1214
1215 /* The size of the thread control block.  */
1216 #define TCB_SIZE        8
1217
1218 struct elf32_arm_obj_tdata
1219 {
1220   struct elf_obj_tdata root;
1221
1222   /* tls_type for each local got entry.  */
1223   char *local_got_tls_type;
1224 };
1225
1226 #define elf32_arm_tdata(abfd) \
1227   ((struct elf32_arm_obj_tdata *) (abfd)->tdata.any)
1228
1229 #define elf32_arm_local_got_tls_type(abfd) \
1230   (elf32_arm_tdata (abfd)->local_got_tls_type)
1231
1232 static bfd_boolean
1233 elf32_arm_mkobject (bfd *abfd)
1234 {
1235   bfd_size_type amt = sizeof (struct elf32_arm_obj_tdata);
1236   abfd->tdata.any = bfd_zalloc (abfd, amt);
1237   if (abfd->tdata.any == NULL)
1238     return FALSE;
1239   return TRUE;
1240 }
1241
1242 /* The ARM linker needs to keep track of the number of relocs that it
1243    decides to copy in check_relocs for each symbol.  This is so that
1244    it can discard PC relative relocs if it doesn't need them when
1245    linking with -Bsymbolic.  We store the information in a field
1246    extending the regular ELF linker hash table.  */
1247
1248 /* This structure keeps track of the number of relocs we have copied
1249    for a given symbol.  */
1250 struct elf32_arm_relocs_copied
1251   {
1252     /* Next section.  */
1253     struct elf32_arm_relocs_copied * next;
1254     /* A section in dynobj.  */
1255     asection * section;
1256     /* Number of relocs copied in this section.  */
1257     bfd_size_type count;
1258     /* Number of PC-relative relocs copied in this section.  */
1259     bfd_size_type pc_count;
1260   };
1261
1262 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
1263
1264 /* Arm ELF linker hash entry.  */
1265 struct elf32_arm_link_hash_entry
1266   {
1267     struct elf_link_hash_entry root;
1268
1269     /* Number of PC relative relocs copied for this symbol.  */
1270     struct elf32_arm_relocs_copied * relocs_copied;
1271
1272     /* We reference count Thumb references to a PLT entry separately,
1273        so that we can emit the Thumb trampoline only if needed.  */
1274     bfd_signed_vma plt_thumb_refcount;
1275
1276     /* Since PLT entries have variable size if the Thumb prologue is
1277        used, we need to record the index into .got.plt instead of
1278        recomputing it from the PLT offset.  */
1279     bfd_signed_vma plt_got_offset;
1280
1281 #define GOT_UNKNOWN     0
1282 #define GOT_NORMAL      1
1283 #define GOT_TLS_GD      2
1284 #define GOT_TLS_IE      4
1285     unsigned char tls_type;
1286   };
1287
1288 /* Traverse an arm ELF linker hash table.  */
1289 #define elf32_arm_link_hash_traverse(table, func, info)                 \
1290   (elf_link_hash_traverse                                               \
1291    (&(table)->root,                                                     \
1292     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
1293     (info)))
1294
1295 /* Get the ARM elf linker hash table from a link_info structure.  */
1296 #define elf32_arm_hash_table(info) \
1297   ((struct elf32_arm_link_hash_table *) ((info)->hash))
1298
1299 /* ARM ELF linker hash table.  */
1300 struct elf32_arm_link_hash_table
1301   {
1302     /* The main hash table.  */
1303     struct elf_link_hash_table root;
1304
1305     /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
1306     bfd_size_type thumb_glue_size;
1307
1308     /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
1309     bfd_size_type arm_glue_size;
1310
1311     /* An arbitrary input BFD chosen to hold the glue sections.  */
1312     bfd * bfd_of_glue_owner;
1313
1314     /* Nonzero to output a BE8 image.  */
1315     int byteswap_code;
1316
1317     /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
1318        Nonzero if R_ARM_TARGET1 means R_ARM_ABS32.  */
1319     int target1_is_rel;
1320
1321     /* The relocation to use for R_ARM_TARGET2 relocations.  */
1322     int target2_reloc;
1323
1324     /* Nonzero to fix BX instructions for ARMv4 targets.  */
1325     int fix_v4bx;
1326
1327     /* The number of bytes in the initial entry in the PLT.  */
1328     bfd_size_type plt_header_size;
1329
1330     /* The number of bytes in the subsequent PLT etries.  */
1331     bfd_size_type plt_entry_size;
1332
1333     /* True if the target system is Symbian OS.  */
1334     int symbian_p;
1335
1336     /* True if the target uses REL relocations.  */
1337     int use_rel;
1338
1339     /* Short-cuts to get to dynamic linker sections.  */
1340     asection *sgot;
1341     asection *sgotplt;
1342     asection *srelgot;
1343     asection *splt;
1344     asection *srelplt;
1345     asection *sdynbss;
1346     asection *srelbss;
1347
1348     /* Data for R_ARM_TLS_LDM32 relocations.  */
1349     union {
1350       bfd_signed_vma refcount;
1351       bfd_vma offset;
1352     } tls_ldm_got;
1353     
1354     /* Small local sym to section mapping cache.  */
1355     struct sym_sec_cache sym_sec;
1356
1357     /* For convenience in allocate_dynrelocs.  */
1358     bfd * obfd;
1359   };
1360
1361 /* Create an entry in an ARM ELF linker hash table.  */
1362
1363 static struct bfd_hash_entry *
1364 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
1365                              struct bfd_hash_table * table,
1366                              const char * string)
1367 {
1368   struct elf32_arm_link_hash_entry * ret =
1369     (struct elf32_arm_link_hash_entry *) entry;
1370
1371   /* Allocate the structure if it has not already been allocated by a
1372      subclass.  */
1373   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
1374     ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
1375   if (ret == NULL)
1376     return (struct bfd_hash_entry *) ret;
1377
1378   /* Call the allocation method of the superclass.  */
1379   ret = ((struct elf32_arm_link_hash_entry *)
1380          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1381                                      table, string));
1382   if (ret != NULL)
1383     {
1384       ret->relocs_copied = NULL;
1385       ret->tls_type = GOT_UNKNOWN;
1386       ret->plt_thumb_refcount = 0;
1387       ret->plt_got_offset = -1;
1388     }
1389
1390   return (struct bfd_hash_entry *) ret;
1391 }
1392
1393 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
1394    shortcuts to them in our hash table.  */
1395
1396 static bfd_boolean
1397 create_got_section (bfd *dynobj, struct bfd_link_info *info)
1398 {
1399   struct elf32_arm_link_hash_table *htab;
1400
1401   htab = elf32_arm_hash_table (info);
1402   /* BPABI objects never have a GOT, or associated sections.  */
1403   if (htab->symbian_p)
1404     return TRUE;
1405
1406   if (! _bfd_elf_create_got_section (dynobj, info))
1407     return FALSE;
1408
1409   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1410   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1411   if (!htab->sgot || !htab->sgotplt)
1412     abort ();
1413
1414   htab->srelgot = bfd_make_section (dynobj, ".rel.got");
1415   if (htab->srelgot == NULL
1416       || ! bfd_set_section_flags (dynobj, htab->srelgot,
1417                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1418                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
1419                                    | SEC_READONLY))
1420       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1421     return FALSE;
1422   return TRUE;
1423 }
1424
1425 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1426    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1427    hash table.  */
1428
1429 static bfd_boolean
1430 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1431 {
1432   struct elf32_arm_link_hash_table *htab;
1433
1434   htab = elf32_arm_hash_table (info);
1435   if (!htab->sgot && !create_got_section (dynobj, info))
1436     return FALSE;
1437
1438   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1439     return FALSE;
1440
1441   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1442   htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
1443   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1444   if (!info->shared)
1445     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
1446
1447   if (!htab->splt 
1448       || !htab->srelplt
1449       || !htab->sdynbss
1450       || (!info->shared && !htab->srelbss))
1451     abort ();
1452
1453   return TRUE;
1454 }
1455
1456 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1457
1458 static void
1459 elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
1460                                 struct elf_link_hash_entry *dir,
1461                                 struct elf_link_hash_entry *ind)
1462 {
1463   struct elf32_arm_link_hash_entry *edir, *eind;
1464
1465   edir = (struct elf32_arm_link_hash_entry *) dir;
1466   eind = (struct elf32_arm_link_hash_entry *) ind;
1467
1468   if (eind->relocs_copied != NULL)
1469     {
1470       if (edir->relocs_copied != NULL)
1471         {
1472           struct elf32_arm_relocs_copied **pp;
1473           struct elf32_arm_relocs_copied *p;
1474
1475           if (ind->root.type == bfd_link_hash_indirect)
1476             abort ();
1477
1478           /* Add reloc counts against the weak sym to the strong sym
1479              list.  Merge any entries against the same section.  */
1480           for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
1481             {
1482               struct elf32_arm_relocs_copied *q;
1483
1484               for (q = edir->relocs_copied; q != NULL; q = q->next)
1485                 if (q->section == p->section)
1486                   {
1487                     q->pc_count += p->pc_count;
1488                     q->count += p->count;
1489                     *pp = p->next;
1490                     break;
1491                   }
1492               if (q == NULL)
1493                 pp = &p->next;
1494             }
1495           *pp = edir->relocs_copied;
1496         }
1497
1498       edir->relocs_copied = eind->relocs_copied;
1499       eind->relocs_copied = NULL;
1500     }
1501
1502   /* If the direct symbol already has an associated PLT entry, the
1503      indirect symbol should not.  If it doesn't, swap refcount information
1504      from the indirect symbol.  */
1505   if (edir->plt_thumb_refcount == 0)
1506     {
1507       edir->plt_thumb_refcount = eind->plt_thumb_refcount;
1508       eind->plt_thumb_refcount = 0;
1509     }
1510   else
1511     BFD_ASSERT (eind->plt_thumb_refcount == 0);
1512
1513   if (ind->root.type == bfd_link_hash_indirect
1514       && dir->got.refcount <= 0)
1515     {
1516       edir->tls_type = eind->tls_type;
1517       eind->tls_type = GOT_UNKNOWN;
1518     }
1519
1520   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1521 }
1522
1523 /* Create an ARM elf linker hash table.  */
1524
1525 static struct bfd_link_hash_table *
1526 elf32_arm_link_hash_table_create (bfd *abfd)
1527 {
1528   struct elf32_arm_link_hash_table *ret;
1529   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
1530
1531   ret = bfd_malloc (amt);
1532   if (ret == NULL)
1533     return NULL;
1534
1535   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
1536                                       elf32_arm_link_hash_newfunc))
1537     {
1538       free (ret);
1539       return NULL;
1540     }
1541
1542   ret->sgot = NULL;
1543   ret->sgotplt = NULL;
1544   ret->srelgot = NULL;
1545   ret->splt = NULL;
1546   ret->srelplt = NULL;
1547   ret->sdynbss = NULL;
1548   ret->srelbss = NULL;
1549   ret->thumb_glue_size = 0;
1550   ret->arm_glue_size = 0;
1551   ret->bfd_of_glue_owner = NULL;
1552   ret->byteswap_code = 0;
1553   ret->target1_is_rel = 0;
1554   ret->target2_reloc = R_ARM_NONE;
1555 #ifdef FOUR_WORD_PLT
1556   ret->plt_header_size = 16;
1557   ret->plt_entry_size = 16;
1558 #else
1559   ret->plt_header_size = 20;
1560   ret->plt_entry_size = 12;
1561 #endif
1562   ret->symbian_p = 0;
1563   ret->use_rel = 1;
1564   ret->sym_sec.abfd = NULL;
1565   ret->obfd = abfd;
1566   ret->tls_ldm_got.refcount = 0;
1567
1568   return &ret->root.root;
1569 }
1570
1571 /* Locate the Thumb encoded calling stub for NAME.  */
1572
1573 static struct elf_link_hash_entry *
1574 find_thumb_glue (struct bfd_link_info *link_info,
1575                  const char *name,
1576                  bfd *input_bfd)
1577 {
1578   char *tmp_name;
1579   struct elf_link_hash_entry *hash;
1580   struct elf32_arm_link_hash_table *hash_table;
1581
1582   /* We need a pointer to the armelf specific hash table.  */
1583   hash_table = elf32_arm_hash_table (link_info);
1584
1585   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1586                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
1587
1588   BFD_ASSERT (tmp_name);
1589
1590   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1591
1592   hash = elf_link_hash_lookup
1593     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1594
1595   if (hash == NULL)
1596     /* xgettext:c-format */
1597     (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
1598                            input_bfd, tmp_name, name);
1599
1600   free (tmp_name);
1601
1602   return hash;
1603 }
1604
1605 /* Locate the ARM encoded calling stub for NAME.  */
1606
1607 static struct elf_link_hash_entry *
1608 find_arm_glue (struct bfd_link_info *link_info,
1609                const char *name,
1610                bfd *input_bfd)
1611 {
1612   char *tmp_name;
1613   struct elf_link_hash_entry *myh;
1614   struct elf32_arm_link_hash_table *hash_table;
1615
1616   /* We need a pointer to the elfarm specific hash table.  */
1617   hash_table = elf32_arm_hash_table (link_info);
1618
1619   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1620                          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
1621
1622   BFD_ASSERT (tmp_name);
1623
1624   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1625
1626   myh = elf_link_hash_lookup
1627     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1628
1629   if (myh == NULL)
1630     /* xgettext:c-format */
1631     (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
1632                            input_bfd, tmp_name, name);
1633
1634   free (tmp_name);
1635
1636   return myh;
1637 }
1638
1639 /* ARM->Thumb glue (static images):
1640
1641    .arm
1642    __func_from_arm:
1643    ldr r12, __func_addr
1644    bx  r12
1645    __func_addr:
1646    .word func    @ behave as if you saw a ARM_32 reloc.  
1647
1648    (relocatable images)
1649    .arm
1650    __func_from_arm:
1651    ldr r12, __func_offset
1652    add r12, r12, pc
1653    bx  r12
1654    __func_offset:
1655    .word func - .
1656    */
1657
1658 #define ARM2THUMB_STATIC_GLUE_SIZE 12
1659 static const insn32 a2t1_ldr_insn = 0xe59fc000;
1660 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
1661 static const insn32 a2t3_func_addr_insn = 0x00000001;
1662
1663 #define ARM2THUMB_PIC_GLUE_SIZE 16
1664 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
1665 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
1666 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
1667
1668 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
1669
1670    .thumb                               .thumb
1671    .align 2                             .align 2
1672    __func_from_thumb:              __func_from_thumb:
1673    bx pc                                push {r6, lr}
1674    nop                                  ldr  r6, __func_addr
1675    .arm                                         mov  lr, pc
1676    __func_change_to_arm:                        bx   r6
1677    b func                       .arm
1678    __func_back_to_thumb:
1679    ldmia r13! {r6, lr}
1680    bx    lr
1681    __func_addr:
1682    .word        func  */
1683
1684 #define THUMB2ARM_GLUE_SIZE 8
1685 static const insn16 t2a1_bx_pc_insn = 0x4778;
1686 static const insn16 t2a2_noop_insn = 0x46c0;
1687 static const insn32 t2a3_b_insn = 0xea000000;
1688
1689 #ifndef ELFARM_NABI_C_INCLUDED
1690 bfd_boolean
1691 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
1692 {
1693   asection * s;
1694   bfd_byte * foo;
1695   struct elf32_arm_link_hash_table * globals;
1696
1697   globals = elf32_arm_hash_table (info);
1698
1699   BFD_ASSERT (globals != NULL);
1700
1701   if (globals->arm_glue_size != 0)
1702     {
1703       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1704
1705       s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1706                                    ARM2THUMB_GLUE_SECTION_NAME);
1707
1708       BFD_ASSERT (s != NULL);
1709
1710       foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
1711
1712       s->size = globals->arm_glue_size;
1713       s->contents = foo;
1714     }
1715
1716   if (globals->thumb_glue_size != 0)
1717     {
1718       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1719
1720       s = bfd_get_section_by_name
1721         (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1722
1723       BFD_ASSERT (s != NULL);
1724
1725       foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
1726
1727       s->size = globals->thumb_glue_size;
1728       s->contents = foo;
1729     }
1730
1731   return TRUE;
1732 }
1733
1734 static void
1735 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
1736                           struct elf_link_hash_entry * h)
1737 {
1738   const char * name = h->root.root.string;
1739   asection * s;
1740   char * tmp_name;
1741   struct elf_link_hash_entry * myh;
1742   struct bfd_link_hash_entry * bh;
1743   struct elf32_arm_link_hash_table * globals;
1744   bfd_vma val;
1745
1746   globals = elf32_arm_hash_table (link_info);
1747
1748   BFD_ASSERT (globals != NULL);
1749   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1750
1751   s = bfd_get_section_by_name
1752     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
1753
1754   BFD_ASSERT (s != NULL);
1755
1756   tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
1757
1758   BFD_ASSERT (tmp_name);
1759
1760   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1761
1762   myh = elf_link_hash_lookup
1763     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
1764
1765   if (myh != NULL)
1766     {
1767       /* We've already seen this guy.  */
1768       free (tmp_name);
1769       return;
1770     }
1771
1772   /* The only trick here is using hash_table->arm_glue_size as the value.
1773      Even though the section isn't allocated yet, this is where we will be
1774      putting it.  */
1775   bh = NULL;
1776   val = globals->arm_glue_size + 1;
1777   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
1778                                     tmp_name, BSF_GLOBAL, s, val,
1779                                     NULL, TRUE, FALSE, &bh);
1780
1781   myh = (struct elf_link_hash_entry *) bh;
1782   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1783   myh->forced_local = 1;
1784
1785   free (tmp_name);
1786
1787   if ((link_info->shared || globals->root.is_relocatable_executable))
1788     globals->arm_glue_size += ARM2THUMB_PIC_GLUE_SIZE;
1789   else
1790     globals->arm_glue_size += ARM2THUMB_STATIC_GLUE_SIZE;
1791
1792   return;
1793 }
1794
1795 static void
1796 record_thumb_to_arm_glue (struct bfd_link_info *link_info,
1797                           struct elf_link_hash_entry *h)
1798 {
1799   const char *name = h->root.root.string;
1800   asection *s;
1801   char *tmp_name;
1802   struct elf_link_hash_entry *myh;
1803   struct bfd_link_hash_entry *bh;
1804   struct elf32_arm_link_hash_table *hash_table;
1805   bfd_vma val;
1806
1807   hash_table = elf32_arm_hash_table (link_info);
1808
1809   BFD_ASSERT (hash_table != NULL);
1810   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
1811
1812   s = bfd_get_section_by_name
1813     (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1814
1815   BFD_ASSERT (s != NULL);
1816
1817   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1818                          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
1819
1820   BFD_ASSERT (tmp_name);
1821
1822   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1823
1824   myh = elf_link_hash_lookup
1825     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1826
1827   if (myh != NULL)
1828     {
1829       /* We've already seen this guy.  */
1830       free (tmp_name);
1831       return;
1832     }
1833
1834   bh = NULL;
1835   val = hash_table->thumb_glue_size + 1;
1836   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1837                                     tmp_name, BSF_GLOBAL, s, val,
1838                                     NULL, TRUE, FALSE, &bh);
1839
1840   /* If we mark it 'Thumb', the disassembler will do a better job.  */
1841   myh = (struct elf_link_hash_entry *) bh;
1842   myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
1843   myh->forced_local = 1;
1844
1845   free (tmp_name);
1846
1847 #define CHANGE_TO_ARM "__%s_change_to_arm"
1848 #define BACK_FROM_ARM "__%s_back_from_arm"
1849
1850   /* Allocate another symbol to mark where we switch to Arm mode.  */
1851   tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1852                          + strlen (CHANGE_TO_ARM) + 1);
1853
1854   BFD_ASSERT (tmp_name);
1855
1856   sprintf (tmp_name, CHANGE_TO_ARM, name);
1857
1858   bh = NULL;
1859   val = hash_table->thumb_glue_size + 4,
1860   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1861                                     tmp_name, BSF_LOCAL, s, val,
1862                                     NULL, TRUE, FALSE, &bh);
1863
1864   free (tmp_name);
1865
1866   hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
1867
1868   return;
1869 }
1870
1871 /* Add the glue sections to ABFD.  This function is called from the
1872    linker scripts in ld/emultempl/{armelf}.em.  */
1873
1874 bfd_boolean
1875 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
1876                                         struct bfd_link_info *info)
1877 {
1878   flagword flags;
1879   asection *sec;
1880
1881   /* If we are only performing a partial
1882      link do not bother adding the glue.  */
1883   if (info->relocatable)
1884     return TRUE;
1885
1886   sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1887
1888   if (sec == NULL)
1889     {
1890       /* Note: we do not include the flag SEC_LINKER_CREATED, as this
1891          will prevent elf_link_input_bfd() from processing the contents
1892          of this section.  */
1893       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
1894
1895       sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1896
1897       if (sec == NULL
1898           || !bfd_set_section_flags (abfd, sec, flags)
1899           || !bfd_set_section_alignment (abfd, sec, 2))
1900         return FALSE;
1901
1902       /* Set the gc mark to prevent the section from being removed by garbage
1903          collection, despite the fact that no relocs refer to this section.  */
1904       sec->gc_mark = 1;
1905     }
1906
1907   sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1908
1909   if (sec == NULL)
1910     {
1911       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1912         | SEC_CODE | SEC_READONLY;
1913
1914       sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1915
1916       if (sec == NULL
1917           || !bfd_set_section_flags (abfd, sec, flags)
1918           || !bfd_set_section_alignment (abfd, sec, 2))
1919         return FALSE;
1920
1921       sec->gc_mark = 1;
1922     }
1923
1924   return TRUE;
1925 }
1926
1927 /* Select a BFD to be used to hold the sections used by the glue code.
1928    This function is called from the linker scripts in ld/emultempl/
1929    {armelf/pe}.em  */
1930
1931 bfd_boolean
1932 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
1933 {
1934   struct elf32_arm_link_hash_table *globals;
1935
1936   /* If we are only performing a partial link
1937      do not bother getting a bfd to hold the glue.  */
1938   if (info->relocatable)
1939     return TRUE;
1940
1941   /* Make sure we don't attach the glue sections to a dynamic object.  */
1942   BFD_ASSERT (!(abfd->flags & DYNAMIC));
1943
1944   globals = elf32_arm_hash_table (info);
1945
1946   BFD_ASSERT (globals != NULL);
1947
1948   if (globals->bfd_of_glue_owner != NULL)
1949     return TRUE;
1950
1951   /* Save the bfd for later use.  */
1952   globals->bfd_of_glue_owner = abfd;
1953
1954   return TRUE;
1955 }
1956
1957 bfd_boolean
1958 bfd_elf32_arm_process_before_allocation (bfd *abfd,
1959                                          struct bfd_link_info *link_info,
1960                                          int byteswap_code)
1961 {
1962   Elf_Internal_Shdr *symtab_hdr;
1963   Elf_Internal_Rela *internal_relocs = NULL;
1964   Elf_Internal_Rela *irel, *irelend;
1965   bfd_byte *contents = NULL;
1966
1967   asection *sec;
1968   struct elf32_arm_link_hash_table *globals;
1969
1970   /* If we are only performing a partial link do not bother
1971      to construct any glue.  */
1972   if (link_info->relocatable)
1973     return TRUE;
1974
1975   /* Here we have a bfd that is to be included on the link.  We have a hook
1976      to do reloc rummaging, before section sizes are nailed down.  */
1977   globals = elf32_arm_hash_table (link_info);
1978
1979   BFD_ASSERT (globals != NULL);
1980   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1981
1982   if (byteswap_code && !bfd_big_endian (abfd))
1983     {
1984       _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
1985                           abfd);
1986       return FALSE;
1987     }
1988   globals->byteswap_code = byteswap_code;
1989
1990   /* Rummage around all the relocs and map the glue vectors.  */
1991   sec = abfd->sections;
1992
1993   if (sec == NULL)
1994     return TRUE;
1995
1996   for (; sec != NULL; sec = sec->next)
1997     {
1998       if (sec->reloc_count == 0)
1999         continue;
2000
2001       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2002
2003       /* Load the relocs.  */
2004       internal_relocs
2005         = _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
2006                                      (Elf_Internal_Rela *) NULL, FALSE);
2007
2008       if (internal_relocs == NULL)
2009         goto error_return;
2010
2011       irelend = internal_relocs + sec->reloc_count;
2012       for (irel = internal_relocs; irel < irelend; irel++)
2013         {
2014           long r_type;
2015           unsigned long r_index;
2016
2017           struct elf_link_hash_entry *h;
2018
2019           r_type = ELF32_R_TYPE (irel->r_info);
2020           r_index = ELF32_R_SYM (irel->r_info);
2021
2022           /* These are the only relocation types we care about.  */
2023           if (   r_type != R_ARM_PC24
2024               && r_type != R_ARM_PLT32
2025 #ifndef OLD_ARM_ABI
2026               && r_type != R_ARM_CALL
2027               && r_type != R_ARM_JUMP24
2028 #endif
2029               && r_type != R_ARM_THM_PC22)
2030             continue;
2031
2032           /* Get the section contents if we haven't done so already.  */
2033           if (contents == NULL)
2034             {
2035               /* Get cached copy if it exists.  */
2036               if (elf_section_data (sec)->this_hdr.contents != NULL)
2037                 contents = elf_section_data (sec)->this_hdr.contents;
2038               else
2039                 {
2040                   /* Go get them off disk.  */
2041                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2042                     goto error_return;
2043                 }
2044             }
2045
2046           /* If the relocation is not against a symbol it cannot concern us.  */
2047           h = NULL;
2048
2049           /* We don't care about local symbols.  */
2050           if (r_index < symtab_hdr->sh_info)
2051             continue;
2052
2053           /* This is an external symbol.  */
2054           r_index -= symtab_hdr->sh_info;
2055           h = (struct elf_link_hash_entry *)
2056             elf_sym_hashes (abfd)[r_index];
2057
2058           /* If the relocation is against a static symbol it must be within
2059              the current section and so cannot be a cross ARM/Thumb relocation.  */
2060           if (h == NULL)
2061             continue;
2062
2063           /* If the call will go through a PLT entry then we do not need
2064              glue.  */
2065           if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
2066             continue;
2067
2068           switch (r_type)
2069             {
2070             case R_ARM_PC24:
2071 #ifndef OLD_ARM_ABI
2072             case R_ARM_CALL:
2073             case R_ARM_JUMP24:
2074 #endif
2075               /* This one is a call from arm code.  We need to look up
2076                  the target of the call.  If it is a thumb target, we
2077                  insert glue.  */
2078               if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
2079                 record_arm_to_thumb_glue (link_info, h);
2080               break;
2081
2082             case R_ARM_THM_PC22:
2083               /* This one is a call from thumb code.  We look
2084                  up the target of the call.  If it is not a thumb
2085                  target, we insert glue.  */
2086               if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
2087                 record_thumb_to_arm_glue (link_info, h);
2088               break;
2089
2090             default:
2091               break;
2092             }
2093         }
2094
2095       if (contents != NULL
2096           && elf_section_data (sec)->this_hdr.contents != contents)
2097         free (contents);
2098       contents = NULL;
2099
2100       if (internal_relocs != NULL
2101           && elf_section_data (sec)->relocs != internal_relocs)
2102         free (internal_relocs);
2103       internal_relocs = NULL;
2104     }
2105
2106   return TRUE;
2107
2108 error_return:
2109   if (contents != NULL
2110       && elf_section_data (sec)->this_hdr.contents != contents)
2111     free (contents);
2112   if (internal_relocs != NULL
2113       && elf_section_data (sec)->relocs != internal_relocs)
2114     free (internal_relocs);
2115
2116   return FALSE;
2117 }
2118 #endif
2119
2120
2121 #ifndef OLD_ARM_ABI
2122 /* Set target relocation values needed during linking.  */
2123
2124 void
2125 bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
2126                                  int target1_is_rel,
2127                                  char * target2_type,
2128                                  int fix_v4bx)
2129 {
2130   struct elf32_arm_link_hash_table *globals;
2131
2132   globals = elf32_arm_hash_table (link_info);
2133
2134   globals->target1_is_rel = target1_is_rel;
2135   if (strcmp (target2_type, "rel") == 0)
2136     globals->target2_reloc = R_ARM_REL32;
2137   else if (strcmp (target2_type, "abs") == 0)
2138     globals->target2_reloc = R_ARM_ABS32;
2139   else if (strcmp (target2_type, "got-rel") == 0)
2140     globals->target2_reloc = R_ARM_GOT_PREL;
2141   else
2142     {
2143       _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
2144                           target2_type);
2145     }
2146   globals->fix_v4bx = fix_v4bx;
2147 }
2148 #endif
2149
2150 /* The thumb form of a long branch is a bit finicky, because the offset
2151    encoding is split over two fields, each in it's own instruction. They
2152    can occur in any order. So given a thumb form of long branch, and an
2153    offset, insert the offset into the thumb branch and return finished
2154    instruction.
2155
2156    It takes two thumb instructions to encode the target address. Each has
2157    11 bits to invest. The upper 11 bits are stored in one (identified by
2158    H-0.. see below), the lower 11 bits are stored in the other (identified
2159    by H-1).
2160
2161    Combine together and shifted left by 1 (it's a half word address) and
2162    there you have it.
2163
2164    Op: 1111 = F,
2165    H-0, upper address-0 = 000
2166    Op: 1111 = F,
2167    H-1, lower address-0 = 800
2168
2169    They can be ordered either way, but the arm tools I've seen always put
2170    the lower one first. It probably doesn't matter. krk@cygnus.com
2171
2172    XXX:  Actually the order does matter.  The second instruction (H-1)
2173    moves the computed address into the PC, so it must be the second one
2174    in the sequence.  The problem, however is that whilst little endian code
2175    stores the instructions in HI then LOW order, big endian code does the
2176    reverse.  nickc@cygnus.com.  */
2177
2178 #define LOW_HI_ORDER      0xF800F000
2179 #define HI_LOW_ORDER      0xF000F800
2180
2181 static insn32
2182 insert_thumb_branch (insn32 br_insn, int rel_off)
2183 {
2184   unsigned int low_bits;
2185   unsigned int high_bits;
2186
2187   BFD_ASSERT ((rel_off & 1) != 1);
2188
2189   rel_off >>= 1;                                /* Half word aligned address.  */
2190   low_bits = rel_off & 0x000007FF;              /* The bottom 11 bits.  */
2191   high_bits = (rel_off >> 11) & 0x000007FF;     /* The top 11 bits.  */
2192
2193   if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
2194     br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
2195   else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
2196     br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
2197   else
2198     /* FIXME: abort is probably not the right call. krk@cygnus.com  */
2199     abort ();   /* Error - not a valid branch instruction form.  */
2200
2201   return br_insn;
2202 }
2203
2204 /* Thumb code calling an ARM function.  */
2205
2206 static int
2207 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
2208                          const char *           name,
2209                          bfd *                  input_bfd,
2210                          bfd *                  output_bfd,
2211                          asection *             input_section,
2212                          bfd_byte *             hit_data,
2213                          asection *             sym_sec,
2214                          bfd_vma                offset,
2215                          bfd_signed_vma         addend,
2216                          bfd_vma                val)
2217 {
2218   asection * s = 0;
2219   bfd_vma my_offset;
2220   unsigned long int tmp;
2221   long int ret_offset;
2222   struct elf_link_hash_entry * myh;
2223   struct elf32_arm_link_hash_table * globals;
2224
2225   myh = find_thumb_glue (info, name, input_bfd);
2226   if (myh == NULL)
2227     return FALSE;
2228
2229   globals = elf32_arm_hash_table (info);
2230
2231   BFD_ASSERT (globals != NULL);
2232   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2233
2234   my_offset = myh->root.u.def.value;
2235
2236   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2237                                THUMB2ARM_GLUE_SECTION_NAME);
2238
2239   BFD_ASSERT (s != NULL);
2240   BFD_ASSERT (s->contents != NULL);
2241   BFD_ASSERT (s->output_section != NULL);
2242
2243   if ((my_offset & 0x01) == 0x01)
2244     {
2245       if (sym_sec != NULL
2246           && sym_sec->owner != NULL
2247           && !INTERWORK_FLAG (sym_sec->owner))
2248         {
2249           (*_bfd_error_handler)
2250             (_("%B(%s): warning: interworking not enabled.\n"
2251                "  first occurrence: %B: thumb call to arm"),
2252              sym_sec->owner, input_bfd, name);
2253
2254           return FALSE;
2255         }
2256
2257       --my_offset;
2258       myh->root.u.def.value = my_offset;
2259
2260       bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
2261                   s->contents + my_offset);
2262
2263       bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
2264                   s->contents + my_offset + 2);
2265
2266       ret_offset =
2267         /* Address of destination of the stub.  */
2268         ((bfd_signed_vma) val)
2269         - ((bfd_signed_vma)
2270            /* Offset from the start of the current section
2271               to the start of the stubs.  */
2272            (s->output_offset
2273             /* Offset of the start of this stub from the start of the stubs.  */
2274             + my_offset
2275             /* Address of the start of the current section.  */
2276             + s->output_section->vma)
2277            /* The branch instruction is 4 bytes into the stub.  */
2278            + 4
2279            /* ARM branches work from the pc of the instruction + 8.  */
2280            + 8);
2281
2282       bfd_put_32 (output_bfd,
2283                   (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
2284                   s->contents + my_offset + 4);
2285     }
2286
2287   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
2288
2289   /* Now go back and fix up the original BL insn to point to here.  */
2290   ret_offset =
2291     /* Address of where the stub is located.  */
2292     (s->output_section->vma + s->output_offset + my_offset)
2293      /* Address of where the BL is located.  */
2294     - (input_section->output_section->vma + input_section->output_offset
2295        + offset)
2296     /* Addend in the relocation.  */
2297     - addend
2298     /* Biassing for PC-relative addressing.  */
2299     - 8;
2300
2301   tmp = bfd_get_32 (input_bfd, hit_data
2302                     - input_section->vma);
2303
2304   bfd_put_32 (output_bfd,
2305               (bfd_vma) insert_thumb_branch (tmp, ret_offset),
2306               hit_data - input_section->vma);
2307
2308   return TRUE;
2309 }
2310
2311 /* Arm code calling a Thumb function.  */
2312
2313 static int
2314 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
2315                          const char *           name,
2316                          bfd *                  input_bfd,
2317                          bfd *                  output_bfd,
2318                          asection *             input_section,
2319                          bfd_byte *             hit_data,
2320                          asection *             sym_sec,
2321                          bfd_vma                offset,
2322                          bfd_signed_vma         addend,
2323                          bfd_vma                val)
2324 {
2325   unsigned long int tmp;
2326   bfd_vma my_offset;
2327   asection * s;
2328   long int ret_offset;
2329   struct elf_link_hash_entry * myh;
2330   struct elf32_arm_link_hash_table * globals;
2331
2332   myh = find_arm_glue (info, name, input_bfd);
2333   if (myh == NULL)
2334     return FALSE;
2335
2336   globals = elf32_arm_hash_table (info);
2337
2338   BFD_ASSERT (globals != NULL);
2339   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2340
2341   my_offset = myh->root.u.def.value;
2342   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2343                                ARM2THUMB_GLUE_SECTION_NAME);
2344   BFD_ASSERT (s != NULL);
2345   BFD_ASSERT (s->contents != NULL);
2346   BFD_ASSERT (s->output_section != NULL);
2347
2348   if ((my_offset & 0x01) == 0x01)
2349     {
2350       if (sym_sec != NULL
2351           && sym_sec->owner != NULL
2352           && !INTERWORK_FLAG (sym_sec->owner))
2353         {
2354           (*_bfd_error_handler)
2355             (_("%B(%s): warning: interworking not enabled.\n"
2356                "  first occurrence: %B: arm call to thumb"),
2357              sym_sec->owner, input_bfd, name);
2358         }
2359
2360       --my_offset;
2361       myh->root.u.def.value = my_offset;
2362
2363       if ((info->shared || globals->root.is_relocatable_executable))
2364         {
2365           /* For relocatable objects we can't use absolute addresses,
2366              so construct the address from a relative offset.  */
2367           /* TODO: If the offset is small it's probably worth
2368              constructing the address with adds.  */
2369           bfd_put_32 (output_bfd, (bfd_vma) a2t1p_ldr_insn,
2370                       s->contents + my_offset);
2371           bfd_put_32 (output_bfd, (bfd_vma) a2t2p_add_pc_insn,
2372                       s->contents + my_offset + 4);
2373           bfd_put_32 (output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
2374                       s->contents + my_offset + 8);
2375           /* Adjust the offset by 4 for the position of the add,
2376              and 8 for the pipeline offset.  */
2377           ret_offset = (val - (s->output_offset
2378                                + s->output_section->vma
2379                                + my_offset + 12))
2380                        | 1;
2381           bfd_put_32 (output_bfd, ret_offset,
2382                       s->contents + my_offset + 12);
2383         }
2384       else
2385         {
2386           bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
2387                       s->contents + my_offset);
2388
2389           bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
2390                       s->contents + my_offset + 4);
2391
2392           /* It's a thumb address.  Add the low order bit.  */
2393           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
2394                       s->contents + my_offset + 8);
2395         }
2396     }
2397
2398   BFD_ASSERT (my_offset <= globals->arm_glue_size);
2399
2400   tmp = bfd_get_32 (input_bfd, hit_data);
2401   tmp = tmp & 0xFF000000;
2402
2403   /* Somehow these are both 4 too far, so subtract 8.  */
2404   ret_offset = (s->output_offset
2405                 + my_offset
2406                 + s->output_section->vma
2407                 - (input_section->output_offset
2408                    + input_section->output_section->vma
2409                    + offset + addend)
2410                 - 8);
2411
2412   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
2413
2414   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
2415
2416   return TRUE;
2417 }
2418
2419
2420 #ifndef OLD_ARM_ABI
2421 /* Some relocations map to different relocations depending on the
2422    target.  Return the real relocation.  */
2423 static int
2424 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
2425                      int r_type)
2426 {
2427   switch (r_type)
2428     {
2429     case R_ARM_TARGET1:
2430       if (globals->target1_is_rel)
2431         return R_ARM_REL32;
2432       else
2433         return R_ARM_ABS32;
2434
2435     case R_ARM_TARGET2:
2436       return globals->target2_reloc;
2437
2438     default:
2439       return r_type;
2440     }
2441 }
2442 #endif /* OLD_ARM_ABI */
2443
2444
2445 /* Return the base VMA address which should be subtracted from real addresses
2446    when resolving @dtpoff relocation.
2447    This is PT_TLS segment p_vaddr.  */
2448
2449 static bfd_vma
2450 dtpoff_base (struct bfd_link_info *info)
2451 {
2452   /* If tls_sec is NULL, we should have signalled an error already.  */
2453   if (elf_hash_table (info)->tls_sec == NULL)
2454     return 0;
2455   return elf_hash_table (info)->tls_sec->vma;
2456 }
2457
2458 /* Return the relocation value for @tpoff relocation
2459    if STT_TLS virtual address is ADDRESS.  */
2460
2461 static bfd_vma
2462 tpoff (struct bfd_link_info *info, bfd_vma address)
2463 {
2464   struct elf_link_hash_table *htab = elf_hash_table (info);
2465   bfd_vma base;
2466
2467   /* If tls_sec is NULL, we should have signalled an error already.  */
2468   if (htab->tls_sec == NULL)
2469     return 0;
2470   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
2471   return address - htab->tls_sec->vma + base;
2472 }
2473
2474 /* Perform a relocation as part of a final link.  */
2475
2476 static bfd_reloc_status_type
2477 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
2478                                bfd *                        input_bfd,
2479                                bfd *                        output_bfd,
2480                                asection *                   input_section,
2481                                bfd_byte *                   contents,
2482                                Elf_Internal_Rela *          rel,
2483                                bfd_vma                      value,
2484                                struct bfd_link_info *       info,
2485                                asection *                   sym_sec,
2486                                const char *                 sym_name,
2487                                int                          sym_flags,
2488                                struct elf_link_hash_entry * h,
2489                                bfd_boolean *                unresolved_reloc_p)
2490 {
2491   unsigned long                 r_type = howto->type;
2492   unsigned long                 r_symndx;
2493   bfd_byte *                    hit_data = contents + rel->r_offset;
2494   bfd *                         dynobj = NULL;
2495   Elf_Internal_Shdr *           symtab_hdr;
2496   struct elf_link_hash_entry ** sym_hashes;
2497   bfd_vma *                     local_got_offsets;
2498   asection *                    sgot = NULL;
2499   asection *                    splt = NULL;
2500   asection *                    sreloc = NULL;
2501   bfd_vma                       addend;
2502   bfd_signed_vma                signed_addend;
2503   struct elf32_arm_link_hash_table * globals;
2504
2505   globals = elf32_arm_hash_table (info);
2506
2507 #ifndef OLD_ARM_ABI
2508   /* Some relocation type map to different relocations depending on the
2509      target.  We pick the right one here.  */
2510   r_type = arm_real_reloc_type (globals, r_type);
2511   if (r_type != howto->type)
2512     howto = elf32_arm_howto_from_type (r_type);
2513 #endif /* OLD_ARM_ABI */
2514
2515   /* If the start address has been set, then set the EF_ARM_HASENTRY
2516      flag.  Setting this more than once is redundant, but the cost is
2517      not too high, and it keeps the code simple.
2518
2519      The test is done  here, rather than somewhere else, because the
2520      start address is only set just before the final link commences.
2521
2522      Note - if the user deliberately sets a start address of 0, the
2523      flag will not be set.  */
2524   if (bfd_get_start_address (output_bfd) != 0)
2525     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
2526
2527   dynobj = elf_hash_table (info)->dynobj;
2528   if (dynobj)
2529     {
2530       sgot = bfd_get_section_by_name (dynobj, ".got");
2531       splt = bfd_get_section_by_name (dynobj, ".plt");
2532     }
2533   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2534   sym_hashes = elf_sym_hashes (input_bfd);
2535   local_got_offsets = elf_local_got_offsets (input_bfd);
2536   r_symndx = ELF32_R_SYM (rel->r_info);
2537
2538   if (globals->use_rel)
2539     {
2540       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
2541
2542       if (addend & ((howto->src_mask + 1) >> 1))
2543         {
2544           signed_addend = -1;
2545           signed_addend &= ~ howto->src_mask;
2546           signed_addend |= addend;
2547         }
2548       else
2549         signed_addend = addend;
2550     }
2551   else
2552     addend = signed_addend = rel->r_addend;
2553
2554   switch (r_type)
2555     {
2556     case R_ARM_NONE:
2557       /* We don't need to find a value for this symbol.  It's just a
2558          marker.  */
2559       *unresolved_reloc_p = FALSE;
2560       return bfd_reloc_ok;
2561
2562     case R_ARM_PC24:
2563     case R_ARM_ABS32:
2564     case R_ARM_REL32:
2565 #ifndef OLD_ARM_ABI
2566     case R_ARM_CALL:
2567     case R_ARM_JUMP24:
2568     case R_ARM_XPC25:
2569     case R_ARM_PREL31:
2570 #endif
2571     case R_ARM_PLT32:
2572       /* r_symndx will be zero only for relocs against symbols
2573          from removed linkonce sections, or sections discarded by
2574          a linker script.  */
2575       if (r_symndx == 0)
2576         return bfd_reloc_ok;
2577
2578       /* Handle relocations which should use the PLT entry.  ABS32/REL32
2579          will use the symbol's value, which may point to a PLT entry, but we
2580          don't need to handle that here.  If we created a PLT entry, all
2581          branches in this object should go to it.  */
2582       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
2583           && h != NULL
2584           && splt != NULL
2585           && h->plt.offset != (bfd_vma) -1)
2586         {
2587           /* If we've created a .plt section, and assigned a PLT entry to
2588              this function, it should not be known to bind locally.  If
2589              it were, we would have cleared the PLT entry.  */
2590           BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
2591
2592           value = (splt->output_section->vma
2593                    + splt->output_offset
2594                    + h->plt.offset);
2595           *unresolved_reloc_p = FALSE;
2596           return _bfd_final_link_relocate (howto, input_bfd, input_section,
2597                                            contents, rel->r_offset, value,
2598                                            (bfd_vma) 0);
2599         }
2600
2601       /* When generating a shared object or relocatable executable, these
2602          relocations are copied into the output file to be resolved at
2603          run time.  */
2604       if ((info->shared || globals->root.is_relocatable_executable)
2605           && (input_section->flags & SEC_ALLOC)
2606           && (r_type != R_ARM_REL32
2607               || !SYMBOL_CALLS_LOCAL (info, h))
2608           && (h == NULL
2609               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2610               || h->root.type != bfd_link_hash_undefweak)
2611           && r_type != R_ARM_PC24
2612 #ifndef OLD_ARM_ABI
2613           && r_type != R_ARM_CALL
2614           && r_type != R_ARM_JUMP24
2615           && r_type != R_ARM_PREL31
2616 #endif
2617           && r_type != R_ARM_PLT32)
2618         {
2619           Elf_Internal_Rela outrel;
2620           bfd_byte *loc;
2621           bfd_boolean skip, relocate;
2622
2623           *unresolved_reloc_p = FALSE;
2624
2625           if (sreloc == NULL)
2626             {
2627               const char * name;
2628
2629               name = (bfd_elf_string_from_elf_section
2630                       (input_bfd,
2631                        elf_elfheader (input_bfd)->e_shstrndx,
2632                        elf_section_data (input_section)->rel_hdr.sh_name));
2633               if (name == NULL)
2634                 return bfd_reloc_notsupported;
2635
2636               BFD_ASSERT (strncmp (name, ".rel", 4) == 0
2637                           && strcmp (bfd_get_section_name (input_bfd,
2638                                                            input_section),
2639                                      name + 4) == 0);
2640
2641               sreloc = bfd_get_section_by_name (dynobj, name);
2642               BFD_ASSERT (sreloc != NULL);
2643             }
2644
2645           skip = FALSE;
2646           relocate = FALSE;
2647
2648           outrel.r_offset =
2649             _bfd_elf_section_offset (output_bfd, info, input_section,
2650                                      rel->r_offset);
2651           if (outrel.r_offset == (bfd_vma) -1)
2652             skip = TRUE;
2653           else if (outrel.r_offset == (bfd_vma) -2)
2654             skip = TRUE, relocate = TRUE;
2655           outrel.r_offset += (input_section->output_section->vma
2656                               + input_section->output_offset);
2657
2658           if (skip)
2659             memset (&outrel, 0, sizeof outrel);
2660           else if (h != NULL
2661                    && h->dynindx != -1
2662                    && (!info->shared
2663                        || !info->symbolic
2664                        || !h->def_regular))
2665             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2666           else
2667             {
2668               int symbol;
2669
2670               /* This symbol is local, or marked to become local.  */
2671               relocate = TRUE;
2672               if (sym_flags == STT_ARM_TFUNC)
2673                 value |= 1;
2674               if (globals->symbian_p)
2675                 {
2676                   /* On Symbian OS, the data segment and text segement
2677                      can be relocated independently.  Therefore, we
2678                      must indicate the segment to which this
2679                      relocation is relative.  The BPABI allows us to
2680                      use any symbol in the right segment; we just use
2681                      the section symbol as it is convenient.  (We
2682                      cannot use the symbol given by "h" directly as it
2683                      will not appear in the dynamic symbol table.)  */
2684                   symbol = elf_section_data (sym_sec->output_section)->dynindx;
2685                   BFD_ASSERT (symbol != 0);
2686                 }
2687               else
2688                 /* On SVR4-ish systems, the dynamic loader cannot
2689                    relocate the text and data segments independently,
2690                    so the symbol does not matter.  */
2691                 symbol = 0;
2692               outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
2693             }
2694
2695           loc = sreloc->contents;
2696           loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2697           bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2698
2699           /* If this reloc is against an external symbol, we do not want to
2700              fiddle with the addend.  Otherwise, we need to include the symbol
2701              value so that it becomes an addend for the dynamic reloc.  */
2702           if (! relocate)
2703             return bfd_reloc_ok;
2704
2705           return _bfd_final_link_relocate (howto, input_bfd, input_section,
2706                                            contents, rel->r_offset, value,
2707                                            (bfd_vma) 0);
2708         }
2709       else switch (r_type)
2710         {
2711 #ifndef OLD_ARM_ABI
2712         case R_ARM_XPC25:         /* Arm BLX instruction.  */
2713         case R_ARM_CALL:
2714         case R_ARM_JUMP24:
2715 #endif
2716         case R_ARM_PC24:          /* Arm B/BL instruction */
2717         case R_ARM_PLT32:
2718 #ifndef OLD_ARM_ABI
2719           if (r_type == R_ARM_XPC25)
2720             {
2721               /* Check for Arm calling Arm function.  */
2722               /* FIXME: Should we translate the instruction into a BL
2723                  instruction instead ?  */
2724               if (sym_flags != STT_ARM_TFUNC)
2725                 (*_bfd_error_handler)
2726                   (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
2727                    input_bfd,
2728                    h ? h->root.root.string : "(local)");
2729             }
2730           else
2731 #endif
2732             {
2733               /* Check for Arm calling Thumb function.  */
2734               if (sym_flags == STT_ARM_TFUNC)
2735                 {
2736                   elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
2737                                            output_bfd, input_section,
2738                                            hit_data, sym_sec, rel->r_offset,
2739                                            signed_addend, value);
2740                   return bfd_reloc_ok;
2741                 }
2742             }
2743
2744           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
2745              where:
2746               S is the address of the symbol in the relocation.
2747               P is address of the instruction being relocated.
2748               A is the addend (extracted from the instruction) in bytes.
2749
2750              S is held in 'value'.
2751              P is the base address of the section containing the
2752                instruction plus the offset of the reloc into that
2753                section, ie:
2754                  (input_section->output_section->vma +
2755                   input_section->output_offset +
2756                   rel->r_offset).
2757              A is the addend, converted into bytes, ie:
2758                  (signed_addend * 4)
2759
2760              Note: None of these operations have knowledge of the pipeline
2761              size of the processor, thus it is up to the assembler to
2762              encode this information into the addend.  */
2763           value -= (input_section->output_section->vma
2764                     + input_section->output_offset);
2765           value -= rel->r_offset;
2766           if (globals->use_rel)
2767             value += (signed_addend << howto->size);
2768           else
2769             /* RELA addends do not have to be adjusted by howto->size.  */
2770             value += signed_addend;
2771
2772           signed_addend = value;
2773           signed_addend >>= howto->rightshift;
2774
2775           /* It is not an error for an undefined weak reference to be
2776              out of range.  Any program that branches to such a symbol
2777              is going to crash anyway, so there is no point worrying
2778              about getting the destination exactly right.  */
2779           if (! h || h->root.type != bfd_link_hash_undefweak)
2780             {
2781               /* Perform a signed range check.  */
2782               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
2783                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
2784                 return bfd_reloc_overflow;
2785             }
2786
2787 #ifndef OLD_ARM_ABI
2788           /* If necessary set the H bit in the BLX instruction.  */
2789           if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
2790             value = (signed_addend & howto->dst_mask)
2791               | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
2792               | (1 << 24);
2793           else
2794 #endif
2795             value = (signed_addend & howto->dst_mask)
2796               | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
2797           break;
2798
2799         case R_ARM_ABS32:
2800           value += addend;
2801           if (sym_flags == STT_ARM_TFUNC)
2802             value |= 1;
2803           break;
2804
2805         case R_ARM_REL32:
2806           value -= (input_section->output_section->vma
2807                     + input_section->output_offset + rel->r_offset);
2808           value += addend;
2809           break;
2810
2811 #ifndef OLD_ARM_ABI
2812         case R_ARM_PREL31:
2813           value -= (input_section->output_section->vma
2814                     + input_section->output_offset + rel->r_offset);
2815           value += signed_addend;
2816           if (! h || h->root.type != bfd_link_hash_undefweak)
2817             {
2818               /* Check for overflow */
2819               if ((value ^ (value >> 1)) & (1 << 30))
2820                 return bfd_reloc_overflow;
2821             }
2822           value &= 0x7fffffff;
2823           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
2824           if (sym_flags == STT_ARM_TFUNC)
2825             value |= 1;
2826           break;
2827 #endif
2828         }
2829
2830       bfd_put_32 (input_bfd, value, hit_data);
2831       return bfd_reloc_ok;
2832
2833     case R_ARM_ABS8:
2834       value += addend;
2835       if ((long) value > 0x7f || (long) value < -0x80)
2836         return bfd_reloc_overflow;
2837
2838       bfd_put_8 (input_bfd, value, hit_data);
2839       return bfd_reloc_ok;
2840
2841     case R_ARM_ABS16:
2842       value += addend;
2843
2844       if ((long) value > 0x7fff || (long) value < -0x8000)
2845         return bfd_reloc_overflow;
2846
2847       bfd_put_16 (input_bfd, value, hit_data);
2848       return bfd_reloc_ok;
2849
2850     case R_ARM_ABS12:
2851       /* Support ldr and str instruction for the arm */
2852       /* Also thumb b (unconditional branch).  ??? Really?  */
2853       value += addend;
2854
2855       if ((long) value > 0x7ff || (long) value < -0x800)
2856         return bfd_reloc_overflow;
2857
2858       value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
2859       bfd_put_32 (input_bfd, value, hit_data);
2860       return bfd_reloc_ok;
2861
2862     case R_ARM_THM_ABS5:
2863       /* Support ldr and str instructions for the thumb.  */
2864       if (globals->use_rel)
2865         {
2866           /* Need to refetch addend.  */
2867           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
2868           /* ??? Need to determine shift amount from operand size.  */
2869           addend >>= howto->rightshift;
2870         }
2871       value += addend;
2872
2873       /* ??? Isn't value unsigned?  */
2874       if ((long) value > 0x1f || (long) value < -0x10)
2875         return bfd_reloc_overflow;
2876
2877       /* ??? Value needs to be properly shifted into place first.  */
2878       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
2879       bfd_put_16 (input_bfd, value, hit_data);
2880       return bfd_reloc_ok;
2881
2882 #ifndef OLD_ARM_ABI
2883     case R_ARM_THM_XPC22:
2884 #endif
2885     case R_ARM_THM_PC22:
2886       /* Thumb BL (branch long instruction).  */
2887       {
2888         bfd_vma relocation;
2889         bfd_boolean overflow = FALSE;
2890         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
2891         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
2892         bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
2893         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
2894         bfd_vma check;
2895         bfd_signed_vma signed_check;
2896
2897         /* Need to refetch the addend and squish the two 11 bit pieces
2898            together.  */
2899         if (globals->use_rel)
2900           {
2901             bfd_vma upper = upper_insn & 0x7ff;
2902             bfd_vma lower = lower_insn & 0x7ff;
2903             upper = (upper ^ 0x400) - 0x400; /* Sign extend.  */
2904             addend = (upper << 12) | (lower << 1);
2905             signed_addend = addend;
2906           }
2907 #ifndef OLD_ARM_ABI
2908         if (r_type == R_ARM_THM_XPC22)
2909           {
2910             /* Check for Thumb to Thumb call.  */
2911             /* FIXME: Should we translate the instruction into a BL
2912                instruction instead ?  */
2913             if (sym_flags == STT_ARM_TFUNC)
2914               (*_bfd_error_handler)
2915                 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
2916                  input_bfd,
2917                  h ? h->root.root.string : "(local)");
2918           }
2919         else
2920 #endif
2921           {
2922             /* If it is not a call to Thumb, assume call to Arm.
2923                If it is a call relative to a section name, then it is not a
2924                function call at all, but rather a long jump.  Calls through
2925                the PLT do not require stubs.  */
2926             if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
2927                 && (h == NULL || splt == NULL
2928                     || h->plt.offset == (bfd_vma) -1))
2929               {
2930                 if (elf32_thumb_to_arm_stub
2931                     (info, sym_name, input_bfd, output_bfd, input_section,
2932                      hit_data, sym_sec, rel->r_offset, signed_addend, value))
2933                   return bfd_reloc_ok;
2934                 else
2935                   return bfd_reloc_dangerous;
2936               }
2937           }
2938
2939         /* Handle calls via the PLT.  */
2940         if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
2941           {
2942             value = (splt->output_section->vma
2943                      + splt->output_offset
2944                      + h->plt.offset);
2945             /* Target the Thumb stub before the ARM PLT entry.  */
2946             value -= 4;
2947             *unresolved_reloc_p = FALSE;
2948           }
2949
2950         relocation = value + signed_addend;
2951
2952         relocation -= (input_section->output_section->vma
2953                        + input_section->output_offset
2954                        + rel->r_offset);
2955
2956         check = relocation >> howto->rightshift;
2957
2958         /* If this is a signed value, the rightshift just dropped
2959            leading 1 bits (assuming twos complement).  */
2960         if ((bfd_signed_vma) relocation >= 0)
2961           signed_check = check;
2962         else
2963           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
2964
2965         /* Assumes two's complement.  */
2966         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
2967           overflow = TRUE;
2968
2969 #ifndef OLD_ARM_ABI
2970         if (r_type == R_ARM_THM_XPC22
2971             && ((lower_insn & 0x1800) == 0x0800))
2972           /* For a BLX instruction, make sure that the relocation is rounded up
2973              to a word boundary.  This follows the semantics of the instruction
2974              which specifies that bit 1 of the target address will come from bit
2975              1 of the base address.  */
2976           relocation = (relocation + 2) & ~ 3;
2977 #endif
2978         /* Put RELOCATION back into the insn.  */
2979         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
2980         lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
2981
2982         /* Put the relocated value back in the object file:  */
2983         bfd_put_16 (input_bfd, upper_insn, hit_data);
2984         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
2985
2986         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
2987       }
2988       break;
2989
2990     case R_ARM_THM_PC11:
2991     case R_ARM_THM_PC9:
2992       /* Thumb B (branch) instruction).  */
2993       {
2994         bfd_signed_vma relocation;
2995         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
2996         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
2997         bfd_signed_vma signed_check;
2998
2999         if (globals->use_rel)
3000           {
3001             /* Need to refetch addend.  */
3002             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
3003             if (addend & ((howto->src_mask + 1) >> 1))
3004               {
3005                 signed_addend = -1;
3006                 signed_addend &= ~ howto->src_mask;
3007                 signed_addend |= addend;
3008               }
3009             else
3010               signed_addend = addend;
3011             /* The value in the insn has been right shifted.  We need to
3012                undo this, so that we can perform the address calculation
3013                in terms of bytes.  */
3014             signed_addend <<= howto->rightshift;
3015           }
3016         relocation = value + signed_addend;
3017
3018         relocation -= (input_section->output_section->vma
3019                        + input_section->output_offset
3020                        + rel->r_offset);
3021
3022         relocation >>= howto->rightshift;
3023         signed_check = relocation;
3024         relocation &= howto->dst_mask;
3025         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
3026
3027         bfd_put_16 (input_bfd, relocation, hit_data);
3028
3029         /* Assumes two's complement.  */
3030         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
3031           return bfd_reloc_overflow;
3032
3033         return bfd_reloc_ok;
3034       }
3035
3036 #ifndef OLD_ARM_ABI
3037     case R_ARM_ALU_PCREL7_0:
3038     case R_ARM_ALU_PCREL15_8:
3039     case R_ARM_ALU_PCREL23_15:
3040       {
3041         bfd_vma insn;
3042         bfd_vma relocation;
3043
3044         insn = bfd_get_32 (input_bfd, hit_data);
3045         if (globals->use_rel)
3046           {
3047             /* Extract the addend.  */
3048             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
3049             signed_addend = addend;
3050           }
3051         relocation = value + signed_addend;
3052
3053         relocation -= (input_section->output_section->vma
3054                        + input_section->output_offset
3055                        + rel->r_offset);
3056         insn = (insn & ~0xfff)
3057                | ((howto->bitpos << 7) & 0xf00)
3058                | ((relocation >> howto->bitpos) & 0xff);
3059         bfd_put_32 (input_bfd, value, hit_data);
3060       }
3061       return bfd_reloc_ok;
3062 #endif
3063
3064     case R_ARM_GNU_VTINHERIT:
3065     case R_ARM_GNU_VTENTRY:
3066       return bfd_reloc_ok;
3067
3068     case R_ARM_COPY:
3069       return bfd_reloc_notsupported;
3070
3071     case R_ARM_GLOB_DAT:
3072       return bfd_reloc_notsupported;
3073
3074     case R_ARM_JUMP_SLOT:
3075       return bfd_reloc_notsupported;
3076
3077     case R_ARM_RELATIVE:
3078       return bfd_reloc_notsupported;
3079
3080     case R_ARM_GOTOFF:
3081       /* Relocation is relative to the start of the
3082          global offset table.  */
3083
3084       BFD_ASSERT (sgot != NULL);
3085       if (sgot == NULL)
3086         return bfd_reloc_notsupported;
3087
3088       /* If we are addressing a Thumb function, we need to adjust the
3089          address by one, so that attempts to call the function pointer will
3090          correctly interpret it as Thumb code.  */
3091       if (sym_flags == STT_ARM_TFUNC)
3092         value += 1;
3093
3094       /* Note that sgot->output_offset is not involved in this
3095          calculation.  We always want the start of .got.  If we
3096          define _GLOBAL_OFFSET_TABLE in a different way, as is
3097          permitted by the ABI, we might have to change this
3098          calculation.  */
3099       value -= sgot->output_section->vma;
3100       return _bfd_final_link_relocate (howto, input_bfd, input_section,
3101                                        contents, rel->r_offset, value,
3102                                        (bfd_vma) 0);
3103
3104     case R_ARM_GOTPC:
3105       /* Use global offset table as symbol value.  */
3106       BFD_ASSERT (sgot != NULL);
3107
3108       if (sgot == NULL)
3109         return bfd_reloc_notsupported;
3110
3111       *unresolved_reloc_p = FALSE;
3112       value = sgot->output_section->vma;
3113       return _bfd_final_link_relocate (howto, input_bfd, input_section,
3114                                        contents, rel->r_offset, value,
3115                                        (bfd_vma) 0);
3116
3117     case R_ARM_GOT32:
3118 #ifndef OLD_ARM_ABI
3119     case R_ARM_GOT_PREL:
3120 #endif
3121       /* Relocation is to the entry for this symbol in the
3122          global offset table.  */
3123       if (sgot == NULL)
3124         return bfd_reloc_notsupported;
3125
3126       if (h != NULL)
3127         {
3128           bfd_vma off;
3129           bfd_boolean dyn;
3130
3131           off = h->got.offset;
3132           BFD_ASSERT (off != (bfd_vma) -1);
3133           dyn = globals->root.dynamic_sections_created;
3134
3135           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3136               || (info->shared
3137                   && SYMBOL_REFERENCES_LOCAL (info, h))
3138               || (ELF_ST_VISIBILITY (h->other)
3139                   && h->root.type == bfd_link_hash_undefweak))
3140             {
3141               /* This is actually a static link, or it is a -Bsymbolic link
3142                  and the symbol is defined locally.  We must initialize this
3143                  entry in the global offset table.  Since the offset must
3144                  always be a multiple of 4, we use the least significant bit
3145                  to record whether we have initialized it already.
3146
3147                  When doing a dynamic link, we create a .rel.got relocation
3148                  entry to initialize the value.  This is done in the
3149                  finish_dynamic_symbol routine.  */
3150               if ((off & 1) != 0)
3151                 off &= ~1;
3152               else
3153                 {
3154                   /* If we are addressing a Thumb function, we need to
3155                      adjust the address by one, so that attempts to
3156                      call the function pointer will correctly
3157                      interpret it as Thumb code.  */
3158                   if (sym_flags == STT_ARM_TFUNC)
3159                     value |= 1;
3160
3161                   bfd_put_32 (output_bfd, value, sgot->contents + off);
3162                   h->got.offset |= 1;
3163                 }
3164             }
3165           else
3166             *unresolved_reloc_p = FALSE;
3167
3168           value = sgot->output_offset + off;
3169         }
3170       else
3171         {
3172           bfd_vma off;
3173
3174           BFD_ASSERT (local_got_offsets != NULL &&
3175                       local_got_offsets[r_symndx] != (bfd_vma) -1);
3176
3177           off = local_got_offsets[r_symndx];
3178
3179           /* The offset must always be a multiple of 4.  We use the
3180              least significant bit to record whether we have already
3181              generated the necessary reloc.  */
3182           if ((off & 1) != 0)
3183             off &= ~1;
3184           else
3185             {
3186               /* If we are addressing a Thumb function, we need to
3187                  adjust the address by one, so that attempts to
3188                  call the function pointer will correctly
3189                  interpret it as Thumb code.  */
3190               if (sym_flags == STT_ARM_TFUNC)
3191                 value |= 1;
3192
3193               bfd_put_32 (output_bfd, value, sgot->contents + off);
3194
3195               if (info->shared)
3196                 {
3197                   asection * srelgot;
3198                   Elf_Internal_Rela outrel;
3199                   bfd_byte *loc;
3200
3201                   srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
3202                   BFD_ASSERT (srelgot != NULL);
3203
3204                   outrel.r_offset = (sgot->output_section->vma
3205                                      + sgot->output_offset
3206                                      + off);
3207                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
3208                   loc = srelgot->contents;
3209                   loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3210                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3211                 }
3212
3213               local_got_offsets[r_symndx] |= 1;
3214             }
3215
3216           value = sgot->output_offset + off;
3217         }
3218       if (r_type != R_ARM_GOT32)
3219         value += sgot->output_section->vma;
3220
3221       return _bfd_final_link_relocate (howto, input_bfd, input_section,
3222                                        contents, rel->r_offset, value,
3223                                        (bfd_vma) 0);
3224
3225     case R_ARM_TLS_LDO32:
3226       value = value - dtpoff_base (info);
3227
3228       return _bfd_final_link_relocate (howto, input_bfd, input_section,
3229                                        contents, rel->r_offset, value, (bfd_vma) 0);
3230
3231     case R_ARM_TLS_LDM32:
3232       {
3233         bfd_vma off;
3234
3235         if (globals->sgot == NULL)
3236           abort ();
3237
3238         off = globals->tls_ldm_got.offset;
3239
3240         if ((off & 1) != 0)
3241           off &= ~1;
3242         else
3243           {
3244             /* If we don't know the module number, create a relocation
3245                for it.  */
3246             if (info->shared)
3247               {
3248                 Elf_Internal_Rela outrel;
3249                 bfd_byte *loc;
3250
3251                 if (globals->srelgot == NULL)
3252                   abort ();
3253
3254                 outrel.r_offset = (globals->sgot->output_section->vma
3255                                    + globals->sgot->output_offset + off);
3256                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
3257
3258                 bfd_put_32 (output_bfd, 0, globals->sgot->contents + off);
3259
3260                 loc = globals->srelgot->contents;
3261                 loc += globals->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3262                 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3263               }
3264             else
3265               bfd_put_32 (output_bfd, 1, globals->sgot->contents + off);
3266
3267             globals->tls_ldm_got.offset |= 1;
3268           }
3269
3270         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off 
3271           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
3272
3273         return _bfd_final_link_relocate (howto, input_bfd, input_section,
3274                                          contents, rel->r_offset, value,
3275                                          (bfd_vma) 0);
3276       }
3277
3278     case R_ARM_TLS_GD32:
3279     case R_ARM_TLS_IE32:
3280       {
3281         bfd_vma off;
3282         int indx;
3283         char tls_type;
3284
3285         if (globals->sgot == NULL)
3286           abort ();
3287
3288         indx = 0;
3289         if (h != NULL)
3290           {
3291             bfd_boolean dyn;
3292             dyn = globals->root.dynamic_sections_created;
3293             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3294                 && (!info->shared
3295                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
3296               {
3297                 *unresolved_reloc_p = FALSE;
3298                 indx = h->dynindx;
3299               }
3300             off = h->got.offset;
3301             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
3302           }
3303         else
3304           {
3305             if (local_got_offsets == NULL)
3306               abort ();
3307             off = local_got_offsets[r_symndx];
3308             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
3309           }
3310
3311         if (tls_type == GOT_UNKNOWN)
3312           abort ();
3313
3314         if ((off & 1) != 0)
3315           off &= ~1;
3316         else
3317           {
3318             bfd_boolean need_relocs = FALSE;
3319             Elf_Internal_Rela outrel;
3320             bfd_byte *loc = NULL;
3321             int cur_off = off;
3322
3323             /* The GOT entries have not been initialized yet.  Do it
3324                now, and emit any relocations.  If both an IE GOT and a
3325                GD GOT are necessary, we emit the GD first.  */
3326
3327             if ((info->shared || indx != 0)
3328                 && (h == NULL
3329                     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3330                     || h->root.type != bfd_link_hash_undefweak))
3331               {
3332                 need_relocs = TRUE;
3333                 if (globals->srelgot == NULL)
3334                   abort ();
3335                 loc = globals->srelgot->contents;
3336                 loc += globals->srelgot->reloc_count * sizeof (Elf32_External_Rel);
3337               }
3338
3339             if (tls_type & GOT_TLS_GD)
3340               {
3341                 if (need_relocs)
3342                   {
3343                     outrel.r_offset = (globals->sgot->output_section->vma
3344                                        + globals->sgot->output_offset + cur_off);
3345                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
3346                     bfd_put_32 (output_bfd, 0, globals->sgot->contents + cur_off);
3347
3348                     bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3349                     globals->srelgot->reloc_count++;
3350                     loc += sizeof (Elf32_External_Rel);
3351
3352                     if (indx == 0)
3353                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
3354                                   globals->sgot->contents + cur_off + 4);
3355                     else
3356                       {
3357                         bfd_put_32 (output_bfd, 0,
3358                                     globals->sgot->contents + cur_off + 4);
3359
3360                         outrel.r_info = ELF32_R_INFO (indx,
3361                                                       R_ARM_TLS_DTPOFF32);
3362                         outrel.r_offset += 4;
3363                         bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3364                         globals->srelgot->reloc_count++;
3365                         loc += sizeof (Elf32_External_Rel);
3366                       }
3367                   }
3368                 else
3369                   {
3370                     /* If we are not emitting relocations for a
3371                        general dynamic reference, then we must be in a
3372                        static link or an executable link with the
3373                        symbol binding locally.  Mark it as belonging
3374                        to module 1, the executable.  */
3375                     bfd_put_32 (output_bfd, 1,
3376                                 globals->sgot->contents + cur_off);
3377                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
3378                                 globals->sgot->contents + cur_off + 4);
3379                   }
3380
3381                 cur_off += 8;
3382               }
3383
3384             if (tls_type & GOT_TLS_IE)
3385               {
3386                 if (need_relocs)
3387                   {
3388                     outrel.r_offset = (globals->sgot->output_section->vma
3389                                        + globals->sgot->output_offset
3390                                        + cur_off);
3391                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
3392
3393                     if (indx == 0)
3394                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
3395                                   globals->sgot->contents + cur_off);
3396                     else
3397                       bfd_put_32 (output_bfd, 0,
3398                                   globals->sgot->contents + cur_off);
3399
3400                     bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3401                     globals->srelgot->reloc_count++;
3402                     loc += sizeof (Elf32_External_Rel);
3403                   }
3404                 else
3405                   bfd_put_32 (output_bfd, tpoff (info, value),
3406                               globals->sgot->contents + cur_off);
3407                 cur_off += 4;
3408               }
3409
3410             if (h != NULL)
3411               h->got.offset |= 1;
3412             else
3413               local_got_offsets[r_symndx] |= 1;
3414           }
3415
3416         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
3417           off += 8;
3418         value = globals->sgot->output_section->vma + globals->sgot->output_offset + off 
3419           - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
3420
3421         return _bfd_final_link_relocate (howto, input_bfd, input_section,
3422                                          contents, rel->r_offset, value,
3423                                          (bfd_vma) 0);
3424       }
3425
3426     case R_ARM_TLS_LE32:
3427       if (info->shared)
3428         {
3429           (*_bfd_error_handler)
3430             (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
3431              input_bfd, input_section,
3432              (long) rel->r_offset, howto->name);
3433           return FALSE;   
3434         }
3435       else
3436         value = tpoff (info, value);
3437       
3438       return _bfd_final_link_relocate (howto, input_bfd, input_section,
3439                                        contents, rel->r_offset, value, (bfd_vma) 0);
3440
3441     case R_ARM_SBREL32:
3442       return bfd_reloc_notsupported;
3443
3444     case R_ARM_AMP_VCALL9:
3445       return bfd_reloc_notsupported;
3446
3447     case R_ARM_RSBREL32:
3448       return bfd_reloc_notsupported;
3449
3450     case R_ARM_THM_RPC22:
3451       return bfd_reloc_notsupported;
3452
3453     case R_ARM_RREL32:
3454       return bfd_reloc_notsupported;
3455
3456     case R_ARM_RABS32:
3457       return bfd_reloc_notsupported;
3458
3459     case R_ARM_RPC24:
3460       return bfd_reloc_notsupported;
3461
3462     case R_ARM_RBASE:
3463       return bfd_reloc_notsupported;
3464
3465     case R_ARM_V4BX:
3466       if (globals->fix_v4bx)
3467         {
3468           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
3469
3470           /* Ensure that we have a BX instruction.  */
3471           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
3472
3473           /* Preserve Rm (lowest four bits) and the condition code
3474              (highest four bits). Other bits encode MOV PC,Rm.  */
3475           insn = (insn & 0xf000000f) | 0x01a0f000;
3476
3477           bfd_put_32 (input_bfd, insn, hit_data);
3478         }
3479       return bfd_reloc_ok;
3480
3481     default:
3482       return bfd_reloc_notsupported;
3483     }
3484 }
3485
3486 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
3487 static void
3488 arm_add_to_rel (bfd *              abfd,
3489                 bfd_byte *         address,
3490                 reloc_howto_type * howto,
3491                 bfd_signed_vma     increment)
3492 {
3493   bfd_signed_vma addend;
3494
3495   if (howto->type == R_ARM_THM_PC22)
3496     {
3497       int upper_insn, lower_insn;
3498       int upper, lower;
3499
3500       upper_insn = bfd_get_16 (abfd, address);
3501       lower_insn = bfd_get_16 (abfd, address + 2);
3502       upper = upper_insn & 0x7ff;
3503       lower = lower_insn & 0x7ff;
3504
3505       addend = (upper << 12) | (lower << 1);
3506       addend += increment;
3507       addend >>= 1;
3508
3509       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
3510       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
3511
3512       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
3513       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
3514     }
3515   else
3516     {
3517       bfd_vma        contents;
3518
3519       contents = bfd_get_32 (abfd, address);
3520
3521       /* Get the (signed) value from the instruction.  */
3522       addend = contents & howto->src_mask;
3523       if (addend & ((howto->src_mask + 1) >> 1))
3524         {
3525           bfd_signed_vma mask;
3526
3527           mask = -1;
3528           mask &= ~ howto->src_mask;
3529           addend |= mask;
3530         }
3531
3532       /* Add in the increment, (which is a byte value).  */
3533       switch (howto->type)
3534         {
3535         default:
3536           addend += increment;
3537           break;
3538
3539         case R_ARM_PC24:
3540 #ifndef OLD_ARM_ABI
3541         case R_ARM_CALL:
3542         case R_ARM_JUMP24:
3543 #endif
3544           addend <<= howto->size;
3545           addend += increment;
3546
3547           /* Should we check for overflow here ?  */
3548
3549           /* Drop any undesired bits.  */
3550           addend >>= howto->rightshift;
3551           break;
3552         }
3553
3554       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
3555
3556       bfd_put_32 (abfd, contents, address);
3557     }
3558 }
3559
3560 #define IS_ARM_TLS_RELOC(R_TYPE)        \
3561   ((R_TYPE) == R_ARM_TLS_GD32           \
3562    || (R_TYPE) == R_ARM_TLS_LDO32       \
3563    || (R_TYPE) == R_ARM_TLS_LDM32       \
3564    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
3565    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
3566    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
3567    || (R_TYPE) == R_ARM_TLS_LE32        \
3568    || (R_TYPE) == R_ARM_TLS_IE32)
3569
3570 /* Relocate an ARM ELF section.  */
3571 static bfd_boolean
3572 elf32_arm_relocate_section (bfd *                  output_bfd,
3573                             struct bfd_link_info * info,
3574                             bfd *                  input_bfd,
3575                             asection *             input_section,
3576                             bfd_byte *             contents,
3577                             Elf_Internal_Rela *    relocs,
3578                             Elf_Internal_Sym *     local_syms,
3579                             asection **            local_sections)
3580 {
3581   Elf_Internal_Shdr *symtab_hdr;
3582   struct elf_link_hash_entry **sym_hashes;
3583   Elf_Internal_Rela *rel;
3584   Elf_Internal_Rela *relend;
3585   const char *name;
3586   struct elf32_arm_link_hash_table * globals;
3587
3588   globals = elf32_arm_hash_table (info);
3589   if (info->relocatable && !globals->use_rel)
3590     return TRUE;
3591
3592   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
3593   sym_hashes = elf_sym_hashes (input_bfd);
3594
3595   rel = relocs;
3596   relend = relocs + input_section->reloc_count;
3597   for (; rel < relend; rel++)
3598     {
3599       int                          r_type;
3600       reloc_howto_type *           howto;
3601       unsigned long                r_symndx;
3602       Elf_Internal_Sym *           sym;
3603       asection *                   sec;
3604       struct elf_link_hash_entry * h;
3605       bfd_vma                      relocation;
3606       bfd_reloc_status_type        r;
3607       arelent                      bfd_reloc;
3608       char                         sym_type;
3609       bfd_boolean                  unresolved_reloc = FALSE;
3610
3611       r_symndx = ELF32_R_SYM (rel->r_info);
3612       r_type   = ELF32_R_TYPE (rel->r_info);
3613       r_type   = arm_real_reloc_type (globals, r_type);
3614
3615       if (   r_type == R_ARM_GNU_VTENTRY
3616           || r_type == R_ARM_GNU_VTINHERIT)
3617         continue;
3618
3619       bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
3620       howto = bfd_reloc.howto;
3621
3622       if (info->relocatable && globals->use_rel)
3623         {
3624           /* This is a relocatable link.  We don't have to change
3625              anything, unless the reloc is against a section symbol,
3626              in which case we have to adjust according to where the
3627              section symbol winds up in the output section.  */
3628           if (r_symndx < symtab_hdr->sh_info)
3629             {
3630               sym = local_syms + r_symndx;
3631               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3632                 {
3633                   sec = local_sections[r_symndx];
3634                   arm_add_to_rel (input_bfd, contents + rel->r_offset,
3635                                   howto,
3636                                   (bfd_signed_vma) (sec->output_offset
3637                                                     + sym->st_value));
3638                 }
3639             }
3640
3641           continue;
3642         }
3643
3644       /* This is a final link.  */
3645       h = NULL;
3646       sym = NULL;
3647       sec = NULL;
3648
3649       if (r_symndx < symtab_hdr->sh_info)
3650         {
3651           sym = local_syms + r_symndx;
3652           sym_type = ELF32_ST_TYPE (sym->st_info);
3653           sec = local_sections[r_symndx];
3654           if (globals->use_rel)
3655             {
3656               relocation = (sec->output_section->vma
3657                             + sec->output_offset
3658                             + sym->st_value);
3659               if ((sec->flags & SEC_MERGE)
3660                        && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3661                 {
3662                   asection *msec;
3663                   bfd_vma addend, value;
3664
3665                   if (howto->rightshift)
3666                     {
3667                       (*_bfd_error_handler)
3668                         (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3669                          input_bfd, input_section,
3670                          (long) rel->r_offset, howto->name);
3671                       return FALSE;
3672                     }
3673
3674                   value = bfd_get_32 (input_bfd, contents + rel->r_offset);
3675
3676                   /* Get the (signed) value from the instruction.  */
3677                   addend = value & howto->src_mask;
3678                   if (addend & ((howto->src_mask + 1) >> 1))
3679                     {
3680                       bfd_signed_vma mask;
3681
3682                       mask = -1;
3683                       mask &= ~ howto->src_mask;
3684                       addend |= mask;
3685                     }
3686                   msec = sec;
3687                   addend =
3688                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3689                     - relocation;
3690                   addend += msec->output_section->vma + msec->output_offset;
3691                   value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
3692                   bfd_put_32 (input_bfd, value, contents + rel->r_offset);
3693                 }
3694             }
3695           else
3696             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3697         }
3698       else
3699         {
3700           bfd_boolean warned;
3701
3702           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3703                                    r_symndx, symtab_hdr, sym_hashes,
3704                                    h, sec, relocation,
3705                                    unresolved_reloc, warned);
3706
3707           sym_type = h->type;
3708         }
3709
3710       if (h != NULL)
3711         name = h->root.root.string;
3712       else
3713         {
3714           name = (bfd_elf_string_from_elf_section
3715                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
3716           if (name == NULL || *name == '\0')
3717             name = bfd_section_name (input_bfd, sec);
3718         }
3719
3720       if (r_symndx != 0
3721           && r_type != R_ARM_NONE
3722           && (h == NULL
3723               || h->root.type == bfd_link_hash_defined
3724               || h->root.type == bfd_link_hash_defweak)
3725           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
3726         {
3727           (*_bfd_error_handler)
3728             ((sym_type == STT_TLS
3729               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
3730               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
3731              input_bfd,
3732              input_section,
3733              (long) rel->r_offset,
3734              howto->name,
3735              name);
3736         }
3737
3738       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
3739                                          input_section, contents, rel,
3740                                          relocation, info, sec, name,
3741                                          (h ? ELF_ST_TYPE (h->type) :
3742                                           ELF_ST_TYPE (sym->st_info)), h,
3743                                          &unresolved_reloc);
3744
3745       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3746          because such sections are not SEC_ALLOC and thus ld.so will
3747          not process them.  */
3748       if (unresolved_reloc
3749           && !((input_section->flags & SEC_DEBUGGING) != 0
3750                && h->def_dynamic))
3751         {
3752           (*_bfd_error_handler)
3753             (_("%B(%A+0x%lx): warning: unresolvable relocation %d against symbol `%s'"),
3754              input_bfd, input_section, (long) rel->r_offset,
3755              r_type, h->root.root.string);
3756           return FALSE;
3757         }
3758
3759       if (r != bfd_reloc_ok)
3760         {
3761           const char * msg = (const char *) 0;
3762
3763           switch (r)
3764             {
3765             case bfd_reloc_overflow:
3766               /* If the overflowing reloc was to an undefined symbol,
3767                  we have already printed one error message and there
3768                  is no point complaining again.  */
3769               if ((! h ||
3770                    h->root.type != bfd_link_hash_undefined)
3771                   && (!((*info->callbacks->reloc_overflow)
3772                         (info, (h ? &h->root : NULL), name, howto->name,
3773                          (bfd_vma) 0, input_bfd, input_section,
3774                          rel->r_offset))))
3775                   return FALSE;
3776               break;
3777
3778             case bfd_reloc_undefined:
3779               if (!((*info->callbacks->undefined_symbol)
3780                     (info, name, input_bfd, input_section,
3781                      rel->r_offset, TRUE)))
3782                 return FALSE;
3783               break;
3784
3785             case bfd_reloc_outofrange:
3786               msg = _("internal error: out of range error");
3787               goto common_error;
3788
3789             case bfd_reloc_notsupported:
3790               msg = _("internal error: unsupported relocation error");
3791               goto common_error;
3792
3793             case bfd_reloc_dangerous:
3794               msg = _("internal error: dangerous error");
3795               goto common_error;
3796
3797             default:
3798               msg = _("internal error: unknown error");
3799               /* fall through */
3800
3801             common_error:
3802               if (!((*info->callbacks->warning)
3803                     (info, msg, name, input_bfd, input_section,
3804                      rel->r_offset)))
3805                 return FALSE;
3806               break;
3807             }
3808         }
3809     }
3810
3811   return TRUE;
3812 }
3813
3814 /* Set the right machine number.  */
3815
3816 static bfd_boolean
3817 elf32_arm_object_p (bfd *abfd)
3818 {
3819   unsigned int mach;
3820
3821   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
3822
3823   if (mach != bfd_mach_arm_unknown)
3824     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
3825
3826   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
3827     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
3828
3829   else
3830     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
3831
3832   return TRUE;
3833 }
3834
3835 /* Function to keep ARM specific flags in the ELF header.  */
3836
3837 static bfd_boolean
3838 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
3839 {
3840   if (elf_flags_init (abfd)
3841       && elf_elfheader (abfd)->e_flags != flags)
3842     {
3843       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
3844         {
3845           if (flags & EF_ARM_INTERWORK)
3846             (*_bfd_error_handler)
3847               (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
3848                abfd);
3849           else
3850             _bfd_error_handler
3851               (_("Warning: Clearing the interworking flag of %B due to outside request"),
3852                abfd);
3853         }
3854     }
3855   else
3856     {
3857       elf_elfheader (abfd)->e_flags = flags;
3858       elf_flags_init (abfd) = TRUE;
3859     }
3860
3861   return TRUE;
3862 }
3863
3864 /* Copy backend specific data from one object module to another.  */
3865
3866 static bfd_boolean
3867 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
3868 {
3869   flagword in_flags;
3870   flagword out_flags;
3871
3872   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3873       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3874     return TRUE;
3875
3876   in_flags  = elf_elfheader (ibfd)->e_flags;
3877   out_flags = elf_elfheader (obfd)->e_flags;
3878
3879   if (elf_flags_init (obfd)
3880       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
3881       && in_flags != out_flags)
3882     {
3883       /* Cannot mix APCS26 and APCS32 code.  */
3884       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
3885         return FALSE;
3886
3887       /* Cannot mix float APCS and non-float APCS code.  */
3888       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
3889         return FALSE;
3890
3891       /* If the src and dest have different interworking flags
3892          then turn off the interworking bit.  */
3893       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
3894         {
3895           if (out_flags & EF_ARM_INTERWORK)
3896             _bfd_error_handler
3897               (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
3898                obfd, ibfd);
3899
3900           in_flags &= ~EF_ARM_INTERWORK;
3901         }
3902
3903       /* Likewise for PIC, though don't warn for this case.  */
3904       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
3905         in_flags &= ~EF_ARM_PIC;
3906     }
3907
3908   elf_elfheader (obfd)->e_flags = in_flags;
3909   elf_flags_init (obfd) = TRUE;
3910
3911   /* Also copy the EI_OSABI field.  */
3912   elf_elfheader (obfd)->e_ident[EI_OSABI] =
3913     elf_elfheader (ibfd)->e_ident[EI_OSABI];
3914
3915   return TRUE;
3916 }
3917
3918 /* Merge backend specific data from an object file to the output
3919    object file when linking.  */
3920
3921 static bfd_boolean
3922 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
3923 {
3924   flagword out_flags;
3925   flagword in_flags;
3926   bfd_boolean flags_compatible = TRUE;
3927   asection *sec;
3928
3929   /* Check if we have the same endianess.  */
3930   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3931     return FALSE;
3932
3933   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3934       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3935     return TRUE;
3936
3937   /* The input BFD must have had its flags initialised.  */
3938   /* The following seems bogus to me -- The flags are initialized in
3939      the assembler but I don't think an elf_flags_init field is
3940      written into the object.  */
3941   /* BFD_ASSERT (elf_flags_init (ibfd)); */
3942
3943   in_flags  = elf_elfheader (ibfd)->e_flags;
3944   out_flags = elf_elfheader (obfd)->e_flags;
3945
3946   if (!elf_flags_init (obfd))
3947     {
3948       /* If the input is the default architecture and had the default
3949          flags then do not bother setting the flags for the output
3950          architecture, instead allow future merges to do this.  If no
3951          future merges ever set these flags then they will retain their
3952          uninitialised values, which surprise surprise, correspond
3953          to the default values.  */
3954       if (bfd_get_arch_info (ibfd)->the_default
3955           && elf_elfheader (ibfd)->e_flags == 0)
3956         return TRUE;
3957
3958       elf_flags_init (obfd) = TRUE;
3959       elf_elfheader (obfd)->e_flags = in_flags;
3960
3961       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3962           && bfd_get_arch_info (obfd)->the_default)
3963         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3964
3965       return TRUE;
3966     }
3967
3968   /* Determine what should happen if the input ARM architecture
3969      does not match the output ARM architecture.  */
3970   if (! bfd_arm_merge_machines (ibfd, obfd))
3971     return FALSE;
3972
3973   /* Identical flags must be compatible.  */
3974   if (in_flags == out_flags)
3975     return TRUE;
3976
3977   /* Check to see if the input BFD actually contains any sections.  If
3978      not, its flags may not have been initialised either, but it
3979      cannot actually cause any incompatibility.  Do not short-circuit
3980      dynamic objects; their section list may be emptied by
3981     elf_link_add_object_symbols.
3982
3983     Also check to see if there are no code sections in the input.
3984     In this case there is no need to check for code specific flags.
3985     XXX - do we need to worry about floating-point format compatability
3986     in data sections ?  */
3987   if (!(ibfd->flags & DYNAMIC))
3988     {
3989       bfd_boolean null_input_bfd = TRUE;
3990       bfd_boolean only_data_sections = TRUE;
3991
3992       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3993         {
3994           /* Ignore synthetic glue sections.  */
3995           if (strcmp (sec->name, ".glue_7")
3996               && strcmp (sec->name, ".glue_7t"))
3997             {
3998               if ((bfd_get_section_flags (ibfd, sec)
3999                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4000                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4001                 only_data_sections = FALSE;
4002
4003               null_input_bfd = FALSE;
4004               break;
4005             }
4006         }
4007
4008       if (null_input_bfd || only_data_sections)
4009         return TRUE;
4010     }
4011
4012   /* Complain about various flag mismatches.  */
4013   if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
4014     {
4015       _bfd_error_handler
4016         (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
4017          ibfd, obfd,
4018          (in_flags & EF_ARM_EABIMASK) >> 24,
4019          (out_flags & EF_ARM_EABIMASK) >> 24);
4020       return FALSE;
4021     }
4022
4023   /* Not sure what needs to be checked for EABI versions >= 1.  */
4024   if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
4025     {
4026       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
4027         {
4028           _bfd_error_handler
4029             (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
4030              ibfd, obfd,
4031              in_flags & EF_ARM_APCS_26 ? 26 : 32,
4032              out_flags & EF_ARM_APCS_26 ? 26 : 32);
4033           flags_compatible = FALSE;
4034         }
4035
4036       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
4037         {
4038           if (in_flags & EF_ARM_APCS_FLOAT)
4039             _bfd_error_handler
4040               (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
4041                ibfd, obfd);
4042           else
4043             _bfd_error_handler
4044               (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
4045                ibfd, obfd);
4046
4047           flags_compatible = FALSE;
4048         }
4049
4050       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
4051         {
4052           if (in_flags & EF_ARM_VFP_FLOAT)
4053             _bfd_error_handler
4054               (_("ERROR: %B uses VFP instructions, whereas %B does not"),
4055                ibfd, obfd);
4056           else
4057             _bfd_error_handler
4058               (_("ERROR: %B uses FPA instructions, whereas %B does not"),
4059                ibfd, obfd);
4060
4061           flags_compatible = FALSE;
4062         }
4063
4064       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
4065         {
4066           if (in_flags & EF_ARM_MAVERICK_FLOAT)
4067             _bfd_error_handler
4068               (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
4069                ibfd, obfd);
4070           else
4071             _bfd_error_handler
4072               (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
4073                ibfd, obfd);
4074
4075           flags_compatible = FALSE;
4076         }
4077
4078 #ifdef EF_ARM_SOFT_FLOAT
4079       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
4080         {
4081           /* We can allow interworking between code that is VFP format
4082              layout, and uses either soft float or integer regs for
4083              passing floating point arguments and results.  We already
4084              know that the APCS_FLOAT flags match; similarly for VFP
4085              flags.  */
4086           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
4087               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
4088             {
4089               if (in_flags & EF_ARM_SOFT_FLOAT)
4090                 _bfd_error_handler
4091                   (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
4092                    ibfd, obfd);
4093               else
4094                 _bfd_error_handler
4095                   (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
4096                    ibfd, obfd);
4097
4098               flags_compatible = FALSE;
4099             }
4100         }
4101 #endif
4102
4103       /* Interworking mismatch is only a warning.  */
4104       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
4105         {
4106           if (in_flags & EF_ARM_INTERWORK)
4107             {
4108               _bfd_error_handler
4109                 (_("Warning: %B supports interworking, whereas %B does not"),
4110                  ibfd, obfd);
4111             }
4112           else
4113             {
4114               _bfd_error_handler
4115                 (_("Warning: %B does not support interworking, whereas %B does"),
4116                  ibfd, obfd);
4117             }
4118         }
4119     }
4120
4121   return flags_compatible;
4122 }
4123
4124 /* Display the flags field.  */
4125
4126 static bfd_boolean
4127 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
4128 {
4129   FILE * file = (FILE *) ptr;
4130   unsigned long flags;
4131
4132   BFD_ASSERT (abfd != NULL && ptr != NULL);
4133
4134   /* Print normal ELF private data.  */
4135   _bfd_elf_print_private_bfd_data (abfd, ptr);
4136
4137   flags = elf_elfheader (abfd)->e_flags;
4138   /* Ignore init flag - it may not be set, despite the flags field
4139      containing valid data.  */
4140
4141   /* xgettext:c-format */
4142   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4143
4144   switch (EF_ARM_EABI_VERSION (flags))
4145     {
4146     case EF_ARM_EABI_UNKNOWN:
4147       /* The following flag bits are GNU extensions and not part of the
4148          official ARM ELF extended ABI.  Hence they are only decoded if
4149          the EABI version is not set.  */
4150       if (flags & EF_ARM_INTERWORK)
4151         fprintf (file, _(" [interworking enabled]"));
4152
4153       if (flags & EF_ARM_APCS_26)
4154         fprintf (file, " [APCS-26]");
4155       else
4156         fprintf (file, " [APCS-32]");
4157
4158       if (flags & EF_ARM_VFP_FLOAT)
4159         fprintf (file, _(" [VFP float format]"));
4160       else if (flags & EF_ARM_MAVERICK_FLOAT)
4161         fprintf (file, _(" [Maverick float format]"));
4162       else
4163         fprintf (file, _(" [FPA float format]"));
4164
4165       if (flags & EF_ARM_APCS_FLOAT)
4166         fprintf (file, _(" [floats passed in float registers]"));
4167
4168       if (flags & EF_ARM_PIC)
4169         fprintf (file, _(" [position independent]"));
4170
4171       if (flags & EF_ARM_NEW_ABI)
4172         fprintf (file, _(" [new ABI]"));
4173
4174       if (flags & EF_ARM_OLD_ABI)
4175         fprintf (file, _(" [old ABI]"));
4176
4177       if (flags & EF_ARM_SOFT_FLOAT)
4178         fprintf (file, _(" [software FP]"));
4179
4180       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
4181                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
4182                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
4183                  | EF_ARM_MAVERICK_FLOAT);
4184       break;
4185
4186     case EF_ARM_EABI_VER1:
4187       fprintf (file, _(" [Version1 EABI]"));
4188
4189       if (flags & EF_ARM_SYMSARESORTED)
4190         fprintf (file, _(" [sorted symbol table]"));
4191       else
4192         fprintf (file, _(" [unsorted symbol table]"));
4193
4194       flags &= ~ EF_ARM_SYMSARESORTED;
4195       break;
4196
4197     case EF_ARM_EABI_VER2:
4198       fprintf (file, _(" [Version2 EABI]"));
4199
4200       if (flags & EF_ARM_SYMSARESORTED)
4201         fprintf (file, _(" [sorted symbol table]"));
4202       else
4203         fprintf (file, _(" [unsorted symbol table]"));
4204
4205       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
4206         fprintf (file, _(" [dynamic symbols use segment index]"));
4207
4208       if (flags & EF_ARM_MAPSYMSFIRST)
4209         fprintf (file, _(" [mapping symbols precede others]"));
4210
4211       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
4212                  | EF_ARM_MAPSYMSFIRST);
4213       break;
4214
4215     case EF_ARM_EABI_VER3:
4216       fprintf (file, _(" [Version3 EABI]"));
4217       break;
4218
4219     case EF_ARM_EABI_VER4:
4220       fprintf (file, _(" [Version4 EABI]"));
4221
4222       if (flags & EF_ARM_BE8)
4223         fprintf (file, _(" [BE8]"));
4224
4225       if (flags & EF_ARM_LE8)
4226         fprintf (file, _(" [LE8]"));
4227
4228       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
4229       break;
4230
4231     default:
4232       fprintf (file, _(" <EABI version unrecognised>"));
4233       break;
4234     }
4235
4236   flags &= ~ EF_ARM_EABIMASK;
4237
4238   if (flags & EF_ARM_RELEXEC)
4239     fprintf (file, _(" [relocatable executable]"));
4240
4241   if (flags & EF_ARM_HASENTRY)
4242     fprintf (file, _(" [has entry point]"));
4243
4244   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
4245
4246   if (flags)
4247     fprintf (file, _("<Unrecognised flag bits set>"));
4248
4249   fputc ('\n', file);
4250
4251   return TRUE;
4252 }
4253
4254 static int
4255 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
4256 {
4257   switch (ELF_ST_TYPE (elf_sym->st_info))
4258     {
4259     case STT_ARM_TFUNC:
4260       return ELF_ST_TYPE (elf_sym->st_info);
4261
4262     case STT_ARM_16BIT:
4263       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
4264          This allows us to distinguish between data used by Thumb instructions
4265          and non-data (which is probably code) inside Thumb regions of an
4266          executable.  */
4267       if (type != STT_OBJECT)
4268         return ELF_ST_TYPE (elf_sym->st_info);
4269       break;
4270
4271     default:
4272       break;
4273     }
4274
4275   return type;
4276 }
4277
4278 static asection *
4279 elf32_arm_gc_mark_hook (asection *                   sec,
4280                         struct bfd_link_info *       info ATTRIBUTE_UNUSED,
4281                         Elf_Internal_Rela *          rel,
4282                         struct elf_link_hash_entry * h,
4283                         Elf_Internal_Sym *           sym)
4284 {
4285   if (h != NULL)
4286     {
4287       switch (ELF32_R_TYPE (rel->r_info))
4288       {
4289       case R_ARM_GNU_VTINHERIT:
4290       case R_ARM_GNU_VTENTRY:
4291         break;
4292
4293       default:
4294         switch (h->root.type)
4295           {
4296           case bfd_link_hash_defined:
4297           case bfd_link_hash_defweak:
4298             return h->root.u.def.section;
4299
4300           case bfd_link_hash_common:
4301             return h->root.u.c.p->section;
4302
4303           default:
4304             break;
4305           }
4306        }
4307      }
4308    else
4309      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4310
4311   return NULL;
4312 }
4313
4314 /* Update the got entry reference counts for the section being removed.  */
4315
4316 static bfd_boolean
4317 elf32_arm_gc_sweep_hook (bfd *                     abfd,
4318                          struct bfd_link_info *    info,
4319                          asection *                sec,
4320                          const Elf_Internal_Rela * relocs)
4321 {
4322   Elf_Internal_Shdr *symtab_hdr;
4323   struct elf_link_hash_entry **sym_hashes;
4324   bfd_signed_vma *local_got_refcounts;
4325   const Elf_Internal_Rela *rel, *relend;
4326   struct elf32_arm_link_hash_table * globals;
4327
4328   globals = elf32_arm_hash_table (info);
4329
4330   elf_section_data (sec)->local_dynrel = NULL;
4331
4332   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4333   sym_hashes = elf_sym_hashes (abfd);
4334   local_got_refcounts = elf_local_got_refcounts (abfd);
4335
4336   relend = relocs + sec->reloc_count;
4337   for (rel = relocs; rel < relend; rel++)
4338     {
4339       unsigned long r_symndx;
4340       struct elf_link_hash_entry *h = NULL;
4341       int r_type;
4342
4343       r_symndx = ELF32_R_SYM (rel->r_info);
4344       if (r_symndx >= symtab_hdr->sh_info)
4345         {
4346           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4347           while (h->root.type == bfd_link_hash_indirect
4348                  || h->root.type == bfd_link_hash_warning)
4349             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4350         }
4351
4352       r_type = ELF32_R_TYPE (rel->r_info);
4353 #ifndef OLD_ARM_ABI
4354       r_type = arm_real_reloc_type (globals, r_type);
4355 #endif
4356       switch (r_type)
4357         {
4358         case R_ARM_GOT32:
4359 #ifndef OLD_ARM_ABI
4360         case R_ARM_GOT_PREL:
4361 #endif
4362         case R_ARM_TLS_GD32:
4363         case R_ARM_TLS_IE32:
4364           if (h != NULL)
4365             {
4366               if (h->got.refcount > 0)
4367                 h->got.refcount -= 1;
4368             }
4369           else if (local_got_refcounts != NULL)
4370             {
4371               if (local_got_refcounts[r_symndx] > 0)
4372                 local_got_refcounts[r_symndx] -= 1;
4373             }
4374           break;
4375
4376         case R_ARM_TLS_LDM32:
4377           elf32_arm_hash_table (info)->tls_ldm_got.refcount -= 1;
4378           break;
4379
4380         case R_ARM_ABS32:
4381         case R_ARM_REL32:
4382         case R_ARM_PC24:
4383         case R_ARM_PLT32:
4384 #ifndef OLD_ARM_ABI
4385         case R_ARM_CALL:
4386         case R_ARM_JUMP24:
4387         case R_ARM_PREL31:
4388 #endif
4389         case R_ARM_THM_PC22:
4390           /* Should the interworking branches be here also?  */
4391
4392           if (h != NULL)
4393             {
4394               struct elf32_arm_link_hash_entry *eh;
4395               struct elf32_arm_relocs_copied **pp;
4396               struct elf32_arm_relocs_copied *p;
4397
4398               eh = (struct elf32_arm_link_hash_entry *) h;
4399
4400               if (h->plt.refcount > 0)
4401                 {
4402                   h->plt.refcount -= 1;
4403                   if (ELF32_R_TYPE (rel->r_info) == R_ARM_THM_PC22)
4404                     eh->plt_thumb_refcount--;
4405                 }
4406
4407               if (r_type == R_ARM_ABS32
4408                   || r_type == R_ARM_REL32)
4409                 {
4410                   for (pp = &eh->relocs_copied; (p = *pp) != NULL;
4411                        pp = &p->next)
4412                   if (p->section == sec)
4413                     {
4414                       p->count -= 1;
4415                       if (ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
4416                         p->pc_count -= 1;
4417                       if (p->count == 0)
4418                         *pp = p->next;
4419                       break;
4420                     }
4421                 }
4422             }
4423           break;
4424
4425         default:
4426           break;
4427         }
4428     }
4429
4430   return TRUE;
4431 }
4432
4433 /* Look through the relocs for a section during the first phase.  */
4434
4435 static bfd_boolean
4436 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
4437                         asection *sec, const Elf_Internal_Rela *relocs)
4438 {
4439   Elf_Internal_Shdr *symtab_hdr;
4440   struct elf_link_hash_entry **sym_hashes;
4441   struct elf_link_hash_entry **sym_hashes_end;
4442   const Elf_Internal_Rela *rel;
4443   const Elf_Internal_Rela *rel_end;
4444   bfd *dynobj;
4445   asection *sreloc;
4446   bfd_vma *local_got_offsets;
4447   struct elf32_arm_link_hash_table *htab;
4448
4449   if (info->relocatable)
4450     return TRUE;
4451
4452   htab = elf32_arm_hash_table (info);
4453   sreloc = NULL;
4454
4455   /* Create dynamic sections for relocatable executables so that we can
4456      copy relocations.  */
4457   if (htab->root.is_relocatable_executable
4458       && ! htab->root.dynamic_sections_created)
4459     {
4460       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4461         return FALSE;
4462     }
4463
4464   dynobj = elf_hash_table (info)->dynobj;
4465   local_got_offsets = elf_local_got_offsets (abfd);
4466
4467   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4468   sym_hashes = elf_sym_hashes (abfd);
4469   sym_hashes_end = sym_hashes
4470     + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4471
4472   if (!elf_bad_symtab (abfd))
4473     sym_hashes_end -= symtab_hdr->sh_info;
4474
4475   rel_end = relocs + sec->reloc_count;
4476   for (rel = relocs; rel < rel_end; rel++)
4477     {
4478       struct elf_link_hash_entry *h;
4479       struct elf32_arm_link_hash_entry *eh;
4480       unsigned long r_symndx;
4481       int r_type;
4482
4483       r_symndx = ELF32_R_SYM (rel->r_info);
4484       r_type = ELF32_R_TYPE (rel->r_info);
4485 #ifndef OLD_ARM_ABI
4486       r_type = arm_real_reloc_type (htab, r_type);
4487 #endif
4488
4489       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
4490         {
4491           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
4492                                  r_symndx);
4493           return FALSE;
4494         }
4495
4496       if (r_symndx < symtab_hdr->sh_info)
4497         h = NULL;
4498       else
4499         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4500
4501       eh = (struct elf32_arm_link_hash_entry *) h;
4502
4503       switch (r_type)
4504         {
4505           case R_ARM_GOT32:
4506 #ifndef OLD_ARM_ABI
4507           case R_ARM_GOT_PREL:
4508 #endif
4509           case R_ARM_TLS_GD32:
4510           case R_ARM_TLS_IE32:
4511             /* This symbol requires a global offset table entry.  */
4512             {
4513               int tls_type, old_tls_type;
4514
4515               switch (r_type)
4516                 {
4517                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
4518                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
4519                 default: tls_type = GOT_NORMAL; break;
4520                 }
4521
4522               if (h != NULL)
4523                 {
4524                   h->got.refcount++;
4525                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
4526                 }
4527               else
4528                 {
4529                   bfd_signed_vma *local_got_refcounts;
4530
4531                   /* This is a global offset table entry for a local symbol.  */
4532                   local_got_refcounts = elf_local_got_refcounts (abfd);
4533                   if (local_got_refcounts == NULL)
4534                     {
4535                       bfd_size_type size;
4536                       
4537                       size = symtab_hdr->sh_info;
4538                       size *= (sizeof (bfd_signed_vma) + sizeof(char));
4539                       local_got_refcounts = bfd_zalloc (abfd, size);
4540                       if (local_got_refcounts == NULL)
4541                         return FALSE;
4542                       elf_local_got_refcounts (abfd) = local_got_refcounts;
4543                       elf32_arm_local_got_tls_type (abfd)
4544                         = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4545                     }
4546                   local_got_refcounts[r_symndx] += 1;
4547                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
4548                 }
4549
4550               /* We will already have issued an error message if there is a
4551                  TLS / non-TLS mismatch, based on the symbol type.  We don't
4552                  support any linker relaxations.  So just combine any TLS
4553                  types needed.  */
4554               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4555                   && tls_type != GOT_NORMAL)
4556                 tls_type |= old_tls_type;
4557
4558               if (old_tls_type != tls_type)
4559                 {
4560                   if (h != NULL)
4561                     elf32_arm_hash_entry (h)->tls_type = tls_type;
4562                   else
4563                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
4564                 }
4565             }
4566             /* Fall through */
4567
4568           case R_ARM_TLS_LDM32:
4569             if (r_type == R_ARM_TLS_LDM32)
4570                 htab->tls_ldm_got.refcount++;
4571             /* Fall through */
4572
4573           case R_ARM_GOTOFF:
4574           case R_ARM_GOTPC:
4575             if (htab->sgot == NULL)
4576               {
4577                 if (htab->root.dynobj == NULL)
4578                   htab->root.dynobj = abfd;
4579                 if (!create_got_section (htab->root.dynobj, info))
4580                   return FALSE;
4581               }
4582             break;
4583
4584           case R_ARM_ABS32:
4585           case R_ARM_REL32:
4586           case R_ARM_PC24:
4587           case R_ARM_PLT32:
4588 #ifndef OLD_ARM_ABI
4589           case R_ARM_CALL:
4590           case R_ARM_JUMP24:
4591           case R_ARM_PREL31:
4592 #endif
4593           case R_ARM_THM_PC22:
4594             /* Should the interworking branches be listed here?  */
4595             if (h != NULL)
4596               {
4597                 /* If this reloc is in a read-only section, we might
4598                    need a copy reloc.  We can't check reliably at this
4599                    stage whether the section is read-only, as input
4600                    sections have not yet been mapped to output sections.
4601                    Tentatively set the flag for now, and correct in
4602                    adjust_dynamic_symbol.  */
4603                 if (!info->shared)
4604                   h->non_got_ref = 1;
4605
4606                 /* We may need a .plt entry if the function this reloc
4607                    refers to is in a different object.  We can't tell for
4608                    sure yet, because something later might force the
4609                    symbol local.  */
4610                 if (r_type == R_ARM_PC24
4611 #ifndef OLD_ARM_ABI
4612                     || r_type == R_ARM_CALL
4613                     || r_type == R_ARM_JUMP24
4614                     || r_type == R_ARM_PREL31
4615 #endif
4616                     || r_type == R_ARM_PLT32
4617                     || r_type == R_ARM_THM_PC22)
4618                   h->needs_plt = 1;
4619
4620                 /* If we create a PLT entry, this relocation will reference
4621                    it, even if it's an ABS32 relocation.  */
4622                 h->plt.refcount += 1;
4623
4624                 if (r_type == R_ARM_THM_PC22)
4625                   eh->plt_thumb_refcount += 1;
4626               }
4627
4628             /* If we are creating a shared library or relocatable executable,
4629                and this is a reloc against a global symbol, or a non PC
4630                relative reloc against a local symbol, then we need to copy
4631                the reloc into the shared library.  However, if we are linking
4632                with -Bsymbolic, we do not need to copy a reloc against a
4633                global symbol which is defined in an object we are
4634                including in the link (i.e., DEF_REGULAR is set).  At
4635                this point we have not seen all the input files, so it is
4636                possible that DEF_REGULAR is not set now but will be set
4637                later (it is never cleared).  We account for that
4638                possibility below by storing information in the
4639                relocs_copied field of the hash table entry.  */
4640             if ((info->shared || htab->root.is_relocatable_executable)
4641                 && (sec->flags & SEC_ALLOC) != 0
4642                 && (r_type == R_ARM_ABS32
4643                     || (h != NULL && ! h->needs_plt
4644                         && (! info->symbolic || ! h->def_regular))))
4645               {
4646                 struct elf32_arm_relocs_copied *p, **head;
4647
4648                 /* When creating a shared object, we must copy these
4649                    reloc types into the output file.  We create a reloc
4650                    section in dynobj and make room for this reloc.  */
4651                 if (sreloc == NULL)
4652                   {
4653                     const char * name;
4654
4655                     name = (bfd_elf_string_from_elf_section
4656                             (abfd,
4657                              elf_elfheader (abfd)->e_shstrndx,
4658                              elf_section_data (sec)->rel_hdr.sh_name));
4659                     if (name == NULL)
4660                       return FALSE;
4661
4662                     BFD_ASSERT (strncmp (name, ".rel", 4) == 0
4663                                 && strcmp (bfd_get_section_name (abfd, sec),
4664                                            name + 4) == 0);
4665
4666                     sreloc = bfd_get_section_by_name (dynobj, name);
4667                     if (sreloc == NULL)
4668                       {
4669                         flagword flags;
4670
4671                         sreloc = bfd_make_section (dynobj, name);
4672                         flags = (SEC_HAS_CONTENTS | SEC_READONLY
4673                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4674                         if ((sec->flags & SEC_ALLOC) != 0
4675                             /* BPABI objects never have dynamic
4676                                relocations mapped.  */
4677                             && !htab->symbian_p)
4678                           flags |= SEC_ALLOC | SEC_LOAD;
4679                         if (sreloc == NULL
4680                             || ! bfd_set_section_flags (dynobj, sreloc, flags)
4681                             || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4682                           return FALSE;
4683                       }
4684
4685                     elf_section_data (sec)->sreloc = sreloc;
4686                   }
4687
4688                 /* If this is a global symbol, we count the number of
4689                    relocations we need for this symbol.  */
4690                 if (h != NULL)
4691                   {
4692                     head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
4693                   }
4694                 else
4695                   {
4696                     /* Track dynamic relocs needed for local syms too.
4697                        We really need local syms available to do this
4698                        easily.  Oh well.  */
4699
4700                     asection *s;
4701                     s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4702                                                    sec, r_symndx);
4703                     if (s == NULL)
4704                       return FALSE;
4705
4706                     head = ((struct elf32_arm_relocs_copied **)
4707                             &elf_section_data (s)->local_dynrel);
4708                   }
4709
4710                 p = *head;
4711                 if (p == NULL || p->section != sec)
4712                   {
4713                     bfd_size_type amt = sizeof *p;
4714
4715                     p = bfd_alloc (htab->root.dynobj, amt);
4716                     if (p == NULL)
4717                       return FALSE;
4718                     p->next = *head;
4719                     *head = p;
4720                     p->section = sec;
4721                     p->count = 0;
4722                     p->pc_count = 0;
4723                   }
4724
4725                 if (r_type == R_ARM_REL32)
4726                   p->pc_count += 1;
4727                 p->count += 1;
4728               }
4729             break;
4730
4731         /* This relocation describes the C++ object vtable hierarchy.
4732            Reconstruct it for later use during GC.  */
4733         case R_ARM_GNU_VTINHERIT:
4734           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4735             return FALSE;
4736           break;
4737
4738         /* This relocation describes which C++ vtable entries are actually
4739            used.  Record for later use during GC.  */
4740         case R_ARM_GNU_VTENTRY:
4741           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4742             return FALSE;
4743           break;
4744         }
4745     }
4746
4747   return TRUE;
4748 }
4749
4750 /* Treat mapping symbols as special target symbols.  */
4751
4752 static bfd_boolean
4753 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
4754 {
4755   return bfd_is_arm_mapping_symbol_name (sym->name);
4756 }
4757
4758 /* This is a copy of elf_find_function() from elf.c except that
4759    ARM mapping symbols are ignored when looking for function names
4760    and STT_ARM_TFUNC is considered to a function type.  */
4761
4762 static bfd_boolean
4763 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
4764                        asection *    section,
4765                        asymbol **    symbols,
4766                        bfd_vma       offset,
4767                        const char ** filename_ptr,
4768                        const char ** functionname_ptr)
4769 {
4770   const char * filename = NULL;
4771   asymbol * func = NULL;
4772   bfd_vma low_func = 0;
4773   asymbol ** p;
4774
4775   for (p = symbols; *p != NULL; p++)
4776     {
4777       elf_symbol_type *q;
4778
4779       q = (elf_symbol_type *) *p;
4780
4781       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4782         {
4783         default:
4784           break;
4785         case STT_FILE:
4786           filename = bfd_asymbol_name (&q->symbol);
4787           break;
4788         case STT_FUNC:
4789         case STT_ARM_TFUNC:
4790         case STT_NOTYPE:
4791           /* Skip $a and $t symbols.  */
4792           if ((q->symbol.flags & BSF_LOCAL)
4793               && bfd_is_arm_mapping_symbol_name (q->symbol.name))
4794             continue;
4795           /* Fall through.  */
4796           if (bfd_get_section (&q->symbol) == section
4797               && q->symbol.value >= low_func
4798               && q->symbol.value <= offset)
4799             {
4800               func = (asymbol *) q;
4801               low_func = q->symbol.value;
4802             }
4803           break;
4804         }
4805     }
4806
4807   if (func == NULL)
4808     return FALSE;
4809
4810   if (filename_ptr)
4811     *filename_ptr = filename;
4812   if (functionname_ptr)
4813     *functionname_ptr = bfd_asymbol_name (func);
4814
4815   return TRUE;
4816 }  
4817
4818
4819 /* Find the nearest line to a particular section and offset, for error
4820    reporting.   This code is a duplicate of the code in elf.c, except
4821    that it uses arm_elf_find_function.  */
4822
4823 static bfd_boolean
4824 elf32_arm_find_nearest_line (bfd *          abfd,
4825                              asection *     section,
4826                              asymbol **     symbols,
4827                              bfd_vma        offset,
4828                              const char **  filename_ptr,
4829                              const char **  functionname_ptr,
4830                              unsigned int * line_ptr)
4831 {
4832   bfd_boolean found = FALSE;
4833
4834   /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
4835
4836   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4837                                      filename_ptr, functionname_ptr,
4838                                      line_ptr, 0,
4839                                      & elf_tdata (abfd)->dwarf2_find_line_info))
4840     {
4841       if (!*functionname_ptr)
4842         arm_elf_find_function (abfd, section, symbols, offset,
4843                                *filename_ptr ? NULL : filename_ptr,
4844                                functionname_ptr);
4845
4846       return TRUE;
4847     }
4848
4849   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4850                                              & found, filename_ptr,
4851                                              functionname_ptr, line_ptr,
4852                                              & elf_tdata (abfd)->line_info))
4853     return FALSE;
4854
4855   if (found && (*functionname_ptr || *line_ptr))
4856     return TRUE;
4857
4858   if (symbols == NULL)
4859     return FALSE;
4860
4861   if (! arm_elf_find_function (abfd, section, symbols, offset,
4862                                filename_ptr, functionname_ptr))
4863     return FALSE;
4864
4865   *line_ptr = 0;
4866   return TRUE;
4867 }
4868
4869 /* Adjust a symbol defined by a dynamic object and referenced by a
4870    regular object.  The current definition is in some section of the
4871    dynamic object, but we're not including those sections.  We have to
4872    change the definition to something the rest of the link can
4873    understand.  */
4874
4875 static bfd_boolean
4876 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
4877                                  struct elf_link_hash_entry * h)
4878 {
4879   bfd * dynobj;
4880   asection * s;
4881   unsigned int power_of_two;
4882   struct elf32_arm_link_hash_entry * eh;
4883   struct elf32_arm_link_hash_table *globals;
4884
4885   globals = elf32_arm_hash_table (info);
4886   dynobj = elf_hash_table (info)->dynobj;
4887
4888   /* Make sure we know what is going on here.  */
4889   BFD_ASSERT (dynobj != NULL
4890               && (h->needs_plt
4891                   || h->u.weakdef != NULL
4892                   || (h->def_dynamic
4893                       && h->ref_regular
4894                       && !h->def_regular)));
4895
4896   eh = (struct elf32_arm_link_hash_entry *) h;
4897
4898   /* If this is a function, put it in the procedure linkage table.  We
4899      will fill in the contents of the procedure linkage table later,
4900      when we know the address of the .got section.  */
4901   if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
4902       || h->needs_plt)
4903     {
4904       if (h->plt.refcount <= 0
4905           || SYMBOL_CALLS_LOCAL (info, h)
4906           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4907               && h->root.type == bfd_link_hash_undefweak))
4908         {
4909           /* This case can occur if we saw a PLT32 reloc in an input
4910              file, but the symbol was never referred to by a dynamic
4911              object, or if all references were garbage collected.  In
4912              such a case, we don't actually need to build a procedure
4913              linkage table, and we can just do a PC24 reloc instead.  */
4914           h->plt.offset = (bfd_vma) -1;
4915           eh->plt_thumb_refcount = 0;
4916           h->needs_plt = 0;
4917         }
4918
4919       return TRUE;
4920     }
4921   else
4922     {
4923       /* It's possible that we incorrectly decided a .plt reloc was
4924          needed for an R_ARM_PC24 or similar reloc to a non-function sym
4925          in check_relocs.  We can't decide accurately between function
4926          and non-function syms in check-relocs; Objects loaded later in
4927          the link may change h->type.  So fix it now.  */
4928       h->plt.offset = (bfd_vma) -1;
4929       eh->plt_thumb_refcount = 0;
4930     }
4931
4932   /* If this is a weak symbol, and there is a real definition, the
4933      processor independent code will have arranged for us to see the
4934      real definition first, and we can just use the same value.  */
4935   if (h->u.weakdef != NULL)
4936     {
4937       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4938                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
4939       h->root.u.def.section = h->u.weakdef->root.u.def.section;
4940       h->root.u.def.value = h->u.weakdef->root.u.def.value;
4941       return TRUE;
4942     }
4943
4944   /* If there are no non-GOT references, we do not need a copy
4945      relocation.  */
4946   if (!h->non_got_ref)
4947     return TRUE;
4948
4949   /* This is a reference to a symbol defined by a dynamic object which
4950      is not a function.  */
4951
4952   /* If we are creating a shared library, we must presume that the
4953      only references to the symbol are via the global offset table.
4954      For such cases we need not do anything here; the relocations will
4955      be handled correctly by relocate_section.  Relocatable executables
4956      can reference data in shared objects directly, so we don't need to
4957      do anything here.  */
4958   if (info->shared || globals->root.is_relocatable_executable)
4959     return TRUE;
4960
4961   /* We must allocate the symbol in our .dynbss section, which will
4962      become part of the .bss section of the executable.  There will be
4963      an entry for this symbol in the .dynsym section.  The dynamic
4964      object will contain position independent code, so all references
4965      from the dynamic object to this symbol will go through the global
4966      offset table.  The dynamic linker will use the .dynsym entry to
4967      determine the address it must put in the global offset table, so
4968      both the dynamic object and the regular object will refer to the
4969      same memory location for the variable.  */
4970   s = bfd_get_section_by_name (dynobj, ".dynbss");
4971   BFD_ASSERT (s != NULL);
4972
4973   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
4974      copy the initial value out of the dynamic object and into the
4975      runtime process image.  We need to remember the offset into the
4976      .rel.bss section we are going to use.  */
4977   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4978     {
4979       asection *srel;
4980
4981       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
4982       BFD_ASSERT (srel != NULL);
4983       srel->size += sizeof (Elf32_External_Rel);
4984       h->needs_copy = 1;
4985     }
4986
4987   /* We need to figure out the alignment required for this symbol.  I
4988      have no idea how ELF linkers handle this.  */
4989   power_of_two = bfd_log2 (h->size);
4990   if (power_of_two > 3)
4991     power_of_two = 3;
4992
4993   /* Apply the required alignment.  */
4994   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4995   if (power_of_two > bfd_get_section_alignment (dynobj, s))
4996     {
4997       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4998         return FALSE;
4999     }
5000
5001   /* Define the symbol as being at this point in the section.  */
5002   h->root.u.def.section = s;
5003   h->root.u.def.value = s->size;
5004
5005   /* Increment the section size to make room for the symbol.  */
5006   s->size += h->size;
5007
5008   return TRUE;
5009 }
5010
5011 /* Allocate space in .plt, .got and associated reloc sections for
5012    dynamic relocs.  */
5013
5014 static bfd_boolean
5015 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
5016 {
5017   struct bfd_link_info *info;
5018   struct elf32_arm_link_hash_table *htab;
5019   struct elf32_arm_link_hash_entry *eh;
5020   struct elf32_arm_relocs_copied *p;
5021
5022   eh = (struct elf32_arm_link_hash_entry *) h;
5023
5024   if (h->root.type == bfd_link_hash_indirect)
5025     return TRUE;
5026
5027   if (h->root.type == bfd_link_hash_warning)
5028     /* When warning symbols are created, they **replace** the "real"
5029        entry in the hash table, thus we never get to see the real
5030        symbol in a hash traversal.  So look at it now.  */
5031     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5032
5033   info = (struct bfd_link_info *) inf;
5034   htab = elf32_arm_hash_table (info);
5035
5036   if (htab->root.dynamic_sections_created
5037       && h->plt.refcount > 0)
5038     {
5039       /* Make sure this symbol is output as a dynamic symbol.
5040          Undefined weak syms won't yet be marked as dynamic.  */
5041       if (h->dynindx == -1
5042           && !h->forced_local)
5043         {
5044           if (! bfd_elf_link_record_dynamic_symbol (info, h))
5045             return FALSE;
5046         }
5047
5048       if (info->shared
5049           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
5050         {
5051           asection *s = htab->splt;
5052
5053           /* If this is the first .plt entry, make room for the special
5054              first entry.  */
5055           if (s->size == 0)
5056             s->size += htab->plt_header_size;
5057
5058           h->plt.offset = s->size;
5059
5060           /* If we will insert a Thumb trampoline before this PLT, leave room
5061              for it.  */
5062           if (!htab->symbian_p && eh->plt_thumb_refcount > 0)
5063             {
5064               h->plt.offset += PLT_THUMB_STUB_SIZE;
5065               s->size += PLT_THUMB_STUB_SIZE;
5066             }
5067
5068           /* If this symbol is not defined in a regular file, and we are
5069              not generating a shared library, then set the symbol to this
5070              location in the .plt.  This is required to make function
5071              pointers compare as equal between the normal executable and
5072              the shared library.  */
5073           if (! info->shared
5074               && !h->def_regular)
5075             {
5076               h->root.u.def.section = s;
5077               h->root.u.def.value = h->plt.offset;
5078
5079               /* Make sure the function is not marked as Thumb, in case
5080                  it is the target of an ABS32 relocation, which will
5081                  point to the PLT entry.  */
5082               if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
5083                 h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
5084             }
5085
5086           /* Make room for this entry.  */
5087           s->size += htab->plt_entry_size;
5088
5089           if (!htab->symbian_p)
5090             {
5091               /* We also need to make an entry in the .got.plt section, which
5092                  will be placed in the .got section by the linker script.  */
5093               eh->plt_got_offset = htab->sgotplt->size;
5094               htab->sgotplt->size += 4;
5095             }
5096
5097           /* We also need to make an entry in the .rel.plt section.  */
5098           htab->srelplt->size += sizeof (Elf32_External_Rel);
5099         }
5100       else
5101         {
5102           h->plt.offset = (bfd_vma) -1;
5103           h->needs_plt = 0;
5104         }
5105     }
5106   else
5107     {
5108       h->plt.offset = (bfd_vma) -1;
5109       h->needs_plt = 0;
5110     }
5111
5112   if (h->got.refcount > 0)
5113     {
5114       asection *s;
5115       bfd_boolean dyn;
5116       int tls_type = elf32_arm_hash_entry (h)->tls_type;
5117       int indx;
5118
5119       /* Make sure this symbol is output as a dynamic symbol.
5120          Undefined weak syms won't yet be marked as dynamic.  */
5121       if (h->dynindx == -1
5122           && !h->forced_local)
5123         {
5124           if (! bfd_elf_link_record_dynamic_symbol (info, h))
5125             return FALSE;
5126         }
5127
5128       if (!htab->symbian_p)
5129         {
5130           s = htab->sgot;
5131           h->got.offset = s->size;
5132
5133           if (tls_type == GOT_UNKNOWN)
5134             abort ();
5135
5136           if (tls_type == GOT_NORMAL)
5137             /* Non-TLS symbols need one GOT slot.  */
5138             s->size += 4;
5139           else
5140             {
5141               if (tls_type & GOT_TLS_GD)
5142                 /* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  */
5143                 s->size += 8;
5144               if (tls_type & GOT_TLS_IE)
5145                 /* R_ARM_TLS_IE32 needs one GOT slot.  */
5146                 s->size += 4;
5147             }
5148
5149           dyn = htab->root.dynamic_sections_created;
5150
5151           indx = 0;
5152           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5153               && (!info->shared
5154                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
5155             indx = h->dynindx;
5156
5157           if (tls_type != GOT_NORMAL
5158               && (info->shared || indx != 0)
5159               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5160                   || h->root.type != bfd_link_hash_undefweak))
5161             {
5162               if (tls_type & GOT_TLS_IE)
5163                 htab->srelgot->size += sizeof (Elf32_External_Rel);
5164
5165               if (tls_type & GOT_TLS_GD)
5166                 htab->srelgot->size += sizeof (Elf32_External_Rel);
5167
5168               if ((tls_type & GOT_TLS_GD) && indx != 0)
5169                 htab->srelgot->size += sizeof (Elf32_External_Rel);
5170             }
5171           else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5172                     || h->root.type != bfd_link_hash_undefweak)
5173                    && (info->shared
5174                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5175             htab->srelgot->size += sizeof (Elf32_External_Rel);
5176         }
5177     }
5178   else
5179     h->got.offset = (bfd_vma) -1;
5180
5181   if (eh->relocs_copied == NULL)
5182     return TRUE;
5183
5184   /* In the shared -Bsymbolic case, discard space allocated for
5185      dynamic pc-relative relocs against symbols which turn out to be
5186      defined in regular objects.  For the normal shared case, discard
5187      space for pc-relative relocs that have become local due to symbol
5188      visibility changes.  */
5189
5190   if (info->shared || htab->root.is_relocatable_executable)
5191     {
5192       /* The only reloc that uses pc_count is R_ARM_REL32, which will
5193          appear on something like ".long foo - .".  We want calls to
5194          protected symbols to resolve directly to the function rather
5195          than going via the plt.  If people want function pointer
5196          comparisons to work as expected then they should avoid
5197          writing assembly like ".long foo - .".  */
5198       if (SYMBOL_CALLS_LOCAL (info, h))
5199         {
5200           struct elf32_arm_relocs_copied **pp;
5201
5202           for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
5203             {
5204               p->count -= p->pc_count;
5205               p->pc_count = 0;
5206               if (p->count == 0)
5207                 *pp = p->next;
5208               else
5209                 pp = &p->next;
5210             }
5211         }
5212
5213       /* Also discard relocs on undefined weak syms with non-default
5214          visibility.  */
5215       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5216           && h->root.type == bfd_link_hash_undefweak)
5217         eh->relocs_copied = NULL;
5218       else if (htab->root.is_relocatable_executable && h->dynindx == -1
5219                && h->root.type == bfd_link_hash_new)
5220         {
5221           /* Output absolute symbols so that we can create relocations
5222              against them.  For normal symbols we output a relocation
5223              against the section that contains them.  */
5224           if (! bfd_elf_link_record_dynamic_symbol (info, h))
5225             return FALSE;
5226         }
5227
5228     }
5229   else
5230     {
5231       /* For the non-shared case, discard space for relocs against
5232          symbols which turn out to need copy relocs or are not
5233          dynamic.  */
5234
5235       if (!h->non_got_ref
5236           && ((h->def_dynamic
5237                && !h->def_regular)
5238               || (htab->root.dynamic_sections_created
5239                   && (h->root.type == bfd_link_hash_undefweak
5240                       || h->root.type == bfd_link_hash_undefined))))
5241         {
5242           /* Make sure this symbol is output as a dynamic symbol.
5243              Undefined weak syms won't yet be marked as dynamic.  */
5244           if (h->dynindx == -1
5245               && !h->forced_local)
5246             {
5247               if (! bfd_elf_link_record_dynamic_symbol (info, h))
5248                 return FALSE;
5249             }
5250
5251           /* If that succeeded, we know we'll be keeping all the
5252              relocs.  */
5253           if (h->dynindx != -1)
5254             goto keep;
5255         }
5256
5257       eh->relocs_copied = NULL;
5258
5259     keep: ;
5260     }
5261
5262   /* Finally, allocate space.  */
5263   for (p = eh->relocs_copied; p != NULL; p = p->next)
5264     {
5265       asection *sreloc = elf_section_data (p->section)->sreloc;
5266       sreloc->size += p->count * sizeof (Elf32_External_Rel);
5267     }
5268
5269   return TRUE;
5270 }
5271
5272 /* Find any dynamic relocs that apply to read-only sections.  */
5273
5274 static bfd_boolean
5275 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5276 {
5277   struct elf32_arm_link_hash_entry *eh;
5278   struct elf32_arm_relocs_copied *p;
5279
5280   if (h->root.type == bfd_link_hash_warning)
5281     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5282
5283   eh = (struct elf32_arm_link_hash_entry *) h;
5284   for (p = eh->relocs_copied; p != NULL; p = p->next)
5285     {
5286       asection *s = p->section;
5287
5288       if (s != NULL && (s->flags & SEC_READONLY) != 0)
5289         {
5290           struct bfd_link_info *info = (struct bfd_link_info *) inf;
5291
5292           info->flags |= DF_TEXTREL;
5293
5294           /* Not an error, just cut short the traversal.  */
5295           return FALSE;
5296         }
5297     }
5298   return TRUE;
5299 }
5300
5301 /* Set the sizes of the dynamic sections.  */
5302
5303 static bfd_boolean
5304 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
5305                                  struct bfd_link_info * info)
5306 {
5307   bfd * dynobj;
5308   asection * s;
5309   bfd_boolean plt;
5310   bfd_boolean relocs;
5311   bfd *ibfd;
5312   struct elf32_arm_link_hash_table *htab;
5313
5314   htab = elf32_arm_hash_table (info);
5315   dynobj = elf_hash_table (info)->dynobj;
5316   BFD_ASSERT (dynobj != NULL);
5317
5318   if (elf_hash_table (info)->dynamic_sections_created)
5319     {
5320       /* Set the contents of the .interp section to the interpreter.  */
5321       if (info->executable)
5322         {
5323           s = bfd_get_section_by_name (dynobj, ".interp");
5324           BFD_ASSERT (s != NULL);
5325           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5326           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5327         }
5328     }
5329
5330   /* Set up .got offsets for local syms, and space for local dynamic
5331      relocs.  */
5332   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5333     {
5334       bfd_signed_vma *local_got;
5335       bfd_signed_vma *end_local_got;
5336       char *local_tls_type;
5337       bfd_size_type locsymcount;
5338       Elf_Internal_Shdr *symtab_hdr;
5339       asection *srel;
5340
5341       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5342         continue;
5343
5344       for (s = ibfd->sections; s != NULL; s = s->next)
5345         {
5346           struct elf32_arm_relocs_copied *p;
5347
5348           for (p = *((struct elf32_arm_relocs_copied **)
5349                      &elf_section_data (s)->local_dynrel);
5350                p != NULL;
5351                p = p->next)
5352             {
5353               if (!bfd_is_abs_section (p->section)
5354                   && bfd_is_abs_section (p->section->output_section))
5355                 {
5356                   /* Input section has been discarded, either because
5357                      it is a copy of a linkonce section or due to
5358                      linker script /DISCARD/, so we'll be discarding
5359                      the relocs too.  */
5360                 }
5361               else if (p->count != 0)
5362                 {
5363                   srel = elf_section_data (p->section)->sreloc;
5364                   srel->size += p->count * sizeof (Elf32_External_Rel);
5365                   if ((p->section->output_section->flags & SEC_READONLY) != 0)
5366                     info->flags |= DF_TEXTREL;
5367                 }
5368             }
5369         }
5370
5371       local_got = elf_local_got_refcounts (ibfd);
5372       if (!local_got)
5373         continue;
5374
5375       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5376       locsymcount = symtab_hdr->sh_info;
5377       end_local_got = local_got + locsymcount;
5378       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
5379       s = htab->sgot;
5380       srel = htab->srelgot;
5381       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5382         {
5383           if (*local_got > 0)
5384             {
5385               *local_got = s->size;
5386               if (*local_tls_type & GOT_TLS_GD)
5387                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
5388                 s->size += 8;
5389               if (*local_tls_type & GOT_TLS_IE)
5390                 s->size += 4;
5391               if (*local_tls_type == GOT_NORMAL)
5392                 s->size += 4;
5393
5394               if (info->shared || *local_tls_type == GOT_TLS_GD)
5395                 srel->size += sizeof (Elf32_External_Rel);
5396             }
5397           else
5398             *local_got = (bfd_vma) -1;
5399         }
5400     }
5401
5402   if (htab->tls_ldm_got.refcount > 0)
5403     {
5404       /* Allocate two GOT entries and one dynamic relocation (if necessary)
5405          for R_ARM_TLS_LDM32 relocations.  */
5406       htab->tls_ldm_got.offset = htab->sgot->size;
5407       htab->sgot->size += 8;
5408       if (info->shared)
5409         htab->srelgot->size += sizeof (Elf32_External_Rel);
5410     }
5411   else
5412     htab->tls_ldm_got.offset = -1;
5413
5414   /* Allocate global sym .plt and .got entries, and space for global
5415      sym dynamic relocs.  */
5416   elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5417
5418   /* The check_relocs and adjust_dynamic_symbol entry points have
5419      determined the sizes of the various dynamic sections.  Allocate
5420      memory for them.  */
5421   plt = FALSE;
5422   relocs = FALSE;
5423   for (s = dynobj->sections; s != NULL; s = s->next)
5424     {
5425       const char * name;
5426       bfd_boolean strip;
5427
5428       if ((s->flags & SEC_LINKER_CREATED) == 0)
5429         continue;
5430
5431       /* It's OK to base decisions on the section name, because none
5432          of the dynobj section names depend upon the input files.  */
5433       name = bfd_get_section_name (dynobj, s);
5434
5435       strip = FALSE;
5436
5437       if (strcmp (name, ".plt") == 0)
5438         {
5439           if (s->size == 0)
5440             {
5441               /* Strip this section if we don't need it; see the
5442                  comment below.  */
5443               strip = TRUE;
5444             }
5445           else
5446             {
5447               /* Remember whether there is a PLT.  */
5448               plt = TRUE;
5449             }
5450         }
5451       else if (strncmp (name, ".rel", 4) == 0)
5452         {
5453           if (s->size == 0)
5454             {
5455               /* If we don't need this section, strip it from the
5456                  output file.  This is mostly to handle .rel.bss and
5457                  .rel.plt.  We must create both sections in
5458                  create_dynamic_sections, because they must be created
5459                  before the linker maps input sections to output
5460                  sections.  The linker does that before
5461                  adjust_dynamic_symbol is called, and it is that
5462                  function which decides whether anything needs to go
5463                  into these sections.  */
5464               strip = TRUE;
5465             }
5466           else
5467             {
5468               /* Remember whether there are any reloc sections other
5469                  than .rel.plt.  */
5470               if (strcmp (name, ".rel.plt") != 0)
5471                 relocs = TRUE;
5472
5473               /* We use the reloc_count field as a counter if we need
5474                  to copy relocs into the output file.  */
5475               s->reloc_count = 0;
5476             }
5477         }
5478       else if (strncmp (name, ".got", 4) != 0)
5479         {
5480           /* It's not one of our sections, so don't allocate space.  */
5481           continue;
5482         }
5483
5484       if (strip)
5485         {
5486           _bfd_strip_section_from_output (info, s);
5487           continue;
5488         }
5489
5490       /* Allocate memory for the section contents.  */
5491       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5492       if (s->contents == NULL && s->size != 0)
5493         return FALSE;
5494     }
5495
5496   if (elf_hash_table (info)->dynamic_sections_created)
5497     {
5498       /* Add some entries to the .dynamic section.  We fill in the
5499          values later, in elf32_arm_finish_dynamic_sections, but we
5500          must add the entries now so that we get the correct size for
5501          the .dynamic section.  The DT_DEBUG entry is filled in by the
5502          dynamic linker and used by the debugger.  */
5503 #define add_dynamic_entry(TAG, VAL) \
5504   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5505
5506       if (!info->shared)
5507         {
5508           if (!add_dynamic_entry (DT_DEBUG, 0))
5509             return FALSE;
5510         }
5511
5512       if (plt)
5513         {
5514           if (   !add_dynamic_entry (DT_PLTGOT, 0)
5515               || !add_dynamic_entry (DT_PLTRELSZ, 0)
5516               || !add_dynamic_entry (DT_PLTREL, DT_REL)
5517               || !add_dynamic_entry (DT_JMPREL, 0))
5518             return FALSE;
5519         }
5520
5521       if (relocs)
5522         {
5523           if (   !add_dynamic_entry (DT_REL, 0)
5524               || !add_dynamic_entry (DT_RELSZ, 0)
5525               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
5526             return FALSE;
5527         }
5528
5529       /* If any dynamic relocs apply to a read-only section,
5530          then we need a DT_TEXTREL entry.  */
5531       if ((info->flags & DF_TEXTREL) == 0)
5532         elf_link_hash_traverse (&htab->root, elf32_arm_readonly_dynrelocs,
5533                                 (PTR) info);
5534
5535       if ((info->flags & DF_TEXTREL) != 0)
5536         {
5537           if (!add_dynamic_entry (DT_TEXTREL, 0))
5538             return FALSE;
5539           info->flags |= DF_TEXTREL;
5540         }
5541     }
5542 #undef add_synamic_entry
5543
5544   return TRUE;
5545 }
5546
5547 /* Finish up dynamic symbol handling.  We set the contents of various
5548    dynamic sections here.  */
5549
5550 static bfd_boolean
5551 elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
5552                                  struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
5553 {
5554   bfd * dynobj;
5555   struct elf32_arm_link_hash_table *htab;
5556   struct elf32_arm_link_hash_entry *eh;
5557
5558   dynobj = elf_hash_table (info)->dynobj;
5559   htab = elf32_arm_hash_table (info);
5560   eh = (struct elf32_arm_link_hash_entry *) h;
5561
5562   if (h->plt.offset != (bfd_vma) -1)
5563     {
5564       asection * splt;
5565       asection * srel;
5566       bfd_byte *loc;
5567       bfd_vma plt_index;
5568       Elf_Internal_Rela rel;
5569
5570       /* This symbol has an entry in the procedure linkage table.  Set
5571          it up.  */
5572
5573       BFD_ASSERT (h->dynindx != -1);
5574
5575       splt = bfd_get_section_by_name (dynobj, ".plt");
5576       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
5577       BFD_ASSERT (splt != NULL && srel != NULL);
5578
5579       /* Fill in the entry in the procedure linkage table.  */
5580       if (htab->symbian_p)
5581         {
5582           unsigned i;
5583           for (i = 0; i < htab->plt_entry_size / 4; ++i)
5584             bfd_put_32 (output_bfd, 
5585                         elf32_arm_symbian_plt_entry[i],
5586                         splt->contents + h->plt.offset + 4 * i);
5587           
5588           /* Fill in the entry in the .rel.plt section.  */
5589           rel.r_offset = (splt->output_section->vma
5590                           + splt->output_offset
5591                           + h->plt.offset + 4 * (i - 1));
5592           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
5593
5594           /* Get the index in the procedure linkage table which
5595              corresponds to this symbol.  This is the index of this symbol
5596              in all the symbols for which we are making plt entries.  The
5597              first entry in the procedure linkage table is reserved.  */
5598           plt_index = ((h->plt.offset - htab->plt_header_size) 
5599                        / htab->plt_entry_size);
5600         }
5601       else
5602         {
5603           bfd_vma got_offset;
5604           bfd_vma got_displacement;
5605           asection * sgot;
5606           
5607           sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5608           BFD_ASSERT (sgot != NULL);
5609
5610           /* Get the offset into the .got.plt table of the entry that
5611              corresponds to this function.  */
5612           got_offset = eh->plt_got_offset;
5613
5614           /* Get the index in the procedure linkage table which
5615              corresponds to this symbol.  This is the index of this symbol
5616              in all the symbols for which we are making plt entries.  The
5617              first three entries in .got.plt are reserved; after that
5618              symbols appear in the same order as in .plt.  */
5619           plt_index = (got_offset - 12) / 4;
5620
5621           /* Calculate the displacement between the PLT slot and the
5622              entry in the GOT.  The eight-byte offset accounts for the
5623              value produced by adding to pc in the first instruction
5624              of the PLT stub.  */
5625           got_displacement = (sgot->output_section->vma
5626                               + sgot->output_offset
5627                               + got_offset
5628                               - splt->output_section->vma
5629                               - splt->output_offset
5630                               - h->plt.offset
5631                               - 8);
5632
5633           BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
5634
5635           if (eh->plt_thumb_refcount > 0)
5636             {
5637               bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[0],
5638                           splt->contents + h->plt.offset - 4);
5639               bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[1],
5640                           splt->contents + h->plt.offset - 2);
5641             }
5642
5643           bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
5644                       splt->contents + h->plt.offset + 0);
5645           bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
5646                       splt->contents + h->plt.offset + 4);
5647           bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
5648                       splt->contents + h->plt.offset + 8);
5649 #ifdef FOUR_WORD_PLT
5650           bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
5651                       splt->contents + h->plt.offset + 12);
5652 #endif
5653
5654           /* Fill in the entry in the global offset table.  */
5655           bfd_put_32 (output_bfd,
5656                       (splt->output_section->vma
5657                        + splt->output_offset),
5658                       sgot->contents + got_offset);
5659           
5660           /* Fill in the entry in the .rel.plt section.  */
5661           rel.r_offset = (sgot->output_section->vma
5662                           + sgot->output_offset
5663                           + got_offset);
5664           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
5665         }
5666
5667       loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
5668       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5669
5670       if (!h->def_regular)
5671         {
5672           /* Mark the symbol as undefined, rather than as defined in
5673              the .plt section.  Leave the value alone.  */
5674           sym->st_shndx = SHN_UNDEF;
5675           /* If the symbol is weak, we do need to clear the value.
5676              Otherwise, the PLT entry would provide a definition for
5677              the symbol even if the symbol wasn't defined anywhere,
5678              and so the symbol would never be NULL.  */
5679           if (!h->ref_regular_nonweak)
5680             sym->st_value = 0;
5681         }
5682     }
5683
5684   if (h->got.offset != (bfd_vma) -1
5685       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5686       && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5687     {
5688       asection * sgot;
5689       asection * srel;
5690       Elf_Internal_Rela rel;
5691       bfd_byte *loc;
5692
5693       /* This symbol has an entry in the global offset table.  Set it
5694          up.  */
5695       sgot = bfd_get_section_by_name (dynobj, ".got");
5696       srel = bfd_get_section_by_name (dynobj, ".rel.got");
5697       BFD_ASSERT (sgot != NULL && srel != NULL);
5698
5699       rel.r_offset = (sgot->output_section->vma
5700                       + sgot->output_offset
5701                       + (h->got.offset &~ (bfd_vma) 1));
5702
5703       /* If this is a static link, or it is a -Bsymbolic link and the
5704          symbol is defined locally or was forced to be local because
5705          of a version file, we just want to emit a RELATIVE reloc.
5706          The entry in the global offset table will already have been
5707          initialized in the relocate_section function.  */
5708       if (info->shared
5709           && SYMBOL_REFERENCES_LOCAL (info, h))
5710         {
5711           BFD_ASSERT((h->got.offset & 1) != 0);
5712           rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
5713         }
5714       else
5715         {
5716           BFD_ASSERT((h->got.offset & 1) == 0);
5717           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5718           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
5719         }
5720
5721       loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
5722       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5723     }
5724
5725   if (h->needs_copy)
5726     {
5727       asection * s;
5728       Elf_Internal_Rela rel;
5729       bfd_byte *loc;
5730
5731       /* This symbol needs a copy reloc.  Set it up.  */
5732       BFD_ASSERT (h->dynindx != -1
5733                   && (h->root.type == bfd_link_hash_defined
5734                       || h->root.type == bfd_link_hash_defweak));
5735
5736       s = bfd_get_section_by_name (h->root.u.def.section->owner,
5737                                    ".rel.bss");
5738       BFD_ASSERT (s != NULL);
5739
5740       rel.r_offset = (h->root.u.def.value
5741                       + h->root.u.def.section->output_section->vma
5742                       + h->root.u.def.section->output_offset);
5743       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
5744       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
5745       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5746     }
5747
5748   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
5749   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5750       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5751     sym->st_shndx = SHN_ABS;
5752
5753   return TRUE;
5754 }
5755
5756 /* Finish up the dynamic sections.  */
5757
5758 static bfd_boolean
5759 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
5760 {
5761   bfd * dynobj;
5762   asection * sgot;
5763   asection * sdyn;
5764
5765   dynobj = elf_hash_table (info)->dynobj;
5766
5767   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5768   BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
5769   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5770
5771   if (elf_hash_table (info)->dynamic_sections_created)
5772     {
5773       asection *splt;
5774       Elf32_External_Dyn *dyncon, *dynconend;
5775       struct elf32_arm_link_hash_table *htab;
5776
5777       htab = elf32_arm_hash_table (info);
5778       splt = bfd_get_section_by_name (dynobj, ".plt");
5779       BFD_ASSERT (splt != NULL && sdyn != NULL);
5780
5781       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5782       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5783
5784       for (; dyncon < dynconend; dyncon++)
5785         {
5786           Elf_Internal_Dyn dyn;
5787           const char * name;
5788           asection * s;
5789
5790           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5791
5792           switch (dyn.d_tag)
5793             {
5794               unsigned int type;
5795
5796             default:
5797               break;
5798
5799             case DT_HASH:
5800               name = ".hash";
5801               goto get_vma_if_bpabi;
5802             case DT_STRTAB:
5803               name = ".dynstr";
5804               goto get_vma_if_bpabi;
5805             case DT_SYMTAB:
5806               name = ".dynsym";
5807               goto get_vma_if_bpabi;
5808             case DT_VERSYM:
5809               name = ".gnu.version";
5810               goto get_vma_if_bpabi;
5811             case DT_VERDEF:
5812               name = ".gnu.version_d";
5813               goto get_vma_if_bpabi;
5814             case DT_VERNEED:
5815               name = ".gnu.version_r";
5816               goto get_vma_if_bpabi;
5817
5818             case DT_PLTGOT:
5819               name = ".got";
5820               goto get_vma;
5821             case DT_JMPREL:
5822               name = ".rel.plt";
5823             get_vma:
5824               s = bfd_get_section_by_name (output_bfd, name);
5825               BFD_ASSERT (s != NULL);
5826               if (!htab->symbian_p)
5827                 dyn.d_un.d_ptr = s->vma;
5828               else
5829                 /* In the BPABI, tags in the PT_DYNAMIC section point
5830                    at the file offset, not the memory address, for the
5831                    convenience of the post linker.  */
5832                 dyn.d_un.d_ptr = s->filepos;
5833               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5834               break;
5835
5836             get_vma_if_bpabi:
5837               if (htab->symbian_p)
5838                 goto get_vma;
5839               break;
5840
5841             case DT_PLTRELSZ:
5842               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5843               BFD_ASSERT (s != NULL);
5844               dyn.d_un.d_val = s->size;
5845               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5846               break;
5847               
5848             case DT_RELSZ:
5849               if (!htab->symbian_p)
5850                 {
5851                   /* My reading of the SVR4 ABI indicates that the
5852                      procedure linkage table relocs (DT_JMPREL) should be
5853                      included in the overall relocs (DT_REL).  This is
5854                      what Solaris does.  However, UnixWare can not handle
5855                      that case.  Therefore, we override the DT_RELSZ entry
5856                      here to make it not include the JMPREL relocs.  Since
5857                      the linker script arranges for .rel.plt to follow all
5858                      other relocation sections, we don't have to worry
5859                      about changing the DT_REL entry.  */
5860                   s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5861                   if (s != NULL)
5862                     dyn.d_un.d_val -= s->size;
5863                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5864                   break;
5865                 }
5866               /* Fall through */
5867
5868             case DT_REL:
5869             case DT_RELA:
5870             case DT_RELASZ:
5871               /* In the BPABI, the DT_REL tag must point at the file
5872                  offset, not the VMA, of the first relocation
5873                  section.  So, we use code similar to that in
5874                  elflink.c, but do not check for SHF_ALLOC on the
5875                  relcoation section, since relocations sections are
5876                  never allocated under the BPABI.  The comments above
5877                  about Unixware notwithstanding, we include all of the
5878                  relocations here.  */
5879               if (htab->symbian_p)
5880                 {
5881                   unsigned int i;
5882                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5883                           ? SHT_REL : SHT_RELA);
5884                   dyn.d_un.d_val = 0;
5885                   for (i = 1; i < elf_numsections (output_bfd); i++)
5886                     {
5887                       Elf_Internal_Shdr *hdr 
5888                         = elf_elfsections (output_bfd)[i];
5889                       if (hdr->sh_type == type)
5890                         {
5891                           if (dyn.d_tag == DT_RELSZ 
5892                               || dyn.d_tag == DT_RELASZ)
5893                             dyn.d_un.d_val += hdr->sh_size;
5894                           else if ((ufile_ptr) hdr->sh_offset
5895                                    <= dyn.d_un.d_val - 1)
5896                             dyn.d_un.d_val = hdr->sh_offset;
5897                         }
5898                     }
5899                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5900                 }
5901               break;
5902
5903               /* Set the bottom bit of DT_INIT/FINI if the
5904                  corresponding function is Thumb.  */
5905             case DT_INIT:
5906               name = info->init_function;
5907               goto get_sym;
5908             case DT_FINI:
5909               name = info->fini_function;
5910             get_sym:
5911               /* If it wasn't set by elf_bfd_final_link
5912                  then there is nothing to adjust.  */
5913               if (dyn.d_un.d_val != 0)
5914                 {
5915                   struct elf_link_hash_entry * eh;
5916
5917                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
5918                                              FALSE, FALSE, TRUE);
5919                   if (eh != (struct elf_link_hash_entry *) NULL
5920                       && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
5921                     {
5922                       dyn.d_un.d_val |= 1;
5923                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5924                     }
5925                 }
5926               break;
5927             }
5928         }
5929
5930       /* Fill in the first entry in the procedure linkage table.  */
5931       if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
5932         {
5933           bfd_vma got_displacement;
5934
5935           /* Calculate the displacement between the PLT slot and &GOT[0].  */
5936           got_displacement = (sgot->output_section->vma
5937                               + sgot->output_offset
5938                               - splt->output_section->vma
5939                               - splt->output_offset
5940                               - 16);
5941
5942           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents +  0);
5943           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents +  4);
5944           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents +  8);
5945           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
5946 #ifdef FOUR_WORD_PLT
5947           /* The displacement value goes in the otherwise-unused last word of
5948              the second entry.  */
5949           bfd_put_32 (output_bfd, got_displacement,        splt->contents + 28);
5950 #else
5951           bfd_put_32 (output_bfd, got_displacement,        splt->contents + 16);
5952 #endif
5953         }
5954
5955       /* UnixWare sets the entsize of .plt to 4, although that doesn't
5956          really seem like the right value.  */
5957       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5958     }
5959
5960   /* Fill in the first three entries in the global offset table.  */
5961   if (sgot)
5962     {
5963       if (sgot->size > 0)
5964         {
5965           if (sdyn == NULL)
5966             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5967           else
5968             bfd_put_32 (output_bfd,
5969                         sdyn->output_section->vma + sdyn->output_offset,
5970                         sgot->contents);
5971           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5972           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5973         }
5974
5975       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5976     }
5977
5978   return TRUE;
5979 }
5980
5981 static void
5982 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
5983 {
5984   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
5985   struct elf32_arm_link_hash_table *globals;
5986
5987   i_ehdrp = elf_elfheader (abfd);
5988
5989   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
5990     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
5991   else
5992     i_ehdrp->e_ident[EI_OSABI] = 0;
5993   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
5994
5995   if (link_info)
5996     {
5997       globals = elf32_arm_hash_table (link_info);
5998       if (globals->byteswap_code)
5999         i_ehdrp->e_flags |= EF_ARM_BE8;
6000     }
6001 }
6002
6003 static enum elf_reloc_type_class
6004 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
6005 {
6006   switch ((int) ELF32_R_TYPE (rela->r_info))
6007     {
6008     case R_ARM_RELATIVE:
6009       return reloc_class_relative;
6010     case R_ARM_JUMP_SLOT:
6011       return reloc_class_plt;
6012     case R_ARM_COPY:
6013       return reloc_class_copy;
6014     default:
6015       return reloc_class_normal;
6016     }
6017 }
6018
6019 /* Set the right machine number for an Arm ELF file.  */
6020
6021 static bfd_boolean
6022 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
6023 {
6024   if (hdr->sh_type == SHT_NOTE)
6025     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
6026
6027   return TRUE;
6028 }
6029
6030 static void
6031 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
6032 {
6033   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
6034 }
6035
6036 /* Return TRUE if this is an unwinding table entry.  */
6037
6038 static bfd_boolean
6039 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
6040 {
6041   size_t len1, len2;
6042
6043   len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
6044   len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
6045   return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
6046           || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
6047 }
6048
6049
6050 /* Set the type and flags for an ARM section.  We do this by
6051    the section name, which is a hack, but ought to work.  */
6052
6053 static bfd_boolean
6054 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
6055 {
6056   const char * name;
6057
6058   name = bfd_get_section_name (abfd, sec);
6059
6060   if (is_arm_elf_unwind_section_name (abfd, name))
6061     {
6062       hdr->sh_type = SHT_ARM_EXIDX;
6063       hdr->sh_flags |= SHF_LINK_ORDER;
6064     }
6065   return TRUE;
6066 }
6067
6068 /* Handle an ARM specific section when reading an object file.  This is
6069    called when bfd_section_from_shdr finds a section with an unknown
6070    type.  */
6071
6072 static bfd_boolean
6073 elf32_arm_section_from_shdr (bfd *abfd,
6074                              Elf_Internal_Shdr * hdr,
6075                              const char *name,
6076                              int shindex)
6077 {
6078   /* There ought to be a place to keep ELF backend specific flags, but
6079      at the moment there isn't one.  We just keep track of the
6080      sections by their name, instead.  Fortunately, the ABI gives
6081      names for all the ARM specific sections, so we will probably get
6082      away with this.  */
6083   switch (hdr->sh_type)
6084     {
6085     case SHT_ARM_EXIDX:
6086       break;
6087
6088     default:
6089       return FALSE;
6090     }
6091
6092   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6093     return FALSE;
6094
6095   return TRUE;
6096 }
6097
6098 /* Called for each symbol.  Builds a section map based on mapping symbols.
6099    Does not alter any of the symbols.  */
6100
6101 static bfd_boolean
6102 elf32_arm_output_symbol_hook (struct bfd_link_info *info,
6103                               const char *name,
6104                               Elf_Internal_Sym *elfsym,
6105                               asection *input_sec,
6106                               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6107 {
6108   int mapcount;
6109   elf32_arm_section_map *map;
6110   struct elf32_arm_link_hash_table *globals;
6111
6112   /* Only do this on final link.  */
6113   if (info->relocatable)
6114     return TRUE;
6115
6116   /* Only build a map if we need to byteswap code.  */
6117   globals = elf32_arm_hash_table (info);
6118   if (!globals->byteswap_code)
6119     return TRUE;
6120
6121   /* We only want mapping symbols.  */
6122   if (! bfd_is_arm_mapping_symbol_name (name))
6123     return TRUE;
6124
6125   mapcount = ++(elf32_arm_section_data (input_sec)->mapcount);
6126   map = elf32_arm_section_data (input_sec)->map;
6127   /* TODO: This may be inefficient, but we probably don't usually have many
6128      mapping symbols per section.  */
6129   map = bfd_realloc (map, mapcount * sizeof (elf32_arm_section_map));
6130   elf32_arm_section_data (input_sec)->map = map;
6131
6132   map[mapcount - 1].vma = elfsym->st_value;
6133   map[mapcount - 1].type = name[1];
6134   return TRUE;
6135 }
6136
6137
6138 /* Allocate target specific section data.  */
6139
6140 static bfd_boolean
6141 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
6142 {
6143   struct _arm_elf_section_data *sdata;
6144   bfd_size_type amt = sizeof (*sdata);
6145
6146   sdata = bfd_zalloc (abfd, amt);
6147   if (sdata == NULL)
6148     return FALSE;
6149   sec->used_by_bfd = sdata;
6150
6151   return _bfd_elf_new_section_hook (abfd, sec);
6152 }
6153
6154
6155 /* Used to order a list of mapping symbols by address.  */
6156
6157 static int
6158 elf32_arm_compare_mapping (const void * a, const void * b)
6159 {
6160   return ((const elf32_arm_section_map *) a)->vma
6161          > ((const elf32_arm_section_map *) b)->vma;
6162 }
6163
6164
6165 /* Do code byteswapping.  Return FALSE afterwards so that the section is
6166    written out as normal.  */
6167
6168 static bfd_boolean
6169 elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
6170                          bfd_byte *contents)
6171 {
6172   int mapcount;
6173   elf32_arm_section_map *map;
6174   bfd_vma ptr;
6175   bfd_vma end;
6176   bfd_vma offset;
6177   bfd_byte tmp;
6178   int i;
6179
6180   mapcount = elf32_arm_section_data (sec)->mapcount;
6181   map = elf32_arm_section_data (sec)->map;
6182
6183   if (mapcount == 0)
6184     return FALSE;
6185
6186   qsort (map, mapcount, sizeof (elf32_arm_section_map),
6187          elf32_arm_compare_mapping);
6188
6189   offset = sec->output_section->vma + sec->output_offset;
6190   ptr = map[0].vma - offset;
6191   for (i = 0; i < mapcount; i++)
6192     {
6193       if (i == mapcount - 1)
6194         end = sec->size;
6195       else
6196         end = map[i + 1].vma - offset;
6197
6198       switch (map[i].type)
6199         {
6200         case 'a':
6201           /* Byte swap code words.  */
6202           while (ptr + 3 < end)
6203             {
6204               tmp = contents[ptr];
6205               contents[ptr] = contents[ptr + 3];
6206               contents[ptr + 3] = tmp;
6207               tmp = contents[ptr + 1];
6208               contents[ptr + 1] = contents[ptr + 2];
6209               contents[ptr + 2] = tmp;
6210               ptr += 4;
6211             }
6212           break;
6213
6214         case 't':
6215           /* Byte swap code halfwords.  */
6216           while (ptr + 1 < end)
6217             {
6218               tmp = contents[ptr];
6219               contents[ptr] = contents[ptr + 1];
6220               contents[ptr + 1] = tmp;
6221               ptr += 2;
6222             }
6223           break;
6224
6225         case 'd':
6226           /* Leave data alone.  */
6227           break;
6228         }
6229       ptr = end;
6230     }
6231   free (map);
6232   return FALSE;
6233 }
6234
6235 /* Display STT_ARM_TFUNC symbols as functions.  */
6236
6237 static void
6238 elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6239                              asymbol *asym)
6240 {
6241   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6242
6243   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
6244     elfsym->symbol.flags |= BSF_FUNCTION;
6245 }
6246
6247
6248 /* Mangle thumb function symbols as we read them in.  */
6249
6250 static void
6251 elf32_arm_swap_symbol_in (bfd * abfd,
6252                           const void *psrc,
6253                           const void *pshn,
6254                           Elf_Internal_Sym *dst)
6255 {
6256   bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst);
6257
6258   /* New EABI objects mark thumb function symbols by setting the low bit of
6259      the address.  Turn these into STT_ARM_TFUNC.  */
6260   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
6261       && (dst->st_value & 1))
6262     {
6263       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
6264       dst->st_value &= ~(bfd_vma) 1;
6265     }
6266 }
6267
6268
6269 /* Mangle thumb function symbols as we write them out.  */
6270
6271 static void
6272 elf32_arm_swap_symbol_out (bfd *abfd,
6273                            const Elf_Internal_Sym *src,
6274                            void *cdst,
6275                            void *shndx)
6276 {
6277   Elf_Internal_Sym newsym;
6278
6279   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
6280      of the address set, as per the new EABI.  We do this unconditionally
6281      because objcopy does not set the elf header flags until after
6282      it writes out the symbol table.  */
6283   if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
6284     {
6285       newsym = *src;
6286       newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
6287       newsym.st_value |= 1;
6288       
6289       src = &newsym;
6290     }
6291   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
6292 }
6293
6294 /* Add the PT_ARM_EXIDX program header.  */
6295
6296 static bfd_boolean
6297 elf32_arm_modify_segment_map (bfd *abfd, 
6298                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
6299 {
6300   struct elf_segment_map *m;
6301   asection *sec;
6302
6303   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
6304   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
6305     {
6306       /* If there is already a PT_ARM_EXIDX header, then we do not
6307          want to add another one.  This situation arises when running
6308          "strip"; the input binary already has the header.  */
6309       m = elf_tdata (abfd)->segment_map;
6310       while (m && m->p_type != PT_ARM_EXIDX)
6311         m = m->next;
6312       if (!m)
6313         {
6314           m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
6315           if (m == NULL)
6316             return FALSE;
6317           m->p_type = PT_ARM_EXIDX;
6318           m->count = 1;
6319           m->sections[0] = sec;
6320
6321           m->next = elf_tdata (abfd)->segment_map;
6322           elf_tdata (abfd)->segment_map = m;
6323         }
6324     }
6325
6326   return TRUE;
6327 }
6328
6329 /* We may add a PT_ARM_EXIDX program header.  */
6330
6331 static int
6332 elf32_arm_additional_program_headers (bfd *abfd)
6333 {
6334   asection *sec;
6335
6336   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
6337   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
6338     return 1;
6339   else
6340     return 0;
6341 }
6342
6343 /* We use this to override swap_symbol_in and swap_symbol_out.  */
6344 const struct elf_size_info elf32_arm_size_info = {
6345   sizeof (Elf32_External_Ehdr),
6346   sizeof (Elf32_External_Phdr),
6347   sizeof (Elf32_External_Shdr),
6348   sizeof (Elf32_External_Rel),
6349   sizeof (Elf32_External_Rela),
6350   sizeof (Elf32_External_Sym),
6351   sizeof (Elf32_External_Dyn),
6352   sizeof (Elf_External_Note),
6353   4,
6354   1,
6355   32, 2,
6356   ELFCLASS32, EV_CURRENT,
6357   bfd_elf32_write_out_phdrs,
6358   bfd_elf32_write_shdrs_and_ehdr,
6359   bfd_elf32_write_relocs,
6360   elf32_arm_swap_symbol_in,
6361   elf32_arm_swap_symbol_out,
6362   bfd_elf32_slurp_reloc_table,
6363   bfd_elf32_slurp_symbol_table,
6364   bfd_elf32_swap_dyn_in,
6365   bfd_elf32_swap_dyn_out,
6366   bfd_elf32_swap_reloc_in,
6367   bfd_elf32_swap_reloc_out,
6368   bfd_elf32_swap_reloca_in,
6369   bfd_elf32_swap_reloca_out
6370 };
6371
6372 #define ELF_ARCH                        bfd_arch_arm
6373 #define ELF_MACHINE_CODE                EM_ARM
6374 #ifdef __QNXTARGET__
6375 #define ELF_MAXPAGESIZE                 0x1000
6376 #else
6377 #define ELF_MAXPAGESIZE                 0x8000
6378 #endif
6379 #define ELF_MINPAGESIZE                 0x1000
6380
6381 #define bfd_elf32_mkobject                      elf32_arm_mkobject
6382
6383 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
6384 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
6385 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
6386 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
6387 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
6388 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
6389 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
6390 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
6391 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
6392
6393 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
6394 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
6395 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
6396 #define elf_backend_check_relocs                elf32_arm_check_relocs
6397 #define elf_backend_relocate_section            elf32_arm_relocate_section
6398 #define elf_backend_write_section               elf32_arm_write_section
6399 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
6400 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
6401 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
6402 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
6403 #define elf_backend_link_output_symbol_hook     elf32_arm_output_symbol_hook
6404 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
6405 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
6406 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
6407 #define elf_backend_object_p                    elf32_arm_object_p
6408 #define elf_backend_section_flags               elf32_arm_section_flags
6409 #define elf_backend_fake_sections               elf32_arm_fake_sections
6410 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
6411 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
6412 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
6413 #define elf_backend_symbol_processing           elf32_arm_symbol_processing
6414 #define elf_backend_size_info                   elf32_arm_size_info
6415 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
6416 #define elf_backend_additional_program_headers \
6417   elf32_arm_additional_program_headers
6418
6419 #define elf_backend_can_refcount    1
6420 #define elf_backend_can_gc_sections 1
6421 #define elf_backend_plt_readonly    1
6422 #define elf_backend_want_got_plt    1
6423 #define elf_backend_want_plt_sym    0
6424 #define elf_backend_may_use_rel_p   1
6425 #define elf_backend_may_use_rela_p  0
6426 #define elf_backend_default_use_rela_p 0
6427 #define elf_backend_rela_normal     0
6428
6429 #define elf_backend_got_header_size     12
6430
6431 #include "elf32-target.h"
6432
6433 /* VxWorks Targets */
6434
6435 #undef TARGET_LITTLE_SYM
6436 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
6437 #undef TARGET_LITTLE_NAME
6438 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
6439 #undef TARGET_BIG_SYM
6440 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
6441 #undef TARGET_BIG_NAME
6442 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
6443
6444 /* Like elf32_arm_link_hash_table_create -- but overrides
6445    appropriately for VxWorks.  */
6446 static struct bfd_link_hash_table *
6447 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
6448 {
6449   struct bfd_link_hash_table *ret;
6450
6451   ret = elf32_arm_link_hash_table_create (abfd);
6452   if (ret)
6453     {
6454       struct elf32_arm_link_hash_table *htab
6455         = (struct elf32_arm_link_hash_table *)ret;
6456       htab->use_rel = 0;
6457     }
6458   return ret;
6459 }     
6460
6461 #undef elf32_bed
6462 #define elf32_bed elf32_arm_vxworks_bed
6463
6464 #undef bfd_elf32_bfd_link_hash_table_create
6465 #define bfd_elf32_bfd_link_hash_table_create \
6466   elf32_arm_vxworks_link_hash_table_create
6467
6468 #undef elf_backend_may_use_rel_p
6469 #define elf_backend_may_use_rel_p   0
6470 #undef elf_backend_may_use_rela_p
6471 #define elf_backend_may_use_rela_p  1
6472 #undef elf_backend_default_use_rela_p
6473 #define elf_backend_default_use_rela_p 1
6474 #undef elf_backend_rela_normal
6475 #define elf_backend_rela_normal     1
6476
6477 #include "elf32-target.h"
6478
6479
6480 /* Symbian OS Targets */
6481
6482 #undef TARGET_LITTLE_SYM
6483 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
6484 #undef TARGET_LITTLE_NAME
6485 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
6486 #undef TARGET_BIG_SYM
6487 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
6488 #undef TARGET_BIG_NAME
6489 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
6490
6491 /* Like elf32_arm_link_hash_table_create -- but overrides
6492    appropriately for Symbian OS.  */
6493 static struct bfd_link_hash_table *
6494 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
6495 {
6496   struct bfd_link_hash_table *ret;
6497
6498   ret = elf32_arm_link_hash_table_create (abfd);
6499   if (ret)
6500     {
6501       struct elf32_arm_link_hash_table *htab
6502         = (struct elf32_arm_link_hash_table *)ret;
6503       /* There is no PLT header for Symbian OS.  */
6504       htab->plt_header_size = 0;
6505       /* The PLT entries are each three instructions.  */
6506       htab->plt_entry_size = 4 * NUM_ELEM (elf32_arm_symbian_plt_entry);
6507       htab->symbian_p = 1;
6508       htab->root.is_relocatable_executable = 1;
6509     }
6510   return ret;
6511 }     
6512
6513 static struct bfd_elf_special_section const 
6514   elf32_arm_symbian_special_sections[]=
6515 {
6516   /* In a BPABI executable, the dynamic linking sections do not go in
6517      the loadable read-only segment.  The post-linker may wish to
6518      refer to these sections, but they are not part of the final
6519      program image.  */
6520   { ".dynamic",        8,  0, SHT_DYNAMIC,  0 },
6521   { ".dynstr",         7,  0, SHT_STRTAB,   0 },
6522   { ".dynsym",         7,  0, SHT_DYNSYM,   0 },
6523   { ".got",            4,  0, SHT_PROGBITS, 0 },
6524   { ".hash",           5,  0, SHT_HASH,     0 },
6525   /* These sections do not need to be writable as the SymbianOS
6526      postlinker will arrange things so that no dynamic relocation is
6527      required.  */
6528   { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC },
6529   { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC },
6530   { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC },
6531   { NULL,              0,  0, 0,            0 }
6532 };
6533
6534 static void
6535 elf32_arm_symbian_begin_write_processing (bfd *abfd, 
6536                                           struct bfd_link_info *link_info
6537                                             ATTRIBUTE_UNUSED)
6538 {
6539   /* BPABI objects are never loaded directly by an OS kernel; they are
6540      processed by a postlinker first, into an OS-specific format.  If
6541      the D_PAGED bit is set on the file, BFD will align segments on
6542      page boundaries, so that an OS can directly map the file.  With
6543      BPABI objects, that just results in wasted space.  In addition,
6544      because we clear the D_PAGED bit, map_sections_to_segments will
6545      recognize that the program headers should not be mapped into any
6546      loadable segment.  */
6547   abfd->flags &= ~D_PAGED;
6548 }
6549
6550 static bfd_boolean
6551 elf32_arm_symbian_modify_segment_map (bfd *abfd, 
6552                                       struct bfd_link_info *info)
6553 {
6554   struct elf_segment_map *m;
6555   asection *dynsec;
6556
6557   /* BPABI shared libraries and executables should have a PT_DYNAMIC
6558      segment.  However, because the .dynamic section is not marked
6559      with SEC_LOAD, the generic ELF code will not create such a
6560      segment.  */
6561   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
6562   if (dynsec)
6563     {
6564       m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
6565       m->next = elf_tdata (abfd)->segment_map;
6566       elf_tdata (abfd)->segment_map = m;
6567     }
6568
6569   /* Also call the generic arm routine.  */
6570   return elf32_arm_modify_segment_map (abfd, info);
6571 }
6572
6573 #undef elf32_bed
6574 #define elf32_bed elf32_arm_symbian_bed
6575
6576 /* The dynamic sections are not allocated on SymbianOS; the postlinker
6577    will process them and then discard them.  */
6578 #undef ELF_DYNAMIC_SEC_FLAGS
6579 #define ELF_DYNAMIC_SEC_FLAGS \
6580   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
6581
6582 #undef bfd_elf32_bfd_link_hash_table_create
6583 #define bfd_elf32_bfd_link_hash_table_create \
6584   elf32_arm_symbian_link_hash_table_create
6585
6586 #undef elf_backend_special_sections
6587 #define elf_backend_special_sections elf32_arm_symbian_special_sections
6588
6589 #undef elf_backend_begin_write_processing
6590 #define elf_backend_begin_write_processing \
6591     elf32_arm_symbian_begin_write_processing
6592
6593 #undef elf_backend_modify_segment_map
6594 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
6595
6596 /* There is no .got section for BPABI objects, and hence no header.  */
6597 #undef elf_backend_got_header_size
6598 #define elf_backend_got_header_size 0
6599
6600 /* Similarly, there is no .got.plt section.  */
6601 #undef elf_backend_want_got_plt
6602 #define elf_backend_want_got_plt 0
6603
6604 #undef elf_backend_may_use_rel_p
6605 #define elf_backend_may_use_rel_p   1
6606 #undef elf_backend_may_use_rela_p
6607 #define elf_backend_may_use_rela_p  0
6608 #undef elf_backend_default_use_rela_p
6609 #define elf_backend_default_use_rela_p 0
6610 #undef elf_backend_rela_normal
6611 #define elf_backend_rela_normal     0
6612
6613 #include "elf32-target.h"