OSDN Git Service

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