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