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