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