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