elf32-arm.c revision 214157
1/* 32-bit ELF support for ARM
2   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3   Free Software Foundation, Inc.
4
5   This file is part of BFD, the Binary File Descriptor library.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libiberty.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf-vxworks.h"
27#include "elf/arm.h"
28
29#ifndef NUM_ELEM
30#define NUM_ELEM(a)  (sizeof (a) / (sizeof (a)[0]))
31#endif
32
33/* Return the relocation section associated with NAME.  HTAB is the
34   bfd's elf32_arm_link_hash_entry.  */
35#define RELOC_SECTION(HTAB, NAME) \
36  ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
37
38/* Return size of a relocation entry.  HTAB is the bfd's
39   elf32_arm_link_hash_entry.  */
40#define RELOC_SIZE(HTAB) \
41  ((HTAB)->use_rel \
42   ? sizeof (Elf32_External_Rel) \
43   : sizeof (Elf32_External_Rela))
44
45/* Return function to swap relocations in.  HTAB is the bfd's
46   elf32_arm_link_hash_entry.  */
47#define SWAP_RELOC_IN(HTAB) \
48  ((HTAB)->use_rel \
49   ? bfd_elf32_swap_reloc_in \
50   : bfd_elf32_swap_reloca_in)
51
52/* Return function to swap relocations out.  HTAB is the bfd's
53   elf32_arm_link_hash_entry.  */
54#define SWAP_RELOC_OUT(HTAB) \
55  ((HTAB)->use_rel \
56   ? bfd_elf32_swap_reloc_out \
57   : bfd_elf32_swap_reloca_out)
58
59#define elf_info_to_howto               0
60#define elf_info_to_howto_rel           elf32_arm_info_to_howto
61
62#define ARM_ELF_ABI_VERSION		0
63#ifdef __FreeBSD__
64#define ARM_ELF_OS_ABI_VERSION		ELFOSABI_FREEBSD
65#else
66#define ARM_ELF_OS_ABI_VERSION		ELFOSABI_ARM
67#endif
68
69static const struct elf_backend_data elf32_arm_vxworks_bed;
70
71/* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
72   R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
73   in that slot.  */
74
75static reloc_howto_type elf32_arm_howto_table_1[] =
76{
77  /* No relocation */
78  HOWTO (R_ARM_NONE,		/* type */
79	 0,			/* rightshift */
80	 0,			/* size (0 = byte, 1 = short, 2 = long) */
81	 0,			/* bitsize */
82	 FALSE,			/* pc_relative */
83	 0,			/* bitpos */
84	 complain_overflow_dont,/* complain_on_overflow */
85	 bfd_elf_generic_reloc,	/* special_function */
86	 "R_ARM_NONE",		/* name */
87	 FALSE,			/* partial_inplace */
88	 0,			/* src_mask */
89	 0,			/* dst_mask */
90	 FALSE),		/* pcrel_offset */
91
92  HOWTO (R_ARM_PC24,		/* type */
93	 2,			/* rightshift */
94	 2,			/* size (0 = byte, 1 = short, 2 = long) */
95	 24,			/* bitsize */
96	 TRUE,			/* pc_relative */
97	 0,			/* bitpos */
98	 complain_overflow_signed,/* complain_on_overflow */
99	 bfd_elf_generic_reloc,	/* special_function */
100	 "R_ARM_PC24",		/* name */
101	 FALSE,			/* partial_inplace */
102	 0x00ffffff,		/* src_mask */
103	 0x00ffffff,		/* dst_mask */
104	 TRUE),			/* pcrel_offset */
105
106  /* 32 bit absolute */
107  HOWTO (R_ARM_ABS32,		/* type */
108	 0,			/* rightshift */
109	 2,			/* size (0 = byte, 1 = short, 2 = long) */
110	 32,			/* bitsize */
111	 FALSE,			/* pc_relative */
112	 0,			/* bitpos */
113	 complain_overflow_bitfield,/* complain_on_overflow */
114	 bfd_elf_generic_reloc,	/* special_function */
115	 "R_ARM_ABS32",		/* name */
116	 FALSE,			/* partial_inplace */
117	 0xffffffff,		/* src_mask */
118	 0xffffffff,		/* dst_mask */
119	 FALSE),		/* pcrel_offset */
120
121  /* standard 32bit pc-relative reloc */
122  HOWTO (R_ARM_REL32,		/* type */
123	 0,			/* rightshift */
124	 2,			/* size (0 = byte, 1 = short, 2 = long) */
125	 32,			/* bitsize */
126	 TRUE,			/* pc_relative */
127	 0,			/* bitpos */
128	 complain_overflow_bitfield,/* complain_on_overflow */
129	 bfd_elf_generic_reloc,	/* special_function */
130	 "R_ARM_REL32",		/* name */
131	 FALSE,			/* partial_inplace */
132	 0xffffffff,		/* src_mask */
133	 0xffffffff,		/* dst_mask */
134	 TRUE),			/* pcrel_offset */
135
136  /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
137  HOWTO (R_ARM_PC13,		/* type */
138	 0,			/* rightshift */
139	 0,			/* size (0 = byte, 1 = short, 2 = long) */
140	 8,			/* bitsize */
141	 FALSE,			/* pc_relative */
142	 0,			/* bitpos */
143	 complain_overflow_bitfield,/* complain_on_overflow */
144	 bfd_elf_generic_reloc,	/* special_function */
145	 "R_ARM_PC13",		/* name */
146	 FALSE,			/* partial_inplace */
147	 0x000000ff,		/* src_mask */
148	 0x000000ff,		/* dst_mask */
149	 FALSE),		/* pcrel_offset */
150
151   /* 16 bit absolute */
152  HOWTO (R_ARM_ABS16,		/* type */
153	 0,			/* rightshift */
154	 1,			/* size (0 = byte, 1 = short, 2 = long) */
155	 16,			/* bitsize */
156	 FALSE,			/* pc_relative */
157	 0,			/* bitpos */
158	 complain_overflow_bitfield,/* complain_on_overflow */
159	 bfd_elf_generic_reloc,	/* special_function */
160	 "R_ARM_ABS16",		/* name */
161	 FALSE,			/* partial_inplace */
162	 0x0000ffff,		/* src_mask */
163	 0x0000ffff,		/* dst_mask */
164	 FALSE),		/* pcrel_offset */
165
166  /* 12 bit absolute */
167  HOWTO (R_ARM_ABS12,		/* type */
168	 0,			/* rightshift */
169	 2,			/* size (0 = byte, 1 = short, 2 = long) */
170	 12,			/* bitsize */
171	 FALSE,			/* pc_relative */
172	 0,			/* bitpos */
173	 complain_overflow_bitfield,/* complain_on_overflow */
174	 bfd_elf_generic_reloc,	/* special_function */
175	 "R_ARM_ABS12",		/* name */
176	 FALSE,			/* partial_inplace */
177	 0x00000fff,		/* src_mask */
178	 0x00000fff,		/* dst_mask */
179	 FALSE),		/* pcrel_offset */
180
181  HOWTO (R_ARM_THM_ABS5,	/* type */
182	 6,			/* rightshift */
183	 1,			/* size (0 = byte, 1 = short, 2 = long) */
184	 5,			/* bitsize */
185	 FALSE,			/* pc_relative */
186	 0,			/* bitpos */
187	 complain_overflow_bitfield,/* complain_on_overflow */
188	 bfd_elf_generic_reloc,	/* special_function */
189	 "R_ARM_THM_ABS5",	/* name */
190	 FALSE,			/* partial_inplace */
191	 0x000007e0,		/* src_mask */
192	 0x000007e0,		/* dst_mask */
193	 FALSE),		/* pcrel_offset */
194
195  /* 8 bit absolute */
196  HOWTO (R_ARM_ABS8,		/* type */
197	 0,			/* rightshift */
198	 0,			/* size (0 = byte, 1 = short, 2 = long) */
199	 8,			/* bitsize */
200	 FALSE,			/* pc_relative */
201	 0,			/* bitpos */
202	 complain_overflow_bitfield,/* complain_on_overflow */
203	 bfd_elf_generic_reloc,	/* special_function */
204	 "R_ARM_ABS8",		/* name */
205	 FALSE,			/* partial_inplace */
206	 0x000000ff,		/* src_mask */
207	 0x000000ff,		/* dst_mask */
208	 FALSE),		/* pcrel_offset */
209
210  HOWTO (R_ARM_SBREL32,		/* type */
211	 0,			/* rightshift */
212	 2,			/* size (0 = byte, 1 = short, 2 = long) */
213	 32,			/* bitsize */
214	 FALSE,			/* pc_relative */
215	 0,			/* bitpos */
216	 complain_overflow_dont,/* complain_on_overflow */
217	 bfd_elf_generic_reloc,	/* special_function */
218	 "R_ARM_SBREL32",	/* name */
219	 FALSE,			/* partial_inplace */
220	 0xffffffff,		/* src_mask */
221	 0xffffffff,		/* dst_mask */
222	 FALSE),		/* pcrel_offset */
223
224  /* FIXME: Has two more bits of offset in Thumb32.  */
225  HOWTO (R_ARM_THM_CALL,	/* type */
226	 1,			/* rightshift */
227	 2,			/* size (0 = byte, 1 = short, 2 = long) */
228	 23,			/* bitsize */
229	 TRUE,			/* pc_relative */
230	 0,			/* bitpos */
231	 complain_overflow_signed,/* complain_on_overflow */
232	 bfd_elf_generic_reloc,	/* special_function */
233	 "R_ARM_THM_CALL",	/* name */
234	 FALSE,			/* partial_inplace */
235	 0x07ff07ff,		/* src_mask */
236	 0x07ff07ff,		/* dst_mask */
237	 TRUE),			/* pcrel_offset */
238
239  HOWTO (R_ARM_THM_PC8,	        /* type */
240	 1,			/* rightshift */
241	 1,			/* size (0 = byte, 1 = short, 2 = long) */
242	 8,			/* bitsize */
243	 TRUE,			/* pc_relative */
244	 0,			/* bitpos */
245	 complain_overflow_signed,/* complain_on_overflow */
246	 bfd_elf_generic_reloc,	/* special_function */
247	 "R_ARM_THM_PC8",	/* name */
248	 FALSE,			/* partial_inplace */
249	 0x000000ff,		/* src_mask */
250	 0x000000ff,		/* dst_mask */
251	 TRUE),			/* pcrel_offset */
252
253  HOWTO (R_ARM_BREL_ADJ,	/* type */
254	 1,			/* rightshift */
255	 1,			/* size (0 = byte, 1 = short, 2 = long) */
256	 32,			/* bitsize */
257	 FALSE,			/* pc_relative */
258	 0,			/* bitpos */
259	 complain_overflow_signed,/* complain_on_overflow */
260	 bfd_elf_generic_reloc,	/* special_function */
261	 "R_ARM_BREL_ADJ",	/* name */
262	 FALSE,			/* partial_inplace */
263	 0xffffffff,		/* src_mask */
264	 0xffffffff,		/* dst_mask */
265	 FALSE),		/* pcrel_offset */
266
267  HOWTO (R_ARM_SWI24,		/* type */
268	 0,			/* rightshift */
269	 0,			/* size (0 = byte, 1 = short, 2 = long) */
270	 0,			/* bitsize */
271	 FALSE,			/* pc_relative */
272	 0,			/* bitpos */
273	 complain_overflow_signed,/* complain_on_overflow */
274	 bfd_elf_generic_reloc,	/* special_function */
275	 "R_ARM_SWI24",		/* name */
276	 FALSE,			/* partial_inplace */
277	 0x00000000,		/* src_mask */
278	 0x00000000,		/* dst_mask */
279	 FALSE),		/* pcrel_offset */
280
281  HOWTO (R_ARM_THM_SWI8,	/* type */
282	 0,			/* rightshift */
283	 0,			/* size (0 = byte, 1 = short, 2 = long) */
284	 0,			/* bitsize */
285	 FALSE,			/* pc_relative */
286	 0,			/* bitpos */
287	 complain_overflow_signed,/* complain_on_overflow */
288	 bfd_elf_generic_reloc,	/* special_function */
289	 "R_ARM_SWI8",		/* name */
290	 FALSE,			/* partial_inplace */
291	 0x00000000,		/* src_mask */
292	 0x00000000,		/* dst_mask */
293	 FALSE),		/* pcrel_offset */
294
295  /* BLX instruction for the ARM.  */
296  HOWTO (R_ARM_XPC25,		/* type */
297	 2,			/* rightshift */
298	 2,			/* size (0 = byte, 1 = short, 2 = long) */
299	 25,			/* bitsize */
300	 TRUE,			/* pc_relative */
301	 0,			/* bitpos */
302	 complain_overflow_signed,/* complain_on_overflow */
303	 bfd_elf_generic_reloc,	/* special_function */
304	 "R_ARM_XPC25",		/* name */
305	 FALSE,			/* partial_inplace */
306	 0x00ffffff,		/* src_mask */
307	 0x00ffffff,		/* dst_mask */
308	 TRUE),			/* pcrel_offset */
309
310  /* BLX instruction for the Thumb.  */
311  HOWTO (R_ARM_THM_XPC22,	/* type */
312	 2,			/* rightshift */
313	 2,			/* size (0 = byte, 1 = short, 2 = long) */
314	 22,			/* bitsize */
315	 TRUE,			/* pc_relative */
316	 0,			/* bitpos */
317	 complain_overflow_signed,/* complain_on_overflow */
318	 bfd_elf_generic_reloc,	/* special_function */
319	 "R_ARM_THM_XPC22",	/* name */
320	 FALSE,			/* partial_inplace */
321	 0x07ff07ff,		/* src_mask */
322	 0x07ff07ff,		/* dst_mask */
323	 TRUE),			/* pcrel_offset */
324
325  /* Dynamic TLS relocations.  */
326
327  HOWTO (R_ARM_TLS_DTPMOD32,	/* type */
328         0,                     /* rightshift */
329         2,                     /* size (0 = byte, 1 = short, 2 = long) */
330         32,                    /* bitsize */
331         FALSE,                 /* pc_relative */
332         0,                     /* bitpos */
333         complain_overflow_bitfield,/* complain_on_overflow */
334         bfd_elf_generic_reloc, /* special_function */
335         "R_ARM_TLS_DTPMOD32",	/* name */
336         TRUE,			/* partial_inplace */
337         0xffffffff,		/* src_mask */
338         0xffffffff,		/* dst_mask */
339         FALSE),                /* pcrel_offset */
340
341  HOWTO (R_ARM_TLS_DTPOFF32,	/* type */
342         0,                     /* rightshift */
343         2,                     /* size (0 = byte, 1 = short, 2 = long) */
344         32,                    /* bitsize */
345         FALSE,                 /* pc_relative */
346         0,                     /* bitpos */
347         complain_overflow_bitfield,/* complain_on_overflow */
348         bfd_elf_generic_reloc, /* special_function */
349         "R_ARM_TLS_DTPOFF32",	/* name */
350         TRUE,			/* partial_inplace */
351         0xffffffff,		/* src_mask */
352         0xffffffff,		/* dst_mask */
353         FALSE),                /* pcrel_offset */
354
355  HOWTO (R_ARM_TLS_TPOFF32,	/* type */
356         0,                     /* rightshift */
357         2,                     /* size (0 = byte, 1 = short, 2 = long) */
358         32,                    /* bitsize */
359         FALSE,                 /* pc_relative */
360         0,                     /* bitpos */
361         complain_overflow_bitfield,/* complain_on_overflow */
362         bfd_elf_generic_reloc, /* special_function */
363         "R_ARM_TLS_TPOFF32",	/* name */
364         TRUE,			/* partial_inplace */
365         0xffffffff,		/* src_mask */
366         0xffffffff,		/* dst_mask */
367         FALSE),                /* pcrel_offset */
368
369  /* Relocs used in ARM Linux */
370
371  HOWTO (R_ARM_COPY,		/* type */
372         0,                     /* rightshift */
373         2,                     /* size (0 = byte, 1 = short, 2 = long) */
374         32,                    /* bitsize */
375         FALSE,                 /* pc_relative */
376         0,                     /* bitpos */
377         complain_overflow_bitfield,/* complain_on_overflow */
378         bfd_elf_generic_reloc, /* special_function */
379         "R_ARM_COPY",		/* name */
380         TRUE,			/* partial_inplace */
381         0xffffffff,		/* src_mask */
382         0xffffffff,		/* dst_mask */
383         FALSE),                /* pcrel_offset */
384
385  HOWTO (R_ARM_GLOB_DAT,	/* type */
386         0,                     /* rightshift */
387         2,                     /* size (0 = byte, 1 = short, 2 = long) */
388         32,                    /* bitsize */
389         FALSE,                 /* pc_relative */
390         0,                     /* bitpos */
391         complain_overflow_bitfield,/* complain_on_overflow */
392         bfd_elf_generic_reloc, /* special_function */
393         "R_ARM_GLOB_DAT",	/* name */
394         TRUE,			/* partial_inplace */
395         0xffffffff,		/* src_mask */
396         0xffffffff,		/* dst_mask */
397         FALSE),                /* pcrel_offset */
398
399  HOWTO (R_ARM_JUMP_SLOT,	/* type */
400         0,                     /* rightshift */
401         2,                     /* size (0 = byte, 1 = short, 2 = long) */
402         32,                    /* bitsize */
403         FALSE,                 /* pc_relative */
404         0,                     /* bitpos */
405         complain_overflow_bitfield,/* complain_on_overflow */
406         bfd_elf_generic_reloc, /* special_function */
407         "R_ARM_JUMP_SLOT",	/* name */
408         TRUE,			/* partial_inplace */
409         0xffffffff,		/* src_mask */
410         0xffffffff,		/* dst_mask */
411         FALSE),                /* pcrel_offset */
412
413  HOWTO (R_ARM_RELATIVE,	/* type */
414         0,                     /* rightshift */
415         2,                     /* size (0 = byte, 1 = short, 2 = long) */
416         32,                    /* bitsize */
417         FALSE,                 /* pc_relative */
418         0,                     /* bitpos */
419         complain_overflow_bitfield,/* complain_on_overflow */
420         bfd_elf_generic_reloc, /* special_function */
421         "R_ARM_RELATIVE",	/* name */
422         TRUE,			/* partial_inplace */
423         0xffffffff,		/* src_mask */
424         0xffffffff,		/* dst_mask */
425         FALSE),                /* pcrel_offset */
426
427  HOWTO (R_ARM_GOTOFF32,	/* type */
428         0,                     /* rightshift */
429         2,                     /* size (0 = byte, 1 = short, 2 = long) */
430         32,                    /* bitsize */
431         FALSE,                 /* pc_relative */
432         0,                     /* bitpos */
433         complain_overflow_bitfield,/* complain_on_overflow */
434         bfd_elf_generic_reloc, /* special_function */
435         "R_ARM_GOTOFF32",	/* name */
436         TRUE,			/* partial_inplace */
437         0xffffffff,		/* src_mask */
438         0xffffffff,		/* dst_mask */
439         FALSE),                /* pcrel_offset */
440
441  HOWTO (R_ARM_GOTPC,		/* type */
442         0,                     /* rightshift */
443         2,                     /* size (0 = byte, 1 = short, 2 = long) */
444         32,                    /* bitsize */
445         TRUE,			/* pc_relative */
446         0,                     /* bitpos */
447         complain_overflow_bitfield,/* complain_on_overflow */
448         bfd_elf_generic_reloc, /* special_function */
449         "R_ARM_GOTPC",		/* name */
450         TRUE,			/* partial_inplace */
451         0xffffffff,		/* src_mask */
452         0xffffffff,		/* dst_mask */
453         TRUE),			/* pcrel_offset */
454
455  HOWTO (R_ARM_GOT32,		/* type */
456         0,                     /* rightshift */
457         2,                     /* size (0 = byte, 1 = short, 2 = long) */
458         32,                    /* bitsize */
459         FALSE,			/* pc_relative */
460         0,                     /* bitpos */
461         complain_overflow_bitfield,/* complain_on_overflow */
462         bfd_elf_generic_reloc, /* special_function */
463         "R_ARM_GOT32",		/* name */
464         TRUE,			/* partial_inplace */
465         0xffffffff,		/* src_mask */
466         0xffffffff,		/* dst_mask */
467         FALSE),		/* pcrel_offset */
468
469  HOWTO (R_ARM_PLT32,		/* type */
470         2,                     /* rightshift */
471         2,                     /* size (0 = byte, 1 = short, 2 = long) */
472         24,                    /* bitsize */
473         TRUE,			/* pc_relative */
474         0,                     /* bitpos */
475         complain_overflow_bitfield,/* complain_on_overflow */
476         bfd_elf_generic_reloc, /* special_function */
477         "R_ARM_PLT32",		/* name */
478         FALSE,			/* partial_inplace */
479         0x00ffffff,		/* src_mask */
480         0x00ffffff,		/* dst_mask */
481         TRUE),			/* pcrel_offset */
482
483  HOWTO (R_ARM_CALL,		/* type */
484	 2,			/* rightshift */
485	 2,			/* size (0 = byte, 1 = short, 2 = long) */
486	 24,			/* bitsize */
487	 TRUE,			/* pc_relative */
488	 0,			/* bitpos */
489	 complain_overflow_signed,/* complain_on_overflow */
490	 bfd_elf_generic_reloc,	/* special_function */
491	 "R_ARM_CALL",		/* name */
492	 FALSE,			/* partial_inplace */
493	 0x00ffffff,		/* src_mask */
494	 0x00ffffff,		/* dst_mask */
495	 TRUE),			/* pcrel_offset */
496
497  HOWTO (R_ARM_JUMP24,		/* type */
498	 2,			/* rightshift */
499	 2,			/* size (0 = byte, 1 = short, 2 = long) */
500	 24,			/* bitsize */
501	 TRUE,			/* pc_relative */
502	 0,			/* bitpos */
503	 complain_overflow_signed,/* complain_on_overflow */
504	 bfd_elf_generic_reloc,	/* special_function */
505	 "R_ARM_JUMP24",	/* name */
506	 FALSE,			/* partial_inplace */
507	 0x00ffffff,		/* src_mask */
508	 0x00ffffff,		/* dst_mask */
509	 TRUE),			/* pcrel_offset */
510
511  HOWTO (R_ARM_THM_JUMP24,	/* type */
512	 1,			/* rightshift */
513	 2,			/* size (0 = byte, 1 = short, 2 = long) */
514	 24,			/* bitsize */
515	 TRUE,			/* pc_relative */
516	 0,			/* bitpos */
517	 complain_overflow_signed,/* complain_on_overflow */
518	 bfd_elf_generic_reloc,	/* special_function */
519	 "R_ARM_THM_JUMP24",	/* name */
520	 FALSE,			/* partial_inplace */
521	 0x07ff2fff,		/* src_mask */
522	 0x07ff2fff,		/* dst_mask */
523	 TRUE),			/* pcrel_offset */
524
525  HOWTO (R_ARM_BASE_ABS,	/* type */
526	 0,			/* rightshift */
527	 2,			/* size (0 = byte, 1 = short, 2 = long) */
528	 32,			/* bitsize */
529	 FALSE,			/* pc_relative */
530	 0,			/* bitpos */
531	 complain_overflow_dont,/* complain_on_overflow */
532	 bfd_elf_generic_reloc,	/* special_function */
533	 "R_ARM_BASE_ABS",	/* name */
534	 FALSE,			/* partial_inplace */
535	 0xffffffff,		/* src_mask */
536	 0xffffffff,		/* dst_mask */
537	 FALSE),		/* pcrel_offset */
538
539  HOWTO (R_ARM_ALU_PCREL7_0,	/* type */
540	 0,			/* rightshift */
541	 2,			/* size (0 = byte, 1 = short, 2 = long) */
542	 12,			/* bitsize */
543	 TRUE,			/* pc_relative */
544	 0,			/* bitpos */
545	 complain_overflow_dont,/* complain_on_overflow */
546	 bfd_elf_generic_reloc,	/* special_function */
547	 "R_ARM_ALU_PCREL_7_0",	/* name */
548	 FALSE,			/* partial_inplace */
549	 0x00000fff,		/* src_mask */
550	 0x00000fff,		/* dst_mask */
551	 TRUE),			/* pcrel_offset */
552
553  HOWTO (R_ARM_ALU_PCREL15_8,	/* type */
554	 0,			/* rightshift */
555	 2,			/* size (0 = byte, 1 = short, 2 = long) */
556	 12,			/* bitsize */
557	 TRUE,			/* pc_relative */
558	 8,			/* bitpos */
559	 complain_overflow_dont,/* complain_on_overflow */
560	 bfd_elf_generic_reloc,	/* special_function */
561	 "R_ARM_ALU_PCREL_15_8",/* name */
562	 FALSE,			/* partial_inplace */
563	 0x00000fff,		/* src_mask */
564	 0x00000fff,		/* dst_mask */
565	 TRUE),			/* pcrel_offset */
566
567  HOWTO (R_ARM_ALU_PCREL23_15,	/* type */
568	 0,			/* rightshift */
569	 2,			/* size (0 = byte, 1 = short, 2 = long) */
570	 12,			/* bitsize */
571	 TRUE,			/* pc_relative */
572	 16,			/* bitpos */
573	 complain_overflow_dont,/* complain_on_overflow */
574	 bfd_elf_generic_reloc,	/* special_function */
575	 "R_ARM_ALU_PCREL_23_15",/* name */
576	 FALSE,			/* partial_inplace */
577	 0x00000fff,		/* src_mask */
578	 0x00000fff,		/* dst_mask */
579	 TRUE),			/* pcrel_offset */
580
581  HOWTO (R_ARM_LDR_SBREL_11_0,	/* type */
582	 0,			/* rightshift */
583	 2,			/* size (0 = byte, 1 = short, 2 = long) */
584	 12,			/* bitsize */
585	 FALSE,			/* pc_relative */
586	 0,			/* bitpos */
587	 complain_overflow_dont,/* complain_on_overflow */
588	 bfd_elf_generic_reloc,	/* special_function */
589	 "R_ARM_LDR_SBREL_11_0",/* name */
590	 FALSE,			/* partial_inplace */
591	 0x00000fff,		/* src_mask */
592	 0x00000fff,		/* dst_mask */
593	 FALSE),		/* pcrel_offset */
594
595  HOWTO (R_ARM_ALU_SBREL_19_12,	/* type */
596	 0,			/* rightshift */
597	 2,			/* size (0 = byte, 1 = short, 2 = long) */
598	 8,			/* bitsize */
599	 FALSE,			/* pc_relative */
600	 12,			/* bitpos */
601	 complain_overflow_dont,/* complain_on_overflow */
602	 bfd_elf_generic_reloc,	/* special_function */
603	 "R_ARM_ALU_SBREL_19_12",/* name */
604	 FALSE,			/* partial_inplace */
605	 0x000ff000,		/* src_mask */
606	 0x000ff000,		/* dst_mask */
607	 FALSE),		/* pcrel_offset */
608
609  HOWTO (R_ARM_ALU_SBREL_27_20,	/* type */
610	 0,			/* rightshift */
611	 2,			/* size (0 = byte, 1 = short, 2 = long) */
612	 8,			/* bitsize */
613	 FALSE,			/* pc_relative */
614	 20,			/* bitpos */
615	 complain_overflow_dont,/* complain_on_overflow */
616	 bfd_elf_generic_reloc,	/* special_function */
617	 "R_ARM_ALU_SBREL_27_20",/* name */
618	 FALSE,			/* partial_inplace */
619	 0x0ff00000,		/* src_mask */
620	 0x0ff00000,		/* dst_mask */
621	 FALSE),		/* pcrel_offset */
622
623  HOWTO (R_ARM_TARGET1,		/* type */
624	 0,			/* rightshift */
625	 2,			/* size (0 = byte, 1 = short, 2 = long) */
626	 32,			/* bitsize */
627	 FALSE,			/* pc_relative */
628	 0,			/* bitpos */
629	 complain_overflow_dont,/* complain_on_overflow */
630	 bfd_elf_generic_reloc,	/* special_function */
631	 "R_ARM_TARGET1",	/* name */
632	 FALSE,			/* partial_inplace */
633	 0xffffffff,		/* src_mask */
634	 0xffffffff,		/* dst_mask */
635	 FALSE),		/* pcrel_offset */
636
637  HOWTO (R_ARM_ROSEGREL32,	/* type */
638	 0,			/* rightshift */
639	 2,			/* size (0 = byte, 1 = short, 2 = long) */
640	 32,			/* bitsize */
641	 FALSE,			/* pc_relative */
642	 0,			/* bitpos */
643	 complain_overflow_dont,/* complain_on_overflow */
644	 bfd_elf_generic_reloc,	/* special_function */
645	 "R_ARM_ROSEGREL32",	/* name */
646	 FALSE,			/* partial_inplace */
647	 0xffffffff,		/* src_mask */
648	 0xffffffff,		/* dst_mask */
649	 FALSE),		/* pcrel_offset */
650
651  HOWTO (R_ARM_V4BX,		/* type */
652	 0,			/* rightshift */
653	 2,			/* size (0 = byte, 1 = short, 2 = long) */
654	 32,			/* bitsize */
655	 FALSE,			/* pc_relative */
656	 0,			/* bitpos */
657	 complain_overflow_dont,/* complain_on_overflow */
658	 bfd_elf_generic_reloc,	/* special_function */
659	 "R_ARM_V4BX",		/* name */
660	 FALSE,			/* partial_inplace */
661	 0xffffffff,		/* src_mask */
662	 0xffffffff,		/* dst_mask */
663	 FALSE),		/* pcrel_offset */
664
665  HOWTO (R_ARM_TARGET2,		/* type */
666	 0,			/* rightshift */
667	 2,			/* size (0 = byte, 1 = short, 2 = long) */
668	 32,			/* bitsize */
669	 FALSE,			/* pc_relative */
670	 0,			/* bitpos */
671	 complain_overflow_signed,/* complain_on_overflow */
672	 bfd_elf_generic_reloc,	/* special_function */
673	 "R_ARM_TARGET2",	/* name */
674	 FALSE,			/* partial_inplace */
675	 0xffffffff,		/* src_mask */
676	 0xffffffff,		/* dst_mask */
677	 TRUE),			/* pcrel_offset */
678
679  HOWTO (R_ARM_PREL31,		/* type */
680	 0,			/* rightshift */
681	 2,			/* size (0 = byte, 1 = short, 2 = long) */
682	 31,			/* bitsize */
683	 TRUE,			/* pc_relative */
684	 0,			/* bitpos */
685	 complain_overflow_signed,/* complain_on_overflow */
686	 bfd_elf_generic_reloc,	/* special_function */
687	 "R_ARM_PREL31",	/* name */
688	 FALSE,			/* partial_inplace */
689	 0x7fffffff,		/* src_mask */
690	 0x7fffffff,		/* dst_mask */
691	 TRUE),			/* pcrel_offset */
692
693  HOWTO (R_ARM_MOVW_ABS_NC,	/* type */
694	 0,			/* rightshift */
695	 2,			/* size (0 = byte, 1 = short, 2 = long) */
696	 16,			/* bitsize */
697	 FALSE,			/* pc_relative */
698	 0,			/* bitpos */
699	 complain_overflow_dont,/* complain_on_overflow */
700	 bfd_elf_generic_reloc,	/* special_function */
701	 "R_ARM_MOVW_ABS_NC",	/* name */
702	 FALSE,			/* partial_inplace */
703	 0x0000ffff,		/* src_mask */
704	 0x0000ffff,		/* dst_mask */
705	 FALSE),		/* pcrel_offset */
706
707  HOWTO (R_ARM_MOVT_ABS,	/* type */
708	 0,			/* rightshift */
709	 2,			/* size (0 = byte, 1 = short, 2 = long) */
710	 16,			/* bitsize */
711	 FALSE,			/* pc_relative */
712	 0,			/* bitpos */
713	 complain_overflow_bitfield,/* complain_on_overflow */
714	 bfd_elf_generic_reloc,	/* special_function */
715	 "R_ARM_MOVT_ABS",	/* name */
716	 FALSE,			/* partial_inplace */
717	 0x0000ffff,		/* src_mask */
718	 0x0000ffff,		/* dst_mask */
719	 FALSE),		/* pcrel_offset */
720
721  HOWTO (R_ARM_MOVW_PREL_NC,	/* type */
722	 0,			/* rightshift */
723	 2,			/* size (0 = byte, 1 = short, 2 = long) */
724	 16,			/* bitsize */
725	 TRUE,			/* pc_relative */
726	 0,			/* bitpos */
727	 complain_overflow_dont,/* complain_on_overflow */
728	 bfd_elf_generic_reloc,	/* special_function */
729	 "R_ARM_MOVW_PREL_NC",	/* name */
730	 FALSE,			/* partial_inplace */
731	 0x0000ffff,		/* src_mask */
732	 0x0000ffff,		/* dst_mask */
733	 TRUE),			/* pcrel_offset */
734
735  HOWTO (R_ARM_MOVT_PREL,	/* type */
736	 0,			/* rightshift */
737	 2,			/* size (0 = byte, 1 = short, 2 = long) */
738	 16,			/* bitsize */
739	 TRUE,			/* pc_relative */
740	 0,			/* bitpos */
741	 complain_overflow_bitfield,/* complain_on_overflow */
742	 bfd_elf_generic_reloc,	/* special_function */
743	 "R_ARM_MOVT_PREL",	/* name */
744	 FALSE,			/* partial_inplace */
745	 0x0000ffff,		/* src_mask */
746	 0x0000ffff,		/* dst_mask */
747	 TRUE),			/* pcrel_offset */
748
749  HOWTO (R_ARM_THM_MOVW_ABS_NC,	/* type */
750	 0,			/* rightshift */
751	 2,			/* size (0 = byte, 1 = short, 2 = long) */
752	 16,			/* bitsize */
753	 FALSE,			/* pc_relative */
754	 0,			/* bitpos */
755	 complain_overflow_dont,/* complain_on_overflow */
756	 bfd_elf_generic_reloc,	/* special_function */
757	 "R_ARM_THM_MOVW_ABS_NC",/* name */
758	 FALSE,			/* partial_inplace */
759	 0x040f70ff,		/* src_mask */
760	 0x040f70ff,		/* dst_mask */
761	 FALSE),		/* pcrel_offset */
762
763  HOWTO (R_ARM_THM_MOVT_ABS,	/* type */
764	 0,			/* rightshift */
765	 2,			/* size (0 = byte, 1 = short, 2 = long) */
766	 16,			/* bitsize */
767	 FALSE,			/* pc_relative */
768	 0,			/* bitpos */
769	 complain_overflow_bitfield,/* complain_on_overflow */
770	 bfd_elf_generic_reloc,	/* special_function */
771	 "R_ARM_THM_MOVT_ABS",	/* name */
772	 FALSE,			/* partial_inplace */
773	 0x040f70ff,		/* src_mask */
774	 0x040f70ff,		/* dst_mask */
775	 FALSE),		/* pcrel_offset */
776
777  HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
778	 0,			/* rightshift */
779	 2,			/* size (0 = byte, 1 = short, 2 = long) */
780	 16,			/* bitsize */
781	 TRUE,			/* pc_relative */
782	 0,			/* bitpos */
783	 complain_overflow_dont,/* complain_on_overflow */
784	 bfd_elf_generic_reloc,	/* special_function */
785	 "R_ARM_THM_MOVW_PREL_NC",/* name */
786	 FALSE,			/* partial_inplace */
787	 0x040f70ff,		/* src_mask */
788	 0x040f70ff,		/* dst_mask */
789	 TRUE),			/* pcrel_offset */
790
791  HOWTO (R_ARM_THM_MOVT_PREL,	/* type */
792	 0,			/* rightshift */
793	 2,			/* size (0 = byte, 1 = short, 2 = long) */
794	 16,			/* bitsize */
795	 TRUE,			/* pc_relative */
796	 0,			/* bitpos */
797	 complain_overflow_bitfield,/* complain_on_overflow */
798	 bfd_elf_generic_reloc,	/* special_function */
799	 "R_ARM_THM_MOVT_PREL",	/* name */
800	 FALSE,			/* partial_inplace */
801	 0x040f70ff,		/* src_mask */
802	 0x040f70ff,		/* dst_mask */
803	 TRUE),			/* pcrel_offset */
804
805  HOWTO (R_ARM_THM_JUMP19,	/* type */
806	 1,			/* rightshift */
807	 2,			/* size (0 = byte, 1 = short, 2 = long) */
808	 19,			/* bitsize */
809	 TRUE,			/* pc_relative */
810	 0,			/* bitpos */
811	 complain_overflow_signed,/* complain_on_overflow */
812	 bfd_elf_generic_reloc, /* special_function */
813	 "R_ARM_THM_JUMP19",	/* name */
814	 FALSE,			/* partial_inplace */
815	 0x043f2fff,		/* src_mask */
816	 0x043f2fff,		/* dst_mask */
817	 TRUE),			/* pcrel_offset */
818
819  HOWTO (R_ARM_THM_JUMP6,	/* type */
820	 1,			/* rightshift */
821	 1,			/* size (0 = byte, 1 = short, 2 = long) */
822	 6,			/* bitsize */
823	 TRUE,			/* pc_relative */
824	 0,			/* bitpos */
825	 complain_overflow_unsigned,/* complain_on_overflow */
826	 bfd_elf_generic_reloc,	/* special_function */
827	 "R_ARM_THM_JUMP6",	/* name */
828	 FALSE,			/* partial_inplace */
829	 0x02f8,		/* src_mask */
830	 0x02f8,		/* dst_mask */
831	 TRUE),			/* pcrel_offset */
832
833  /* These are declared as 13-bit signed relocations because we can
834     address -4095 .. 4095(base) by altering ADDW to SUBW or vice
835     versa.  */
836  HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
837	 0,			/* rightshift */
838	 2,			/* size (0 = byte, 1 = short, 2 = long) */
839	 13,			/* bitsize */
840	 TRUE,			/* pc_relative */
841	 0,			/* bitpos */
842	 complain_overflow_signed,/* complain_on_overflow */
843	 bfd_elf_generic_reloc,	/* special_function */
844	 "R_ARM_THM_ALU_PREL_11_0",/* name */
845	 FALSE,			/* partial_inplace */
846	 0x040070ff,		/* src_mask */
847	 0x040070ff,		/* dst_mask */
848	 TRUE),			/* pcrel_offset */
849
850  HOWTO (R_ARM_THM_PC12,	/* type */
851	 0,			/* rightshift */
852	 2,			/* size (0 = byte, 1 = short, 2 = long) */
853	 13,			/* bitsize */
854	 TRUE,			/* pc_relative */
855	 0,			/* bitpos */
856	 complain_overflow_signed,/* complain_on_overflow */
857	 bfd_elf_generic_reloc,	/* special_function */
858	 "R_ARM_THM_PC12",	/* name */
859	 FALSE,			/* partial_inplace */
860	 0x040070ff,		/* src_mask */
861	 0x040070ff,		/* dst_mask */
862	 TRUE),			/* pcrel_offset */
863
864  HOWTO (R_ARM_ABS32_NOI,	/* type */
865	 0,			/* rightshift */
866	 2,			/* size (0 = byte, 1 = short, 2 = long) */
867	 32,			/* bitsize */
868	 FALSE,			/* pc_relative */
869	 0,			/* bitpos */
870	 complain_overflow_dont,/* complain_on_overflow */
871	 bfd_elf_generic_reloc,	/* special_function */
872	 "R_ARM_ABS32_NOI",	/* name */
873	 FALSE,			/* partial_inplace */
874	 0xffffffff,		/* src_mask */
875	 0xffffffff,		/* dst_mask */
876	 FALSE),		/* pcrel_offset */
877
878  HOWTO (R_ARM_REL32_NOI,	/* type */
879	 0,			/* rightshift */
880	 2,			/* size (0 = byte, 1 = short, 2 = long) */
881	 32,			/* bitsize */
882	 TRUE,			/* pc_relative */
883	 0,			/* bitpos */
884	 complain_overflow_dont,/* complain_on_overflow */
885	 bfd_elf_generic_reloc,	/* special_function */
886	 "R_ARM_REL32_NOI",	/* name */
887	 FALSE,			/* partial_inplace */
888	 0xffffffff,		/* src_mask */
889	 0xffffffff,		/* dst_mask */
890	 FALSE),		/* pcrel_offset */
891};
892
893/* Relocations 57 .. 83 are the "group relocations" which we do not
894   support.  */
895
896static reloc_howto_type elf32_arm_howto_table_2[] =
897{
898  HOWTO (R_ARM_MOVW_BREL_NC,	/* type */
899	 0,			/* rightshift */
900	 2,			/* size (0 = byte, 1 = short, 2 = long) */
901	 16,			/* bitsize */
902	 FALSE,			/* pc_relative */
903	 0,			/* bitpos */
904	 complain_overflow_dont,/* complain_on_overflow */
905	 bfd_elf_generic_reloc,	/* special_function */
906	 "R_ARM_MOVW_BREL_NC",	/* name */
907	 FALSE,			/* partial_inplace */
908	 0x0000ffff,		/* src_mask */
909	 0x0000ffff,		/* dst_mask */
910	 FALSE),		/* pcrel_offset */
911
912  HOWTO (R_ARM_MOVT_BREL,	/* type */
913	 0,			/* rightshift */
914	 2,			/* size (0 = byte, 1 = short, 2 = long) */
915	 16,			/* bitsize */
916	 FALSE,			/* pc_relative */
917	 0,			/* bitpos */
918	 complain_overflow_bitfield,/* complain_on_overflow */
919	 bfd_elf_generic_reloc,	/* special_function */
920	 "R_ARM_MOVT_BREL",	/* name */
921	 FALSE,			/* partial_inplace */
922	 0x0000ffff,		/* src_mask */
923	 0x0000ffff,		/* dst_mask */
924	 FALSE),		/* pcrel_offset */
925
926  HOWTO (R_ARM_MOVW_BREL,	/* type */
927	 0,			/* rightshift */
928	 2,			/* size (0 = byte, 1 = short, 2 = long) */
929	 16,			/* bitsize */
930	 FALSE,			/* pc_relative */
931	 0,			/* bitpos */
932	 complain_overflow_dont,/* complain_on_overflow */
933	 bfd_elf_generic_reloc,	/* special_function */
934	 "R_ARM_MOVW_BREL",	/* name */
935	 FALSE,			/* partial_inplace */
936	 0x0000ffff,		/* src_mask */
937	 0x0000ffff,		/* dst_mask */
938	 FALSE),		/* pcrel_offset */
939
940  HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
941	 0,			/* rightshift */
942	 2,			/* size (0 = byte, 1 = short, 2 = long) */
943	 16,			/* bitsize */
944	 FALSE,			/* pc_relative */
945	 0,			/* bitpos */
946	 complain_overflow_dont,/* complain_on_overflow */
947	 bfd_elf_generic_reloc,	/* special_function */
948	 "R_ARM_THM_MOVW_BREL_NC",/* name */
949	 FALSE,			/* partial_inplace */
950	 0x040f70ff,		/* src_mask */
951	 0x040f70ff,		/* dst_mask */
952	 FALSE),		/* pcrel_offset */
953
954  HOWTO (R_ARM_THM_MOVT_BREL,	/* type */
955	 0,			/* rightshift */
956	 2,			/* size (0 = byte, 1 = short, 2 = long) */
957	 16,			/* bitsize */
958	 FALSE,			/* pc_relative */
959	 0,			/* bitpos */
960	 complain_overflow_bitfield,/* complain_on_overflow */
961	 bfd_elf_generic_reloc,	/* special_function */
962	 "R_ARM_THM_MOVT_BREL",	/* name */
963	 FALSE,			/* partial_inplace */
964	 0x040f70ff,		/* src_mask */
965	 0x040f70ff,		/* dst_mask */
966	 FALSE),		/* pcrel_offset */
967
968  HOWTO (R_ARM_THM_MOVW_BREL,	/* type */
969	 0,			/* rightshift */
970	 2,			/* size (0 = byte, 1 = short, 2 = long) */
971	 16,			/* bitsize */
972	 FALSE,			/* pc_relative */
973	 0,			/* bitpos */
974	 complain_overflow_dont,/* complain_on_overflow */
975	 bfd_elf_generic_reloc,	/* special_function */
976	 "R_ARM_THM_MOVW_BREL",	/* name */
977	 FALSE,			/* partial_inplace */
978	 0x040f70ff,		/* src_mask */
979	 0x040f70ff,		/* dst_mask */
980	 FALSE),		/* pcrel_offset */
981
982  EMPTY_HOWTO (90),   /* unallocated */
983  EMPTY_HOWTO (91),
984  EMPTY_HOWTO (92),
985  EMPTY_HOWTO (93),
986
987  HOWTO (R_ARM_PLT32_ABS,	/* type */
988	 0,			/* rightshift */
989	 2,			/* size (0 = byte, 1 = short, 2 = long) */
990	 32,			/* bitsize */
991	 FALSE,			/* pc_relative */
992	 0,			/* bitpos */
993	 complain_overflow_dont,/* complain_on_overflow */
994	 bfd_elf_generic_reloc,	/* special_function */
995	 "R_ARM_PLT32_ABS",	/* name */
996	 FALSE,			/* partial_inplace */
997	 0xffffffff,		/* src_mask */
998	 0xffffffff,		/* dst_mask */
999	 FALSE),		/* pcrel_offset */
1000
1001  HOWTO (R_ARM_GOT_ABS,		/* type */
1002	 0,			/* rightshift */
1003	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1004	 32,			/* bitsize */
1005	 FALSE,			/* pc_relative */
1006	 0,			/* bitpos */
1007	 complain_overflow_dont,/* complain_on_overflow */
1008	 bfd_elf_generic_reloc,	/* special_function */
1009	 "R_ARM_GOT_ABS",	/* name */
1010	 FALSE,			/* partial_inplace */
1011	 0xffffffff,		/* src_mask */
1012	 0xffffffff,		/* dst_mask */
1013	 FALSE),			/* pcrel_offset */
1014
1015  HOWTO (R_ARM_GOT_PREL,	/* type */
1016	 0,			/* rightshift */
1017	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1018	 32,			/* bitsize */
1019	 TRUE,			/* pc_relative */
1020	 0,			/* bitpos */
1021	 complain_overflow_dont,	/* complain_on_overflow */
1022	 bfd_elf_generic_reloc,	/* special_function */
1023	 "R_ARM_GOT_PREL",	/* name */
1024	 FALSE,			/* partial_inplace */
1025	 0xffffffff,		/* src_mask */
1026	 0xffffffff,		/* dst_mask */
1027	 TRUE),			/* pcrel_offset */
1028
1029  HOWTO (R_ARM_GOT_BREL12,	/* type */
1030	 0,			/* rightshift */
1031	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1032	 12,			/* bitsize */
1033	 FALSE,			/* pc_relative */
1034	 0,			/* bitpos */
1035	 complain_overflow_bitfield,/* complain_on_overflow */
1036	 bfd_elf_generic_reloc,	/* special_function */
1037	 "R_ARM_GOT_BREL12",	/* name */
1038	 FALSE,			/* partial_inplace */
1039	 0x00000fff,		/* src_mask */
1040	 0x00000fff,		/* dst_mask */
1041	 FALSE),		/* pcrel_offset */
1042
1043  HOWTO (R_ARM_GOTOFF12,	/* type */
1044	 0,			/* rightshift */
1045	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1046	 12,			/* bitsize */
1047	 FALSE,			/* pc_relative */
1048	 0,			/* bitpos */
1049	 complain_overflow_bitfield,/* complain_on_overflow */
1050	 bfd_elf_generic_reloc,	/* special_function */
1051	 "R_ARM_GOTOFF12",	/* name */
1052	 FALSE,			/* partial_inplace */
1053	 0x00000fff,		/* src_mask */
1054	 0x00000fff,		/* dst_mask */
1055	 FALSE),		/* pcrel_offset */
1056
1057  EMPTY_HOWTO (R_ARM_GOTRELAX),  /* reserved for future GOT-load optimizations */
1058
1059  /* GNU extension to record C++ vtable member usage */
1060  HOWTO (R_ARM_GNU_VTENTRY,     /* type */
1061         0,                     /* rightshift */
1062         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1063         0,                     /* bitsize */
1064         FALSE,                 /* pc_relative */
1065         0,                     /* bitpos */
1066         complain_overflow_dont, /* complain_on_overflow */
1067         _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
1068         "R_ARM_GNU_VTENTRY",   /* name */
1069         FALSE,                 /* partial_inplace */
1070         0,                     /* src_mask */
1071         0,                     /* dst_mask */
1072         FALSE),                /* pcrel_offset */
1073
1074  /* GNU extension to record C++ vtable hierarchy */
1075  HOWTO (R_ARM_GNU_VTINHERIT, /* type */
1076         0,                     /* rightshift */
1077         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1078         0,                     /* bitsize */
1079         FALSE,                 /* pc_relative */
1080         0,                     /* bitpos */
1081         complain_overflow_dont, /* complain_on_overflow */
1082         NULL,                  /* special_function */
1083         "R_ARM_GNU_VTINHERIT", /* name */
1084         FALSE,                 /* partial_inplace */
1085         0,                     /* src_mask */
1086         0,                     /* dst_mask */
1087         FALSE),                /* pcrel_offset */
1088
1089  HOWTO (R_ARM_THM_JUMP11,	/* type */
1090	 1,			/* rightshift */
1091	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1092	 11,			/* bitsize */
1093	 TRUE,			/* pc_relative */
1094	 0,			/* bitpos */
1095	 complain_overflow_signed,	/* complain_on_overflow */
1096	 bfd_elf_generic_reloc,	/* special_function */
1097	 "R_ARM_THM_JUMP11",	/* name */
1098	 FALSE,			/* partial_inplace */
1099	 0x000007ff,		/* src_mask */
1100	 0x000007ff,		/* dst_mask */
1101	 TRUE),			/* pcrel_offset */
1102
1103  HOWTO (R_ARM_THM_JUMP8,	/* type */
1104	 1,			/* rightshift */
1105	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1106	 8,			/* bitsize */
1107	 TRUE,			/* pc_relative */
1108	 0,			/* bitpos */
1109	 complain_overflow_signed,	/* complain_on_overflow */
1110	 bfd_elf_generic_reloc,	/* special_function */
1111	 "R_ARM_THM_JUMP8",	/* name */
1112	 FALSE,			/* partial_inplace */
1113	 0x000000ff,		/* src_mask */
1114	 0x000000ff,		/* dst_mask */
1115	 TRUE),			/* pcrel_offset */
1116
1117  /* TLS relocations */
1118  HOWTO (R_ARM_TLS_GD32,	/* type */
1119         0,                     /* rightshift */
1120         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1121         32,                    /* bitsize */
1122         FALSE,                 /* pc_relative */
1123         0,                     /* bitpos */
1124         complain_overflow_bitfield,/* complain_on_overflow */
1125         NULL,			/* special_function */
1126         "R_ARM_TLS_GD32",	/* name */
1127         TRUE,			/* partial_inplace */
1128         0xffffffff,		/* src_mask */
1129         0xffffffff,		/* dst_mask */
1130         FALSE),                /* pcrel_offset */
1131
1132  HOWTO (R_ARM_TLS_LDM32,	/* type */
1133         0,                     /* rightshift */
1134         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1135         32,                    /* bitsize */
1136         FALSE,                 /* pc_relative */
1137         0,                     /* bitpos */
1138         complain_overflow_bitfield,/* complain_on_overflow */
1139         bfd_elf_generic_reloc, /* special_function */
1140         "R_ARM_TLS_LDM32",	/* name */
1141         TRUE,			/* partial_inplace */
1142         0xffffffff,		/* src_mask */
1143         0xffffffff,		/* dst_mask */
1144         FALSE),                /* pcrel_offset */
1145
1146  HOWTO (R_ARM_TLS_LDO32,	/* type */
1147         0,                     /* rightshift */
1148         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1149         32,                    /* bitsize */
1150         FALSE,                 /* pc_relative */
1151         0,                     /* bitpos */
1152         complain_overflow_bitfield,/* complain_on_overflow */
1153         bfd_elf_generic_reloc, /* special_function */
1154         "R_ARM_TLS_LDO32",	/* name */
1155         TRUE,			/* partial_inplace */
1156         0xffffffff,		/* src_mask */
1157         0xffffffff,		/* dst_mask */
1158         FALSE),                /* pcrel_offset */
1159
1160  HOWTO (R_ARM_TLS_IE32,	/* type */
1161         0,                     /* rightshift */
1162         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1163         32,                    /* bitsize */
1164         FALSE,                  /* pc_relative */
1165         0,                     /* bitpos */
1166         complain_overflow_bitfield,/* complain_on_overflow */
1167         NULL,			/* special_function */
1168         "R_ARM_TLS_IE32",	/* name */
1169         TRUE,			/* partial_inplace */
1170         0xffffffff,		/* src_mask */
1171         0xffffffff,		/* dst_mask */
1172         FALSE),                /* pcrel_offset */
1173
1174  HOWTO (R_ARM_TLS_LE32,	/* type */
1175         0,                     /* rightshift */
1176         2,                     /* size (0 = byte, 1 = short, 2 = long) */
1177         32,                    /* bitsize */
1178         FALSE,                 /* pc_relative */
1179         0,                     /* bitpos */
1180         complain_overflow_bitfield,/* complain_on_overflow */
1181         bfd_elf_generic_reloc, /* special_function */
1182         "R_ARM_TLS_LE32",	/* name */
1183         TRUE,			/* partial_inplace */
1184         0xffffffff,		/* src_mask */
1185         0xffffffff,		/* dst_mask */
1186         FALSE),                /* pcrel_offset */
1187
1188  HOWTO (R_ARM_TLS_LDO12,	/* type */
1189	 0,			/* rightshift */
1190	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1191	 12,			/* bitsize */
1192	 FALSE,			/* pc_relative */
1193	 0,			/* bitpos */
1194	 complain_overflow_bitfield,/* complain_on_overflow */
1195	 bfd_elf_generic_reloc,	/* special_function */
1196	 "R_ARM_TLS_LDO12",	/* name */
1197	 FALSE,			/* partial_inplace */
1198	 0x00000fff,		/* src_mask */
1199	 0x00000fff,		/* dst_mask */
1200	 FALSE),		/* pcrel_offset */
1201
1202  HOWTO (R_ARM_TLS_LE12,	/* type */
1203	 0,			/* rightshift */
1204	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1205	 12,			/* bitsize */
1206	 FALSE,			/* pc_relative */
1207	 0,			/* bitpos */
1208	 complain_overflow_bitfield,/* complain_on_overflow */
1209	 bfd_elf_generic_reloc,	/* special_function */
1210	 "R_ARM_TLS_LE12",	/* name */
1211	 FALSE,			/* partial_inplace */
1212	 0x00000fff,		/* src_mask */
1213	 0x00000fff,		/* dst_mask */
1214	 FALSE),		/* pcrel_offset */
1215
1216  HOWTO (R_ARM_TLS_IE12GP,	/* type */
1217	 0,			/* rightshift */
1218	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1219	 12,			/* bitsize */
1220	 FALSE,			/* pc_relative */
1221	 0,			/* bitpos */
1222	 complain_overflow_bitfield,/* complain_on_overflow */
1223	 bfd_elf_generic_reloc,	/* special_function */
1224	 "R_ARM_TLS_IE12GP",	/* name */
1225	 FALSE,			/* partial_inplace */
1226	 0x00000fff,		/* src_mask */
1227	 0x00000fff,		/* dst_mask */
1228	 FALSE),		/* pcrel_offset */
1229};
1230
1231/* 112-127 private relocations
1232   128 R_ARM_ME_TOO, obsolete
1233   129-255 unallocated in AAELF.
1234
1235   249-255 extended, currently unused, relocations:  */
1236
1237static reloc_howto_type elf32_arm_howto_table_3[4] =
1238{
1239  HOWTO (R_ARM_RREL32,		/* type */
1240	 0,			/* rightshift */
1241	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1242	 0,			/* bitsize */
1243	 FALSE,			/* pc_relative */
1244	 0,			/* bitpos */
1245	 complain_overflow_dont,/* complain_on_overflow */
1246	 bfd_elf_generic_reloc,	/* special_function */
1247	 "R_ARM_RREL32",	/* name */
1248	 FALSE,			/* partial_inplace */
1249	 0,			/* src_mask */
1250	 0,			/* dst_mask */
1251	 FALSE),		/* pcrel_offset */
1252
1253  HOWTO (R_ARM_RABS32,		/* type */
1254	 0,			/* rightshift */
1255	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1256	 0,			/* bitsize */
1257	 FALSE,			/* pc_relative */
1258	 0,			/* bitpos */
1259	 complain_overflow_dont,/* complain_on_overflow */
1260	 bfd_elf_generic_reloc,	/* special_function */
1261	 "R_ARM_RABS32",	/* name */
1262	 FALSE,			/* partial_inplace */
1263	 0,			/* src_mask */
1264	 0,			/* dst_mask */
1265	 FALSE),		/* pcrel_offset */
1266
1267  HOWTO (R_ARM_RPC24,		/* type */
1268	 0,			/* rightshift */
1269	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1270	 0,			/* bitsize */
1271	 FALSE,			/* pc_relative */
1272	 0,			/* bitpos */
1273	 complain_overflow_dont,/* complain_on_overflow */
1274	 bfd_elf_generic_reloc,	/* special_function */
1275	 "R_ARM_RPC24",		/* name */
1276	 FALSE,			/* partial_inplace */
1277	 0,			/* src_mask */
1278	 0,			/* dst_mask */
1279	 FALSE),		/* pcrel_offset */
1280
1281  HOWTO (R_ARM_RBASE,		/* type */
1282	 0,			/* rightshift */
1283	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1284	 0,			/* bitsize */
1285	 FALSE,			/* pc_relative */
1286	 0,			/* bitpos */
1287	 complain_overflow_dont,/* complain_on_overflow */
1288	 bfd_elf_generic_reloc,	/* special_function */
1289	 "R_ARM_RBASE",		/* name */
1290	 FALSE,			/* partial_inplace */
1291	 0,			/* src_mask */
1292	 0,			/* dst_mask */
1293	 FALSE)			/* pcrel_offset */
1294};
1295
1296static reloc_howto_type *
1297elf32_arm_howto_from_type (unsigned int r_type)
1298{
1299  if (r_type < NUM_ELEM (elf32_arm_howto_table_1))
1300    return &elf32_arm_howto_table_1[r_type];
1301
1302  if (r_type >= R_ARM_MOVW_BREL_NC
1303      && r_type < R_ARM_MOVW_BREL_NC + NUM_ELEM (elf32_arm_howto_table_2))
1304    return &elf32_arm_howto_table_2[r_type - R_ARM_MOVW_BREL_NC];
1305
1306  if (r_type >= R_ARM_RREL32
1307      && r_type < R_ARM_RREL32 + NUM_ELEM (elf32_arm_howto_table_2))
1308    return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1309
1310  return NULL;
1311}
1312
1313static void
1314elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
1315			 Elf_Internal_Rela * elf_reloc)
1316{
1317  unsigned int r_type;
1318
1319  r_type = ELF32_R_TYPE (elf_reloc->r_info);
1320  bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
1321}
1322
1323struct elf32_arm_reloc_map
1324  {
1325    bfd_reloc_code_real_type  bfd_reloc_val;
1326    unsigned char             elf_reloc_val;
1327  };
1328
1329/* All entries in this list must also be present in elf32_arm_howto_table.  */
1330static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1331  {
1332    {BFD_RELOC_NONE,                 R_ARM_NONE},
1333    {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1334    {BFD_RELOC_ARM_PCREL_CALL,	     R_ARM_CALL},
1335    {BFD_RELOC_ARM_PCREL_JUMP,	     R_ARM_JUMP24},
1336    {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1337    {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1338    {BFD_RELOC_32,                   R_ARM_ABS32},
1339    {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1340    {BFD_RELOC_8,                    R_ARM_ABS8},
1341    {BFD_RELOC_16,                   R_ARM_ABS16},
1342    {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1343    {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1344    {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1345    {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1346    {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1347    {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1348    {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1349    {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1350    {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1351    {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1352    {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1353    {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1354    {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1355    {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1356    {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1357    {BFD_RELOC_ARM_TARGET1,	     R_ARM_TARGET1},
1358    {BFD_RELOC_ARM_ROSEGREL32,	     R_ARM_ROSEGREL32},
1359    {BFD_RELOC_ARM_SBREL32,	     R_ARM_SBREL32},
1360    {BFD_RELOC_ARM_PREL31,	     R_ARM_PREL31},
1361    {BFD_RELOC_ARM_TARGET2,	     R_ARM_TARGET2},
1362    {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1363    {BFD_RELOC_ARM_TLS_GD32,	     R_ARM_TLS_GD32},
1364    {BFD_RELOC_ARM_TLS_LDO32,	     R_ARM_TLS_LDO32},
1365    {BFD_RELOC_ARM_TLS_LDM32,	     R_ARM_TLS_LDM32},
1366    {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
1367    {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
1368    {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
1369    {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
1370    {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
1371    {BFD_RELOC_VTABLE_INHERIT,	     R_ARM_GNU_VTINHERIT},
1372    {BFD_RELOC_VTABLE_ENTRY,	     R_ARM_GNU_VTENTRY},
1373  };
1374
1375static reloc_howto_type *
1376elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1377			     bfd_reloc_code_real_type code)
1378{
1379  unsigned int i;
1380  for (i = 0; i < NUM_ELEM (elf32_arm_reloc_map); i ++)
1381    if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
1382      return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
1383
1384  return NULL;
1385}
1386
1387/* Support for core dump NOTE sections */
1388static bfd_boolean
1389elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1390{
1391  int offset;
1392  size_t size;
1393
1394  switch (note->descsz)
1395    {
1396      default:
1397	return FALSE;
1398
1399      case 148:		/* Linux/ARM 32-bit*/
1400	/* pr_cursig */
1401	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1402
1403	/* pr_pid */
1404	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1405
1406	/* pr_reg */
1407	offset = 72;
1408	size = 72;
1409
1410	break;
1411
1412      case 96:		/* FreeBSD/ARM */
1413	/* pr_cursig */
1414	if (elf_tdata(abfd)->core_signal == 0)
1415	  elf_tdata (abfd)->core_signal = ((int *)(note->descdata))[5];
1416
1417	/* pr_pid */
1418	elf_tdata (abfd)->core_pid = ((int *)(note->descdata))[6];
1419
1420	/* pr_reg */
1421	offset = 28;
1422	size = 68;
1423	break;
1424    }
1425
1426  /* Make a ".reg/999" section.  */
1427  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1428					  size, note->descpos + offset);
1429}
1430
1431static bfd_boolean
1432elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1433{
1434  switch (note->descsz)
1435    {
1436      default:
1437	return FALSE;
1438
1439      case 124:		/* Linux/ARM elf_prpsinfo */
1440	elf_tdata (abfd)->core_program
1441	 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1442	elf_tdata (abfd)->core_command
1443	 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1444    }
1445
1446  /* Note that for some reason, a spurious space is tacked
1447     onto the end of the args in some (at least one anyway)
1448     implementations, so strip it off if it exists.  */
1449
1450  {
1451    char *command = elf_tdata (abfd)->core_command;
1452    int n = strlen (command);
1453
1454    if (0 < n && command[n - 1] == ' ')
1455      command[n - 1] = '\0';
1456  }
1457
1458  return TRUE;
1459}
1460
1461#define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
1462#define TARGET_LITTLE_NAME              "elf32-littlearm"
1463#define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
1464#define TARGET_BIG_NAME                 "elf32-bigarm"
1465
1466#define elf_backend_grok_prstatus	elf32_arm_nabi_grok_prstatus
1467#define elf_backend_grok_psinfo		elf32_arm_nabi_grok_psinfo
1468
1469typedef unsigned long int insn32;
1470typedef unsigned short int insn16;
1471
1472/* In lieu of proper flags, assume all EABIv4 or later objects are
1473   interworkable.  */
1474#define INTERWORK_FLAG(abfd)  \
1475  (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
1476  || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
1477
1478/* The linker script knows the section names for placement.
1479   The entry_names are used to do simple name mangling on the stubs.
1480   Given a function name, and its type, the stub can be found. The
1481   name can be changed. The only requirement is the %s be present.  */
1482#define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1483#define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
1484
1485#define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1486#define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
1487
1488/* The name of the dynamic interpreter.  This is put in the .interp
1489   section.  */
1490#define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
1491
1492#ifdef FOUR_WORD_PLT
1493
1494/* The first entry in a procedure linkage table looks like
1495   this.  It is set up so that any shared library function that is
1496   called before the relocation has been set up calls the dynamic
1497   linker first.  */
1498static const bfd_vma elf32_arm_plt0_entry [] =
1499  {
1500    0xe52de004,		/* str   lr, [sp, #-4]! */
1501    0xe59fe010,		/* ldr   lr, [pc, #16]  */
1502    0xe08fe00e,		/* add   lr, pc, lr     */
1503    0xe5bef008,		/* ldr   pc, [lr, #8]!  */
1504  };
1505
1506/* Subsequent entries in a procedure linkage table look like
1507   this.  */
1508static const bfd_vma elf32_arm_plt_entry [] =
1509  {
1510    0xe28fc600,		/* add   ip, pc, #NN	*/
1511    0xe28cca00,		/* add	 ip, ip, #NN	*/
1512    0xe5bcf000,		/* ldr	 pc, [ip, #NN]! */
1513    0x00000000,		/* unused		*/
1514  };
1515
1516#else
1517
1518/* The first entry in a procedure linkage table looks like
1519   this.  It is set up so that any shared library function that is
1520   called before the relocation has been set up calls the dynamic
1521   linker first.  */
1522static const bfd_vma elf32_arm_plt0_entry [] =
1523  {
1524    0xe52de004,		/* str   lr, [sp, #-4]! */
1525    0xe59fe004,		/* ldr   lr, [pc, #4]   */
1526    0xe08fe00e,		/* add   lr, pc, lr     */
1527    0xe5bef008,		/* ldr   pc, [lr, #8]!  */
1528    0x00000000,		/* &GOT[0] - .          */
1529  };
1530
1531/* Subsequent entries in a procedure linkage table look like
1532   this.  */
1533static const bfd_vma elf32_arm_plt_entry [] =
1534  {
1535    0xe28fc600,		/* add   ip, pc, #0xNN00000 */
1536    0xe28cca00,		/* add	 ip, ip, #0xNN000   */
1537    0xe5bcf000,		/* ldr	 pc, [ip, #0xNNN]!  */
1538  };
1539
1540#endif
1541
1542/* The format of the first entry in the procedure linkage table
1543   for a VxWorks executable.  */
1544static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
1545  {
1546    0xe52dc008,	        /* str    ip,[sp,#-8]!			*/
1547    0xe59fc000,         /* ldr    ip,[pc]			*/
1548    0xe59cf008,         /* ldr    pc,[ip,#8]			*/
1549    0x00000000,         /* .long  _GLOBAL_OFFSET_TABLE_		*/
1550  };
1551
1552/* The format of subsequent entries in a VxWorks executable.  */
1553static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
1554  {
1555    0xe59fc000,         /* ldr    ip,[pc]			*/
1556    0xe59cf000,         /* ldr    pc,[ip]			*/
1557    0x00000000,         /* .long  @got				*/
1558    0xe59fc000,         /* ldr    ip,[pc]			*/
1559    0xea000000,         /* b      _PLT				*/
1560    0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)	*/
1561  };
1562
1563/* The format of entries in a VxWorks shared library.  */
1564static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
1565  {
1566    0xe59fc000,         /* ldr    ip,[pc]			*/
1567    0xe79cf009,         /* ldr    pc,[ip,r9]			*/
1568    0x00000000,         /* .long  @got				*/
1569    0xe59fc000,         /* ldr    ip,[pc]			*/
1570    0xe599f008,         /* ldr    pc,[r9,#8]			*/
1571    0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)	*/
1572  };
1573
1574/* An initial stub used if the PLT entry is referenced from Thumb code.  */
1575#define PLT_THUMB_STUB_SIZE 4
1576static const bfd_vma elf32_arm_plt_thumb_stub [] =
1577  {
1578    0x4778,		/* bx pc */
1579    0x46c0		/* nop   */
1580  };
1581
1582/* The entries in a PLT when using a DLL-based target with multiple
1583   address spaces.  */
1584static const bfd_vma elf32_arm_symbian_plt_entry [] =
1585  {
1586    0xe51ff004,         /* ldr   pc, [pc, #-4] */
1587    0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
1588  };
1589
1590/* Used to build a map of a section.  This is required for mixed-endian
1591   code/data.  */
1592
1593typedef struct elf32_elf_section_map
1594{
1595  bfd_vma vma;
1596  char type;
1597}
1598elf32_arm_section_map;
1599
1600typedef struct _arm_elf_section_data
1601{
1602  struct bfd_elf_section_data elf;
1603  unsigned int mapcount;
1604  elf32_arm_section_map *map;
1605}
1606_arm_elf_section_data;
1607
1608#define elf32_arm_section_data(sec) \
1609  ((_arm_elf_section_data *) elf_section_data (sec))
1610
1611/* The size of the thread control block.  */
1612#define TCB_SIZE	8
1613
1614#define NUM_KNOWN_ATTRIBUTES 32
1615
1616typedef struct aeabi_attribute
1617{
1618  int type;
1619  unsigned int i;
1620  char *s;
1621} aeabi_attribute;
1622
1623typedef struct aeabi_attribute_list
1624{
1625  struct aeabi_attribute_list *next;
1626  int tag;
1627  aeabi_attribute attr;
1628} aeabi_attribute_list;
1629
1630struct elf32_arm_obj_tdata
1631{
1632  struct elf_obj_tdata root;
1633
1634  /* tls_type for each local got entry.  */
1635  char *local_got_tls_type;
1636
1637  aeabi_attribute known_eabi_attributes[NUM_KNOWN_ATTRIBUTES];
1638  aeabi_attribute_list *other_eabi_attributes;
1639};
1640
1641#define elf32_arm_tdata(abfd) \
1642  ((struct elf32_arm_obj_tdata *) (abfd)->tdata.any)
1643
1644#define elf32_arm_local_got_tls_type(abfd) \
1645  (elf32_arm_tdata (abfd)->local_got_tls_type)
1646
1647static bfd_boolean
1648elf32_arm_mkobject (bfd *abfd)
1649{
1650  bfd_size_type amt = sizeof (struct elf32_arm_obj_tdata);
1651  abfd->tdata.any = bfd_zalloc (abfd, amt);
1652  if (abfd->tdata.any == NULL)
1653    return FALSE;
1654  return TRUE;
1655}
1656
1657/* The ARM linker needs to keep track of the number of relocs that it
1658   decides to copy in check_relocs for each symbol.  This is so that
1659   it can discard PC relative relocs if it doesn't need them when
1660   linking with -Bsymbolic.  We store the information in a field
1661   extending the regular ELF linker hash table.  */
1662
1663/* This structure keeps track of the number of relocs we have copied
1664   for a given symbol.  */
1665struct elf32_arm_relocs_copied
1666  {
1667    /* Next section.  */
1668    struct elf32_arm_relocs_copied * next;
1669    /* A section in dynobj.  */
1670    asection * section;
1671    /* Number of relocs copied in this section.  */
1672    bfd_size_type count;
1673    /* Number of PC-relative relocs copied in this section.  */
1674    bfd_size_type pc_count;
1675  };
1676
1677#define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
1678
1679/* Arm ELF linker hash entry.  */
1680struct elf32_arm_link_hash_entry
1681  {
1682    struct elf_link_hash_entry root;
1683
1684    /* Number of PC relative relocs copied for this symbol.  */
1685    struct elf32_arm_relocs_copied * relocs_copied;
1686
1687    /* We reference count Thumb references to a PLT entry separately,
1688       so that we can emit the Thumb trampoline only if needed.  */
1689    bfd_signed_vma plt_thumb_refcount;
1690
1691    /* Since PLT entries have variable size if the Thumb prologue is
1692       used, we need to record the index into .got.plt instead of
1693       recomputing it from the PLT offset.  */
1694    bfd_signed_vma plt_got_offset;
1695
1696#define GOT_UNKNOWN	0
1697#define GOT_NORMAL	1
1698#define GOT_TLS_GD	2
1699#define GOT_TLS_IE	4
1700    unsigned char tls_type;
1701  };
1702
1703/* Traverse an arm ELF linker hash table.  */
1704#define elf32_arm_link_hash_traverse(table, func, info)			\
1705  (elf_link_hash_traverse						\
1706   (&(table)->root,							\
1707    (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
1708    (info)))
1709
1710/* Get the ARM elf linker hash table from a link_info structure.  */
1711#define elf32_arm_hash_table(info) \
1712  ((struct elf32_arm_link_hash_table *) ((info)->hash))
1713
1714/* ARM ELF linker hash table.  */
1715struct elf32_arm_link_hash_table
1716  {
1717    /* The main hash table.  */
1718    struct elf_link_hash_table root;
1719
1720    /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
1721    bfd_size_type thumb_glue_size;
1722
1723    /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
1724    bfd_size_type arm_glue_size;
1725
1726    /* An arbitrary input BFD chosen to hold the glue sections.  */
1727    bfd * bfd_of_glue_owner;
1728
1729    /* Nonzero to output a BE8 image.  */
1730    int byteswap_code;
1731
1732    /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
1733       Nonzero if R_ARM_TARGET1 means R_ARM_ABS32.  */
1734    int target1_is_rel;
1735
1736    /* The relocation to use for R_ARM_TARGET2 relocations.  */
1737    int target2_reloc;
1738
1739    /* Nonzero to fix BX instructions for ARMv4 targets.  */
1740    int fix_v4bx;
1741
1742    /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
1743    int use_blx;
1744
1745    /* The number of bytes in the initial entry in the PLT.  */
1746    bfd_size_type plt_header_size;
1747
1748    /* The number of bytes in the subsequent PLT etries.  */
1749    bfd_size_type plt_entry_size;
1750
1751    /* True if the target system is VxWorks.  */
1752    int vxworks_p;
1753
1754    /* True if the target system is Symbian OS.  */
1755    int symbian_p;
1756
1757    /* True if the target uses REL relocations.  */
1758    int use_rel;
1759
1760    /* Short-cuts to get to dynamic linker sections.  */
1761    asection *sgot;
1762    asection *sgotplt;
1763    asection *srelgot;
1764    asection *splt;
1765    asection *srelplt;
1766    asection *sdynbss;
1767    asection *srelbss;
1768
1769    /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
1770    asection *srelplt2;
1771
1772    /* Data for R_ARM_TLS_LDM32 relocations.  */
1773    union {
1774      bfd_signed_vma refcount;
1775      bfd_vma offset;
1776    } tls_ldm_got;
1777
1778    /* Small local sym to section mapping cache.  */
1779    struct sym_sec_cache sym_sec;
1780
1781    /* For convenience in allocate_dynrelocs.  */
1782    bfd * obfd;
1783  };
1784
1785/* Create an entry in an ARM ELF linker hash table.  */
1786
1787static struct bfd_hash_entry *
1788elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
1789                             struct bfd_hash_table * table,
1790                             const char * string)
1791{
1792  struct elf32_arm_link_hash_entry * ret =
1793    (struct elf32_arm_link_hash_entry *) entry;
1794
1795  /* Allocate the structure if it has not already been allocated by a
1796     subclass.  */
1797  if (ret == (struct elf32_arm_link_hash_entry *) NULL)
1798    ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
1799  if (ret == NULL)
1800    return (struct bfd_hash_entry *) ret;
1801
1802  /* Call the allocation method of the superclass.  */
1803  ret = ((struct elf32_arm_link_hash_entry *)
1804	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1805				     table, string));
1806  if (ret != NULL)
1807    {
1808      ret->relocs_copied = NULL;
1809      ret->tls_type = GOT_UNKNOWN;
1810      ret->plt_thumb_refcount = 0;
1811      ret->plt_got_offset = -1;
1812    }
1813
1814  return (struct bfd_hash_entry *) ret;
1815}
1816
1817/* Return true if NAME is the name of the relocation section associated
1818   with S.  */
1819
1820static bfd_boolean
1821reloc_section_p (struct elf32_arm_link_hash_table *htab,
1822		 const char *name, asection *s)
1823{
1824  if (htab->use_rel)
1825    return strncmp (name, ".rel", 4) == 0 && strcmp (s->name, name + 4) == 0;
1826  else
1827    return strncmp (name, ".rela", 5) == 0 && strcmp (s->name, name + 5) == 0;
1828}
1829
1830/* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
1831   shortcuts to them in our hash table.  */
1832
1833static bfd_boolean
1834create_got_section (bfd *dynobj, struct bfd_link_info *info)
1835{
1836  struct elf32_arm_link_hash_table *htab;
1837
1838  htab = elf32_arm_hash_table (info);
1839  /* BPABI objects never have a GOT, or associated sections.  */
1840  if (htab->symbian_p)
1841    return TRUE;
1842
1843  if (! _bfd_elf_create_got_section (dynobj, info))
1844    return FALSE;
1845
1846  htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1847  htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1848  if (!htab->sgot || !htab->sgotplt)
1849    abort ();
1850
1851  htab->srelgot = bfd_make_section_with_flags (dynobj,
1852					       RELOC_SECTION (htab, ".got"),
1853					       (SEC_ALLOC | SEC_LOAD
1854						| SEC_HAS_CONTENTS
1855						| SEC_IN_MEMORY
1856						| SEC_LINKER_CREATED
1857						| SEC_READONLY));
1858  if (htab->srelgot == NULL
1859      || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1860    return FALSE;
1861  return TRUE;
1862}
1863
1864/* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
1865   .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
1866   hash table.  */
1867
1868static bfd_boolean
1869elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1870{
1871  struct elf32_arm_link_hash_table *htab;
1872
1873  htab = elf32_arm_hash_table (info);
1874  if (!htab->sgot && !create_got_section (dynobj, info))
1875    return FALSE;
1876
1877  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1878    return FALSE;
1879
1880  htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1881  htab->srelplt = bfd_get_section_by_name (dynobj,
1882					   RELOC_SECTION (htab, ".plt"));
1883  htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1884  if (!info->shared)
1885    htab->srelbss = bfd_get_section_by_name (dynobj,
1886					     RELOC_SECTION (htab, ".bss"));
1887
1888  if (htab->vxworks_p)
1889    {
1890      if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
1891	return FALSE;
1892
1893      if (info->shared)
1894	{
1895	  htab->plt_header_size = 0;
1896	  htab->plt_entry_size
1897	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
1898	}
1899      else
1900	{
1901	  htab->plt_header_size
1902	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
1903	  htab->plt_entry_size
1904	    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
1905	}
1906    }
1907
1908  if (!htab->splt
1909      || !htab->srelplt
1910      || !htab->sdynbss
1911      || (!info->shared && !htab->srelbss))
1912    abort ();
1913
1914  return TRUE;
1915}
1916
1917/* Copy the extra info we tack onto an elf_link_hash_entry.  */
1918
1919static void
1920elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
1921				struct elf_link_hash_entry *dir,
1922				struct elf_link_hash_entry *ind)
1923{
1924  struct elf32_arm_link_hash_entry *edir, *eind;
1925
1926  edir = (struct elf32_arm_link_hash_entry *) dir;
1927  eind = (struct elf32_arm_link_hash_entry *) ind;
1928
1929  if (eind->relocs_copied != NULL)
1930    {
1931      if (edir->relocs_copied != NULL)
1932	{
1933	  struct elf32_arm_relocs_copied **pp;
1934	  struct elf32_arm_relocs_copied *p;
1935
1936	  /* Add reloc counts against the indirect sym to the direct sym
1937	     list.  Merge any entries against the same section.  */
1938	  for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
1939	    {
1940	      struct elf32_arm_relocs_copied *q;
1941
1942	      for (q = edir->relocs_copied; q != NULL; q = q->next)
1943		if (q->section == p->section)
1944		  {
1945		    q->pc_count += p->pc_count;
1946		    q->count += p->count;
1947		    *pp = p->next;
1948		    break;
1949		  }
1950	      if (q == NULL)
1951		pp = &p->next;
1952	    }
1953	  *pp = edir->relocs_copied;
1954	}
1955
1956      edir->relocs_copied = eind->relocs_copied;
1957      eind->relocs_copied = NULL;
1958    }
1959
1960  /* Copy over PLT info.  */
1961  edir->plt_thumb_refcount += eind->plt_thumb_refcount;
1962  eind->plt_thumb_refcount = 0;
1963
1964  if (ind->root.type == bfd_link_hash_indirect
1965      && dir->got.refcount <= 0)
1966    {
1967      edir->tls_type = eind->tls_type;
1968      eind->tls_type = GOT_UNKNOWN;
1969    }
1970
1971  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1972}
1973
1974/* Create an ARM elf linker hash table.  */
1975
1976static struct bfd_link_hash_table *
1977elf32_arm_link_hash_table_create (bfd *abfd)
1978{
1979  struct elf32_arm_link_hash_table *ret;
1980  bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
1981
1982  ret = bfd_malloc (amt);
1983  if (ret == NULL)
1984    return NULL;
1985
1986  if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
1987				      elf32_arm_link_hash_newfunc,
1988				      sizeof (struct elf32_arm_link_hash_entry)))
1989    {
1990      free (ret);
1991      return NULL;
1992    }
1993
1994  ret->sgot = NULL;
1995  ret->sgotplt = NULL;
1996  ret->srelgot = NULL;
1997  ret->splt = NULL;
1998  ret->srelplt = NULL;
1999  ret->sdynbss = NULL;
2000  ret->srelbss = NULL;
2001  ret->srelplt2 = NULL;
2002  ret->thumb_glue_size = 0;
2003  ret->arm_glue_size = 0;
2004  ret->bfd_of_glue_owner = NULL;
2005  ret->byteswap_code = 0;
2006  ret->target1_is_rel = 0;
2007  ret->target2_reloc = R_ARM_NONE;
2008#ifdef FOUR_WORD_PLT
2009  ret->plt_header_size = 16;
2010  ret->plt_entry_size = 16;
2011#else
2012  ret->plt_header_size = 20;
2013  ret->plt_entry_size = 12;
2014#endif
2015  ret->fix_v4bx = 0;
2016  ret->use_blx = 0;
2017  ret->vxworks_p = 0;
2018  ret->symbian_p = 0;
2019  ret->use_rel = 1;
2020  ret->sym_sec.abfd = NULL;
2021  ret->obfd = abfd;
2022  ret->tls_ldm_got.refcount = 0;
2023
2024  return &ret->root.root;
2025}
2026
2027/* Locate the Thumb encoded calling stub for NAME.  */
2028
2029static struct elf_link_hash_entry *
2030find_thumb_glue (struct bfd_link_info *link_info,
2031		 const char *name,
2032		 bfd *input_bfd)
2033{
2034  char *tmp_name;
2035  struct elf_link_hash_entry *hash;
2036  struct elf32_arm_link_hash_table *hash_table;
2037
2038  /* We need a pointer to the armelf specific hash table.  */
2039  hash_table = elf32_arm_hash_table (link_info);
2040
2041  tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2042			 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
2043
2044  BFD_ASSERT (tmp_name);
2045
2046  sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
2047
2048  hash = elf_link_hash_lookup
2049    (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2050
2051  if (hash == NULL)
2052    /* xgettext:c-format */
2053    (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
2054			   input_bfd, tmp_name, name);
2055
2056  free (tmp_name);
2057
2058  return hash;
2059}
2060
2061/* Locate the ARM encoded calling stub for NAME.  */
2062
2063static struct elf_link_hash_entry *
2064find_arm_glue (struct bfd_link_info *link_info,
2065	       const char *name,
2066	       bfd *input_bfd)
2067{
2068  char *tmp_name;
2069  struct elf_link_hash_entry *myh;
2070  struct elf32_arm_link_hash_table *hash_table;
2071
2072  /* We need a pointer to the elfarm specific hash table.  */
2073  hash_table = elf32_arm_hash_table (link_info);
2074
2075  tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2076			 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
2077
2078  BFD_ASSERT (tmp_name);
2079
2080  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
2081
2082  myh = elf_link_hash_lookup
2083    (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2084
2085  if (myh == NULL)
2086    /* xgettext:c-format */
2087    (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
2088			   input_bfd, tmp_name, name);
2089
2090  free (tmp_name);
2091
2092  return myh;
2093}
2094
2095/* ARM->Thumb glue (static images):
2096
2097   .arm
2098   __func_from_arm:
2099   ldr r12, __func_addr
2100   bx  r12
2101   __func_addr:
2102   .word func    @ behave as if you saw a ARM_32 reloc.
2103
2104   (relocatable images)
2105   .arm
2106   __func_from_arm:
2107   ldr r12, __func_offset
2108   add r12, r12, pc
2109   bx  r12
2110   __func_offset:
2111   .word func - .
2112   */
2113
2114#define ARM2THUMB_STATIC_GLUE_SIZE 12
2115static const insn32 a2t1_ldr_insn = 0xe59fc000;
2116static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
2117static const insn32 a2t3_func_addr_insn = 0x00000001;
2118
2119#define ARM2THUMB_PIC_GLUE_SIZE 16
2120static const insn32 a2t1p_ldr_insn = 0xe59fc004;
2121static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
2122static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
2123
2124/* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
2125
2126   .thumb                               .thumb
2127   .align 2                             .align 2
2128   __func_from_thumb:              __func_from_thumb:
2129   bx pc                                push {r6, lr}
2130   nop                                  ldr  r6, __func_addr
2131   .arm                                         mov  lr, pc
2132   __func_change_to_arm:                        bx   r6
2133   b func                       .arm
2134   __func_back_to_thumb:
2135   ldmia r13! {r6, lr}
2136   bx    lr
2137   __func_addr:
2138   .word        func  */
2139
2140#define THUMB2ARM_GLUE_SIZE 8
2141static const insn16 t2a1_bx_pc_insn = 0x4778;
2142static const insn16 t2a2_noop_insn = 0x46c0;
2143static const insn32 t2a3_b_insn = 0xea000000;
2144
2145#ifndef ELFARM_NABI_C_INCLUDED
2146bfd_boolean
2147bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
2148{
2149  asection * s;
2150  bfd_byte * foo;
2151  struct elf32_arm_link_hash_table * globals;
2152
2153  globals = elf32_arm_hash_table (info);
2154
2155  BFD_ASSERT (globals != NULL);
2156
2157  if (globals->arm_glue_size != 0)
2158    {
2159      BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2160
2161      s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2162				   ARM2THUMB_GLUE_SECTION_NAME);
2163
2164      BFD_ASSERT (s != NULL);
2165
2166      foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
2167
2168      s->size = globals->arm_glue_size;
2169      s->contents = foo;
2170    }
2171
2172  if (globals->thumb_glue_size != 0)
2173    {
2174      BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2175
2176      s = bfd_get_section_by_name
2177	(globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
2178
2179      BFD_ASSERT (s != NULL);
2180
2181      foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
2182
2183      s->size = globals->thumb_glue_size;
2184      s->contents = foo;
2185    }
2186
2187  return TRUE;
2188}
2189
2190static void
2191record_arm_to_thumb_glue (struct bfd_link_info * link_info,
2192			  struct elf_link_hash_entry * h)
2193{
2194  const char * name = h->root.root.string;
2195  asection * s;
2196  char * tmp_name;
2197  struct elf_link_hash_entry * myh;
2198  struct bfd_link_hash_entry * bh;
2199  struct elf32_arm_link_hash_table * globals;
2200  bfd_vma val;
2201
2202  globals = elf32_arm_hash_table (link_info);
2203
2204  BFD_ASSERT (globals != NULL);
2205  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2206
2207  s = bfd_get_section_by_name
2208    (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
2209
2210  BFD_ASSERT (s != NULL);
2211
2212  tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
2213
2214  BFD_ASSERT (tmp_name);
2215
2216  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
2217
2218  myh = elf_link_hash_lookup
2219    (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
2220
2221  if (myh != NULL)
2222    {
2223      /* We've already seen this guy.  */
2224      free (tmp_name);
2225      return;
2226    }
2227
2228  /* The only trick here is using hash_table->arm_glue_size as the value.
2229     Even though the section isn't allocated yet, this is where we will be
2230     putting it.  */
2231  bh = NULL;
2232  val = globals->arm_glue_size + 1;
2233  _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
2234				    tmp_name, BSF_GLOBAL, s, val,
2235				    NULL, TRUE, FALSE, &bh);
2236
2237  myh = (struct elf_link_hash_entry *) bh;
2238  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
2239  myh->forced_local = 1;
2240
2241  free (tmp_name);
2242
2243  if ((link_info->shared || globals->root.is_relocatable_executable))
2244    globals->arm_glue_size += ARM2THUMB_PIC_GLUE_SIZE;
2245  else
2246    globals->arm_glue_size += ARM2THUMB_STATIC_GLUE_SIZE;
2247
2248  return;
2249}
2250
2251static void
2252record_thumb_to_arm_glue (struct bfd_link_info *link_info,
2253			  struct elf_link_hash_entry *h)
2254{
2255  const char *name = h->root.root.string;
2256  asection *s;
2257  char *tmp_name;
2258  struct elf_link_hash_entry *myh;
2259  struct bfd_link_hash_entry *bh;
2260  struct elf32_arm_link_hash_table *hash_table;
2261  bfd_vma val;
2262
2263  hash_table = elf32_arm_hash_table (link_info);
2264
2265  BFD_ASSERT (hash_table != NULL);
2266  BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
2267
2268  s = bfd_get_section_by_name
2269    (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
2270
2271  BFD_ASSERT (s != NULL);
2272
2273  tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2274			 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
2275
2276  BFD_ASSERT (tmp_name);
2277
2278  sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
2279
2280  myh = elf_link_hash_lookup
2281    (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
2282
2283  if (myh != NULL)
2284    {
2285      /* We've already seen this guy.  */
2286      free (tmp_name);
2287      return;
2288    }
2289
2290  bh = NULL;
2291  val = hash_table->thumb_glue_size + 1;
2292  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
2293				    tmp_name, BSF_GLOBAL, s, val,
2294				    NULL, TRUE, FALSE, &bh);
2295
2296  /* If we mark it 'Thumb', the disassembler will do a better job.  */
2297  myh = (struct elf_link_hash_entry *) bh;
2298  myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
2299  myh->forced_local = 1;
2300
2301  free (tmp_name);
2302
2303#define CHANGE_TO_ARM "__%s_change_to_arm"
2304#define BACK_FROM_ARM "__%s_back_from_arm"
2305
2306  /* Allocate another symbol to mark where we switch to Arm mode.  */
2307  tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
2308			 + strlen (CHANGE_TO_ARM) + 1);
2309
2310  BFD_ASSERT (tmp_name);
2311
2312  sprintf (tmp_name, CHANGE_TO_ARM, name);
2313
2314  bh = NULL;
2315  val = hash_table->thumb_glue_size + 4,
2316  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
2317				    tmp_name, BSF_LOCAL, s, val,
2318				    NULL, TRUE, FALSE, &bh);
2319
2320  free (tmp_name);
2321
2322  hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
2323
2324  return;
2325}
2326
2327/* Add the glue sections to ABFD.  This function is called from the
2328   linker scripts in ld/emultempl/{armelf}.em.  */
2329
2330bfd_boolean
2331bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
2332					struct bfd_link_info *info)
2333{
2334  flagword flags;
2335  asection *sec;
2336
2337  /* If we are only performing a partial
2338     link do not bother adding the glue.  */
2339  if (info->relocatable)
2340    return TRUE;
2341
2342  sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
2343
2344  if (sec == NULL)
2345    {
2346      /* Note: we do not include the flag SEC_LINKER_CREATED, as this
2347	 will prevent elf_link_input_bfd() from processing the contents
2348	 of this section.  */
2349      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
2350
2351      sec = bfd_make_section_with_flags (abfd,
2352					 ARM2THUMB_GLUE_SECTION_NAME,
2353					 flags);
2354
2355      if (sec == NULL
2356	  || !bfd_set_section_alignment (abfd, sec, 2))
2357	return FALSE;
2358
2359      /* Set the gc mark to prevent the section from being removed by garbage
2360	 collection, despite the fact that no relocs refer to this section.  */
2361      sec->gc_mark = 1;
2362    }
2363
2364  sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
2365
2366  if (sec == NULL)
2367    {
2368      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2369	| SEC_CODE | SEC_READONLY;
2370
2371      sec = bfd_make_section_with_flags (abfd,
2372					 THUMB2ARM_GLUE_SECTION_NAME,
2373					 flags);
2374
2375      if (sec == NULL
2376	  || !bfd_set_section_alignment (abfd, sec, 2))
2377	return FALSE;
2378
2379      sec->gc_mark = 1;
2380    }
2381
2382  return TRUE;
2383}
2384
2385/* Select a BFD to be used to hold the sections used by the glue code.
2386   This function is called from the linker scripts in ld/emultempl/
2387   {armelf/pe}.em  */
2388
2389bfd_boolean
2390bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
2391{
2392  struct elf32_arm_link_hash_table *globals;
2393
2394  /* If we are only performing a partial link
2395     do not bother getting a bfd to hold the glue.  */
2396  if (info->relocatable)
2397    return TRUE;
2398
2399  /* Make sure we don't attach the glue sections to a dynamic object.  */
2400  BFD_ASSERT (!(abfd->flags & DYNAMIC));
2401
2402  globals = elf32_arm_hash_table (info);
2403
2404  BFD_ASSERT (globals != NULL);
2405
2406  if (globals->bfd_of_glue_owner != NULL)
2407    return TRUE;
2408
2409  /* Save the bfd for later use.  */
2410  globals->bfd_of_glue_owner = abfd;
2411
2412  return TRUE;
2413}
2414
2415static void check_use_blx(struct elf32_arm_link_hash_table *globals)
2416{
2417  if (elf32_arm_get_eabi_attr_int (globals->obfd, Tag_CPU_arch) > 2)
2418    globals->use_blx = 1;
2419}
2420
2421bfd_boolean
2422bfd_elf32_arm_process_before_allocation (bfd *abfd,
2423					 struct bfd_link_info *link_info,
2424					 int byteswap_code)
2425{
2426  Elf_Internal_Shdr *symtab_hdr;
2427  Elf_Internal_Rela *internal_relocs = NULL;
2428  Elf_Internal_Rela *irel, *irelend;
2429  bfd_byte *contents = NULL;
2430
2431  asection *sec;
2432  struct elf32_arm_link_hash_table *globals;
2433
2434  /* If we are only performing a partial link do not bother
2435     to construct any glue.  */
2436  if (link_info->relocatable)
2437    return TRUE;
2438
2439  /* Here we have a bfd that is to be included on the link.  We have a hook
2440     to do reloc rummaging, before section sizes are nailed down.  */
2441  globals = elf32_arm_hash_table (link_info);
2442  check_use_blx (globals);
2443
2444  BFD_ASSERT (globals != NULL);
2445  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2446
2447  if (byteswap_code && !bfd_big_endian (abfd))
2448    {
2449      _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
2450			  abfd);
2451      return FALSE;
2452    }
2453  globals->byteswap_code = byteswap_code;
2454
2455  /* Rummage around all the relocs and map the glue vectors.  */
2456  sec = abfd->sections;
2457
2458  if (sec == NULL)
2459    return TRUE;
2460
2461  for (; sec != NULL; sec = sec->next)
2462    {
2463      if (sec->reloc_count == 0)
2464	continue;
2465
2466      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2467
2468      /* Load the relocs.  */
2469      internal_relocs
2470	= _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
2471				     (Elf_Internal_Rela *) NULL, FALSE);
2472
2473      if (internal_relocs == NULL)
2474	goto error_return;
2475
2476      irelend = internal_relocs + sec->reloc_count;
2477      for (irel = internal_relocs; irel < irelend; irel++)
2478	{
2479	  long r_type;
2480	  unsigned long r_index;
2481
2482	  struct elf_link_hash_entry *h;
2483
2484	  r_type = ELF32_R_TYPE (irel->r_info);
2485	  r_index = ELF32_R_SYM (irel->r_info);
2486
2487	  /* These are the only relocation types we care about.  */
2488	  if (   r_type != R_ARM_PC24
2489	      && r_type != R_ARM_PLT32
2490	      && r_type != R_ARM_CALL
2491	      && r_type != R_ARM_JUMP24
2492	      && r_type != R_ARM_THM_CALL)
2493	    continue;
2494
2495	  /* Get the section contents if we haven't done so already.  */
2496	  if (contents == NULL)
2497	    {
2498	      /* Get cached copy if it exists.  */
2499	      if (elf_section_data (sec)->this_hdr.contents != NULL)
2500		contents = elf_section_data (sec)->this_hdr.contents;
2501	      else
2502		{
2503		  /* Go get them off disk.  */
2504		  if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2505		    goto error_return;
2506		}
2507	    }
2508
2509	  /* If the relocation is not against a symbol it cannot concern us.  */
2510	  h = NULL;
2511
2512	  /* We don't care about local symbols.  */
2513	  if (r_index < symtab_hdr->sh_info)
2514	    continue;
2515
2516	  /* This is an external symbol.  */
2517	  r_index -= symtab_hdr->sh_info;
2518	  h = (struct elf_link_hash_entry *)
2519	    elf_sym_hashes (abfd)[r_index];
2520
2521	  /* If the relocation is against a static symbol it must be within
2522	     the current section and so cannot be a cross ARM/Thumb relocation.  */
2523	  if (h == NULL)
2524	    continue;
2525
2526	  /* If the call will go through a PLT entry then we do not need
2527	     glue.  */
2528	  if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
2529	    continue;
2530
2531	  switch (r_type)
2532	    {
2533	    case R_ARM_PC24:
2534	    case R_ARM_PLT32:
2535	    case R_ARM_CALL:
2536	    case R_ARM_JUMP24:
2537	      /* This one is a call from arm code.  We need to look up
2538	         the target of the call.  If it is a thumb target, we
2539	         insert glue.  */
2540	      if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC
2541		  && !(r_type == R_ARM_CALL && globals->use_blx))
2542		record_arm_to_thumb_glue (link_info, h);
2543	      break;
2544
2545	    case R_ARM_THM_CALL:
2546	      /* This one is a call from thumb code.  We look
2547	         up the target of the call.  If it is not a thumb
2548                 target, we insert glue.  */
2549	      if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC && !globals->use_blx)
2550		record_thumb_to_arm_glue (link_info, h);
2551	      break;
2552
2553	    default:
2554	      abort ();
2555	    }
2556	}
2557
2558      if (contents != NULL
2559	  && elf_section_data (sec)->this_hdr.contents != contents)
2560	free (contents);
2561      contents = NULL;
2562
2563      if (internal_relocs != NULL
2564	  && elf_section_data (sec)->relocs != internal_relocs)
2565	free (internal_relocs);
2566      internal_relocs = NULL;
2567    }
2568
2569  return TRUE;
2570
2571error_return:
2572  if (contents != NULL
2573      && elf_section_data (sec)->this_hdr.contents != contents)
2574    free (contents);
2575  if (internal_relocs != NULL
2576      && elf_section_data (sec)->relocs != internal_relocs)
2577    free (internal_relocs);
2578
2579  return FALSE;
2580}
2581#endif
2582
2583
2584/* Set target relocation values needed during linking.  */
2585
2586void
2587bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
2588				 int target1_is_rel,
2589				 char * target2_type,
2590                                 int fix_v4bx,
2591				 int use_blx)
2592{
2593  struct elf32_arm_link_hash_table *globals;
2594
2595  globals = elf32_arm_hash_table (link_info);
2596
2597  globals->target1_is_rel = target1_is_rel;
2598  if (strcmp (target2_type, "rel") == 0)
2599    globals->target2_reloc = R_ARM_REL32;
2600  else if (strcmp (target2_type, "abs") == 0)
2601    globals->target2_reloc = R_ARM_ABS32;
2602  else if (strcmp (target2_type, "got-rel") == 0)
2603    globals->target2_reloc = R_ARM_GOT_PREL;
2604  else
2605    {
2606      _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
2607			  target2_type);
2608    }
2609  globals->fix_v4bx = fix_v4bx;
2610  globals->use_blx |= use_blx;
2611}
2612
2613/* The thumb form of a long branch is a bit finicky, because the offset
2614   encoding is split over two fields, each in it's own instruction. They
2615   can occur in any order. So given a thumb form of long branch, and an
2616   offset, insert the offset into the thumb branch and return finished
2617   instruction.
2618
2619   It takes two thumb instructions to encode the target address. Each has
2620   11 bits to invest. The upper 11 bits are stored in one (identified by
2621   H-0.. see below), the lower 11 bits are stored in the other (identified
2622   by H-1).
2623
2624   Combine together and shifted left by 1 (it's a half word address) and
2625   there you have it.
2626
2627   Op: 1111 = F,
2628   H-0, upper address-0 = 000
2629   Op: 1111 = F,
2630   H-1, lower address-0 = 800
2631
2632   They can be ordered either way, but the arm tools I've seen always put
2633   the lower one first. It probably doesn't matter. krk@cygnus.com
2634
2635   XXX:  Actually the order does matter.  The second instruction (H-1)
2636   moves the computed address into the PC, so it must be the second one
2637   in the sequence.  The problem, however is that whilst little endian code
2638   stores the instructions in HI then LOW order, big endian code does the
2639   reverse.  nickc@cygnus.com.  */
2640
2641#define LOW_HI_ORDER      0xF800F000
2642#define HI_LOW_ORDER      0xF000F800
2643
2644static insn32
2645insert_thumb_branch (insn32 br_insn, int rel_off)
2646{
2647  unsigned int low_bits;
2648  unsigned int high_bits;
2649
2650  BFD_ASSERT ((rel_off & 1) != 1);
2651
2652  rel_off >>= 1;				/* Half word aligned address.  */
2653  low_bits = rel_off & 0x000007FF;		/* The bottom 11 bits.  */
2654  high_bits = (rel_off >> 11) & 0x000007FF;	/* The top 11 bits.  */
2655
2656  if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
2657    br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
2658  else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
2659    br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
2660  else
2661    /* FIXME: abort is probably not the right call. krk@cygnus.com  */
2662    abort ();	/* Error - not a valid branch instruction form.  */
2663
2664  return br_insn;
2665}
2666
2667/* Thumb code calling an ARM function.  */
2668
2669static int
2670elf32_thumb_to_arm_stub (struct bfd_link_info * info,
2671			 const char *           name,
2672			 bfd *                  input_bfd,
2673			 bfd *                  output_bfd,
2674			 asection *             input_section,
2675			 bfd_byte *             hit_data,
2676			 asection *             sym_sec,
2677			 bfd_vma                offset,
2678			 bfd_signed_vma         addend,
2679			 bfd_vma                val)
2680{
2681  asection * s = 0;
2682  bfd_vma my_offset;
2683  unsigned long int tmp;
2684  long int ret_offset;
2685  struct elf_link_hash_entry * myh;
2686  struct elf32_arm_link_hash_table * globals;
2687
2688  myh = find_thumb_glue (info, name, input_bfd);
2689  if (myh == NULL)
2690    return FALSE;
2691
2692  globals = elf32_arm_hash_table (info);
2693
2694  BFD_ASSERT (globals != NULL);
2695  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2696
2697  my_offset = myh->root.u.def.value;
2698
2699  s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2700			       THUMB2ARM_GLUE_SECTION_NAME);
2701
2702  BFD_ASSERT (s != NULL);
2703  BFD_ASSERT (s->contents != NULL);
2704  BFD_ASSERT (s->output_section != NULL);
2705
2706  if ((my_offset & 0x01) == 0x01)
2707    {
2708      if (sym_sec != NULL
2709	  && sym_sec->owner != NULL
2710	  && !INTERWORK_FLAG (sym_sec->owner))
2711	{
2712	  (*_bfd_error_handler)
2713	    (_("%B(%s): warning: interworking not enabled.\n"
2714	       "  first occurrence: %B: thumb call to arm"),
2715	     sym_sec->owner, input_bfd, name);
2716
2717	  return FALSE;
2718	}
2719
2720      --my_offset;
2721      myh->root.u.def.value = my_offset;
2722
2723      bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
2724		  s->contents + my_offset);
2725
2726      bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
2727		  s->contents + my_offset + 2);
2728
2729      ret_offset =
2730	/* Address of destination of the stub.  */
2731	((bfd_signed_vma) val)
2732	- ((bfd_signed_vma)
2733	   /* Offset from the start of the current section
2734	      to the start of the stubs.  */
2735	   (s->output_offset
2736	    /* Offset of the start of this stub from the start of the stubs.  */
2737	    + my_offset
2738	    /* Address of the start of the current section.  */
2739	    + s->output_section->vma)
2740	   /* The branch instruction is 4 bytes into the stub.  */
2741	   + 4
2742	   /* ARM branches work from the pc of the instruction + 8.  */
2743	   + 8);
2744
2745      bfd_put_32 (output_bfd,
2746		  (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
2747		  s->contents + my_offset + 4);
2748    }
2749
2750  BFD_ASSERT (my_offset <= globals->thumb_glue_size);
2751
2752  /* Now go back and fix up the original BL insn to point to here.  */
2753  ret_offset =
2754    /* Address of where the stub is located.  */
2755    (s->output_section->vma + s->output_offset + my_offset)
2756     /* Address of where the BL is located.  */
2757    - (input_section->output_section->vma + input_section->output_offset
2758       + offset)
2759    /* Addend in the relocation.  */
2760    - addend
2761    /* Biassing for PC-relative addressing.  */
2762    - 8;
2763
2764  tmp = bfd_get_32 (input_bfd, hit_data
2765		    - input_section->vma);
2766
2767  bfd_put_32 (output_bfd,
2768	      (bfd_vma) insert_thumb_branch (tmp, ret_offset),
2769	      hit_data - input_section->vma);
2770
2771  return TRUE;
2772}
2773
2774/* Arm code calling a Thumb function.  */
2775
2776static int
2777elf32_arm_to_thumb_stub (struct bfd_link_info * info,
2778			 const char *           name,
2779			 bfd *                  input_bfd,
2780			 bfd *                  output_bfd,
2781			 asection *             input_section,
2782			 bfd_byte *             hit_data,
2783			 asection *             sym_sec,
2784			 bfd_vma                offset,
2785			 bfd_signed_vma         addend,
2786			 bfd_vma                val)
2787{
2788  unsigned long int tmp;
2789  bfd_vma my_offset;
2790  asection * s;
2791  long int ret_offset;
2792  struct elf_link_hash_entry * myh;
2793  struct elf32_arm_link_hash_table * globals;
2794
2795  myh = find_arm_glue (info, name, input_bfd);
2796  if (myh == NULL)
2797    return FALSE;
2798
2799  globals = elf32_arm_hash_table (info);
2800
2801  BFD_ASSERT (globals != NULL);
2802  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2803
2804  my_offset = myh->root.u.def.value;
2805  s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2806			       ARM2THUMB_GLUE_SECTION_NAME);
2807  BFD_ASSERT (s != NULL);
2808  BFD_ASSERT (s->contents != NULL);
2809  BFD_ASSERT (s->output_section != NULL);
2810
2811  if ((my_offset & 0x01) == 0x01)
2812    {
2813      if (sym_sec != NULL
2814	  && sym_sec->owner != NULL
2815	  && !INTERWORK_FLAG (sym_sec->owner))
2816	{
2817	  (*_bfd_error_handler)
2818	    (_("%B(%s): warning: interworking not enabled.\n"
2819	       "  first occurrence: %B: arm call to thumb"),
2820	     sym_sec->owner, input_bfd, name);
2821	}
2822
2823      --my_offset;
2824      myh->root.u.def.value = my_offset;
2825
2826      if ((info->shared || globals->root.is_relocatable_executable))
2827	{
2828	  /* For relocatable objects we can't use absolute addresses,
2829	     so construct the address from a relative offset.  */
2830	  /* TODO: If the offset is small it's probably worth
2831	     constructing the address with adds.  */
2832	  bfd_put_32 (output_bfd, (bfd_vma) a2t1p_ldr_insn,
2833		      s->contents + my_offset);
2834	  bfd_put_32 (output_bfd, (bfd_vma) a2t2p_add_pc_insn,
2835		      s->contents + my_offset + 4);
2836	  bfd_put_32 (output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
2837		      s->contents + my_offset + 8);
2838	  /* Adjust the offset by 4 for the position of the add,
2839	     and 8 for the pipeline offset.  */
2840	  ret_offset = (val - (s->output_offset
2841			       + s->output_section->vma
2842			       + my_offset + 12))
2843		       | 1;
2844	  bfd_put_32 (output_bfd, ret_offset,
2845		      s->contents + my_offset + 12);
2846	}
2847      else
2848	{
2849	  bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
2850		      s->contents + my_offset);
2851
2852	  bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
2853		      s->contents + my_offset + 4);
2854
2855	  /* It's a thumb address.  Add the low order bit.  */
2856	  bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
2857		      s->contents + my_offset + 8);
2858	}
2859    }
2860
2861  BFD_ASSERT (my_offset <= globals->arm_glue_size);
2862
2863  tmp = bfd_get_32 (input_bfd, hit_data);
2864  tmp = tmp & 0xFF000000;
2865
2866  /* Somehow these are both 4 too far, so subtract 8.  */
2867  ret_offset = (s->output_offset
2868		+ my_offset
2869		+ s->output_section->vma
2870		- (input_section->output_offset
2871		   + input_section->output_section->vma
2872		   + offset + addend)
2873		- 8);
2874
2875  tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
2876
2877  bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
2878
2879  return TRUE;
2880}
2881
2882/* Some relocations map to different relocations depending on the
2883   target.  Return the real relocation.  */
2884static int
2885arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
2886		     int r_type)
2887{
2888  switch (r_type)
2889    {
2890    case R_ARM_TARGET1:
2891      if (globals->target1_is_rel)
2892	return R_ARM_REL32;
2893      else
2894	return R_ARM_ABS32;
2895
2896    case R_ARM_TARGET2:
2897      return globals->target2_reloc;
2898
2899    default:
2900      return r_type;
2901    }
2902}
2903
2904/* Return the base VMA address which should be subtracted from real addresses
2905   when resolving @dtpoff relocation.
2906   This is PT_TLS segment p_vaddr.  */
2907
2908static bfd_vma
2909dtpoff_base (struct bfd_link_info *info)
2910{
2911  /* If tls_sec is NULL, we should have signalled an error already.  */
2912  if (elf_hash_table (info)->tls_sec == NULL)
2913    return 0;
2914  return elf_hash_table (info)->tls_sec->vma;
2915}
2916
2917/* Return the relocation value for @tpoff relocation
2918   if STT_TLS virtual address is ADDRESS.  */
2919
2920static bfd_vma
2921tpoff (struct bfd_link_info *info, bfd_vma address)
2922{
2923  struct elf_link_hash_table *htab = elf_hash_table (info);
2924  bfd_vma base;
2925
2926  /* If tls_sec is NULL, we should have signalled an error already.  */
2927  if (htab->tls_sec == NULL)
2928    return 0;
2929  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
2930  return address - htab->tls_sec->vma + base;
2931}
2932
2933/* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
2934   VALUE is the relocation value.  */
2935
2936static bfd_reloc_status_type
2937elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
2938{
2939  if (value > 0xfff)
2940    return bfd_reloc_overflow;
2941
2942  value |= bfd_get_32 (abfd, data) & 0xfffff000;
2943  bfd_put_32 (abfd, value, data);
2944  return bfd_reloc_ok;
2945}
2946
2947/* Perform a relocation as part of a final link.  */
2948
2949static bfd_reloc_status_type
2950elf32_arm_final_link_relocate (reloc_howto_type *           howto,
2951			       bfd *                        input_bfd,
2952			       bfd *                        output_bfd,
2953			       asection *                   input_section,
2954			       bfd_byte *                   contents,
2955			       Elf_Internal_Rela *          rel,
2956			       bfd_vma                      value,
2957			       struct bfd_link_info *       info,
2958			       asection *                   sym_sec,
2959			       const char *                 sym_name,
2960			       int		            sym_flags,
2961			       struct elf_link_hash_entry * h,
2962			       bfd_boolean *                unresolved_reloc_p)
2963{
2964  unsigned long                 r_type = howto->type;
2965  unsigned long                 r_symndx;
2966  bfd_byte *                    hit_data = contents + rel->r_offset;
2967  bfd *                         dynobj = NULL;
2968  Elf_Internal_Shdr *           symtab_hdr;
2969  struct elf_link_hash_entry ** sym_hashes;
2970  bfd_vma *                     local_got_offsets;
2971  asection *                    sgot = NULL;
2972  asection *                    splt = NULL;
2973  asection *                    sreloc = NULL;
2974  bfd_vma                       addend;
2975  bfd_signed_vma                signed_addend;
2976  struct elf32_arm_link_hash_table * globals;
2977
2978  globals = elf32_arm_hash_table (info);
2979
2980  /* Some relocation type map to different relocations depending on the
2981     target.  We pick the right one here.  */
2982  r_type = arm_real_reloc_type (globals, r_type);
2983  if (r_type != howto->type)
2984    howto = elf32_arm_howto_from_type (r_type);
2985
2986  /* If the start address has been set, then set the EF_ARM_HASENTRY
2987     flag.  Setting this more than once is redundant, but the cost is
2988     not too high, and it keeps the code simple.
2989
2990     The test is done  here, rather than somewhere else, because the
2991     start address is only set just before the final link commences.
2992
2993     Note - if the user deliberately sets a start address of 0, the
2994     flag will not be set.  */
2995  if (bfd_get_start_address (output_bfd) != 0)
2996    elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
2997
2998  dynobj = elf_hash_table (info)->dynobj;
2999  if (dynobj)
3000    {
3001      sgot = bfd_get_section_by_name (dynobj, ".got");
3002      splt = bfd_get_section_by_name (dynobj, ".plt");
3003    }
3004  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
3005  sym_hashes = elf_sym_hashes (input_bfd);
3006  local_got_offsets = elf_local_got_offsets (input_bfd);
3007  r_symndx = ELF32_R_SYM (rel->r_info);
3008
3009  if (globals->use_rel)
3010    {
3011      addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
3012
3013      if (addend & ((howto->src_mask + 1) >> 1))
3014	{
3015	  signed_addend = -1;
3016	  signed_addend &= ~ howto->src_mask;
3017	  signed_addend |= addend;
3018	}
3019      else
3020	signed_addend = addend;
3021    }
3022  else
3023    addend = signed_addend = rel->r_addend;
3024
3025  switch (r_type)
3026    {
3027    case R_ARM_NONE:
3028      /* We don't need to find a value for this symbol.  It's just a
3029	 marker.  */
3030      *unresolved_reloc_p = FALSE;
3031      return bfd_reloc_ok;
3032
3033    case R_ARM_ABS12:
3034      if (!globals->vxworks_p)
3035	return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
3036
3037    case R_ARM_PC24:
3038    case R_ARM_ABS32:
3039    case R_ARM_REL32:
3040    case R_ARM_CALL:
3041    case R_ARM_JUMP24:
3042    case R_ARM_XPC25:
3043    case R_ARM_PREL31:
3044    case R_ARM_PLT32:
3045      /* r_symndx will be zero only for relocs against symbols
3046	 from removed linkonce sections, or sections discarded by
3047	 a linker script.  */
3048      if (r_symndx == 0)
3049	return bfd_reloc_ok;
3050
3051      /* Handle relocations which should use the PLT entry.  ABS32/REL32
3052	 will use the symbol's value, which may point to a PLT entry, but we
3053	 don't need to handle that here.  If we created a PLT entry, all
3054	 branches in this object should go to it.  */
3055      if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
3056	  && h != NULL
3057	  && splt != NULL
3058	  && h->plt.offset != (bfd_vma) -1)
3059	{
3060	  /* If we've created a .plt section, and assigned a PLT entry to
3061	     this function, it should not be known to bind locally.  If
3062	     it were, we would have cleared the PLT entry.  */
3063	  BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3064
3065	  value = (splt->output_section->vma
3066		   + splt->output_offset
3067		   + h->plt.offset);
3068	  *unresolved_reloc_p = FALSE;
3069	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
3070					   contents, rel->r_offset, value,
3071					   rel->r_addend);
3072	}
3073
3074      /* When generating a shared object or relocatable executable, these
3075	 relocations are copied into the output file to be resolved at
3076	 run time.  */
3077      if ((info->shared || globals->root.is_relocatable_executable)
3078	  && (input_section->flags & SEC_ALLOC)
3079	  && (r_type != R_ARM_REL32
3080	      || !SYMBOL_CALLS_LOCAL (info, h))
3081	  && (h == NULL
3082	      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3083	      || h->root.type != bfd_link_hash_undefweak)
3084	  && r_type != R_ARM_PC24
3085	  && r_type != R_ARM_CALL
3086	  && r_type != R_ARM_JUMP24
3087	  && r_type != R_ARM_PREL31
3088	  && r_type != R_ARM_PLT32)
3089	{
3090	  Elf_Internal_Rela outrel;
3091	  bfd_byte *loc;
3092	  bfd_boolean skip, relocate;
3093
3094	  *unresolved_reloc_p = FALSE;
3095
3096	  if (sreloc == NULL)
3097	    {
3098	      const char * name;
3099
3100	      name = (bfd_elf_string_from_elf_section
3101		      (input_bfd,
3102		       elf_elfheader (input_bfd)->e_shstrndx,
3103		       elf_section_data (input_section)->rel_hdr.sh_name));
3104	      if (name == NULL)
3105		return bfd_reloc_notsupported;
3106
3107	      BFD_ASSERT (reloc_section_p (globals, name, input_section));
3108
3109	      sreloc = bfd_get_section_by_name (dynobj, name);
3110	      BFD_ASSERT (sreloc != NULL);
3111	    }
3112
3113	  skip = FALSE;
3114	  relocate = FALSE;
3115
3116	  outrel.r_addend = addend;
3117	  outrel.r_offset =
3118	    _bfd_elf_section_offset (output_bfd, info, input_section,
3119				     rel->r_offset);
3120	  if (outrel.r_offset == (bfd_vma) -1)
3121	    skip = TRUE;
3122	  else if (outrel.r_offset == (bfd_vma) -2)
3123	    skip = TRUE, relocate = TRUE;
3124	  outrel.r_offset += (input_section->output_section->vma
3125			      + input_section->output_offset);
3126
3127	  if (skip)
3128	    memset (&outrel, 0, sizeof outrel);
3129	  else if (h != NULL
3130		   && h->dynindx != -1
3131		   && (!info->shared
3132		       || !info->symbolic
3133		       || !h->def_regular))
3134	    outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3135	  else
3136	    {
3137	      int symbol;
3138
3139	      /* This symbol is local, or marked to become local.  */
3140	      if (sym_flags == STT_ARM_TFUNC)
3141		value |= 1;
3142	      if (globals->symbian_p)
3143		{
3144		  /* On Symbian OS, the data segment and text segement
3145		     can be relocated independently.  Therefore, we
3146		     must indicate the segment to which this
3147		     relocation is relative.  The BPABI allows us to
3148		     use any symbol in the right segment; we just use
3149		     the section symbol as it is convenient.  (We
3150		     cannot use the symbol given by "h" directly as it
3151		     will not appear in the dynamic symbol table.)  */
3152		  if (sym_sec)
3153		    symbol = elf_section_data (sym_sec->output_section)->dynindx;
3154		  else
3155		    symbol = elf_section_data (input_section->output_section)->dynindx;
3156		  BFD_ASSERT (symbol != 0);
3157		}
3158	      else
3159		/* On SVR4-ish systems, the dynamic loader cannot
3160		   relocate the text and data segments independently,
3161		   so the symbol does not matter.  */
3162		symbol = 0;
3163	      outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
3164	      if (globals->use_rel)
3165		relocate = TRUE;
3166	      else
3167		outrel.r_addend += value;
3168	    }
3169
3170	  loc = sreloc->contents;
3171	  loc += sreloc->reloc_count++ * RELOC_SIZE (globals);
3172	  SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
3173
3174	  /* If this reloc is against an external symbol, we do not want to
3175	     fiddle with the addend.  Otherwise, we need to include the symbol
3176	     value so that it becomes an addend for the dynamic reloc.  */
3177	  if (! relocate)
3178	    return bfd_reloc_ok;
3179
3180	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
3181					   contents, rel->r_offset, value,
3182					   (bfd_vma) 0);
3183	}
3184      else switch (r_type)
3185	{
3186	case R_ARM_ABS12:
3187	  return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
3188
3189	case R_ARM_XPC25:	  /* Arm BLX instruction.  */
3190	case R_ARM_CALL:
3191	case R_ARM_JUMP24:
3192	case R_ARM_PC24:	  /* Arm B/BL instruction */
3193	case R_ARM_PLT32:
3194	  if (r_type == R_ARM_XPC25)
3195	    {
3196	      /* Check for Arm calling Arm function.  */
3197	      /* FIXME: Should we translate the instruction into a BL
3198		 instruction instead ?  */
3199	      if (sym_flags != STT_ARM_TFUNC)
3200		(*_bfd_error_handler)
3201		  (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
3202		   input_bfd,
3203		   h ? h->root.root.string : "(local)");
3204	    }
3205	  else if (r_type != R_ARM_CALL || !globals->use_blx)
3206	    {
3207	      /* Check for Arm calling Thumb function.  */
3208	      if (sym_flags == STT_ARM_TFUNC)
3209		{
3210		  elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
3211					   output_bfd, input_section,
3212					   hit_data, sym_sec, rel->r_offset,
3213					   signed_addend, value);
3214		  return bfd_reloc_ok;
3215		}
3216	    }
3217
3218	  /* The ARM ELF ABI says that this reloc is computed as: S - P + A
3219	     where:
3220	      S is the address of the symbol in the relocation.
3221	      P is address of the instruction being relocated.
3222	      A is the addend (extracted from the instruction) in bytes.
3223
3224	     S is held in 'value'.
3225	     P is the base address of the section containing the
3226	       instruction plus the offset of the reloc into that
3227	       section, ie:
3228		 (input_section->output_section->vma +
3229		  input_section->output_offset +
3230		  rel->r_offset).
3231	     A is the addend, converted into bytes, ie:
3232		 (signed_addend * 4)
3233
3234	     Note: None of these operations have knowledge of the pipeline
3235	     size of the processor, thus it is up to the assembler to
3236	     encode this information into the addend.  */
3237	  value -= (input_section->output_section->vma
3238		    + input_section->output_offset);
3239	  value -= rel->r_offset;
3240	  if (globals->use_rel)
3241	    value += (signed_addend << howto->size);
3242	  else
3243	    /* RELA addends do not have to be adjusted by howto->size.  */
3244	    value += signed_addend;
3245
3246	  signed_addend = value;
3247	  signed_addend >>= howto->rightshift;
3248
3249	  /* It is not an error for an undefined weak reference to be
3250	     out of range.  Any program that branches to such a symbol
3251	     is going to crash anyway, so there is no point worrying
3252	     about getting the destination exactly right.  */
3253	  if (! h || h->root.type != bfd_link_hash_undefweak)
3254	    {
3255	      /* Perform a signed range check.  */
3256	      if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
3257		  || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
3258		return bfd_reloc_overflow;
3259	    }
3260
3261	  addend = (value & 2);
3262
3263	  value = (signed_addend & howto->dst_mask)
3264	    | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
3265
3266	  /* Set the H bit in the BLX instruction.  */
3267	  if (sym_flags == STT_ARM_TFUNC)
3268	    {
3269	      if (addend)
3270		value |= (1 << 24);
3271	      else
3272		value &= ~(bfd_vma)(1 << 24);
3273	    }
3274	  if (r_type == R_ARM_CALL)
3275	    {
3276	      /* Select the correct instruction (BL or BLX).  */
3277	      if (sym_flags == STT_ARM_TFUNC)
3278		value |= (1 << 28);
3279	      else
3280		{
3281		  value &= ~(bfd_vma)(1 << 28);
3282		  value |= (1 << 24);
3283		}
3284	    }
3285	  break;
3286
3287	case R_ARM_ABS32:
3288	  value += addend;
3289	  if (sym_flags == STT_ARM_TFUNC)
3290	    value |= 1;
3291	  break;
3292
3293	case R_ARM_REL32:
3294	  value += addend;
3295	  if (sym_flags == STT_ARM_TFUNC)
3296	    value |= 1;
3297	  value -= (input_section->output_section->vma
3298		    + input_section->output_offset + rel->r_offset);
3299	  break;
3300
3301	case R_ARM_PREL31:
3302	  value -= (input_section->output_section->vma
3303		    + input_section->output_offset + rel->r_offset);
3304	  value += signed_addend;
3305	  if (! h || h->root.type != bfd_link_hash_undefweak)
3306	    {
3307	      /* Check for overflow */
3308	      if ((value ^ (value >> 1)) & (1 << 30))
3309		return bfd_reloc_overflow;
3310	    }
3311	  value &= 0x7fffffff;
3312	  value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
3313	  if (sym_flags == STT_ARM_TFUNC)
3314	    value |= 1;
3315	  break;
3316	}
3317
3318      bfd_put_32 (input_bfd, value, hit_data);
3319      return bfd_reloc_ok;
3320
3321    case R_ARM_ABS8:
3322      value += addend;
3323      if ((long) value > 0x7f || (long) value < -0x80)
3324	return bfd_reloc_overflow;
3325
3326      bfd_put_8 (input_bfd, value, hit_data);
3327      return bfd_reloc_ok;
3328
3329    case R_ARM_ABS16:
3330      value += addend;
3331
3332      if ((long) value > 0x7fff || (long) value < -0x8000)
3333	return bfd_reloc_overflow;
3334
3335      bfd_put_16 (input_bfd, value, hit_data);
3336      return bfd_reloc_ok;
3337
3338    case R_ARM_THM_ABS5:
3339      /* Support ldr and str instructions for the thumb.  */
3340      if (globals->use_rel)
3341	{
3342	  /* Need to refetch addend.  */
3343	  addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
3344	  /* ??? Need to determine shift amount from operand size.  */
3345	  addend >>= howto->rightshift;
3346	}
3347      value += addend;
3348
3349      /* ??? Isn't value unsigned?  */
3350      if ((long) value > 0x1f || (long) value < -0x10)
3351	return bfd_reloc_overflow;
3352
3353      /* ??? Value needs to be properly shifted into place first.  */
3354      value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
3355      bfd_put_16 (input_bfd, value, hit_data);
3356      return bfd_reloc_ok;
3357
3358    case R_ARM_THM_XPC22:
3359    case R_ARM_THM_CALL:
3360      /* Thumb BL (branch long instruction).  */
3361      {
3362	bfd_vma relocation;
3363	bfd_boolean overflow = FALSE;
3364	bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
3365	bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
3366	bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
3367	bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
3368	bfd_vma check;
3369	bfd_signed_vma signed_check;
3370
3371	/* Need to refetch the addend and squish the two 11 bit pieces
3372	   together.  */
3373	if (globals->use_rel)
3374	  {
3375	    bfd_vma upper = upper_insn & 0x7ff;
3376	    bfd_vma lower = lower_insn & 0x7ff;
3377	    upper = (upper ^ 0x400) - 0x400; /* Sign extend.  */
3378	    addend = (upper << 12) | (lower << 1);
3379	    signed_addend = addend;
3380	  }
3381
3382	if (r_type == R_ARM_THM_XPC22)
3383	  {
3384	    /* Check for Thumb to Thumb call.  */
3385	    /* FIXME: Should we translate the instruction into a BL
3386	       instruction instead ?  */
3387	    if (sym_flags == STT_ARM_TFUNC)
3388	      (*_bfd_error_handler)
3389		(_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
3390		 input_bfd,
3391		 h ? h->root.root.string : "(local)");
3392	  }
3393	else
3394	  {
3395	    /* If it is not a call to Thumb, assume call to Arm.
3396	       If it is a call relative to a section name, then it is not a
3397	       function call at all, but rather a long jump.  Calls through
3398	       the PLT do not require stubs.  */
3399	    if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
3400		&& (h == NULL || splt == NULL
3401		    || h->plt.offset == (bfd_vma) -1))
3402	      {
3403		if (globals->use_blx)
3404		  {
3405		    /* Convert BL to BLX.  */
3406		    lower_insn = (lower_insn & ~0x1000) | 0x0800;
3407		  }
3408		else if (elf32_thumb_to_arm_stub
3409		    (info, sym_name, input_bfd, output_bfd, input_section,
3410		     hit_data, sym_sec, rel->r_offset, signed_addend, value))
3411		  return bfd_reloc_ok;
3412		else
3413		  return bfd_reloc_dangerous;
3414	      }
3415	    else if (sym_flags == STT_ARM_TFUNC && globals->use_blx)
3416	      {
3417		/* Make sure this is a BL.  */
3418		lower_insn |= 0x1800;
3419	      }
3420	  }
3421
3422	/* Handle calls via the PLT.  */
3423	if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3424	  {
3425	    value = (splt->output_section->vma
3426		     + splt->output_offset
3427		     + h->plt.offset);
3428 	    if (globals->use_blx)
3429 	      {
3430 		/* If the Thumb BLX instruction is available, convert the
3431		   BL to a BLX instruction to call the ARM-mode PLT entry.  */
3432		lower_insn = (lower_insn & ~0x1000) | 0x0800;
3433 	      }
3434 	    else
3435 	      /* Target the Thumb stub before the ARM PLT entry.  */
3436 	      value -= PLT_THUMB_STUB_SIZE;
3437	    *unresolved_reloc_p = FALSE;
3438	  }
3439
3440	relocation = value + signed_addend;
3441
3442	relocation -= (input_section->output_section->vma
3443		       + input_section->output_offset
3444		       + rel->r_offset);
3445
3446	check = relocation >> howto->rightshift;
3447
3448	/* If this is a signed value, the rightshift just dropped
3449	   leading 1 bits (assuming twos complement).  */
3450	if ((bfd_signed_vma) relocation >= 0)
3451	  signed_check = check;
3452	else
3453	  signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
3454
3455	/* Assumes two's complement.  */
3456	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
3457	  overflow = TRUE;
3458
3459	if ((lower_insn & 0x1800) == 0x0800)
3460	  /* For a BLX instruction, make sure that the relocation is rounded up
3461	     to a word boundary.  This follows the semantics of the instruction
3462	     which specifies that bit 1 of the target address will come from bit
3463	     1 of the base address.  */
3464	  relocation = (relocation + 2) & ~ 3;
3465
3466	/* Put RELOCATION back into the insn.  */
3467	upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
3468	lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
3469
3470	/* Put the relocated value back in the object file:  */
3471	bfd_put_16 (input_bfd, upper_insn, hit_data);
3472	bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
3473
3474	return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
3475      }
3476      break;
3477
3478    case R_ARM_THM_JUMP24:
3479      /* Thumb32 unconditional branch instruction.  */
3480      {
3481	bfd_vma relocation;
3482	bfd_boolean overflow = FALSE;
3483	bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
3484	bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
3485	bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
3486	bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
3487	bfd_vma check;
3488	bfd_signed_vma signed_check;
3489
3490	/* Need to refetch the addend, reconstruct the top three bits, and glue the
3491	   two pieces together.  */
3492	if (globals->use_rel)
3493	  {
3494	    bfd_vma S  = (upper_insn & 0x0400) >> 10;
3495	    bfd_vma hi = (upper_insn & 0x03ff);
3496	    bfd_vma I1 = (lower_insn & 0x2000) >> 13;
3497	    bfd_vma I2 = (lower_insn & 0x0800) >> 11;
3498	    bfd_vma lo = (lower_insn & 0x07ff);
3499
3500	    I1 = !(I1 ^ S);
3501	    I2 = !(I2 ^ S);
3502	    S  = !S;
3503
3504	    signed_addend = (S << 24) | (I1 << 23) | (I2 << 22) | (hi << 12) | (lo << 1);
3505	    signed_addend -= (1 << 24); /* Sign extend.  */
3506	  }
3507
3508	/* ??? Should handle interworking?  GCC might someday try to
3509	   use this for tail calls.  */
3510
3511      	relocation = value + signed_addend;
3512	relocation -= (input_section->output_section->vma
3513		       + input_section->output_offset
3514		       + rel->r_offset);
3515
3516	check = relocation >> howto->rightshift;
3517
3518	/* If this is a signed value, the rightshift just dropped
3519	   leading 1 bits (assuming twos complement).  */
3520	if ((bfd_signed_vma) relocation >= 0)
3521	  signed_check = check;
3522	else
3523	  signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
3524
3525	/* Assumes two's complement.  */
3526	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
3527	  overflow = TRUE;
3528
3529	/* Put RELOCATION back into the insn.  */
3530	{
3531	  bfd_vma S  = (relocation & 0x01000000) >> 24;
3532	  bfd_vma I1 = (relocation & 0x00800000) >> 23;
3533	  bfd_vma I2 = (relocation & 0x00400000) >> 22;
3534	  bfd_vma hi = (relocation & 0x003ff000) >> 12;
3535	  bfd_vma lo = (relocation & 0x00000ffe) >>  1;
3536
3537	  I1 = !(I1 ^ S);
3538	  I2 = !(I2 ^ S);
3539
3540	  upper_insn = (upper_insn & (bfd_vma) 0xf800) | (S << 10) | hi;
3541	  lower_insn = (lower_insn & (bfd_vma) 0xd000) | (I1 << 13) | (I2 << 11) | lo;
3542	}
3543
3544	/* Put the relocated value back in the object file:  */
3545	bfd_put_16 (input_bfd, upper_insn, hit_data);
3546	bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
3547
3548	return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
3549      }
3550
3551    case R_ARM_THM_JUMP19:
3552      /* Thumb32 conditional branch instruction.  */
3553      {
3554	bfd_vma relocation;
3555	bfd_boolean overflow = FALSE;
3556	bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
3557	bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
3558	bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
3559	bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
3560	bfd_vma check;
3561	bfd_signed_vma signed_check;
3562
3563	/* Need to refetch the addend, reconstruct the top three bits,
3564	   and squish the two 11 bit pieces together.  */
3565	if (globals->use_rel)
3566	  {
3567	    bfd_vma S     = (upper_insn & 0x0400) >> 10;
3568	    bfd_vma upper = (upper_insn & 0x001f);
3569	    bfd_vma J1    = (lower_insn & 0x2000) >> 13;
3570	    bfd_vma J2    = (lower_insn & 0x0800) >> 11;
3571	    bfd_vma lower = (lower_insn & 0x07ff);
3572
3573	    upper |= J2 << 6;
3574	    upper |= J1 << 7;
3575	    upper |= ~S << 8;
3576	    upper -= 0x0100; /* Sign extend.  */
3577
3578	    addend = (upper << 12) | (lower << 1);
3579	    signed_addend = addend;
3580	  }
3581
3582	/* ??? Should handle interworking?  GCC might someday try to
3583	   use this for tail calls.  */
3584
3585      	relocation = value + signed_addend;
3586	relocation -= (input_section->output_section->vma
3587		       + input_section->output_offset
3588		       + rel->r_offset);
3589
3590	check = relocation >> howto->rightshift;
3591
3592	/* If this is a signed value, the rightshift just dropped
3593	   leading 1 bits (assuming twos complement).  */
3594	if ((bfd_signed_vma) relocation >= 0)
3595	  signed_check = check;
3596	else
3597	  signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
3598
3599	/* Assumes two's complement.  */
3600	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
3601	  overflow = TRUE;
3602
3603	/* Put RELOCATION back into the insn.  */
3604	{
3605	  bfd_vma S  = (relocation & 0x00100000) >> 20;
3606	  bfd_vma J2 = (relocation & 0x00080000) >> 19;
3607	  bfd_vma J1 = (relocation & 0x00040000) >> 18;
3608	  bfd_vma hi = (relocation & 0x0003f000) >> 12;
3609	  bfd_vma lo = (relocation & 0x00000ffe) >>  1;
3610
3611	  upper_insn = (upper_insn & 0xfb30) | (S << 10) | hi;
3612	  lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
3613	}
3614
3615	/* Put the relocated value back in the object file:  */
3616	bfd_put_16 (input_bfd, upper_insn, hit_data);
3617	bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
3618
3619	return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
3620      }
3621
3622    case R_ARM_THM_JUMP11:
3623    case R_ARM_THM_JUMP8:
3624    case R_ARM_THM_JUMP6:
3625      /* Thumb B (branch) instruction).  */
3626      {
3627	bfd_signed_vma relocation;
3628	bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
3629	bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
3630	bfd_signed_vma signed_check;
3631
3632	/* CZB cannot jump backward.  */
3633	if (r_type == R_ARM_THM_JUMP6)
3634	  reloc_signed_min = 0;
3635
3636	if (globals->use_rel)
3637	  {
3638	    /* Need to refetch addend.  */
3639	    addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
3640	    if (addend & ((howto->src_mask + 1) >> 1))
3641	      {
3642		signed_addend = -1;
3643		signed_addend &= ~ howto->src_mask;
3644		signed_addend |= addend;
3645	      }
3646	    else
3647	      signed_addend = addend;
3648	    /* The value in the insn has been right shifted.  We need to
3649	       undo this, so that we can perform the address calculation
3650	       in terms of bytes.  */
3651	    signed_addend <<= howto->rightshift;
3652	  }
3653	relocation = value + signed_addend;
3654
3655	relocation -= (input_section->output_section->vma
3656		       + input_section->output_offset
3657		       + rel->r_offset);
3658
3659	relocation >>= howto->rightshift;
3660	signed_check = relocation;
3661
3662	if (r_type == R_ARM_THM_JUMP6)
3663	  relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
3664	else
3665	  relocation &= howto->dst_mask;
3666	relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
3667
3668	bfd_put_16 (input_bfd, relocation, hit_data);
3669
3670	/* Assumes two's complement.  */
3671	if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
3672	  return bfd_reloc_overflow;
3673
3674	return bfd_reloc_ok;
3675      }
3676
3677    case R_ARM_ALU_PCREL7_0:
3678    case R_ARM_ALU_PCREL15_8:
3679    case R_ARM_ALU_PCREL23_15:
3680      {
3681	bfd_vma insn;
3682	bfd_vma relocation;
3683
3684	insn = bfd_get_32 (input_bfd, hit_data);
3685	if (globals->use_rel)
3686	  {
3687	    /* Extract the addend.  */
3688	    addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
3689	    signed_addend = addend;
3690	  }
3691	relocation = value + signed_addend;
3692
3693	relocation -= (input_section->output_section->vma
3694		       + input_section->output_offset
3695		       + rel->r_offset);
3696	insn = (insn & ~0xfff)
3697	       | ((howto->bitpos << 7) & 0xf00)
3698	       | ((relocation >> howto->bitpos) & 0xff);
3699	bfd_put_32 (input_bfd, value, hit_data);
3700      }
3701      return bfd_reloc_ok;
3702
3703    case R_ARM_GNU_VTINHERIT:
3704    case R_ARM_GNU_VTENTRY:
3705      return bfd_reloc_ok;
3706
3707    case R_ARM_GOTOFF32:
3708      /* Relocation is relative to the start of the
3709         global offset table.  */
3710
3711      BFD_ASSERT (sgot != NULL);
3712      if (sgot == NULL)
3713        return bfd_reloc_notsupported;
3714
3715      /* If we are addressing a Thumb function, we need to adjust the
3716	 address by one, so that attempts to call the function pointer will
3717	 correctly interpret it as Thumb code.  */
3718      if (sym_flags == STT_ARM_TFUNC)
3719	value += 1;
3720
3721      /* Note that sgot->output_offset is not involved in this
3722         calculation.  We always want the start of .got.  If we
3723         define _GLOBAL_OFFSET_TABLE in a different way, as is
3724         permitted by the ABI, we might have to change this
3725         calculation.  */
3726      value -= sgot->output_section->vma;
3727      return _bfd_final_link_relocate (howto, input_bfd, input_section,
3728				       contents, rel->r_offset, value,
3729				       rel->r_addend);
3730
3731    case R_ARM_GOTPC:
3732      /* Use global offset table as symbol value.  */
3733      BFD_ASSERT (sgot != NULL);
3734
3735      if (sgot == NULL)
3736        return bfd_reloc_notsupported;
3737
3738      *unresolved_reloc_p = FALSE;
3739      value = sgot->output_section->vma;
3740      return _bfd_final_link_relocate (howto, input_bfd, input_section,
3741				       contents, rel->r_offset, value,
3742				       rel->r_addend);
3743
3744    case R_ARM_GOT32:
3745    case R_ARM_GOT_PREL:
3746      /* Relocation is to the entry for this symbol in the
3747         global offset table.  */
3748      if (sgot == NULL)
3749	return bfd_reloc_notsupported;
3750
3751      if (h != NULL)
3752	{
3753	  bfd_vma off;
3754	  bfd_boolean dyn;
3755
3756	  off = h->got.offset;
3757	  BFD_ASSERT (off != (bfd_vma) -1);
3758	  dyn = globals->root.dynamic_sections_created;
3759
3760	  if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3761	      || (info->shared
3762		  && SYMBOL_REFERENCES_LOCAL (info, h))
3763	      || (ELF_ST_VISIBILITY (h->other)
3764		  && h->root.type == bfd_link_hash_undefweak))
3765	    {
3766	      /* This is actually a static link, or it is a -Bsymbolic link
3767		 and the symbol is defined locally.  We must initialize this
3768		 entry in the global offset table.  Since the offset must
3769		 always be a multiple of 4, we use the least significant bit
3770		 to record whether we have initialized it already.
3771
3772		 When doing a dynamic link, we create a .rel(a).got relocation
3773		 entry to initialize the value.  This is done in the
3774		 finish_dynamic_symbol routine.  */
3775	      if ((off & 1) != 0)
3776		off &= ~1;
3777	      else
3778		{
3779		  /* If we are addressing a Thumb function, we need to
3780		     adjust the address by one, so that attempts to
3781		     call the function pointer will correctly
3782		     interpret it as Thumb code.  */
3783		  if (sym_flags == STT_ARM_TFUNC)
3784		    value |= 1;
3785
3786		  bfd_put_32 (output_bfd, value, sgot->contents + off);
3787		  h->got.offset |= 1;
3788		}
3789	    }
3790	  else
3791	    *unresolved_reloc_p = FALSE;
3792
3793	  value = sgot->output_offset + off;
3794	}
3795      else
3796	{
3797	  bfd_vma off;
3798
3799	  BFD_ASSERT (local_got_offsets != NULL &&
3800		      local_got_offsets[r_symndx] != (bfd_vma) -1);
3801
3802	  off = local_got_offsets[r_symndx];
3803
3804	  /* The offset must always be a multiple of 4.  We use the
3805	     least significant bit to record whether we have already
3806	     generated the necessary reloc.  */
3807	  if ((off & 1) != 0)
3808	    off &= ~1;
3809	  else
3810	    {
3811	      /* If we are addressing a Thumb function, we need to
3812		 adjust the address by one, so that attempts to
3813		 call the function pointer will correctly
3814		 interpret it as Thumb code.  */
3815	      if (sym_flags == STT_ARM_TFUNC)
3816		value |= 1;
3817
3818	      if (globals->use_rel)
3819		bfd_put_32 (output_bfd, value, sgot->contents + off);
3820
3821	      if (info->shared)
3822		{
3823		  asection * srelgot;
3824		  Elf_Internal_Rela outrel;
3825		  bfd_byte *loc;
3826
3827		  srelgot = (bfd_get_section_by_name
3828			     (dynobj, RELOC_SECTION (globals, ".got")));
3829		  BFD_ASSERT (srelgot != NULL);
3830
3831		  outrel.r_addend = addend + value;
3832		  outrel.r_offset = (sgot->output_section->vma
3833				     + sgot->output_offset
3834				     + off);
3835		  outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
3836		  loc = srelgot->contents;
3837		  loc += srelgot->reloc_count++ * RELOC_SIZE (globals);
3838		  SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
3839		}
3840
3841	      local_got_offsets[r_symndx] |= 1;
3842	    }
3843
3844	  value = sgot->output_offset + off;
3845	}
3846      if (r_type != R_ARM_GOT32)
3847	value += sgot->output_section->vma;
3848
3849      return _bfd_final_link_relocate (howto, input_bfd, input_section,
3850				       contents, rel->r_offset, value,
3851				       rel->r_addend);
3852
3853    case R_ARM_TLS_LDO32:
3854      value = value - dtpoff_base (info);
3855
3856      return _bfd_final_link_relocate (howto, input_bfd, input_section,
3857				       contents, rel->r_offset, value,
3858				       rel->r_addend);
3859
3860    case R_ARM_TLS_LDM32:
3861      {
3862	bfd_vma off;
3863
3864	if (globals->sgot == NULL)
3865	  abort ();
3866
3867	off = globals->tls_ldm_got.offset;
3868
3869	if ((off & 1) != 0)
3870	  off &= ~1;
3871	else
3872	  {
3873	    /* If we don't know the module number, create a relocation
3874	       for it.  */
3875	    if (info->shared)
3876	      {
3877		Elf_Internal_Rela outrel;
3878		bfd_byte *loc;
3879
3880		if (globals->srelgot == NULL)
3881		  abort ();
3882
3883		outrel.r_addend = 0;
3884		outrel.r_offset = (globals->sgot->output_section->vma
3885				   + globals->sgot->output_offset + off);
3886		outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
3887
3888		if (globals->use_rel)
3889		  bfd_put_32 (output_bfd, outrel.r_addend,
3890			      globals->sgot->contents + off);
3891
3892		loc = globals->srelgot->contents;
3893		loc += globals->srelgot->reloc_count++ * RELOC_SIZE (globals);
3894		SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
3895	      }
3896	    else
3897	      bfd_put_32 (output_bfd, 1, globals->sgot->contents + off);
3898
3899	    globals->tls_ldm_got.offset |= 1;
3900	  }
3901
3902	value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
3903	  - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
3904
3905	return _bfd_final_link_relocate (howto, input_bfd, input_section,
3906					 contents, rel->r_offset, value,
3907					 rel->r_addend);
3908      }
3909
3910    case R_ARM_TLS_GD32:
3911    case R_ARM_TLS_IE32:
3912      {
3913	bfd_vma off;
3914	int indx;
3915	char tls_type;
3916
3917	if (globals->sgot == NULL)
3918	  abort ();
3919
3920	indx = 0;
3921	if (h != NULL)
3922	  {
3923	    bfd_boolean dyn;
3924	    dyn = globals->root.dynamic_sections_created;
3925	    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3926		&& (!info->shared
3927		    || !SYMBOL_REFERENCES_LOCAL (info, h)))
3928	      {
3929		*unresolved_reloc_p = FALSE;
3930		indx = h->dynindx;
3931	      }
3932	    off = h->got.offset;
3933	    tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
3934	  }
3935	else
3936	  {
3937	    if (local_got_offsets == NULL)
3938	      abort ();
3939	    off = local_got_offsets[r_symndx];
3940	    tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
3941	  }
3942
3943	if (tls_type == GOT_UNKNOWN)
3944	  abort ();
3945
3946	if ((off & 1) != 0)
3947	  off &= ~1;
3948	else
3949	  {
3950	    bfd_boolean need_relocs = FALSE;
3951	    Elf_Internal_Rela outrel;
3952	    bfd_byte *loc = NULL;
3953	    int cur_off = off;
3954
3955	    /* The GOT entries have not been initialized yet.  Do it
3956	       now, and emit any relocations.  If both an IE GOT and a
3957	       GD GOT are necessary, we emit the GD first.  */
3958
3959	    if ((info->shared || indx != 0)
3960		&& (h == NULL
3961		    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3962		    || h->root.type != bfd_link_hash_undefweak))
3963	      {
3964		need_relocs = TRUE;
3965		if (globals->srelgot == NULL)
3966		  abort ();
3967		loc = globals->srelgot->contents;
3968		loc += globals->srelgot->reloc_count * RELOC_SIZE (globals);
3969	      }
3970
3971	    if (tls_type & GOT_TLS_GD)
3972	      {
3973		if (need_relocs)
3974		  {
3975		    outrel.r_addend = 0;
3976		    outrel.r_offset = (globals->sgot->output_section->vma
3977				       + globals->sgot->output_offset
3978				       + cur_off);
3979		    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
3980
3981		    if (globals->use_rel)
3982		      bfd_put_32 (output_bfd, outrel.r_addend,
3983				  globals->sgot->contents + cur_off);
3984
3985		    SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
3986		    globals->srelgot->reloc_count++;
3987		    loc += RELOC_SIZE (globals);
3988
3989		    if (indx == 0)
3990		      bfd_put_32 (output_bfd, value - dtpoff_base (info),
3991				  globals->sgot->contents + cur_off + 4);
3992		    else
3993		      {
3994			outrel.r_addend = 0;
3995			outrel.r_info = ELF32_R_INFO (indx,
3996						      R_ARM_TLS_DTPOFF32);
3997			outrel.r_offset += 4;
3998
3999			if (globals->use_rel)
4000			  bfd_put_32 (output_bfd, outrel.r_addend,
4001				      globals->sgot->contents + cur_off + 4);
4002
4003
4004			SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4005			globals->srelgot->reloc_count++;
4006			loc += RELOC_SIZE (globals);
4007		      }
4008		  }
4009		else
4010		  {
4011		    /* If we are not emitting relocations for a
4012		       general dynamic reference, then we must be in a
4013		       static link or an executable link with the
4014		       symbol binding locally.  Mark it as belonging
4015		       to module 1, the executable.  */
4016		    bfd_put_32 (output_bfd, 1,
4017				globals->sgot->contents + cur_off);
4018		    bfd_put_32 (output_bfd, value - dtpoff_base (info),
4019				globals->sgot->contents + cur_off + 4);
4020		  }
4021
4022		cur_off += 8;
4023	      }
4024
4025	    if (tls_type & GOT_TLS_IE)
4026	      {
4027		if (need_relocs)
4028		  {
4029		    if (indx == 0)
4030		      outrel.r_addend = value - dtpoff_base (info);
4031		    else
4032		      outrel.r_addend = 0;
4033		    outrel.r_offset = (globals->sgot->output_section->vma
4034				       + globals->sgot->output_offset
4035				       + cur_off);
4036		    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
4037
4038		    if (globals->use_rel)
4039		      bfd_put_32 (output_bfd, outrel.r_addend,
4040				  globals->sgot->contents + cur_off);
4041
4042		    SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
4043		    globals->srelgot->reloc_count++;
4044		    loc += RELOC_SIZE (globals);
4045		  }
4046		else
4047		  bfd_put_32 (output_bfd, tpoff (info, value),
4048			      globals->sgot->contents + cur_off);
4049		cur_off += 4;
4050	      }
4051
4052	    if (h != NULL)
4053	      h->got.offset |= 1;
4054	    else
4055	      local_got_offsets[r_symndx] |= 1;
4056	  }
4057
4058	if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32)
4059	  off += 8;
4060	value = globals->sgot->output_section->vma + globals->sgot->output_offset + off
4061	  - (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
4062
4063	return _bfd_final_link_relocate (howto, input_bfd, input_section,
4064					 contents, rel->r_offset, value,
4065					 rel->r_addend);
4066      }
4067
4068    case R_ARM_TLS_LE32:
4069      if (info->shared)
4070	{
4071	  (*_bfd_error_handler)
4072	    (_("%B(%A+0x%lx): R_ARM_TLS_LE32 relocation not permitted in shared object"),
4073	     input_bfd, input_section,
4074	     (long) rel->r_offset, howto->name);
4075	  return FALSE;
4076	}
4077      else
4078	value = tpoff (info, value);
4079
4080      return _bfd_final_link_relocate (howto, input_bfd, input_section,
4081				       contents, rel->r_offset, value,
4082				       rel->r_addend);
4083
4084    case R_ARM_V4BX:
4085      if (globals->fix_v4bx)
4086        {
4087          bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
4088
4089          /* Ensure that we have a BX instruction.  */
4090          BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
4091
4092          /* Preserve Rm (lowest four bits) and the condition code
4093             (highest four bits). Other bits encode MOV PC,Rm.  */
4094          insn = (insn & 0xf000000f) | 0x01a0f000;
4095
4096          bfd_put_32 (input_bfd, insn, hit_data);
4097        }
4098      return bfd_reloc_ok;
4099
4100    default:
4101      return bfd_reloc_notsupported;
4102    }
4103}
4104
4105
4106static int
4107uleb128_size (unsigned int i)
4108{
4109  int size;
4110  size = 1;
4111  while (i >= 0x80)
4112    {
4113      i >>= 7;
4114      size++;
4115    }
4116  return size;
4117}
4118
4119/* Return TRUE if the attribute has the default value (0/"").  */
4120static bfd_boolean
4121is_default_attr (aeabi_attribute *attr)
4122{
4123  if ((attr->type & 1) && attr->i != 0)
4124    return FALSE;
4125  if ((attr->type & 2) && attr->s && *attr->s)
4126    return FALSE;
4127
4128  return TRUE;
4129}
4130
4131/* Return the size of a single attribute.  */
4132static bfd_vma
4133eabi_attr_size(int tag, aeabi_attribute *attr)
4134{
4135  bfd_vma size;
4136
4137  if (is_default_attr (attr))
4138    return 0;
4139
4140  size = uleb128_size (tag);
4141  if (attr->type & 1)
4142    size += uleb128_size (attr->i);
4143  if (attr->type & 2)
4144    size += strlen ((char *)attr->s) + 1;
4145  return size;
4146}
4147
4148/* Returns the size of the eabi object attributess section.  */
4149bfd_vma
4150elf32_arm_eabi_attr_size (bfd *abfd)
4151{
4152  bfd_vma size;
4153  aeabi_attribute *attr;
4154  aeabi_attribute_list *list;
4155  int i;
4156
4157  attr = elf32_arm_tdata (abfd)->known_eabi_attributes;
4158  size = 16; /* 'A' <size> "aeabi" 0x1 <size>.  */
4159  for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
4160    size += eabi_attr_size (i, &attr[i]);
4161
4162  for (list = elf32_arm_tdata (abfd)->other_eabi_attributes;
4163       list;
4164       list = list->next)
4165    size += eabi_attr_size (list->tag, &list->attr);
4166
4167  return size;
4168}
4169
4170static bfd_byte *
4171write_uleb128 (bfd_byte *p, unsigned int val)
4172{
4173  bfd_byte c;
4174  do
4175    {
4176      c = val & 0x7f;
4177      val >>= 7;
4178      if (val)
4179	c |= 0x80;
4180      *(p++) = c;
4181    }
4182  while (val);
4183  return p;
4184}
4185
4186/* Write attribute ATTR to butter P, and return a pointer to the following
4187   byte.  */
4188static bfd_byte *
4189write_eabi_attribute (bfd_byte *p, int tag, aeabi_attribute *attr)
4190{
4191  /* Suppress default entries.  */
4192  if (is_default_attr(attr))
4193    return p;
4194
4195  p = write_uleb128 (p, tag);
4196  if (attr->type & 1)
4197    p = write_uleb128 (p, attr->i);
4198  if (attr->type & 2)
4199    {
4200      int len;
4201
4202      len = strlen (attr->s) + 1;
4203      memcpy (p, attr->s, len);
4204      p += len;
4205    }
4206
4207  return p;
4208}
4209
4210/* Write the contents of the eabi attributes section to p.  */
4211void
4212elf32_arm_set_eabi_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
4213{
4214  bfd_byte *p;
4215  aeabi_attribute *attr;
4216  aeabi_attribute_list *list;
4217  int i;
4218
4219  p = contents;
4220  *(p++) = 'A';
4221  bfd_put_32 (abfd, size - 1, p);
4222  p += 4;
4223  memcpy (p, "aeabi", 6);
4224  p += 6;
4225  *(p++) = Tag_File;
4226  bfd_put_32 (abfd, size - 11, p);
4227  p += 4;
4228
4229  attr = elf32_arm_tdata (abfd)->known_eabi_attributes;
4230  for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
4231    p = write_eabi_attribute (p, i, &attr[i]);
4232
4233  for (list = elf32_arm_tdata (abfd)->other_eabi_attributes;
4234       list;
4235       list = list->next)
4236    p = write_eabi_attribute (p, list->tag, &list->attr);
4237}
4238
4239/* Override final_link to handle EABI object attribute sections.  */
4240
4241static bfd_boolean
4242elf32_arm_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4243{
4244  asection *o;
4245  struct bfd_link_order *p;
4246  asection *attr_section = NULL;
4247  bfd_byte *contents;
4248  bfd_vma size = 0;
4249
4250  /* elf32_arm_merge_private_bfd_data will already have merged the
4251     object attributes.  Remove the input sections from the link, and set
4252     the contents of the output secton.  */
4253  for (o = abfd->sections; o != NULL; o = o->next)
4254    {
4255      if (strcmp (o->name, ".ARM.attributes") == 0)
4256	{
4257	  for (p = o->map_head.link_order; p != NULL; p = p->next)
4258	    {
4259	      asection *input_section;
4260
4261	      if (p->type != bfd_indirect_link_order)
4262		continue;
4263	      input_section = p->u.indirect.section;
4264	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
4265		 elf_link_input_bfd ignores this section.  */
4266	      input_section->flags &= ~SEC_HAS_CONTENTS;
4267	    }
4268
4269	  size = elf32_arm_eabi_attr_size (abfd);
4270	  bfd_set_section_size (abfd, o, size);
4271	  attr_section = o;
4272	  /* Skip this section later on.  */
4273	  o->map_head.link_order = NULL;
4274	}
4275    }
4276  /* Invoke the ELF linker to do all the work.  */
4277  if (!bfd_elf_final_link (abfd, info))
4278    return FALSE;
4279
4280  if (attr_section)
4281    {
4282      contents = bfd_malloc(size);
4283      if (contents == NULL)
4284	return FALSE;
4285      elf32_arm_set_eabi_attr_contents (abfd, contents, size);
4286      bfd_set_section_contents (abfd, attr_section, contents, 0, size);
4287      free (contents);
4288    }
4289  return TRUE;
4290}
4291
4292
4293/* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
4294static void
4295arm_add_to_rel (bfd *              abfd,
4296		bfd_byte *         address,
4297		reloc_howto_type * howto,
4298		bfd_signed_vma     increment)
4299{
4300  bfd_signed_vma addend;
4301
4302  if (howto->type == R_ARM_THM_CALL)
4303    {
4304      int upper_insn, lower_insn;
4305      int upper, lower;
4306
4307      upper_insn = bfd_get_16 (abfd, address);
4308      lower_insn = bfd_get_16 (abfd, address + 2);
4309      upper = upper_insn & 0x7ff;
4310      lower = lower_insn & 0x7ff;
4311
4312      addend = (upper << 12) | (lower << 1);
4313      addend += increment;
4314      addend >>= 1;
4315
4316      upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
4317      lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
4318
4319      bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
4320      bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
4321    }
4322  else
4323    {
4324      bfd_vma        contents;
4325
4326      contents = bfd_get_32 (abfd, address);
4327
4328      /* Get the (signed) value from the instruction.  */
4329      addend = contents & howto->src_mask;
4330      if (addend & ((howto->src_mask + 1) >> 1))
4331	{
4332	  bfd_signed_vma mask;
4333
4334	  mask = -1;
4335	  mask &= ~ howto->src_mask;
4336	  addend |= mask;
4337	}
4338
4339      /* Add in the increment, (which is a byte value).  */
4340      switch (howto->type)
4341	{
4342	default:
4343	  addend += increment;
4344	  break;
4345
4346	case R_ARM_PC24:
4347	case R_ARM_PLT32:
4348	case R_ARM_CALL:
4349	case R_ARM_JUMP24:
4350	  addend <<= howto->size;
4351	  addend += increment;
4352
4353	  /* Should we check for overflow here ?  */
4354
4355	  /* Drop any undesired bits.  */
4356	  addend >>= howto->rightshift;
4357	  break;
4358	}
4359
4360      contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
4361
4362      bfd_put_32 (abfd, contents, address);
4363    }
4364}
4365
4366#define IS_ARM_TLS_RELOC(R_TYPE)	\
4367  ((R_TYPE) == R_ARM_TLS_GD32		\
4368   || (R_TYPE) == R_ARM_TLS_LDO32	\
4369   || (R_TYPE) == R_ARM_TLS_LDM32	\
4370   || (R_TYPE) == R_ARM_TLS_DTPOFF32	\
4371   || (R_TYPE) == R_ARM_TLS_DTPMOD32	\
4372   || (R_TYPE) == R_ARM_TLS_TPOFF32	\
4373   || (R_TYPE) == R_ARM_TLS_LE32	\
4374   || (R_TYPE) == R_ARM_TLS_IE32)
4375
4376/* Relocate an ARM ELF section.  */
4377static bfd_boolean
4378elf32_arm_relocate_section (bfd *                  output_bfd,
4379			    struct bfd_link_info * info,
4380			    bfd *                  input_bfd,
4381			    asection *             input_section,
4382			    bfd_byte *             contents,
4383			    Elf_Internal_Rela *    relocs,
4384			    Elf_Internal_Sym *     local_syms,
4385			    asection **            local_sections)
4386{
4387  Elf_Internal_Shdr *symtab_hdr;
4388  struct elf_link_hash_entry **sym_hashes;
4389  Elf_Internal_Rela *rel;
4390  Elf_Internal_Rela *relend;
4391  const char *name;
4392  struct elf32_arm_link_hash_table * globals;
4393
4394  globals = elf32_arm_hash_table (info);
4395  if (info->relocatable && !globals->use_rel)
4396    return TRUE;
4397
4398  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
4399  sym_hashes = elf_sym_hashes (input_bfd);
4400
4401  rel = relocs;
4402  relend = relocs + input_section->reloc_count;
4403  for (; rel < relend; rel++)
4404    {
4405      int                          r_type;
4406      reloc_howto_type *           howto;
4407      unsigned long                r_symndx;
4408      Elf_Internal_Sym *           sym;
4409      asection *                   sec;
4410      struct elf_link_hash_entry * h;
4411      bfd_vma                      relocation;
4412      bfd_reloc_status_type        r;
4413      arelent                      bfd_reloc;
4414      char                         sym_type;
4415      bfd_boolean                  unresolved_reloc = FALSE;
4416
4417      r_symndx = ELF32_R_SYM (rel->r_info);
4418      r_type   = ELF32_R_TYPE (rel->r_info);
4419      r_type   = arm_real_reloc_type (globals, r_type);
4420
4421      if (   r_type == R_ARM_GNU_VTENTRY
4422          || r_type == R_ARM_GNU_VTINHERIT)
4423        continue;
4424
4425      bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
4426      howto = bfd_reloc.howto;
4427
4428      if (info->relocatable && globals->use_rel)
4429	{
4430	  /* This is a relocatable link.  We don't have to change
4431	     anything, unless the reloc is against a section symbol,
4432	     in which case we have to adjust according to where the
4433	     section symbol winds up in the output section.  */
4434	  if (r_symndx < symtab_hdr->sh_info)
4435	    {
4436	      sym = local_syms + r_symndx;
4437	      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4438		{
4439		  sec = local_sections[r_symndx];
4440		  arm_add_to_rel (input_bfd, contents + rel->r_offset,
4441				  howto,
4442				  (bfd_signed_vma) (sec->output_offset
4443						    + sym->st_value));
4444		}
4445	    }
4446
4447	  continue;
4448	}
4449
4450      /* This is a final link.  */
4451      h = NULL;
4452      sym = NULL;
4453      sec = NULL;
4454
4455      if (r_symndx < symtab_hdr->sh_info)
4456	{
4457	  sym = local_syms + r_symndx;
4458	  sym_type = ELF32_ST_TYPE (sym->st_info);
4459	  sec = local_sections[r_symndx];
4460	  if (globals->use_rel)
4461	    {
4462	      relocation = (sec->output_section->vma
4463			    + sec->output_offset
4464			    + sym->st_value);
4465	      if ((sec->flags & SEC_MERGE)
4466		       && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4467		{
4468		  asection *msec;
4469		  bfd_vma addend, value;
4470
4471		  if (howto->rightshift)
4472		    {
4473		      (*_bfd_error_handler)
4474			(_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4475			 input_bfd, input_section,
4476			 (long) rel->r_offset, howto->name);
4477		      return FALSE;
4478		    }
4479
4480		  value = bfd_get_32 (input_bfd, contents + rel->r_offset);
4481
4482		  /* Get the (signed) value from the instruction.  */
4483		  addend = value & howto->src_mask;
4484		  if (addend & ((howto->src_mask + 1) >> 1))
4485		    {
4486		      bfd_signed_vma mask;
4487
4488		      mask = -1;
4489		      mask &= ~ howto->src_mask;
4490		      addend |= mask;
4491		    }
4492		  msec = sec;
4493		  addend =
4494		    _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4495		    - relocation;
4496		  addend += msec->output_section->vma + msec->output_offset;
4497		  value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
4498		  bfd_put_32 (input_bfd, value, contents + rel->r_offset);
4499		}
4500	    }
4501	  else
4502	    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4503	}
4504      else
4505	{
4506	  bfd_boolean warned;
4507
4508	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4509				   r_symndx, symtab_hdr, sym_hashes,
4510				   h, sec, relocation,
4511				   unresolved_reloc, warned);
4512
4513	  sym_type = h->type;
4514	}
4515
4516      if (h != NULL)
4517	name = h->root.root.string;
4518      else
4519	{
4520	  name = (bfd_elf_string_from_elf_section
4521		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
4522	  if (name == NULL || *name == '\0')
4523	    name = bfd_section_name (input_bfd, sec);
4524	}
4525
4526      if (r_symndx != 0
4527	  && r_type != R_ARM_NONE
4528	  && (h == NULL
4529	      || h->root.type == bfd_link_hash_defined
4530	      || h->root.type == bfd_link_hash_defweak)
4531	  && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
4532	{
4533	  (*_bfd_error_handler)
4534	    ((sym_type == STT_TLS
4535	      ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
4536	      : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
4537	     input_bfd,
4538	     input_section,
4539	     (long) rel->r_offset,
4540	     howto->name,
4541	     name);
4542	}
4543
4544      r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
4545					 input_section, contents, rel,
4546					 relocation, info, sec, name,
4547					 (h ? ELF_ST_TYPE (h->type) :
4548					  ELF_ST_TYPE (sym->st_info)), h,
4549					 &unresolved_reloc);
4550
4551      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4552	 because such sections are not SEC_ALLOC and thus ld.so will
4553	 not process them.  */
4554      if (unresolved_reloc
4555          && !((input_section->flags & SEC_DEBUGGING) != 0
4556               && h->def_dynamic))
4557	{
4558	  (*_bfd_error_handler)
4559	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4560	     input_bfd,
4561	     input_section,
4562	     (long) rel->r_offset,
4563	     howto->name,
4564	     h->root.root.string);
4565	  return FALSE;
4566	}
4567
4568      if (r != bfd_reloc_ok)
4569	{
4570	  const char * msg = (const char *) 0;
4571
4572	  switch (r)
4573	    {
4574	    case bfd_reloc_overflow:
4575	      /* If the overflowing reloc was to an undefined symbol,
4576		 we have already printed one error message and there
4577		 is no point complaining again.  */
4578	      if ((! h ||
4579		   h->root.type != bfd_link_hash_undefined)
4580		  && (!((*info->callbacks->reloc_overflow)
4581			(info, (h ? &h->root : NULL), name, howto->name,
4582			 (bfd_vma) 0, input_bfd, input_section,
4583			 rel->r_offset))))
4584		  return FALSE;
4585	      break;
4586
4587	    case bfd_reloc_undefined:
4588	      if (!((*info->callbacks->undefined_symbol)
4589		    (info, name, input_bfd, input_section,
4590		     rel->r_offset, TRUE)))
4591		return FALSE;
4592	      break;
4593
4594	    case bfd_reloc_outofrange:
4595	      msg = _("internal error: out of range error");
4596	      goto common_error;
4597
4598	    case bfd_reloc_notsupported:
4599	      msg = _("internal error: unsupported relocation error");
4600	      goto common_error;
4601
4602	    case bfd_reloc_dangerous:
4603	      msg = _("internal error: dangerous error");
4604	      goto common_error;
4605
4606	    default:
4607	      msg = _("internal error: unknown error");
4608	      /* fall through */
4609
4610	    common_error:
4611	      if (!((*info->callbacks->warning)
4612		    (info, msg, name, input_bfd, input_section,
4613		     rel->r_offset)))
4614		return FALSE;
4615	      break;
4616	    }
4617	}
4618    }
4619
4620  return TRUE;
4621}
4622
4623/* Allocate/find an object attribute.  */
4624static aeabi_attribute *
4625elf32_arm_new_eabi_attr (bfd *abfd, int tag)
4626{
4627  aeabi_attribute *attr;
4628  aeabi_attribute_list *list;
4629  aeabi_attribute_list *p;
4630  aeabi_attribute_list **lastp;
4631
4632
4633  if (tag < NUM_KNOWN_ATTRIBUTES)
4634    {
4635      /* Knwon tags are preallocated.  */
4636      attr = &elf32_arm_tdata (abfd)->known_eabi_attributes[tag];
4637    }
4638  else
4639    {
4640      /* Create a new tag.  */
4641      list = (aeabi_attribute_list *)
4642	bfd_alloc (abfd, sizeof (aeabi_attribute_list));
4643      memset (list, 0, sizeof (aeabi_attribute_list));
4644      list->tag = tag;
4645      /* Keep the tag list in order.  */
4646      lastp = &elf32_arm_tdata (abfd)->other_eabi_attributes;
4647      for (p = *lastp; p; p = p->next)
4648	{
4649	  if (tag < p->tag)
4650	    break;
4651	  lastp = &p->next;
4652	}
4653      list->next = *lastp;
4654      *lastp = list;
4655      attr = &list->attr;
4656    }
4657
4658  return attr;
4659}
4660
4661int
4662elf32_arm_get_eabi_attr_int (bfd *abfd, int tag)
4663{
4664  aeabi_attribute_list *p;
4665
4666  if (tag < NUM_KNOWN_ATTRIBUTES)
4667    {
4668      /* Knwon tags are preallocated.  */
4669      return elf32_arm_tdata (abfd)->known_eabi_attributes[tag].i;
4670    }
4671  else
4672    {
4673      for (p = elf32_arm_tdata (abfd)->other_eabi_attributes;
4674	   p;
4675	   p = p->next)
4676	{
4677	  if (tag == p->tag)
4678	    return p->attr.i;
4679	  if (tag < p->tag)
4680	    break;
4681	}
4682      return 0;
4683    }
4684}
4685
4686void
4687elf32_arm_add_eabi_attr_int (bfd *abfd, int tag, unsigned int i)
4688{
4689  aeabi_attribute *attr;
4690
4691  attr = elf32_arm_new_eabi_attr (abfd, tag);
4692  attr->type = 1;
4693  attr->i = i;
4694}
4695
4696static char *
4697attr_strdup (bfd *abfd, const char * s)
4698{
4699  char * p;
4700  int len;
4701
4702  len = strlen (s) + 1;
4703  p = (char *)bfd_alloc(abfd, len);
4704  return memcpy (p, s, len);
4705}
4706
4707void
4708elf32_arm_add_eabi_attr_string (bfd *abfd, int tag, const char *s)
4709{
4710  aeabi_attribute *attr;
4711
4712  attr = elf32_arm_new_eabi_attr (abfd, tag);
4713  attr->type = 2;
4714  attr->s = attr_strdup (abfd, s);
4715}
4716
4717void
4718elf32_arm_add_eabi_attr_compat (bfd *abfd, unsigned int i, const char *s)
4719{
4720  aeabi_attribute_list *list;
4721  aeabi_attribute_list *p;
4722  aeabi_attribute_list **lastp;
4723
4724  list = (aeabi_attribute_list *)
4725    bfd_alloc (abfd, sizeof (aeabi_attribute_list));
4726  memset (list, 0, sizeof (aeabi_attribute_list));
4727  list->tag = Tag_compatibility;
4728  list->attr.type = 3;
4729  list->attr.i = i;
4730  list->attr.s = attr_strdup (abfd, s);
4731
4732  lastp = &elf32_arm_tdata (abfd)->other_eabi_attributes;
4733  for (p = *lastp; p; p = p->next)
4734    {
4735      int cmp;
4736      if (p->tag != Tag_compatibility)
4737	break;
4738      cmp = strcmp(s, p->attr.s);
4739      if (cmp < 0 || (cmp == 0 && i < p->attr.i))
4740	break;
4741      lastp = &p->next;
4742    }
4743  list->next = *lastp;
4744  *lastp = list;
4745}
4746
4747/* Set the right machine number.  */
4748
4749static bfd_boolean
4750elf32_arm_object_p (bfd *abfd)
4751{
4752  unsigned int mach;
4753
4754  mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
4755
4756  if (mach != bfd_mach_arm_unknown)
4757    bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
4758
4759  else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
4760    bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
4761
4762  else
4763    bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
4764
4765  return TRUE;
4766}
4767
4768/* Function to keep ARM specific flags in the ELF header.  */
4769
4770static bfd_boolean
4771elf32_arm_set_private_flags (bfd *abfd, flagword flags)
4772{
4773  if (elf_flags_init (abfd)
4774      && elf_elfheader (abfd)->e_flags != flags)
4775    {
4776      if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
4777	{
4778	  if (flags & EF_ARM_INTERWORK)
4779	    (*_bfd_error_handler)
4780	      (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
4781	       abfd);
4782	  else
4783	    _bfd_error_handler
4784	      (_("Warning: Clearing the interworking flag of %B due to outside request"),
4785	       abfd);
4786	}
4787    }
4788  else
4789    {
4790      elf_elfheader (abfd)->e_flags = flags;
4791      elf_flags_init (abfd) = TRUE;
4792    }
4793
4794  return TRUE;
4795}
4796
4797/* Copy the eabi object attribute from IBFD to OBFD.  */
4798static void
4799copy_eabi_attributes (bfd *ibfd, bfd *obfd)
4800{
4801  aeabi_attribute *in_attr;
4802  aeabi_attribute *out_attr;
4803  aeabi_attribute_list *list;
4804  int i;
4805
4806  in_attr = elf32_arm_tdata (ibfd)->known_eabi_attributes;
4807  out_attr = elf32_arm_tdata (obfd)->known_eabi_attributes;
4808  for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
4809    {
4810      out_attr->i = in_attr->i;
4811      if (in_attr->s && *in_attr->s)
4812	out_attr->s = attr_strdup (obfd, in_attr->s);
4813      in_attr++;
4814      out_attr++;
4815    }
4816
4817  for (list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
4818       list;
4819       list = list->next)
4820    {
4821      in_attr = &list->attr;
4822      switch (in_attr->type)
4823	{
4824	case 1:
4825	  elf32_arm_add_eabi_attr_int (obfd, list->tag, in_attr->i);
4826	  break;
4827	case 2:
4828	  elf32_arm_add_eabi_attr_string (obfd, list->tag, in_attr->s);
4829	  break;
4830	case 3:
4831	  elf32_arm_add_eabi_attr_compat (obfd, in_attr->i, in_attr->s);
4832	  break;
4833	default:
4834	  abort();
4835	}
4836    }
4837}
4838
4839
4840/* Copy backend specific data from one object module to another.  */
4841
4842static bfd_boolean
4843elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4844{
4845  flagword in_flags;
4846  flagword out_flags;
4847
4848  if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4849      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4850    return TRUE;
4851
4852  in_flags  = elf_elfheader (ibfd)->e_flags;
4853  out_flags = elf_elfheader (obfd)->e_flags;
4854
4855  if (elf_flags_init (obfd)
4856      && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
4857      && in_flags != out_flags)
4858    {
4859      /* Cannot mix APCS26 and APCS32 code.  */
4860      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
4861	return FALSE;
4862
4863      /* Cannot mix float APCS and non-float APCS code.  */
4864      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
4865	return FALSE;
4866
4867      /* If the src and dest have different interworking flags
4868         then turn off the interworking bit.  */
4869      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
4870	{
4871	  if (out_flags & EF_ARM_INTERWORK)
4872	    _bfd_error_handler
4873	      (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
4874	       obfd, ibfd);
4875
4876	  in_flags &= ~EF_ARM_INTERWORK;
4877	}
4878
4879      /* Likewise for PIC, though don't warn for this case.  */
4880      if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
4881	in_flags &= ~EF_ARM_PIC;
4882    }
4883
4884  elf_elfheader (obfd)->e_flags = in_flags;
4885  elf_flags_init (obfd) = TRUE;
4886
4887  /* Also copy the EI_OSABI field.  */
4888  elf_elfheader (obfd)->e_ident[EI_OSABI] =
4889    elf_elfheader (ibfd)->e_ident[EI_OSABI];
4890
4891  /* Copy EABI object attributes.  */
4892  copy_eabi_attributes (ibfd, obfd);
4893
4894  return TRUE;
4895}
4896
4897/* Values for Tag_ABI_PCS_R9_use.  */
4898enum
4899{
4900  AEABI_R9_V6,
4901  AEABI_R9_SB,
4902  AEABI_R9_TLS,
4903  AEABI_R9_unused
4904};
4905
4906/* Values for Tag_ABI_PCS_RW_data.  */
4907enum
4908{
4909  AEABI_PCS_RW_data_absolute,
4910  AEABI_PCS_RW_data_PCrel,
4911  AEABI_PCS_RW_data_SBrel,
4912  AEABI_PCS_RW_data_unused
4913};
4914
4915/* Values for Tag_ABI_enum_size.  */
4916enum
4917{
4918  AEABI_enum_unused,
4919  AEABI_enum_short,
4920  AEABI_enum_wide,
4921  AEABI_enum_forced_wide
4922};
4923
4924/* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
4925   are conflicting attributes.  */
4926static bfd_boolean
4927elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
4928{
4929  aeabi_attribute *in_attr;
4930  aeabi_attribute *out_attr;
4931  aeabi_attribute_list *in_list;
4932  aeabi_attribute_list *out_list;
4933  /* Some tags have 0 = don't care, 1 = strong requirement,
4934     2 = weak requirement.  */
4935  static const int order_312[3] = {3, 1, 2};
4936  int i;
4937
4938  if (!elf32_arm_tdata (ibfd)->known_eabi_attributes[0].i)
4939    {
4940      /* This is the first object.  Copy the attributes.  */
4941      copy_eabi_attributes (ibfd, obfd);
4942      return TRUE;
4943    }
4944
4945  /* Use the Tag_null value to indicate the attributes have been
4946     initialized.  */
4947  elf32_arm_tdata (ibfd)->known_eabi_attributes[0].i = 1;
4948
4949  in_attr = elf32_arm_tdata (ibfd)->known_eabi_attributes;
4950  out_attr = elf32_arm_tdata (obfd)->known_eabi_attributes;
4951  /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
4952  if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
4953    {
4954      /* Ignore mismatches if teh object doesn't use floating point.  */
4955      if (out_attr[Tag_ABI_FP_number_model].i == 0)
4956	out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
4957      else if (in_attr[Tag_ABI_FP_number_model].i != 0)
4958	{
4959	  _bfd_error_handler
4960	    (_("ERROR: %B uses VFP register arguments, %B does not"),
4961	     ibfd, obfd);
4962	  return FALSE;
4963	}
4964    }
4965
4966  for (i = 4; i < NUM_KNOWN_ATTRIBUTES; i++)
4967    {
4968      /* Merge this attribute with existing attributes.  */
4969      switch (i)
4970	{
4971	case Tag_CPU_raw_name:
4972	case Tag_CPU_name:
4973	  /* Use whichever has the greatest architecture requirements.  */
4974	  if (in_attr[Tag_CPU_arch].i > out_attr[Tag_CPU_arch].i)
4975	    out_attr[i].s = attr_strdup(obfd, in_attr[i].s);
4976	  break;
4977
4978	case Tag_ABI_optimization_goals:
4979	case Tag_ABI_FP_optimization_goals:
4980	  /* Use the first value seen.  */
4981	  break;
4982
4983	case Tag_CPU_arch:
4984	case Tag_ARM_ISA_use:
4985	case Tag_THUMB_ISA_use:
4986	case Tag_VFP_arch:
4987	case Tag_WMMX_arch:
4988	case Tag_NEON_arch:
4989	  /* ??? Do NEON and WMMX conflict?  */
4990	case Tag_ABI_FP_rounding:
4991	case Tag_ABI_FP_denormal:
4992	case Tag_ABI_FP_exceptions:
4993	case Tag_ABI_FP_user_exceptions:
4994	case Tag_ABI_FP_number_model:
4995	case Tag_ABI_align8_preserved:
4996	case Tag_ABI_HardFP_use:
4997	  /* Use the largest value specified.  */
4998	  if (in_attr[i].i > out_attr[i].i)
4999	    out_attr[i].i = in_attr[i].i;
5000	  break;
5001
5002	case Tag_CPU_arch_profile:
5003	  /* Warn if conflicting architecture profiles used.  */
5004	  if (out_attr[i].i && in_attr[i].i && in_attr[i].i != out_attr[i].i)
5005	    {
5006	      _bfd_error_handler
5007		(_("ERROR: %B: Conflicting architecture profiles %c/%c"),
5008		 ibfd, in_attr[i].i, out_attr[i].i);
5009	      return FALSE;
5010	    }
5011	  if (in_attr[i].i)
5012	    out_attr[i].i = in_attr[i].i;
5013	  break;
5014	case Tag_PCS_config:
5015	  if (out_attr[i].i == 0)
5016	    out_attr[i].i = in_attr[i].i;
5017	  else if (in_attr[i].i != 0 && out_attr[i].i != 0)
5018	    {
5019	      /* It's sometimes ok to mix different configs, so this is only
5020	         a warning.  */
5021	      _bfd_error_handler
5022		(_("Warning: %B: Conflicting platform configuration"), ibfd);
5023	    }
5024	  break;
5025	case Tag_ABI_PCS_R9_use:
5026	  if (out_attr[i].i != AEABI_R9_unused
5027	      && in_attr[i].i != AEABI_R9_unused)
5028	    {
5029	      _bfd_error_handler
5030		(_("ERROR: %B: Conflicting use of R9"), ibfd);
5031	      return FALSE;
5032	    }
5033	  if (out_attr[i].i == AEABI_R9_unused)
5034	    out_attr[i].i = in_attr[i].i;
5035	  break;
5036	case Tag_ABI_PCS_RW_data:
5037	  if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
5038	      && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
5039	      && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
5040	    {
5041	      _bfd_error_handler
5042		(_("ERROR: %B: SB relative addressing conflicts with use of R9"),
5043		 ibfd);
5044	      return FALSE;
5045	    }
5046	  /* Use the smallest value specified.  */
5047	  if (in_attr[i].i < out_attr[i].i)
5048	    out_attr[i].i = in_attr[i].i;
5049	  break;
5050	case Tag_ABI_PCS_RO_data:
5051	  /* Use the smallest value specified.  */
5052	  if (in_attr[i].i < out_attr[i].i)
5053	    out_attr[i].i = in_attr[i].i;
5054	  break;
5055	case Tag_ABI_PCS_GOT_use:
5056	  if (in_attr[i].i > 2 || out_attr[i].i > 2
5057	      || order_312[in_attr[i].i] < order_312[out_attr[i].i])
5058	    out_attr[i].i = in_attr[i].i;
5059	  break;
5060	case Tag_ABI_PCS_wchar_t:
5061	  if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i)
5062	    {
5063	      _bfd_error_handler
5064		(_("ERROR: %B: Conflicting definitions of wchar_t"), ibfd);
5065	      return FALSE;
5066	    }
5067	  if (in_attr[i].i)
5068	    out_attr[i].i = in_attr[i].i;
5069	  break;
5070	case Tag_ABI_align8_needed:
5071	  /* ??? Check against Tag_ABI_align8_preserved.  */
5072	  if (in_attr[i].i > 2 || out_attr[i].i > 2
5073	      || order_312[in_attr[i].i] < order_312[out_attr[i].i])
5074	    out_attr[i].i = in_attr[i].i;
5075	  break;
5076	case Tag_ABI_enum_size:
5077	  if (in_attr[i].i != AEABI_enum_unused)
5078	    {
5079	      if (out_attr[i].i == AEABI_enum_unused
5080		  || out_attr[i].i == AEABI_enum_forced_wide)
5081		{
5082		  /* The existing object is compatible with anything.
5083		     Use whatever requirements the new object has.  */
5084		  out_attr[i].i = in_attr[i].i;
5085		}
5086	      else if (in_attr[i].i != AEABI_enum_forced_wide
5087		       && out_attr[i].i != in_attr[i].i)
5088		{
5089		  _bfd_error_handler
5090		    (_("ERROR: %B: Conflicting enum sizes"), ibfd);
5091		}
5092	    }
5093	  break;
5094	case Tag_ABI_VFP_args:
5095	  /* Aready done.  */
5096	  break;
5097	case Tag_ABI_WMMX_args:
5098	  if (in_attr[i].i != out_attr[i].i)
5099	    {
5100	      _bfd_error_handler
5101		(_("ERROR: %B uses iWMMXt register arguments, %B does not"),
5102		 ibfd, obfd);
5103	      return FALSE;
5104	    }
5105	  break;
5106	default: /* All known attributes should be explicitly covered.   */
5107	  abort ();
5108	}
5109    }
5110
5111  in_list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
5112  out_list = elf32_arm_tdata (ibfd)->other_eabi_attributes;
5113  while (in_list && in_list->tag == Tag_compatibility)
5114    {
5115      in_attr = &in_list->attr;
5116      if (in_attr->i == 0)
5117	continue;
5118      if (in_attr->i == 1)
5119	{
5120	  _bfd_error_handler
5121	    (_("ERROR: %B: Must be processed by '%s' toolchain"),
5122	     ibfd, in_attr->s);
5123	  return FALSE;
5124	}
5125      if (!out_list || out_list->tag != Tag_compatibility
5126	  || strcmp (in_attr->s, out_list->attr.s) != 0)
5127	{
5128	  /* Add this compatibility tag to the output.  */
5129	  elf32_arm_add_eabi_attr_compat (obfd, in_attr->i, in_attr->s);
5130	  continue;
5131	}
5132      out_attr = &out_list->attr;
5133      /* Check all the input tags with the same identifier.  */
5134      for (;;)
5135	{
5136	  if (out_list->tag != Tag_compatibility
5137	      || in_attr->i != out_attr->i
5138	      || strcmp (in_attr->s, out_attr->s) != 0)
5139	    {
5140	      _bfd_error_handler
5141		(_("ERROR: %B: Incompatible object tag '%s':%d"),
5142		 ibfd, in_attr->s, in_attr->i);
5143	      return FALSE;
5144	    }
5145	  in_list = in_list->next;
5146	  if (in_list->tag != Tag_compatibility
5147	      || strcmp (in_attr->s, in_list->attr.s) != 0)
5148	    break;
5149	  in_attr = &in_list->attr;
5150	  out_list = out_list->next;
5151	  if (out_list)
5152	    out_attr = &out_list->attr;
5153	}
5154
5155      /* Check the output doesn't have extra tags with this identifier.  */
5156      if (out_list && out_list->tag == Tag_compatibility
5157	  && strcmp (in_attr->s, out_list->attr.s) == 0)
5158	{
5159	  _bfd_error_handler
5160	    (_("ERROR: %B: Incompatible object tag '%s':%d"),
5161	     ibfd, in_attr->s, out_list->attr.i);
5162	  return FALSE;
5163	}
5164    }
5165
5166  for (; in_list; in_list = in_list->next)
5167    {
5168      if ((in_list->tag & 128) < 64)
5169	{
5170	  _bfd_error_handler
5171	    (_("Warning: %B: Unknown EABI object attribute %d"),
5172	     ibfd, in_list->tag);
5173	  break;
5174	}
5175    }
5176  return TRUE;
5177}
5178
5179
5180/* Return TRUE if the two EABI versions are incompatible.  */
5181
5182static bfd_boolean
5183elf32_arm_versions_compatible (unsigned iver, unsigned over)
5184{
5185  /* v4 and v5 are the same spec before and after it was released,
5186     so allow mixing them.  */
5187  if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
5188      || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
5189    return TRUE;
5190
5191  return (iver == over);
5192}
5193
5194/* Merge backend specific data from an object file to the output
5195   object file when linking.  */
5196
5197static bfd_boolean
5198elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
5199{
5200  flagword out_flags;
5201  flagword in_flags;
5202  bfd_boolean flags_compatible = TRUE;
5203  asection *sec;
5204
5205  /* Check if we have the same endianess.  */
5206  if (! _bfd_generic_verify_endian_match (ibfd, obfd))
5207    return FALSE;
5208
5209  if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5210      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5211    return TRUE;
5212
5213  if (!elf32_arm_merge_eabi_attributes (ibfd, obfd))
5214    return FALSE;
5215
5216  /* The input BFD must have had its flags initialised.  */
5217  /* The following seems bogus to me -- The flags are initialized in
5218     the assembler but I don't think an elf_flags_init field is
5219     written into the object.  */
5220  /* BFD_ASSERT (elf_flags_init (ibfd)); */
5221
5222  in_flags  = elf_elfheader (ibfd)->e_flags;
5223  out_flags = elf_elfheader (obfd)->e_flags;
5224
5225  if (!elf_flags_init (obfd))
5226    {
5227      /* If the input is the default architecture and had the default
5228	 flags then do not bother setting the flags for the output
5229	 architecture, instead allow future merges to do this.  If no
5230	 future merges ever set these flags then they will retain their
5231         uninitialised values, which surprise surprise, correspond
5232         to the default values.  */
5233      if (bfd_get_arch_info (ibfd)->the_default
5234	  && elf_elfheader (ibfd)->e_flags == 0)
5235	return TRUE;
5236
5237      elf_flags_init (obfd) = TRUE;
5238      elf_elfheader (obfd)->e_flags = in_flags;
5239
5240      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5241	  && bfd_get_arch_info (obfd)->the_default)
5242	return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
5243
5244      return TRUE;
5245    }
5246
5247  /* Determine what should happen if the input ARM architecture
5248     does not match the output ARM architecture.  */
5249  if (! bfd_arm_merge_machines (ibfd, obfd))
5250    return FALSE;
5251
5252  /* Identical flags must be compatible.  */
5253  if (in_flags == out_flags)
5254    return TRUE;
5255
5256  /* Check to see if the input BFD actually contains any sections.  If
5257     not, its flags may not have been initialised either, but it
5258     cannot actually cause any incompatiblity.  Do not short-circuit
5259     dynamic objects; their section list may be emptied by
5260    elf_link_add_object_symbols.
5261
5262    Also check to see if there are no code sections in the input.
5263    In this case there is no need to check for code specific flags.
5264    XXX - do we need to worry about floating-point format compatability
5265    in data sections ?  */
5266  if (!(ibfd->flags & DYNAMIC))
5267    {
5268      bfd_boolean null_input_bfd = TRUE;
5269      bfd_boolean only_data_sections = TRUE;
5270
5271      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5272	{
5273	  /* Ignore synthetic glue sections.  */
5274	  if (strcmp (sec->name, ".glue_7")
5275	      && strcmp (sec->name, ".glue_7t"))
5276	    {
5277	      if ((bfd_get_section_flags (ibfd, sec)
5278		   & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
5279		  == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
5280	    	only_data_sections = FALSE;
5281
5282	      null_input_bfd = FALSE;
5283	      break;
5284	    }
5285	}
5286
5287      if (null_input_bfd || only_data_sections)
5288	return TRUE;
5289    }
5290
5291  /* Complain about various flag mismatches.  */
5292  if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
5293				      EF_ARM_EABI_VERSION (out_flags)))
5294    {
5295      _bfd_error_handler
5296	(_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
5297	 ibfd, obfd,
5298	 (in_flags & EF_ARM_EABIMASK) >> 24,
5299	 (out_flags & EF_ARM_EABIMASK) >> 24);
5300      return FALSE;
5301    }
5302
5303  /* Not sure what needs to be checked for EABI versions >= 1.  */
5304  /* VxWorks libraries do not use these flags.  */
5305  if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
5306      && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
5307      && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
5308    {
5309      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
5310	{
5311	  _bfd_error_handler
5312	    (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
5313	     ibfd, obfd,
5314	     in_flags & EF_ARM_APCS_26 ? 26 : 32,
5315	     out_flags & EF_ARM_APCS_26 ? 26 : 32);
5316	  flags_compatible = FALSE;
5317	}
5318
5319      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
5320	{
5321	  if (in_flags & EF_ARM_APCS_FLOAT)
5322	    _bfd_error_handler
5323	      (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
5324	       ibfd, obfd);
5325	  else
5326	    _bfd_error_handler
5327	      (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
5328	       ibfd, obfd);
5329
5330	  flags_compatible = FALSE;
5331	}
5332
5333      if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
5334	{
5335	  if (in_flags & EF_ARM_VFP_FLOAT)
5336	    _bfd_error_handler
5337	      (_("ERROR: %B uses VFP instructions, whereas %B does not"),
5338	       ibfd, obfd);
5339	  else
5340	    _bfd_error_handler
5341	      (_("ERROR: %B uses FPA instructions, whereas %B does not"),
5342	       ibfd, obfd);
5343
5344	  flags_compatible = FALSE;
5345	}
5346
5347      if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
5348	{
5349	  if (in_flags & EF_ARM_MAVERICK_FLOAT)
5350	    _bfd_error_handler
5351	      (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
5352	       ibfd, obfd);
5353	  else
5354	    _bfd_error_handler
5355	      (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
5356	       ibfd, obfd);
5357
5358	  flags_compatible = FALSE;
5359	}
5360
5361#ifdef EF_ARM_SOFT_FLOAT
5362      if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
5363	{
5364	  /* We can allow interworking between code that is VFP format
5365	     layout, and uses either soft float or integer regs for
5366	     passing floating point arguments and results.  We already
5367	     know that the APCS_FLOAT flags match; similarly for VFP
5368	     flags.  */
5369	  if ((in_flags & EF_ARM_APCS_FLOAT) != 0
5370	      || (in_flags & EF_ARM_VFP_FLOAT) == 0)
5371	    {
5372	      if (in_flags & EF_ARM_SOFT_FLOAT)
5373		_bfd_error_handler
5374		  (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
5375		   ibfd, obfd);
5376	      else
5377		_bfd_error_handler
5378		  (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
5379		   ibfd, obfd);
5380
5381	      flags_compatible = FALSE;
5382	    }
5383	}
5384#endif
5385
5386      /* Interworking mismatch is only a warning.  */
5387      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
5388	{
5389	  if (in_flags & EF_ARM_INTERWORK)
5390	    {
5391	      _bfd_error_handler
5392		(_("Warning: %B supports interworking, whereas %B does not"),
5393		 ibfd, obfd);
5394	    }
5395	  else
5396	    {
5397	      _bfd_error_handler
5398		(_("Warning: %B does not support interworking, whereas %B does"),
5399		 ibfd, obfd);
5400	    }
5401	}
5402    }
5403
5404  return flags_compatible;
5405}
5406
5407/* Display the flags field.  */
5408
5409static bfd_boolean
5410elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
5411{
5412  FILE * file = (FILE *) ptr;
5413  unsigned long flags;
5414
5415  BFD_ASSERT (abfd != NULL && ptr != NULL);
5416
5417  /* Print normal ELF private data.  */
5418  _bfd_elf_print_private_bfd_data (abfd, ptr);
5419
5420  flags = elf_elfheader (abfd)->e_flags;
5421  /* Ignore init flag - it may not be set, despite the flags field
5422     containing valid data.  */
5423
5424  /* xgettext:c-format */
5425  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
5426
5427  switch (EF_ARM_EABI_VERSION (flags))
5428    {
5429    case EF_ARM_EABI_UNKNOWN:
5430      /* The following flag bits are GNU extensions and not part of the
5431	 official ARM ELF extended ABI.  Hence they are only decoded if
5432	 the EABI version is not set.  */
5433      if (flags & EF_ARM_INTERWORK)
5434	fprintf (file, _(" [interworking enabled]"));
5435
5436      if (flags & EF_ARM_APCS_26)
5437	fprintf (file, " [APCS-26]");
5438      else
5439	fprintf (file, " [APCS-32]");
5440
5441      if (flags & EF_ARM_VFP_FLOAT)
5442	fprintf (file, _(" [VFP float format]"));
5443      else if (flags & EF_ARM_MAVERICK_FLOAT)
5444	fprintf (file, _(" [Maverick float format]"));
5445      else
5446	fprintf (file, _(" [FPA float format]"));
5447
5448      if (flags & EF_ARM_APCS_FLOAT)
5449	fprintf (file, _(" [floats passed in float registers]"));
5450
5451      if (flags & EF_ARM_PIC)
5452	fprintf (file, _(" [position independent]"));
5453
5454      if (flags & EF_ARM_NEW_ABI)
5455	fprintf (file, _(" [new ABI]"));
5456
5457      if (flags & EF_ARM_OLD_ABI)
5458	fprintf (file, _(" [old ABI]"));
5459
5460      if (flags & EF_ARM_SOFT_FLOAT)
5461	fprintf (file, _(" [software FP]"));
5462
5463      flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
5464		 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
5465		 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
5466		 | EF_ARM_MAVERICK_FLOAT);
5467      break;
5468
5469    case EF_ARM_EABI_VER1:
5470      fprintf (file, _(" [Version1 EABI]"));
5471
5472      if (flags & EF_ARM_SYMSARESORTED)
5473	fprintf (file, _(" [sorted symbol table]"));
5474      else
5475	fprintf (file, _(" [unsorted symbol table]"));
5476
5477      flags &= ~ EF_ARM_SYMSARESORTED;
5478      break;
5479
5480    case EF_ARM_EABI_VER2:
5481      fprintf (file, _(" [Version2 EABI]"));
5482
5483      if (flags & EF_ARM_SYMSARESORTED)
5484	fprintf (file, _(" [sorted symbol table]"));
5485      else
5486	fprintf (file, _(" [unsorted symbol table]"));
5487
5488      if (flags & EF_ARM_DYNSYMSUSESEGIDX)
5489	fprintf (file, _(" [dynamic symbols use segment index]"));
5490
5491      if (flags & EF_ARM_MAPSYMSFIRST)
5492	fprintf (file, _(" [mapping symbols precede others]"));
5493
5494      flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
5495		 | EF_ARM_MAPSYMSFIRST);
5496      break;
5497
5498    case EF_ARM_EABI_VER3:
5499      fprintf (file, _(" [Version3 EABI]"));
5500      break;
5501
5502    case EF_ARM_EABI_VER4:
5503      fprintf (file, _(" [Version4 EABI]"));
5504      goto eabi;
5505
5506    case EF_ARM_EABI_VER5:
5507      fprintf (file, _(" [Version5 EABI]"));
5508    eabi:
5509      if (flags & EF_ARM_BE8)
5510	fprintf (file, _(" [BE8]"));
5511
5512      if (flags & EF_ARM_LE8)
5513	fprintf (file, _(" [LE8]"));
5514
5515      flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
5516      break;
5517
5518    default:
5519      fprintf (file, _(" <EABI version unrecognised>"));
5520      break;
5521    }
5522
5523  flags &= ~ EF_ARM_EABIMASK;
5524
5525  if (flags & EF_ARM_RELEXEC)
5526    fprintf (file, _(" [relocatable executable]"));
5527
5528  if (flags & EF_ARM_HASENTRY)
5529    fprintf (file, _(" [has entry point]"));
5530
5531  flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
5532
5533  if (flags)
5534    fprintf (file, _("<Unrecognised flag bits set>"));
5535
5536  fputc ('\n', file);
5537
5538  return TRUE;
5539}
5540
5541static int
5542elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
5543{
5544  switch (ELF_ST_TYPE (elf_sym->st_info))
5545    {
5546    case STT_ARM_TFUNC:
5547      return ELF_ST_TYPE (elf_sym->st_info);
5548
5549    case STT_ARM_16BIT:
5550      /* If the symbol is not an object, return the STT_ARM_16BIT flag.
5551	 This allows us to distinguish between data used by Thumb instructions
5552	 and non-data (which is probably code) inside Thumb regions of an
5553	 executable.  */
5554      if (type != STT_OBJECT && type != STT_TLS)
5555	return ELF_ST_TYPE (elf_sym->st_info);
5556      break;
5557
5558    default:
5559      break;
5560    }
5561
5562  return type;
5563}
5564
5565static asection *
5566elf32_arm_gc_mark_hook (asection *                   sec,
5567			struct bfd_link_info *       info ATTRIBUTE_UNUSED,
5568			Elf_Internal_Rela *          rel,
5569			struct elf_link_hash_entry * h,
5570			Elf_Internal_Sym *           sym)
5571{
5572  if (h != NULL)
5573    {
5574      switch (ELF32_R_TYPE (rel->r_info))
5575      {
5576      case R_ARM_GNU_VTINHERIT:
5577      case R_ARM_GNU_VTENTRY:
5578        break;
5579
5580      default:
5581        switch (h->root.type)
5582          {
5583          case bfd_link_hash_defined:
5584          case bfd_link_hash_defweak:
5585            return h->root.u.def.section;
5586
5587          case bfd_link_hash_common:
5588            return h->root.u.c.p->section;
5589
5590	  default:
5591	    break;
5592          }
5593       }
5594     }
5595   else
5596     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5597
5598  return NULL;
5599}
5600
5601/* Update the got entry reference counts for the section being removed.  */
5602
5603static bfd_boolean
5604elf32_arm_gc_sweep_hook (bfd *                     abfd,
5605			 struct bfd_link_info *    info,
5606			 asection *                sec,
5607			 const Elf_Internal_Rela * relocs)
5608{
5609  Elf_Internal_Shdr *symtab_hdr;
5610  struct elf_link_hash_entry **sym_hashes;
5611  bfd_signed_vma *local_got_refcounts;
5612  const Elf_Internal_Rela *rel, *relend;
5613  struct elf32_arm_link_hash_table * globals;
5614
5615  globals = elf32_arm_hash_table (info);
5616
5617  elf_section_data (sec)->local_dynrel = NULL;
5618
5619  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5620  sym_hashes = elf_sym_hashes (abfd);
5621  local_got_refcounts = elf_local_got_refcounts (abfd);
5622
5623  relend = relocs + sec->reloc_count;
5624  for (rel = relocs; rel < relend; rel++)
5625    {
5626      unsigned long r_symndx;
5627      struct elf_link_hash_entry *h = NULL;
5628      int r_type;
5629
5630      r_symndx = ELF32_R_SYM (rel->r_info);
5631      if (r_symndx >= symtab_hdr->sh_info)
5632	{
5633	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5634	  while (h->root.type == bfd_link_hash_indirect
5635		 || h->root.type == bfd_link_hash_warning)
5636	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5637	}
5638
5639      r_type = ELF32_R_TYPE (rel->r_info);
5640      r_type = arm_real_reloc_type (globals, r_type);
5641      switch (r_type)
5642	{
5643	case R_ARM_GOT32:
5644	case R_ARM_GOT_PREL:
5645	case R_ARM_TLS_GD32:
5646	case R_ARM_TLS_IE32:
5647	  if (h != NULL)
5648	    {
5649	      if (h->got.refcount > 0)
5650		h->got.refcount -= 1;
5651	    }
5652	  else if (local_got_refcounts != NULL)
5653	    {
5654	      if (local_got_refcounts[r_symndx] > 0)
5655		local_got_refcounts[r_symndx] -= 1;
5656	    }
5657	  break;
5658
5659	case R_ARM_TLS_LDM32:
5660	  elf32_arm_hash_table (info)->tls_ldm_got.refcount -= 1;
5661	  break;
5662
5663	case R_ARM_ABS32:
5664	case R_ARM_REL32:
5665	case R_ARM_PC24:
5666	case R_ARM_PLT32:
5667	case R_ARM_CALL:
5668	case R_ARM_JUMP24:
5669	case R_ARM_PREL31:
5670	case R_ARM_THM_CALL:
5671	  /* Should the interworking branches be here also?  */
5672
5673	  if (h != NULL)
5674	    {
5675	      struct elf32_arm_link_hash_entry *eh;
5676	      struct elf32_arm_relocs_copied **pp;
5677	      struct elf32_arm_relocs_copied *p;
5678
5679	      eh = (struct elf32_arm_link_hash_entry *) h;
5680
5681	      if (h->plt.refcount > 0)
5682		{
5683		  h->plt.refcount -= 1;
5684		  if (ELF32_R_TYPE (rel->r_info) == R_ARM_THM_CALL)
5685		    eh->plt_thumb_refcount--;
5686		}
5687
5688	      if (r_type == R_ARM_ABS32
5689		  || r_type == R_ARM_REL32)
5690		{
5691		  for (pp = &eh->relocs_copied; (p = *pp) != NULL;
5692		       pp = &p->next)
5693		  if (p->section == sec)
5694		    {
5695		      p->count -= 1;
5696		      if (ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
5697			p->pc_count -= 1;
5698		      if (p->count == 0)
5699			*pp = p->next;
5700		      break;
5701		    }
5702		}
5703	    }
5704	  break;
5705
5706	default:
5707	  break;
5708	}
5709    }
5710
5711  return TRUE;
5712}
5713
5714/* Look through the relocs for a section during the first phase.  */
5715
5716static bfd_boolean
5717elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
5718			asection *sec, const Elf_Internal_Rela *relocs)
5719{
5720  Elf_Internal_Shdr *symtab_hdr;
5721  struct elf_link_hash_entry **sym_hashes;
5722  struct elf_link_hash_entry **sym_hashes_end;
5723  const Elf_Internal_Rela *rel;
5724  const Elf_Internal_Rela *rel_end;
5725  bfd *dynobj;
5726  asection *sreloc;
5727  bfd_vma *local_got_offsets;
5728  struct elf32_arm_link_hash_table *htab;
5729
5730  if (info->relocatable)
5731    return TRUE;
5732
5733  htab = elf32_arm_hash_table (info);
5734  sreloc = NULL;
5735
5736  /* Create dynamic sections for relocatable executables so that we can
5737     copy relocations.  */
5738  if (htab->root.is_relocatable_executable
5739      && ! htab->root.dynamic_sections_created)
5740    {
5741      if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
5742	return FALSE;
5743    }
5744
5745  dynobj = elf_hash_table (info)->dynobj;
5746  local_got_offsets = elf_local_got_offsets (abfd);
5747
5748  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5749  sym_hashes = elf_sym_hashes (abfd);
5750  sym_hashes_end = sym_hashes
5751    + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5752
5753  if (!elf_bad_symtab (abfd))
5754    sym_hashes_end -= symtab_hdr->sh_info;
5755
5756  rel_end = relocs + sec->reloc_count;
5757  for (rel = relocs; rel < rel_end; rel++)
5758    {
5759      struct elf_link_hash_entry *h;
5760      struct elf32_arm_link_hash_entry *eh;
5761      unsigned long r_symndx;
5762      int r_type;
5763
5764      r_symndx = ELF32_R_SYM (rel->r_info);
5765      r_type = ELF32_R_TYPE (rel->r_info);
5766      r_type = arm_real_reloc_type (htab, r_type);
5767
5768      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
5769	{
5770	  (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
5771				 r_symndx);
5772	  return FALSE;
5773	}
5774
5775      if (r_symndx < symtab_hdr->sh_info)
5776        h = NULL;
5777      else
5778	{
5779	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5780	  while (h->root.type == bfd_link_hash_indirect
5781		 || h->root.type == bfd_link_hash_warning)
5782	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5783	}
5784
5785      eh = (struct elf32_arm_link_hash_entry *) h;
5786
5787      switch (r_type)
5788        {
5789	  case R_ARM_GOT32:
5790	  case R_ARM_GOT_PREL:
5791	  case R_ARM_TLS_GD32:
5792	  case R_ARM_TLS_IE32:
5793	    /* This symbol requires a global offset table entry.  */
5794	    {
5795	      int tls_type, old_tls_type;
5796
5797	      switch (r_type)
5798		{
5799		case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
5800		case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
5801		default: tls_type = GOT_NORMAL; break;
5802		}
5803
5804	      if (h != NULL)
5805		{
5806		  h->got.refcount++;
5807		  old_tls_type = elf32_arm_hash_entry (h)->tls_type;
5808		}
5809	      else
5810		{
5811		  bfd_signed_vma *local_got_refcounts;
5812
5813		  /* This is a global offset table entry for a local symbol.  */
5814		  local_got_refcounts = elf_local_got_refcounts (abfd);
5815		  if (local_got_refcounts == NULL)
5816		    {
5817		      bfd_size_type size;
5818
5819		      size = symtab_hdr->sh_info;
5820		      size *= (sizeof (bfd_signed_vma) + sizeof(char));
5821		      local_got_refcounts = bfd_zalloc (abfd, size);
5822		      if (local_got_refcounts == NULL)
5823			return FALSE;
5824		      elf_local_got_refcounts (abfd) = local_got_refcounts;
5825		      elf32_arm_local_got_tls_type (abfd)
5826			= (char *) (local_got_refcounts + symtab_hdr->sh_info);
5827		    }
5828		  local_got_refcounts[r_symndx] += 1;
5829		  old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
5830		}
5831
5832	      /* We will already have issued an error message if there is a
5833		 TLS / non-TLS mismatch, based on the symbol type.  We don't
5834		 support any linker relaxations.  So just combine any TLS
5835		 types needed.  */
5836	      if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
5837		  && tls_type != GOT_NORMAL)
5838		tls_type |= old_tls_type;
5839
5840	      if (old_tls_type != tls_type)
5841		{
5842		  if (h != NULL)
5843		    elf32_arm_hash_entry (h)->tls_type = tls_type;
5844		  else
5845		    elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
5846		}
5847	    }
5848	    /* Fall through */
5849
5850	  case R_ARM_TLS_LDM32:
5851	    if (r_type == R_ARM_TLS_LDM32)
5852		htab->tls_ldm_got.refcount++;
5853	    /* Fall through */
5854
5855	  case R_ARM_GOTOFF32:
5856	  case R_ARM_GOTPC:
5857	    if (htab->sgot == NULL)
5858	      {
5859		if (htab->root.dynobj == NULL)
5860		  htab->root.dynobj = abfd;
5861		if (!create_got_section (htab->root.dynobj, info))
5862		  return FALSE;
5863	      }
5864	    break;
5865
5866	  case R_ARM_ABS12:
5867	    /* VxWorks uses dynamic R_ARM_ABS12 relocations for
5868	       ldr __GOTT_INDEX__ offsets.  */
5869	    if (!htab->vxworks_p)
5870	      break;
5871	    /* Fall through */
5872
5873	  case R_ARM_ABS32:
5874	  case R_ARM_REL32:
5875	  case R_ARM_PC24:
5876	  case R_ARM_PLT32:
5877	  case R_ARM_CALL:
5878	  case R_ARM_JUMP24:
5879	  case R_ARM_PREL31:
5880	  case R_ARM_THM_CALL:
5881	    /* Should the interworking branches be listed here?  */
5882	    if (h != NULL)
5883	      {
5884		/* If this reloc is in a read-only section, we might
5885		   need a copy reloc.  We can't check reliably at this
5886		   stage whether the section is read-only, as input
5887		   sections have not yet been mapped to output sections.
5888		   Tentatively set the flag for now, and correct in
5889		   adjust_dynamic_symbol.  */
5890		if (!info->shared)
5891		  h->non_got_ref = 1;
5892
5893		/* We may need a .plt entry if the function this reloc
5894		   refers to is in a different object.  We can't tell for
5895		   sure yet, because something later might force the
5896		   symbol local.  */
5897		if (r_type == R_ARM_PC24
5898		    || r_type == R_ARM_CALL
5899		    || r_type == R_ARM_JUMP24
5900		    || r_type == R_ARM_PREL31
5901		    || r_type == R_ARM_PLT32
5902		    || r_type == R_ARM_THM_CALL)
5903		  h->needs_plt = 1;
5904
5905		/* If we create a PLT entry, this relocation will reference
5906		   it, even if it's an ABS32 relocation.  */
5907		h->plt.refcount += 1;
5908
5909		if (r_type == R_ARM_THM_CALL)
5910		  eh->plt_thumb_refcount += 1;
5911	      }
5912
5913	    /* If we are creating a shared library or relocatable executable,
5914	       and this is a reloc against a global symbol, or a non PC
5915	       relative reloc against a local symbol, then we need to copy
5916	       the reloc into the shared library.  However, if we are linking
5917	       with -Bsymbolic, we do not need to copy a reloc against a
5918               global symbol which is defined in an object we are
5919               including in the link (i.e., DEF_REGULAR is set).  At
5920               this point we have not seen all the input files, so it is
5921               possible that DEF_REGULAR is not set now but will be set
5922               later (it is never cleared).  We account for that
5923               possibility below by storing information in the
5924               relocs_copied field of the hash table entry.  */
5925	    if ((info->shared || htab->root.is_relocatable_executable)
5926		&& (sec->flags & SEC_ALLOC) != 0
5927		&& (r_type == R_ARM_ABS32
5928		    || (h != NULL && ! h->needs_plt
5929			&& (! info->symbolic || ! h->def_regular))))
5930	      {
5931		struct elf32_arm_relocs_copied *p, **head;
5932
5933	        /* When creating a shared object, we must copy these
5934                   reloc types into the output file.  We create a reloc
5935                   section in dynobj and make room for this reloc.  */
5936	        if (sreloc == NULL)
5937		  {
5938		    const char * name;
5939
5940		    name = (bfd_elf_string_from_elf_section
5941			    (abfd,
5942			     elf_elfheader (abfd)->e_shstrndx,
5943			     elf_section_data (sec)->rel_hdr.sh_name));
5944		    if (name == NULL)
5945		      return FALSE;
5946
5947		    BFD_ASSERT (reloc_section_p (htab, name, sec));
5948
5949		    sreloc = bfd_get_section_by_name (dynobj, name);
5950		    if (sreloc == NULL)
5951		      {
5952		        flagword flags;
5953
5954		        flags = (SEC_HAS_CONTENTS | SEC_READONLY
5955			         | SEC_IN_MEMORY | SEC_LINKER_CREATED);
5956		        if ((sec->flags & SEC_ALLOC) != 0
5957			    /* BPABI objects never have dynamic
5958			       relocations mapped.  */
5959			    && !htab->symbian_p)
5960			  flags |= SEC_ALLOC | SEC_LOAD;
5961		        sreloc = bfd_make_section_with_flags (dynobj,
5962							      name,
5963							      flags);
5964		        if (sreloc == NULL
5965			    || ! bfd_set_section_alignment (dynobj, sreloc, 2))
5966			  return FALSE;
5967		      }
5968
5969		    elf_section_data (sec)->sreloc = sreloc;
5970		  }
5971
5972		/* If this is a global symbol, we count the number of
5973		   relocations we need for this symbol.  */
5974		if (h != NULL)
5975		  {
5976		    head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
5977		  }
5978		else
5979		  {
5980		    /* Track dynamic relocs needed for local syms too.
5981		       We really need local syms available to do this
5982		       easily.  Oh well.  */
5983
5984		    asection *s;
5985		    void *vpp;
5986
5987		    s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
5988						   sec, r_symndx);
5989		    if (s == NULL)
5990		      return FALSE;
5991
5992		    vpp = &elf_section_data (s)->local_dynrel;
5993		    head = (struct elf32_arm_relocs_copied **) vpp;
5994		  }
5995
5996		p = *head;
5997		if (p == NULL || p->section != sec)
5998		  {
5999		    bfd_size_type amt = sizeof *p;
6000
6001		    p = bfd_alloc (htab->root.dynobj, amt);
6002		    if (p == NULL)
6003		      return FALSE;
6004		    p->next = *head;
6005		    *head = p;
6006		    p->section = sec;
6007		    p->count = 0;
6008		    p->pc_count = 0;
6009		  }
6010
6011		if (r_type == R_ARM_REL32)
6012		  p->pc_count += 1;
6013		p->count += 1;
6014	      }
6015	    break;
6016
6017        /* This relocation describes the C++ object vtable hierarchy.
6018           Reconstruct it for later use during GC.  */
6019        case R_ARM_GNU_VTINHERIT:
6020          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6021            return FALSE;
6022          break;
6023
6024        /* This relocation describes which C++ vtable entries are actually
6025           used.  Record for later use during GC.  */
6026        case R_ARM_GNU_VTENTRY:
6027          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6028            return FALSE;
6029          break;
6030        }
6031    }
6032
6033  return TRUE;
6034}
6035
6036/* Treat mapping symbols as special target symbols.  */
6037
6038static bfd_boolean
6039elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
6040{
6041  return bfd_is_arm_mapping_symbol_name (sym->name);
6042}
6043
6044/* This is a copy of elf_find_function() from elf.c except that
6045   ARM mapping symbols are ignored when looking for function names
6046   and STT_ARM_TFUNC is considered to a function type.  */
6047
6048static bfd_boolean
6049arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
6050		       asection *    section,
6051		       asymbol **    symbols,
6052		       bfd_vma       offset,
6053		       const char ** filename_ptr,
6054		       const char ** functionname_ptr)
6055{
6056  const char * filename = NULL;
6057  asymbol * func = NULL;
6058  bfd_vma low_func = 0;
6059  asymbol ** p;
6060
6061  for (p = symbols; *p != NULL; p++)
6062    {
6063      elf_symbol_type *q;
6064
6065      q = (elf_symbol_type *) *p;
6066
6067      switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6068	{
6069	default:
6070	  break;
6071	case STT_FILE:
6072	  filename = bfd_asymbol_name (&q->symbol);
6073	  break;
6074	case STT_FUNC:
6075	case STT_ARM_TFUNC:
6076	case STT_NOTYPE:
6077	  /* Skip $a and $t symbols.  */
6078	  if ((q->symbol.flags & BSF_LOCAL)
6079	      && bfd_is_arm_mapping_symbol_name (q->symbol.name))
6080	    continue;
6081	  /* Fall through.  */
6082	  if (bfd_get_section (&q->symbol) == section
6083	      && q->symbol.value >= low_func
6084	      && q->symbol.value <= offset)
6085	    {
6086	      func = (asymbol *) q;
6087	      low_func = q->symbol.value;
6088	    }
6089	  break;
6090	}
6091    }
6092
6093  if (func == NULL)
6094    return FALSE;
6095
6096  if (filename_ptr)
6097    *filename_ptr = filename;
6098  if (functionname_ptr)
6099    *functionname_ptr = bfd_asymbol_name (func);
6100
6101  return TRUE;
6102}
6103
6104
6105/* Find the nearest line to a particular section and offset, for error
6106   reporting.   This code is a duplicate of the code in elf.c, except
6107   that it uses arm_elf_find_function.  */
6108
6109static bfd_boolean
6110elf32_arm_find_nearest_line (bfd *          abfd,
6111			     asection *     section,
6112			     asymbol **     symbols,
6113			     bfd_vma        offset,
6114			     const char **  filename_ptr,
6115			     const char **  functionname_ptr,
6116			     unsigned int * line_ptr)
6117{
6118  bfd_boolean found = FALSE;
6119
6120  /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it.  */
6121
6122  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6123				     filename_ptr, functionname_ptr,
6124				     line_ptr, 0,
6125				     & elf_tdata (abfd)->dwarf2_find_line_info))
6126    {
6127      if (!*functionname_ptr)
6128	arm_elf_find_function (abfd, section, symbols, offset,
6129			       *filename_ptr ? NULL : filename_ptr,
6130			       functionname_ptr);
6131
6132      return TRUE;
6133    }
6134
6135  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6136					     & found, filename_ptr,
6137					     functionname_ptr, line_ptr,
6138					     & elf_tdata (abfd)->line_info))
6139    return FALSE;
6140
6141  if (found && (*functionname_ptr || *line_ptr))
6142    return TRUE;
6143
6144  if (symbols == NULL)
6145    return FALSE;
6146
6147  if (! arm_elf_find_function (abfd, section, symbols, offset,
6148			       filename_ptr, functionname_ptr))
6149    return FALSE;
6150
6151  *line_ptr = 0;
6152  return TRUE;
6153}
6154
6155static bfd_boolean
6156elf32_arm_find_inliner_info (bfd *          abfd,
6157			     const char **  filename_ptr,
6158			     const char **  functionname_ptr,
6159			     unsigned int * line_ptr)
6160{
6161  bfd_boolean found;
6162  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
6163					 functionname_ptr, line_ptr,
6164					 & elf_tdata (abfd)->dwarf2_find_line_info);
6165  return found;
6166}
6167
6168/* Adjust a symbol defined by a dynamic object and referenced by a
6169   regular object.  The current definition is in some section of the
6170   dynamic object, but we're not including those sections.  We have to
6171   change the definition to something the rest of the link can
6172   understand.  */
6173
6174static bfd_boolean
6175elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
6176				 struct elf_link_hash_entry * h)
6177{
6178  bfd * dynobj;
6179  asection * s;
6180  unsigned int power_of_two;
6181  struct elf32_arm_link_hash_entry * eh;
6182  struct elf32_arm_link_hash_table *globals;
6183
6184  globals = elf32_arm_hash_table (info);
6185  dynobj = elf_hash_table (info)->dynobj;
6186
6187  /* Make sure we know what is going on here.  */
6188  BFD_ASSERT (dynobj != NULL
6189	      && (h->needs_plt
6190		  || h->u.weakdef != NULL
6191		  || (h->def_dynamic
6192		      && h->ref_regular
6193		      && !h->def_regular)));
6194
6195  eh = (struct elf32_arm_link_hash_entry *) h;
6196
6197  /* If this is a function, put it in the procedure linkage table.  We
6198     will fill in the contents of the procedure linkage table later,
6199     when we know the address of the .got section.  */
6200  if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
6201      || h->needs_plt)
6202    {
6203      if (h->plt.refcount <= 0
6204	  || SYMBOL_CALLS_LOCAL (info, h)
6205	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6206	      && h->root.type == bfd_link_hash_undefweak))
6207	{
6208	  /* This case can occur if we saw a PLT32 reloc in an input
6209	     file, but the symbol was never referred to by a dynamic
6210	     object, or if all references were garbage collected.  In
6211	     such a case, we don't actually need to build a procedure
6212	     linkage table, and we can just do a PC24 reloc instead.  */
6213	  h->plt.offset = (bfd_vma) -1;
6214	  eh->plt_thumb_refcount = 0;
6215	  h->needs_plt = 0;
6216	}
6217
6218      return TRUE;
6219    }
6220  else
6221    {
6222      /* It's possible that we incorrectly decided a .plt reloc was
6223	 needed for an R_ARM_PC24 or similar reloc to a non-function sym
6224	 in check_relocs.  We can't decide accurately between function
6225	 and non-function syms in check-relocs; Objects loaded later in
6226	 the link may change h->type.  So fix it now.  */
6227      h->plt.offset = (bfd_vma) -1;
6228      eh->plt_thumb_refcount = 0;
6229    }
6230
6231  /* If this is a weak symbol, and there is a real definition, the
6232     processor independent code will have arranged for us to see the
6233     real definition first, and we can just use the same value.  */
6234  if (h->u.weakdef != NULL)
6235    {
6236      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6237		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
6238      h->root.u.def.section = h->u.weakdef->root.u.def.section;
6239      h->root.u.def.value = h->u.weakdef->root.u.def.value;
6240      return TRUE;
6241    }
6242
6243  /* If there are no non-GOT references, we do not need a copy
6244     relocation.  */
6245  if (!h->non_got_ref)
6246    return TRUE;
6247
6248  /* This is a reference to a symbol defined by a dynamic object which
6249     is not a function.  */
6250
6251  /* If we are creating a shared library, we must presume that the
6252     only references to the symbol are via the global offset table.
6253     For such cases we need not do anything here; the relocations will
6254     be handled correctly by relocate_section.  Relocatable executables
6255     can reference data in shared objects directly, so we don't need to
6256     do anything here.  */
6257  if (info->shared || globals->root.is_relocatable_executable)
6258    return TRUE;
6259
6260  if (h->size == 0)
6261    {
6262      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6263			     h->root.root.string);
6264      return TRUE;
6265    }
6266
6267  /* We must allocate the symbol in our .dynbss section, which will
6268     become part of the .bss section of the executable.  There will be
6269     an entry for this symbol in the .dynsym section.  The dynamic
6270     object will contain position independent code, so all references
6271     from the dynamic object to this symbol will go through the global
6272     offset table.  The dynamic linker will use the .dynsym entry to
6273     determine the address it must put in the global offset table, so
6274     both the dynamic object and the regular object will refer to the
6275     same memory location for the variable.  */
6276  s = bfd_get_section_by_name (dynobj, ".dynbss");
6277  BFD_ASSERT (s != NULL);
6278
6279  /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
6280     copy the initial value out of the dynamic object and into the
6281     runtime process image.  We need to remember the offset into the
6282     .rel(a).bss section we are going to use.  */
6283  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6284    {
6285      asection *srel;
6286
6287      srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (globals, ".bss"));
6288      BFD_ASSERT (srel != NULL);
6289      srel->size += RELOC_SIZE (globals);
6290      h->needs_copy = 1;
6291    }
6292
6293  /* We need to figure out the alignment required for this symbol.  I
6294     have no idea how ELF linkers handle this.  */
6295  power_of_two = bfd_log2 (h->size);
6296  if (power_of_two > 3)
6297    power_of_two = 3;
6298
6299  /* Apply the required alignment.  */
6300  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
6301  if (power_of_two > bfd_get_section_alignment (dynobj, s))
6302    {
6303      if (! bfd_set_section_alignment (dynobj, s, power_of_two))
6304	return FALSE;
6305    }
6306
6307  /* Define the symbol as being at this point in the section.  */
6308  h->root.u.def.section = s;
6309  h->root.u.def.value = s->size;
6310
6311  /* Increment the section size to make room for the symbol.  */
6312  s->size += h->size;
6313
6314  return TRUE;
6315}
6316
6317/* Allocate space in .plt, .got and associated reloc sections for
6318   dynamic relocs.  */
6319
6320static bfd_boolean
6321allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
6322{
6323  struct bfd_link_info *info;
6324  struct elf32_arm_link_hash_table *htab;
6325  struct elf32_arm_link_hash_entry *eh;
6326  struct elf32_arm_relocs_copied *p;
6327
6328  eh = (struct elf32_arm_link_hash_entry *) h;
6329
6330  if (h->root.type == bfd_link_hash_indirect)
6331    return TRUE;
6332
6333  if (h->root.type == bfd_link_hash_warning)
6334    /* When warning symbols are created, they **replace** the "real"
6335       entry in the hash table, thus we never get to see the real
6336       symbol in a hash traversal.  So look at it now.  */
6337    h = (struct elf_link_hash_entry *) h->root.u.i.link;
6338
6339  info = (struct bfd_link_info *) inf;
6340  htab = elf32_arm_hash_table (info);
6341
6342  if (htab->root.dynamic_sections_created
6343      && h->plt.refcount > 0)
6344    {
6345      /* Make sure this symbol is output as a dynamic symbol.
6346	 Undefined weak syms won't yet be marked as dynamic.  */
6347      if (h->dynindx == -1
6348	  && !h->forced_local)
6349	{
6350	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6351	    return FALSE;
6352	}
6353
6354      if (info->shared
6355	  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
6356	{
6357	  asection *s = htab->splt;
6358
6359	  /* If this is the first .plt entry, make room for the special
6360	     first entry.  */
6361	  if (s->size == 0)
6362	    s->size += htab->plt_header_size;
6363
6364	  h->plt.offset = s->size;
6365
6366	  /* If we will insert a Thumb trampoline before this PLT, leave room
6367	     for it.  */
6368	  if (!htab->use_blx && eh->plt_thumb_refcount > 0)
6369	    {
6370	      h->plt.offset += PLT_THUMB_STUB_SIZE;
6371	      s->size += PLT_THUMB_STUB_SIZE;
6372	    }
6373
6374	  /* If this symbol is not defined in a regular file, and we are
6375	     not generating a shared library, then set the symbol to this
6376	     location in the .plt.  This is required to make function
6377	     pointers compare as equal between the normal executable and
6378	     the shared library.  */
6379	  if (! info->shared
6380	      && !h->def_regular)
6381	    {
6382	      h->root.u.def.section = s;
6383	      h->root.u.def.value = h->plt.offset;
6384
6385	      /* Make sure the function is not marked as Thumb, in case
6386		 it is the target of an ABS32 relocation, which will
6387		 point to the PLT entry.  */
6388	      if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
6389		h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
6390	    }
6391
6392	  /* Make room for this entry.  */
6393	  s->size += htab->plt_entry_size;
6394
6395	  if (!htab->symbian_p)
6396	    {
6397	      /* We also need to make an entry in the .got.plt section, which
6398		 will be placed in the .got section by the linker script.  */
6399	      eh->plt_got_offset = htab->sgotplt->size;
6400	      htab->sgotplt->size += 4;
6401	    }
6402
6403	  /* We also need to make an entry in the .rel(a).plt section.  */
6404	  htab->srelplt->size += RELOC_SIZE (htab);
6405
6406	  /* VxWorks executables have a second set of relocations for
6407	     each PLT entry.  They go in a separate relocation section,
6408	     which is processed by the kernel loader.  */
6409	  if (htab->vxworks_p && !info->shared)
6410	    {
6411	      /* There is a relocation for the initial PLT entry:
6412		 an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
6413	      if (h->plt.offset == htab->plt_header_size)
6414		htab->srelplt2->size += RELOC_SIZE (htab);
6415
6416	      /* There are two extra relocations for each subsequent
6417		 PLT entry: an R_ARM_32 relocation for the GOT entry,
6418		 and an R_ARM_32 relocation for the PLT entry.  */
6419	      htab->srelplt2->size += RELOC_SIZE (htab) * 2;
6420	    }
6421	}
6422      else
6423	{
6424	  h->plt.offset = (bfd_vma) -1;
6425	  h->needs_plt = 0;
6426	}
6427    }
6428  else
6429    {
6430      h->plt.offset = (bfd_vma) -1;
6431      h->needs_plt = 0;
6432    }
6433
6434  if (h->got.refcount > 0)
6435    {
6436      asection *s;
6437      bfd_boolean dyn;
6438      int tls_type = elf32_arm_hash_entry (h)->tls_type;
6439      int indx;
6440
6441      /* Make sure this symbol is output as a dynamic symbol.
6442	 Undefined weak syms won't yet be marked as dynamic.  */
6443      if (h->dynindx == -1
6444	  && !h->forced_local)
6445	{
6446	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6447	    return FALSE;
6448	}
6449
6450      if (!htab->symbian_p)
6451	{
6452	  s = htab->sgot;
6453	  h->got.offset = s->size;
6454
6455	  if (tls_type == GOT_UNKNOWN)
6456	    abort ();
6457
6458	  if (tls_type == GOT_NORMAL)
6459	    /* Non-TLS symbols need one GOT slot.  */
6460	    s->size += 4;
6461	  else
6462	    {
6463	      if (tls_type & GOT_TLS_GD)
6464		/* R_ARM_TLS_GD32 needs 2 consecutive GOT slots.  */
6465		s->size += 8;
6466	      if (tls_type & GOT_TLS_IE)
6467		/* R_ARM_TLS_IE32 needs one GOT slot.  */
6468		s->size += 4;
6469	    }
6470
6471	  dyn = htab->root.dynamic_sections_created;
6472
6473	  indx = 0;
6474	  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6475	      && (!info->shared
6476		  || !SYMBOL_REFERENCES_LOCAL (info, h)))
6477	    indx = h->dynindx;
6478
6479	  if (tls_type != GOT_NORMAL
6480	      && (info->shared || indx != 0)
6481	      && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6482		  || h->root.type != bfd_link_hash_undefweak))
6483	    {
6484	      if (tls_type & GOT_TLS_IE)
6485		htab->srelgot->size += RELOC_SIZE (htab);
6486
6487	      if (tls_type & GOT_TLS_GD)
6488		htab->srelgot->size += RELOC_SIZE (htab);
6489
6490	      if ((tls_type & GOT_TLS_GD) && indx != 0)
6491		htab->srelgot->size += RELOC_SIZE (htab);
6492	    }
6493	  else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6494		    || h->root.type != bfd_link_hash_undefweak)
6495		   && (info->shared
6496	    	   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
6497	    htab->srelgot->size += RELOC_SIZE (htab);
6498	}
6499    }
6500  else
6501    h->got.offset = (bfd_vma) -1;
6502
6503  if (eh->relocs_copied == NULL)
6504    return TRUE;
6505
6506  /* In the shared -Bsymbolic case, discard space allocated for
6507     dynamic pc-relative relocs against symbols which turn out to be
6508     defined in regular objects.  For the normal shared case, discard
6509     space for pc-relative relocs that have become local due to symbol
6510     visibility changes.  */
6511
6512  if (info->shared || htab->root.is_relocatable_executable)
6513    {
6514      /* The only reloc that uses pc_count is R_ARM_REL32, which will
6515	 appear on something like ".long foo - .".  We want calls to
6516	 protected symbols to resolve directly to the function rather
6517	 than going via the plt.  If people want function pointer
6518	 comparisons to work as expected then they should avoid
6519	 writing assembly like ".long foo - .".  */
6520      if (SYMBOL_CALLS_LOCAL (info, h))
6521	{
6522	  struct elf32_arm_relocs_copied **pp;
6523
6524	  for (pp = &eh->relocs_copied; (p = *pp) != NULL; )
6525	    {
6526	      p->count -= p->pc_count;
6527	      p->pc_count = 0;
6528	      if (p->count == 0)
6529		*pp = p->next;
6530	      else
6531		pp = &p->next;
6532	    }
6533	}
6534
6535      /* Also discard relocs on undefined weak syms with non-default
6536         visibility.  */
6537      if (eh->relocs_copied != NULL
6538	  && h->root.type == bfd_link_hash_undefweak)
6539	{
6540	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
6541	    eh->relocs_copied = NULL;
6542
6543	  /* Make sure undefined weak symbols are output as a dynamic
6544	     symbol in PIEs.  */
6545	  else if (h->dynindx == -1
6546		   && !h->forced_local)
6547	    {
6548	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6549		return FALSE;
6550	    }
6551	}
6552
6553      else if (htab->root.is_relocatable_executable && h->dynindx == -1
6554	       && h->root.type == bfd_link_hash_new)
6555	{
6556	  /* Output absolute symbols so that we can create relocations
6557	     against them.  For normal symbols we output a relocation
6558	     against the section that contains them.  */
6559	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6560	    return FALSE;
6561	}
6562
6563    }
6564  else
6565    {
6566      /* For the non-shared case, discard space for relocs against
6567	 symbols which turn out to need copy relocs or are not
6568	 dynamic.  */
6569
6570      if (!h->non_got_ref
6571	  && ((h->def_dynamic
6572	       && !h->def_regular)
6573	      || (htab->root.dynamic_sections_created
6574		  && (h->root.type == bfd_link_hash_undefweak
6575		      || h->root.type == bfd_link_hash_undefined))))
6576	{
6577	  /* Make sure this symbol is output as a dynamic symbol.
6578	     Undefined weak syms won't yet be marked as dynamic.  */
6579	  if (h->dynindx == -1
6580	      && !h->forced_local)
6581	    {
6582	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6583		return FALSE;
6584	    }
6585
6586	  /* If that succeeded, we know we'll be keeping all the
6587	     relocs.  */
6588	  if (h->dynindx != -1)
6589	    goto keep;
6590	}
6591
6592      eh->relocs_copied = NULL;
6593
6594    keep: ;
6595    }
6596
6597  /* Finally, allocate space.  */
6598  for (p = eh->relocs_copied; p != NULL; p = p->next)
6599    {
6600      asection *sreloc = elf_section_data (p->section)->sreloc;
6601      sreloc->size += p->count * RELOC_SIZE (htab);
6602    }
6603
6604  return TRUE;
6605}
6606
6607/* Find any dynamic relocs that apply to read-only sections.  */
6608
6609static bfd_boolean
6610elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
6611{
6612  struct elf32_arm_link_hash_entry *eh;
6613  struct elf32_arm_relocs_copied *p;
6614
6615  if (h->root.type == bfd_link_hash_warning)
6616    h = (struct elf_link_hash_entry *) h->root.u.i.link;
6617
6618  eh = (struct elf32_arm_link_hash_entry *) h;
6619  for (p = eh->relocs_copied; p != NULL; p = p->next)
6620    {
6621      asection *s = p->section;
6622
6623      if (s != NULL && (s->flags & SEC_READONLY) != 0)
6624	{
6625	  struct bfd_link_info *info = (struct bfd_link_info *) inf;
6626
6627	  info->flags |= DF_TEXTREL;
6628
6629	  /* Not an error, just cut short the traversal.  */
6630	  return FALSE;
6631	}
6632    }
6633  return TRUE;
6634}
6635
6636/* Set the sizes of the dynamic sections.  */
6637
6638static bfd_boolean
6639elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
6640				 struct bfd_link_info * info)
6641{
6642  bfd * dynobj;
6643  asection * s;
6644  bfd_boolean plt;
6645  bfd_boolean relocs;
6646  bfd *ibfd;
6647  struct elf32_arm_link_hash_table *htab;
6648
6649  htab = elf32_arm_hash_table (info);
6650  dynobj = elf_hash_table (info)->dynobj;
6651  BFD_ASSERT (dynobj != NULL);
6652  check_use_blx (htab);
6653
6654  if (elf_hash_table (info)->dynamic_sections_created)
6655    {
6656      /* Set the contents of the .interp section to the interpreter.  */
6657      if (info->executable)
6658	{
6659	  s = bfd_get_section_by_name (dynobj, ".interp");
6660	  BFD_ASSERT (s != NULL);
6661	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
6662	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6663	}
6664    }
6665
6666  /* Set up .got offsets for local syms, and space for local dynamic
6667     relocs.  */
6668  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6669    {
6670      bfd_signed_vma *local_got;
6671      bfd_signed_vma *end_local_got;
6672      char *local_tls_type;
6673      bfd_size_type locsymcount;
6674      Elf_Internal_Shdr *symtab_hdr;
6675      asection *srel;
6676
6677      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6678	continue;
6679
6680      for (s = ibfd->sections; s != NULL; s = s->next)
6681	{
6682	  struct elf32_arm_relocs_copied *p;
6683
6684	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
6685	    {
6686	      if (!bfd_is_abs_section (p->section)
6687		  && bfd_is_abs_section (p->section->output_section))
6688		{
6689		  /* Input section has been discarded, either because
6690		     it is a copy of a linkonce section or due to
6691		     linker script /DISCARD/, so we'll be discarding
6692		     the relocs too.  */
6693		}
6694	      else if (p->count != 0)
6695		{
6696		  srel = elf_section_data (p->section)->sreloc;
6697		  srel->size += p->count * RELOC_SIZE (htab);
6698		  if ((p->section->output_section->flags & SEC_READONLY) != 0)
6699		    info->flags |= DF_TEXTREL;
6700		}
6701	    }
6702	}
6703
6704      local_got = elf_local_got_refcounts (ibfd);
6705      if (!local_got)
6706	continue;
6707
6708      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6709      locsymcount = symtab_hdr->sh_info;
6710      end_local_got = local_got + locsymcount;
6711      local_tls_type = elf32_arm_local_got_tls_type (ibfd);
6712      s = htab->sgot;
6713      srel = htab->srelgot;
6714      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
6715	{
6716	  if (*local_got > 0)
6717	    {
6718	      *local_got = s->size;
6719	      if (*local_tls_type & GOT_TLS_GD)
6720		/* TLS_GD relocs need an 8-byte structure in the GOT.  */
6721		s->size += 8;
6722	      if (*local_tls_type & GOT_TLS_IE)
6723		s->size += 4;
6724	      if (*local_tls_type == GOT_NORMAL)
6725		s->size += 4;
6726
6727	      if (info->shared || *local_tls_type == GOT_TLS_GD)
6728		srel->size += RELOC_SIZE (htab);
6729	    }
6730	  else
6731	    *local_got = (bfd_vma) -1;
6732	}
6733    }
6734
6735  if (htab->tls_ldm_got.refcount > 0)
6736    {
6737      /* Allocate two GOT entries and one dynamic relocation (if necessary)
6738	 for R_ARM_TLS_LDM32 relocations.  */
6739      htab->tls_ldm_got.offset = htab->sgot->size;
6740      htab->sgot->size += 8;
6741      if (info->shared)
6742	htab->srelgot->size += RELOC_SIZE (htab);
6743    }
6744  else
6745    htab->tls_ldm_got.offset = -1;
6746
6747  /* Allocate global sym .plt and .got entries, and space for global
6748     sym dynamic relocs.  */
6749  elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
6750
6751  /* The check_relocs and adjust_dynamic_symbol entry points have
6752     determined the sizes of the various dynamic sections.  Allocate
6753     memory for them.  */
6754  plt = FALSE;
6755  relocs = FALSE;
6756  for (s = dynobj->sections; s != NULL; s = s->next)
6757    {
6758      const char * name;
6759
6760      if ((s->flags & SEC_LINKER_CREATED) == 0)
6761	continue;
6762
6763      /* It's OK to base decisions on the section name, because none
6764	 of the dynobj section names depend upon the input files.  */
6765      name = bfd_get_section_name (dynobj, s);
6766
6767      if (strcmp (name, ".plt") == 0)
6768	{
6769	  /* Remember whether there is a PLT.  */
6770	  plt = s->size != 0;
6771	}
6772      else if (strncmp (name, ".rel", 4) == 0)
6773	{
6774	  if (s->size != 0)
6775	    {
6776	      /* Remember whether there are any reloc sections other
6777		 than .rel(a).plt and .rela.plt.unloaded.  */
6778	      if (s != htab->srelplt && s != htab->srelplt2)
6779		relocs = TRUE;
6780
6781	      /* We use the reloc_count field as a counter if we need
6782		 to copy relocs into the output file.  */
6783	      s->reloc_count = 0;
6784	    }
6785	}
6786      else if (strncmp (name, ".got", 4) != 0
6787	       && strcmp (name, ".dynbss") != 0)
6788	{
6789	  /* It's not one of our sections, so don't allocate space.  */
6790	  continue;
6791	}
6792
6793      if (s->size == 0)
6794	{
6795	  /* If we don't need this section, strip it from the
6796	     output file.  This is mostly to handle .rel(a).bss and
6797	     .rel(a).plt.  We must create both sections in
6798	     create_dynamic_sections, because they must be created
6799	     before the linker maps input sections to output
6800	     sections.  The linker does that before
6801	     adjust_dynamic_symbol is called, and it is that
6802	     function which decides whether anything needs to go
6803	     into these sections.  */
6804	  s->flags |= SEC_EXCLUDE;
6805	  continue;
6806	}
6807
6808      if ((s->flags & SEC_HAS_CONTENTS) == 0)
6809	continue;
6810
6811      /* Allocate memory for the section contents.  */
6812      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
6813      if (s->contents == NULL)
6814	return FALSE;
6815    }
6816
6817  if (elf_hash_table (info)->dynamic_sections_created)
6818    {
6819      /* Add some entries to the .dynamic section.  We fill in the
6820	 values later, in elf32_arm_finish_dynamic_sections, but we
6821	 must add the entries now so that we get the correct size for
6822	 the .dynamic section.  The DT_DEBUG entry is filled in by the
6823	 dynamic linker and used by the debugger.  */
6824#define add_dynamic_entry(TAG, VAL) \
6825  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6826
6827     if (info->executable)
6828	{
6829	  if (!add_dynamic_entry (DT_DEBUG, 0))
6830	    return FALSE;
6831	}
6832
6833      if (plt)
6834	{
6835	  if (   !add_dynamic_entry (DT_PLTGOT, 0)
6836	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
6837	      || !add_dynamic_entry (DT_PLTREL,
6838				     htab->use_rel ? DT_REL : DT_RELA)
6839	      || !add_dynamic_entry (DT_JMPREL, 0))
6840	    return FALSE;
6841	}
6842
6843      if (relocs)
6844	{
6845	  if (htab->use_rel)
6846	    {
6847	      if (!add_dynamic_entry (DT_REL, 0)
6848		  || !add_dynamic_entry (DT_RELSZ, 0)
6849		  || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
6850		return FALSE;
6851	    }
6852	  else
6853	    {
6854	      if (!add_dynamic_entry (DT_RELA, 0)
6855		  || !add_dynamic_entry (DT_RELASZ, 0)
6856		  || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
6857		return FALSE;
6858	    }
6859	}
6860
6861      /* If any dynamic relocs apply to a read-only section,
6862	 then we need a DT_TEXTREL entry.  */
6863      if ((info->flags & DF_TEXTREL) == 0)
6864	elf_link_hash_traverse (&htab->root, elf32_arm_readonly_dynrelocs,
6865				(PTR) info);
6866
6867      if ((info->flags & DF_TEXTREL) != 0)
6868	{
6869	  if (!add_dynamic_entry (DT_TEXTREL, 0))
6870	    return FALSE;
6871	}
6872    }
6873#undef add_dynamic_entry
6874
6875  return TRUE;
6876}
6877
6878/* Finish up dynamic symbol handling.  We set the contents of various
6879   dynamic sections here.  */
6880
6881static bfd_boolean
6882elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
6883				 struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
6884{
6885  bfd * dynobj;
6886  struct elf32_arm_link_hash_table *htab;
6887  struct elf32_arm_link_hash_entry *eh;
6888
6889  dynobj = elf_hash_table (info)->dynobj;
6890  htab = elf32_arm_hash_table (info);
6891  eh = (struct elf32_arm_link_hash_entry *) h;
6892
6893  if (h->plt.offset != (bfd_vma) -1)
6894    {
6895      asection * splt;
6896      asection * srel;
6897      bfd_byte *loc;
6898      bfd_vma plt_index;
6899      Elf_Internal_Rela rel;
6900
6901      /* This symbol has an entry in the procedure linkage table.  Set
6902	 it up.  */
6903
6904      BFD_ASSERT (h->dynindx != -1);
6905
6906      splt = bfd_get_section_by_name (dynobj, ".plt");
6907      srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".plt"));
6908      BFD_ASSERT (splt != NULL && srel != NULL);
6909
6910      /* Fill in the entry in the procedure linkage table.  */
6911      if (htab->symbian_p)
6912	{
6913	  unsigned i;
6914	  for (i = 0; i < htab->plt_entry_size / 4; ++i)
6915	    bfd_put_32 (output_bfd,
6916			elf32_arm_symbian_plt_entry[i],
6917			splt->contents + h->plt.offset + 4 * i);
6918
6919	  /* Fill in the entry in the .rel.plt section.  */
6920	  rel.r_offset = (splt->output_section->vma
6921			  + splt->output_offset
6922			  + h->plt.offset + 4 * (i - 1));
6923	  rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
6924
6925	  /* Get the index in the procedure linkage table which
6926	     corresponds to this symbol.  This is the index of this symbol
6927	     in all the symbols for which we are making plt entries.  The
6928	     first entry in the procedure linkage table is reserved.  */
6929	  plt_index = ((h->plt.offset - htab->plt_header_size)
6930		       / htab->plt_entry_size);
6931	}
6932      else
6933	{
6934	  bfd_vma got_offset, got_address, plt_address;
6935	  bfd_vma got_displacement;
6936	  asection * sgot;
6937
6938	  sgot = bfd_get_section_by_name (dynobj, ".got.plt");
6939	  BFD_ASSERT (sgot != NULL);
6940
6941	  /* Get the offset into the .got.plt table of the entry that
6942	     corresponds to this function.  */
6943	  got_offset = eh->plt_got_offset;
6944
6945	  /* Get the index in the procedure linkage table which
6946	     corresponds to this symbol.  This is the index of this symbol
6947	     in all the symbols for which we are making plt entries.  The
6948	     first three entries in .got.plt are reserved; after that
6949	     symbols appear in the same order as in .plt.  */
6950	  plt_index = (got_offset - 12) / 4;
6951
6952	  /* Calculate the address of the GOT entry.  */
6953	  got_address = (sgot->output_section->vma
6954			 + sgot->output_offset
6955			 + got_offset);
6956
6957	  /* ...and the address of the PLT entry.  */
6958	  plt_address = (splt->output_section->vma
6959			 + splt->output_offset
6960			 + h->plt.offset);
6961
6962	  if (htab->vxworks_p && info->shared)
6963	    {
6964	      unsigned int i;
6965	      bfd_vma val;
6966
6967	      for (i = 0; i != htab->plt_entry_size / 4; i++)
6968		{
6969		  val = elf32_arm_vxworks_shared_plt_entry[i];
6970		  if (i == 2)
6971		    val |= got_address - sgot->output_section->vma;
6972		  if (i == 5)
6973		    val |= plt_index * RELOC_SIZE (htab);
6974		  bfd_put_32 (output_bfd, val,
6975			      htab->splt->contents + h->plt.offset + i * 4);
6976		}
6977	    }
6978	  else if (htab->vxworks_p)
6979	    {
6980	      unsigned int i;
6981	      bfd_vma val;
6982
6983	      for (i = 0; i != htab->plt_entry_size / 4; i++)
6984		{
6985		  val = elf32_arm_vxworks_exec_plt_entry[i];
6986		  if (i == 2)
6987		    val |= got_address;
6988		  if (i == 4)
6989		    val |= 0xffffff & -((h->plt.offset + i * 4 + 8) >> 2);
6990		  if (i == 5)
6991		    val |= plt_index * RELOC_SIZE (htab);
6992		  bfd_put_32 (output_bfd, val,
6993			      htab->splt->contents + h->plt.offset + i * 4);
6994		}
6995
6996	      loc = (htab->srelplt2->contents
6997		     + (plt_index * 2 + 1) * RELOC_SIZE (htab));
6998
6999	      /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
7000		 referencing the GOT for this PLT entry.  */
7001	      rel.r_offset = plt_address + 8;
7002	      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
7003	      rel.r_addend = got_offset;
7004	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7005	      loc += RELOC_SIZE (htab);
7006
7007	      /* Create the R_ARM_ABS32 relocation referencing the
7008		 beginning of the PLT for this GOT entry.  */
7009	      rel.r_offset = got_address;
7010	      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
7011	      rel.r_addend = 0;
7012	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7013	    }
7014	  else
7015	    {
7016	      /* Calculate the displacement between the PLT slot and the
7017		 entry in the GOT.  The eight-byte offset accounts for the
7018		 value produced by adding to pc in the first instruction
7019		 of the PLT stub.  */
7020	      got_displacement = got_address - (plt_address + 8);
7021
7022	      BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
7023
7024	      if (!htab->use_blx && eh->plt_thumb_refcount > 0)
7025		{
7026		  bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[0],
7027			      splt->contents + h->plt.offset - 4);
7028		  bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[1],
7029			      splt->contents + h->plt.offset - 2);
7030		}
7031
7032	      bfd_put_32 (output_bfd,
7033			  elf32_arm_plt_entry[0]
7034			  | ((got_displacement & 0x0ff00000) >> 20),
7035			  splt->contents + h->plt.offset + 0);
7036	      bfd_put_32 (output_bfd,
7037			  elf32_arm_plt_entry[1]
7038			  | ((got_displacement & 0x000ff000) >> 12),
7039			  splt->contents + h->plt.offset + 4);
7040	      bfd_put_32 (output_bfd,
7041			  elf32_arm_plt_entry[2]
7042			  | (got_displacement & 0x00000fff),
7043			  splt->contents + h->plt.offset + 8);
7044#ifdef FOUR_WORD_PLT
7045	      bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
7046			  splt->contents + h->plt.offset + 12);
7047#endif
7048	    }
7049
7050	  /* Fill in the entry in the global offset table.  */
7051	  bfd_put_32 (output_bfd,
7052		      (splt->output_section->vma
7053		       + splt->output_offset),
7054		      sgot->contents + got_offset);
7055
7056	  /* Fill in the entry in the .rel(a).plt section.  */
7057	  rel.r_addend = 0;
7058	  rel.r_offset = got_address;
7059	  rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
7060	}
7061
7062      loc = srel->contents + plt_index * RELOC_SIZE (htab);
7063      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7064
7065      if (!h->def_regular)
7066	{
7067	  /* Mark the symbol as undefined, rather than as defined in
7068	     the .plt section.  Leave the value alone.  */
7069	  sym->st_shndx = SHN_UNDEF;
7070	  /* If the symbol is weak, we do need to clear the value.
7071	     Otherwise, the PLT entry would provide a definition for
7072	     the symbol even if the symbol wasn't defined anywhere,
7073	     and so the symbol would never be NULL.  */
7074	  if (!h->ref_regular_nonweak)
7075	    sym->st_value = 0;
7076	}
7077    }
7078
7079  if (h->got.offset != (bfd_vma) -1
7080      && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
7081      && (elf32_arm_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
7082    {
7083      asection * sgot;
7084      asection * srel;
7085      Elf_Internal_Rela rel;
7086      bfd_byte *loc;
7087      bfd_vma offset;
7088
7089      /* This symbol has an entry in the global offset table.  Set it
7090	 up.  */
7091      sgot = bfd_get_section_by_name (dynobj, ".got");
7092      srel = bfd_get_section_by_name (dynobj, RELOC_SECTION (htab, ".got"));
7093      BFD_ASSERT (sgot != NULL && srel != NULL);
7094
7095      offset = (h->got.offset & ~(bfd_vma) 1);
7096      rel.r_addend = 0;
7097      rel.r_offset = (sgot->output_section->vma
7098		      + sgot->output_offset
7099		      + offset);
7100
7101      /* If this is a static link, or it is a -Bsymbolic link and the
7102	 symbol is defined locally or was forced to be local because
7103	 of a version file, we just want to emit a RELATIVE reloc.
7104	 The entry in the global offset table will already have been
7105	 initialized in the relocate_section function.  */
7106      if (info->shared
7107	  && SYMBOL_REFERENCES_LOCAL (info, h))
7108	{
7109	  BFD_ASSERT((h->got.offset & 1) != 0);
7110	  rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
7111	  if (!htab->use_rel)
7112	    {
7113	      rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + offset);
7114	      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
7115	    }
7116	}
7117      else
7118	{
7119	  BFD_ASSERT((h->got.offset & 1) == 0);
7120	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
7121	  rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
7122	}
7123
7124      loc = srel->contents + srel->reloc_count++ * RELOC_SIZE (htab);
7125      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7126    }
7127
7128  if (h->needs_copy)
7129    {
7130      asection * s;
7131      Elf_Internal_Rela rel;
7132      bfd_byte *loc;
7133
7134      /* This symbol needs a copy reloc.  Set it up.  */
7135      BFD_ASSERT (h->dynindx != -1
7136		  && (h->root.type == bfd_link_hash_defined
7137		      || h->root.type == bfd_link_hash_defweak));
7138
7139      s = bfd_get_section_by_name (h->root.u.def.section->owner,
7140				   RELOC_SECTION (htab, ".bss"));
7141      BFD_ASSERT (s != NULL);
7142
7143      rel.r_addend = 0;
7144      rel.r_offset = (h->root.u.def.value
7145		      + h->root.u.def.section->output_section->vma
7146		      + h->root.u.def.section->output_offset);
7147      rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
7148      loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
7149      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
7150    }
7151
7152  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
7153     the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it is relative
7154     to the ".got" section.  */
7155  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
7156      || (!htab->vxworks_p && h == htab->root.hgot))
7157    sym->st_shndx = SHN_ABS;
7158
7159  return TRUE;
7160}
7161
7162/* Finish up the dynamic sections.  */
7163
7164static bfd_boolean
7165elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
7166{
7167  bfd * dynobj;
7168  asection * sgot;
7169  asection * sdyn;
7170
7171  dynobj = elf_hash_table (info)->dynobj;
7172
7173  sgot = bfd_get_section_by_name (dynobj, ".got.plt");
7174  BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
7175  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7176
7177  if (elf_hash_table (info)->dynamic_sections_created)
7178    {
7179      asection *splt;
7180      Elf32_External_Dyn *dyncon, *dynconend;
7181      struct elf32_arm_link_hash_table *htab;
7182
7183      htab = elf32_arm_hash_table (info);
7184      splt = bfd_get_section_by_name (dynobj, ".plt");
7185      BFD_ASSERT (splt != NULL && sdyn != NULL);
7186
7187      dyncon = (Elf32_External_Dyn *) sdyn->contents;
7188      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
7189
7190      for (; dyncon < dynconend; dyncon++)
7191	{
7192	  Elf_Internal_Dyn dyn;
7193	  const char * name;
7194	  asection * s;
7195
7196	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7197
7198	  switch (dyn.d_tag)
7199	    {
7200	      unsigned int type;
7201
7202	    default:
7203	      break;
7204
7205	    case DT_HASH:
7206	      name = ".hash";
7207	      goto get_vma_if_bpabi;
7208	    case DT_STRTAB:
7209	      name = ".dynstr";
7210	      goto get_vma_if_bpabi;
7211	    case DT_SYMTAB:
7212	      name = ".dynsym";
7213	      goto get_vma_if_bpabi;
7214	    case DT_VERSYM:
7215	      name = ".gnu.version";
7216	      goto get_vma_if_bpabi;
7217	    case DT_VERDEF:
7218	      name = ".gnu.version_d";
7219	      goto get_vma_if_bpabi;
7220	    case DT_VERNEED:
7221	      name = ".gnu.version_r";
7222	      goto get_vma_if_bpabi;
7223
7224	    case DT_PLTGOT:
7225	      name = ".got";
7226	      goto get_vma;
7227	    case DT_JMPREL:
7228	      name = RELOC_SECTION (htab, ".plt");
7229	    get_vma:
7230	      s = bfd_get_section_by_name (output_bfd, name);
7231	      BFD_ASSERT (s != NULL);
7232	      if (!htab->symbian_p)
7233		dyn.d_un.d_ptr = s->vma;
7234	      else
7235		/* In the BPABI, tags in the PT_DYNAMIC section point
7236		   at the file offset, not the memory address, for the
7237		   convenience of the post linker.  */
7238		dyn.d_un.d_ptr = s->filepos;
7239	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7240	      break;
7241
7242	    get_vma_if_bpabi:
7243	      if (htab->symbian_p)
7244		goto get_vma;
7245	      break;
7246
7247	    case DT_PLTRELSZ:
7248	      s = bfd_get_section_by_name (output_bfd,
7249					   RELOC_SECTION (htab, ".plt"));
7250	      BFD_ASSERT (s != NULL);
7251	      dyn.d_un.d_val = s->size;
7252	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7253	      break;
7254
7255	    case DT_RELSZ:
7256	    case DT_RELASZ:
7257	      if (!htab->symbian_p)
7258		{
7259		  /* My reading of the SVR4 ABI indicates that the
7260		     procedure linkage table relocs (DT_JMPREL) should be
7261		     included in the overall relocs (DT_REL).  This is
7262		     what Solaris does.  However, UnixWare can not handle
7263		     that case.  Therefore, we override the DT_RELSZ entry
7264		     here to make it not include the JMPREL relocs.  Since
7265		     the linker script arranges for .rel(a).plt to follow all
7266		     other relocation sections, we don't have to worry
7267		     about changing the DT_REL entry.  */
7268		  s = bfd_get_section_by_name (output_bfd,
7269					       RELOC_SECTION (htab, ".plt"));
7270		  if (s != NULL)
7271		    dyn.d_un.d_val -= s->size;
7272		  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7273		  break;
7274		}
7275	      /* Fall through */
7276
7277	    case DT_REL:
7278	    case DT_RELA:
7279	      /* In the BPABI, the DT_REL tag must point at the file
7280		 offset, not the VMA, of the first relocation
7281		 section.  So, we use code similar to that in
7282		 elflink.c, but do not check for SHF_ALLOC on the
7283		 relcoation section, since relocations sections are
7284		 never allocated under the BPABI.  The comments above
7285		 about Unixware notwithstanding, we include all of the
7286		 relocations here.  */
7287	      if (htab->symbian_p)
7288		{
7289		  unsigned int i;
7290		  type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
7291			  ? SHT_REL : SHT_RELA);
7292		  dyn.d_un.d_val = 0;
7293		  for (i = 1; i < elf_numsections (output_bfd); i++)
7294		    {
7295		      Elf_Internal_Shdr *hdr
7296			= elf_elfsections (output_bfd)[i];
7297		      if (hdr->sh_type == type)
7298			{
7299			  if (dyn.d_tag == DT_RELSZ
7300			      || dyn.d_tag == DT_RELASZ)
7301			    dyn.d_un.d_val += hdr->sh_size;
7302			  else if ((ufile_ptr) hdr->sh_offset
7303				   <= dyn.d_un.d_val - 1)
7304			    dyn.d_un.d_val = hdr->sh_offset;
7305			}
7306		    }
7307		  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7308		}
7309	      break;
7310
7311	      /* Set the bottom bit of DT_INIT/FINI if the
7312		 corresponding function is Thumb.  */
7313	    case DT_INIT:
7314	      name = info->init_function;
7315	      goto get_sym;
7316	    case DT_FINI:
7317	      name = info->fini_function;
7318	    get_sym:
7319	      /* If it wasn't set by elf_bfd_final_link
7320		 then there is nothing to adjust.  */
7321	      if (dyn.d_un.d_val != 0)
7322		{
7323		  struct elf_link_hash_entry * eh;
7324
7325		  eh = elf_link_hash_lookup (elf_hash_table (info), name,
7326					     FALSE, FALSE, TRUE);
7327		  if (eh != (struct elf_link_hash_entry *) NULL
7328		      && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
7329		    {
7330		      dyn.d_un.d_val |= 1;
7331		      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7332		    }
7333		}
7334	      break;
7335	    }
7336	}
7337
7338      /* Fill in the first entry in the procedure linkage table.  */
7339      if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
7340	{
7341	  const bfd_vma *plt0_entry;
7342	  bfd_vma got_address, plt_address, got_displacement;
7343
7344	  /* Calculate the addresses of the GOT and PLT.  */
7345	  got_address = sgot->output_section->vma + sgot->output_offset;
7346	  plt_address = splt->output_section->vma + splt->output_offset;
7347
7348	  if (htab->vxworks_p)
7349	    {
7350	      /* The VxWorks GOT is relocated by the dynamic linker.
7351		 Therefore, we must emit relocations rather than simply
7352		 computing the values now.  */
7353	      Elf_Internal_Rela rel;
7354
7355	      plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
7356	      bfd_put_32 (output_bfd, plt0_entry[0], splt->contents + 0);
7357	      bfd_put_32 (output_bfd, plt0_entry[1], splt->contents + 4);
7358	      bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
7359	      bfd_put_32 (output_bfd, got_address, splt->contents + 12);
7360
7361	      /* Generate a relocation for _GLOBAL_OFFSET_TABLE_. */
7362	      rel.r_offset = plt_address + 12;
7363	      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
7364	      rel.r_addend = 0;
7365	      SWAP_RELOC_OUT (htab) (output_bfd, &rel,
7366				     htab->srelplt2->contents);
7367	    }
7368	  else
7369	    {
7370	      got_displacement = got_address - (plt_address + 16);
7371
7372	      plt0_entry = elf32_arm_plt0_entry;
7373	      bfd_put_32 (output_bfd, plt0_entry[0], splt->contents + 0);
7374	      bfd_put_32 (output_bfd, plt0_entry[1], splt->contents + 4);
7375	      bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
7376	      bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
7377
7378#ifdef FOUR_WORD_PLT
7379	      /* The displacement value goes in the otherwise-unused
7380		 last word of the second entry.  */
7381	      bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
7382#else
7383	      bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
7384#endif
7385	    }
7386	}
7387
7388      /* UnixWare sets the entsize of .plt to 4, although that doesn't
7389	 really seem like the right value.  */
7390      elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7391
7392      if (htab->vxworks_p && !info->shared && htab->splt->size > 0)
7393	{
7394	  /* Correct the .rel(a).plt.unloaded relocations.  They will have
7395	     incorrect symbol indexes.  */
7396	  int num_plts;
7397	  unsigned char *p;
7398
7399	  num_plts = ((htab->splt->size - htab->plt_header_size)
7400		      / htab->plt_entry_size);
7401	  p = htab->srelplt2->contents + RELOC_SIZE (htab);
7402
7403	  for (; num_plts; num_plts--)
7404	    {
7405	      Elf_Internal_Rela rel;
7406
7407	      SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
7408	      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
7409	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
7410	      p += RELOC_SIZE (htab);
7411
7412	      SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
7413	      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
7414	      SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
7415	      p += RELOC_SIZE (htab);
7416	    }
7417	}
7418    }
7419
7420  /* Fill in the first three entries in the global offset table.  */
7421  if (sgot)
7422    {
7423      if (sgot->size > 0)
7424	{
7425	  if (sdyn == NULL)
7426	    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7427	  else
7428	    bfd_put_32 (output_bfd,
7429			sdyn->output_section->vma + sdyn->output_offset,
7430			sgot->contents);
7431	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
7432	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
7433	}
7434
7435      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7436    }
7437
7438  return TRUE;
7439}
7440
7441static void
7442elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
7443{
7444  Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
7445  struct elf32_arm_link_hash_table *globals;
7446
7447  i_ehdrp = elf_elfheader (abfd);
7448
7449  if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
7450    i_ehdrp->e_ident[EI_OSABI] = ARM_ELF_OS_ABI_VERSION;
7451  else
7452    i_ehdrp->e_ident[EI_OSABI] = 0;
7453  i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
7454
7455  if (link_info)
7456    {
7457      globals = elf32_arm_hash_table (link_info);
7458      if (globals->byteswap_code)
7459	i_ehdrp->e_flags |= EF_ARM_BE8;
7460    }
7461}
7462
7463static enum elf_reloc_type_class
7464elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
7465{
7466  switch ((int) ELF32_R_TYPE (rela->r_info))
7467    {
7468    case R_ARM_RELATIVE:
7469      return reloc_class_relative;
7470    case R_ARM_JUMP_SLOT:
7471      return reloc_class_plt;
7472    case R_ARM_COPY:
7473      return reloc_class_copy;
7474    default:
7475      return reloc_class_normal;
7476    }
7477}
7478
7479/* Set the right machine number for an Arm ELF file.  */
7480
7481static bfd_boolean
7482elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
7483{
7484  if (hdr->sh_type == SHT_NOTE)
7485    *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
7486
7487  return TRUE;
7488}
7489
7490static void
7491elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
7492{
7493  bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
7494}
7495
7496/* Return TRUE if this is an unwinding table entry.  */
7497
7498static bfd_boolean
7499is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
7500{
7501  size_t len1, len2;
7502
7503  len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
7504  len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
7505  return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
7506	  || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
7507}
7508
7509
7510/* Set the type and flags for an ARM section.  We do this by
7511   the section name, which is a hack, but ought to work.  */
7512
7513static bfd_boolean
7514elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
7515{
7516  const char * name;
7517
7518  name = bfd_get_section_name (abfd, sec);
7519
7520  if (is_arm_elf_unwind_section_name (abfd, name))
7521    {
7522      hdr->sh_type = SHT_ARM_EXIDX;
7523      hdr->sh_flags |= SHF_LINK_ORDER;
7524    }
7525  else if (strcmp(name, ".ARM.attributes") == 0)
7526    {
7527      hdr->sh_type = SHT_ARM_ATTRIBUTES;
7528    }
7529  return TRUE;
7530}
7531
7532/* Parse an Arm EABI attributes section.  */
7533static void
7534elf32_arm_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
7535{
7536  bfd_byte *contents;
7537  bfd_byte *p;
7538  bfd_vma len;
7539
7540  contents = bfd_malloc (hdr->sh_size);
7541  if (!contents)
7542    return;
7543  if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
7544				 hdr->sh_size))
7545    {
7546      free (contents);
7547      return;
7548    }
7549  p = contents;
7550  if (*(p++) == 'A')
7551    {
7552      len = hdr->sh_size - 1;
7553      while (len > 0)
7554	{
7555	  int namelen;
7556	  bfd_vma section_len;
7557
7558	  section_len = bfd_get_32 (abfd, p);
7559	  p += 4;
7560	  if (section_len > len)
7561	    section_len = len;
7562	  len -= section_len;
7563	  namelen = strlen ((char *)p) + 1;
7564	  section_len -= namelen + 4;
7565	  if (strcmp((char *)p, "aeabi") != 0)
7566	    {
7567	      /* Vendor section.  Ignore it.  */
7568	      p += namelen + section_len;
7569	    }
7570	  else
7571	    {
7572	      p += namelen;
7573	      while (section_len > 0)
7574		{
7575		  int tag;
7576		  unsigned int n;
7577		  unsigned int val;
7578		  bfd_vma subsection_len;
7579		  bfd_byte *end;
7580
7581		  tag = read_unsigned_leb128 (abfd, p, &n);
7582		  p += n;
7583		  subsection_len = bfd_get_32 (abfd, p);
7584		  p += 4;
7585		  if (subsection_len > section_len)
7586		    subsection_len = section_len;
7587		  section_len -= subsection_len;
7588		  subsection_len -= n + 4;
7589		  end = p + subsection_len;
7590		  switch (tag)
7591		    {
7592		    case Tag_File:
7593		      while (p < end)
7594			{
7595			  bfd_boolean is_string;
7596
7597			  tag = read_unsigned_leb128 (abfd, p, &n);
7598			  p += n;
7599			  if (tag == 4 || tag == 5)
7600			    is_string = 1;
7601			  else if (tag < 32)
7602			    is_string = 0;
7603			  else
7604			    is_string = (tag & 1) != 0;
7605			  if (tag == Tag_compatibility)
7606			    {
7607			      val = read_unsigned_leb128 (abfd, p, &n);
7608			      p += n;
7609			      elf32_arm_add_eabi_attr_compat (abfd, val,
7610							      (char *)p);
7611			      p += strlen ((char *)p) + 1;
7612			    }
7613			  else if (is_string)
7614			    {
7615			      elf32_arm_add_eabi_attr_string (abfd, tag,
7616							      (char *)p);
7617			      p += strlen ((char *)p) + 1;
7618			    }
7619			  else
7620			    {
7621			      val = read_unsigned_leb128 (abfd, p, &n);
7622			      p += n;
7623			      elf32_arm_add_eabi_attr_int (abfd, tag, val);
7624			    }
7625			}
7626		      break;
7627		    case Tag_Section:
7628		    case Tag_Symbol:
7629		      /* Don't have anywhere convenient to attach these.
7630		         Fall through for now.  */
7631		    default:
7632		      /* Ignore things we don't kow about.  */
7633		      p += subsection_len;
7634		      subsection_len = 0;
7635		      break;
7636		    }
7637		}
7638	    }
7639	}
7640    }
7641  free (contents);
7642}
7643
7644/* Handle an ARM specific section when reading an object file.  This is
7645   called when bfd_section_from_shdr finds a section with an unknown
7646   type.  */
7647
7648static bfd_boolean
7649elf32_arm_section_from_shdr (bfd *abfd,
7650			     Elf_Internal_Shdr * hdr,
7651			     const char *name,
7652			     int shindex)
7653{
7654  /* There ought to be a place to keep ELF backend specific flags, but
7655     at the moment there isn't one.  We just keep track of the
7656     sections by their name, instead.  Fortunately, the ABI gives
7657     names for all the ARM specific sections, so we will probably get
7658     away with this.  */
7659  switch (hdr->sh_type)
7660    {
7661    case SHT_ARM_EXIDX:
7662    case SHT_ARM_PREEMPTMAP:
7663    case SHT_ARM_ATTRIBUTES:
7664      break;
7665
7666    default:
7667      return FALSE;
7668    }
7669
7670  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7671    return FALSE;
7672
7673  if (hdr->sh_type == SHT_ARM_ATTRIBUTES)
7674    elf32_arm_parse_attributes(abfd, hdr);
7675  return TRUE;
7676}
7677
7678/* A structure used to record a list of sections, independently
7679   of the next and prev fields in the asection structure.  */
7680typedef struct section_list
7681{
7682  asection * sec;
7683  struct section_list * next;
7684  struct section_list * prev;
7685}
7686section_list;
7687
7688/* Unfortunately we need to keep a list of sections for which
7689   an _arm_elf_section_data structure has been allocated.  This
7690   is because it is possible for functions like elf32_arm_write_section
7691   to be called on a section which has had an elf_data_structure
7692   allocated for it (and so the used_by_bfd field is valid) but
7693   for which the ARM extended version of this structure - the
7694   _arm_elf_section_data structure - has not been allocated.  */
7695static section_list * sections_with_arm_elf_section_data = NULL;
7696
7697static void
7698record_section_with_arm_elf_section_data (asection * sec)
7699{
7700  struct section_list * entry;
7701
7702  entry = bfd_malloc (sizeof (* entry));
7703  if (entry == NULL)
7704    return;
7705  entry->sec = sec;
7706  entry->next = sections_with_arm_elf_section_data;
7707  entry->prev = NULL;
7708  if (entry->next != NULL)
7709    entry->next->prev = entry;
7710  sections_with_arm_elf_section_data = entry;
7711}
7712
7713static struct section_list *
7714find_arm_elf_section_entry (asection * sec)
7715{
7716  struct section_list * entry;
7717  static struct section_list * last_entry = NULL;
7718
7719  /* This is a short cut for the typical case where the sections are added
7720     to the sections_with_arm_elf_section_data list in forward order and
7721     then looked up here in backwards order.  This makes a real difference
7722     to the ld-srec/sec64k.exp linker test.  */
7723  entry = sections_with_arm_elf_section_data;
7724  if (last_entry != NULL)
7725    {
7726      if (last_entry->sec == sec)
7727	entry = last_entry;
7728      else if (last_entry->next != NULL
7729	       && last_entry->next->sec == sec)
7730	entry = last_entry->next;
7731    }
7732
7733  for (; entry; entry = entry->next)
7734    if (entry->sec == sec)
7735      break;
7736
7737  if (entry)
7738    /* Record the entry prior to this one - it is the entry we are most
7739       likely to want to locate next time.  Also this way if we have been
7740       called from unrecord_section_with_arm_elf_section_data() we will not
7741       be caching a pointer that is about to be freed.  */
7742    last_entry = entry->prev;
7743
7744  return entry;
7745}
7746
7747static _arm_elf_section_data *
7748get_arm_elf_section_data (asection * sec)
7749{
7750  struct section_list * entry;
7751
7752  entry = find_arm_elf_section_entry (sec);
7753
7754  if (entry)
7755    return elf32_arm_section_data (entry->sec);
7756  else
7757    return NULL;
7758}
7759
7760static void
7761unrecord_section_with_arm_elf_section_data (asection * sec)
7762{
7763  struct section_list * entry;
7764
7765  entry = find_arm_elf_section_entry (sec);
7766
7767  if (entry)
7768    {
7769      if (entry->prev != NULL)
7770	entry->prev->next = entry->next;
7771      if (entry->next != NULL)
7772	entry->next->prev = entry->prev;
7773      if (entry == sections_with_arm_elf_section_data)
7774	sections_with_arm_elf_section_data = entry->next;
7775      free (entry);
7776    }
7777}
7778
7779/* Called for each symbol.  Builds a section map based on mapping symbols.
7780   Does not alter any of the symbols.  */
7781
7782static bfd_boolean
7783elf32_arm_output_symbol_hook (struct bfd_link_info *info,
7784			      const char *name,
7785			      Elf_Internal_Sym *elfsym,
7786			      asection *input_sec,
7787			      struct elf_link_hash_entry *h)
7788{
7789  int mapcount;
7790  elf32_arm_section_map *map;
7791  elf32_arm_section_map *newmap;
7792  _arm_elf_section_data *arm_data;
7793  struct elf32_arm_link_hash_table *globals;
7794
7795  globals = elf32_arm_hash_table (info);
7796  if (globals->vxworks_p
7797      && !elf_vxworks_link_output_symbol_hook (info, name, elfsym,
7798					       input_sec, h))
7799    return FALSE;
7800
7801  /* Only do this on final link.  */
7802  if (info->relocatable)
7803    return TRUE;
7804
7805  /* Only build a map if we need to byteswap code.  */
7806  if (!globals->byteswap_code)
7807    return TRUE;
7808
7809  /* We only want mapping symbols.  */
7810  if (! bfd_is_arm_mapping_symbol_name (name))
7811    return TRUE;
7812
7813  /* If this section has not been allocated an _arm_elf_section_data
7814     structure then we cannot record anything.  */
7815  arm_data = get_arm_elf_section_data (input_sec);
7816  if (arm_data == NULL)
7817    return TRUE;
7818
7819  mapcount = arm_data->mapcount + 1;
7820  map = arm_data->map;
7821
7822  /* TODO: This may be inefficient, but we probably don't usually have many
7823     mapping symbols per section.  */
7824  newmap = bfd_realloc (map, mapcount * sizeof (* map));
7825  if (newmap != NULL)
7826    {
7827      arm_data->map = newmap;
7828      arm_data->mapcount = mapcount;
7829
7830      newmap[mapcount - 1].vma = elfsym->st_value;
7831      newmap[mapcount - 1].type = name[1];
7832    }
7833
7834  return TRUE;
7835}
7836
7837/* Allocate target specific section data.  */
7838
7839static bfd_boolean
7840elf32_arm_new_section_hook (bfd *abfd, asection *sec)
7841{
7842  _arm_elf_section_data *sdata;
7843  bfd_size_type amt = sizeof (*sdata);
7844
7845  sdata = bfd_zalloc (abfd, amt);
7846  if (sdata == NULL)
7847    return FALSE;
7848  sec->used_by_bfd = sdata;
7849
7850  record_section_with_arm_elf_section_data (sec);
7851
7852  return _bfd_elf_new_section_hook (abfd, sec);
7853}
7854
7855
7856/* Used to order a list of mapping symbols by address.  */
7857
7858static int
7859elf32_arm_compare_mapping (const void * a, const void * b)
7860{
7861  return ((const elf32_arm_section_map *) a)->vma
7862	 > ((const elf32_arm_section_map *) b)->vma;
7863}
7864
7865
7866/* Do code byteswapping.  Return FALSE afterwards so that the section is
7867   written out as normal.  */
7868
7869static bfd_boolean
7870elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
7871			 bfd_byte *contents)
7872{
7873  int mapcount;
7874  _arm_elf_section_data *arm_data;
7875  elf32_arm_section_map *map;
7876  bfd_vma ptr;
7877  bfd_vma end;
7878  bfd_vma offset;
7879  bfd_byte tmp;
7880  int i;
7881
7882  /* If this section has not been allocated an _arm_elf_section_data
7883     structure then we cannot record anything.  */
7884  arm_data = get_arm_elf_section_data (sec);
7885  if (arm_data == NULL)
7886    return FALSE;
7887
7888  mapcount = arm_data->mapcount;
7889  map = arm_data->map;
7890
7891  if (mapcount == 0)
7892    return FALSE;
7893
7894  qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
7895
7896  offset = sec->output_section->vma + sec->output_offset;
7897  ptr = map[0].vma - offset;
7898  for (i = 0; i < mapcount; i++)
7899    {
7900      if (i == mapcount - 1)
7901	end = sec->size;
7902      else
7903	end = map[i + 1].vma - offset;
7904
7905      switch (map[i].type)
7906	{
7907	case 'a':
7908	  /* Byte swap code words.  */
7909	  while (ptr + 3 < end)
7910	    {
7911	      tmp = contents[ptr];
7912	      contents[ptr] = contents[ptr + 3];
7913	      contents[ptr + 3] = tmp;
7914	      tmp = contents[ptr + 1];
7915	      contents[ptr + 1] = contents[ptr + 2];
7916	      contents[ptr + 2] = tmp;
7917	      ptr += 4;
7918	    }
7919	  break;
7920
7921	case 't':
7922	  /* Byte swap code halfwords.  */
7923	  while (ptr + 1 < end)
7924	    {
7925	      tmp = contents[ptr];
7926	      contents[ptr] = contents[ptr + 1];
7927	      contents[ptr + 1] = tmp;
7928	      ptr += 2;
7929	    }
7930	  break;
7931
7932	case 'd':
7933	  /* Leave data alone.  */
7934	  break;
7935	}
7936      ptr = end;
7937    }
7938
7939  free (map);
7940  arm_data->mapcount = 0;
7941  arm_data->map = NULL;
7942  unrecord_section_with_arm_elf_section_data (sec);
7943
7944  return FALSE;
7945}
7946
7947static void
7948unrecord_section_via_map_over_sections (bfd * abfd ATTRIBUTE_UNUSED,
7949					asection * sec,
7950					void * ignore ATTRIBUTE_UNUSED)
7951{
7952  unrecord_section_with_arm_elf_section_data (sec);
7953}
7954
7955static bfd_boolean
7956elf32_arm_close_and_cleanup (bfd * abfd)
7957{
7958  bfd_map_over_sections (abfd, unrecord_section_via_map_over_sections, NULL);
7959
7960  return _bfd_elf_close_and_cleanup (abfd);
7961}
7962
7963/* Display STT_ARM_TFUNC symbols as functions.  */
7964
7965static void
7966elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
7967			     asymbol *asym)
7968{
7969  elf_symbol_type *elfsym = (elf_symbol_type *) asym;
7970
7971  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
7972    elfsym->symbol.flags |= BSF_FUNCTION;
7973}
7974
7975
7976/* Mangle thumb function symbols as we read them in.  */
7977
7978static void
7979elf32_arm_swap_symbol_in (bfd * abfd,
7980			  const void *psrc,
7981			  const void *pshn,
7982			  Elf_Internal_Sym *dst)
7983{
7984  bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst);
7985
7986  /* New EABI objects mark thumb function symbols by setting the low bit of
7987     the address.  Turn these into STT_ARM_TFUNC.  */
7988  if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
7989      && (dst->st_value & 1))
7990    {
7991      dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
7992      dst->st_value &= ~(bfd_vma) 1;
7993    }
7994}
7995
7996
7997/* Mangle thumb function symbols as we write them out.  */
7998
7999static void
8000elf32_arm_swap_symbol_out (bfd *abfd,
8001			   const Elf_Internal_Sym *src,
8002			   void *cdst,
8003			   void *shndx)
8004{
8005  Elf_Internal_Sym newsym;
8006
8007  /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
8008     of the address set, as per the new EABI.  We do this unconditionally
8009     because objcopy does not set the elf header flags until after
8010     it writes out the symbol table.  */
8011  if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
8012    {
8013      newsym = *src;
8014      newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
8015      newsym.st_value |= 1;
8016
8017      src = &newsym;
8018    }
8019  bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
8020}
8021
8022/* Add the PT_ARM_EXIDX program header.  */
8023
8024static bfd_boolean
8025elf32_arm_modify_segment_map (bfd *abfd,
8026			      struct bfd_link_info *info ATTRIBUTE_UNUSED)
8027{
8028  struct elf_segment_map *m;
8029  asection *sec;
8030
8031  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
8032  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
8033    {
8034      /* If there is already a PT_ARM_EXIDX header, then we do not
8035	 want to add another one.  This situation arises when running
8036	 "strip"; the input binary already has the header.  */
8037      m = elf_tdata (abfd)->segment_map;
8038      while (m && m->p_type != PT_ARM_EXIDX)
8039	m = m->next;
8040      if (!m)
8041	{
8042	  m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
8043	  if (m == NULL)
8044	    return FALSE;
8045	  m->p_type = PT_ARM_EXIDX;
8046	  m->count = 1;
8047	  m->sections[0] = sec;
8048
8049	  m->next = elf_tdata (abfd)->segment_map;
8050	  elf_tdata (abfd)->segment_map = m;
8051	}
8052    }
8053
8054  return TRUE;
8055}
8056
8057/* We may add a PT_ARM_EXIDX program header.  */
8058
8059static int
8060elf32_arm_additional_program_headers (bfd *abfd)
8061{
8062  asection *sec;
8063
8064  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
8065  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
8066    return 1;
8067  else
8068    return 0;
8069}
8070
8071/* We use this to override swap_symbol_in and swap_symbol_out.  */
8072const struct elf_size_info elf32_arm_size_info = {
8073  sizeof (Elf32_External_Ehdr),
8074  sizeof (Elf32_External_Phdr),
8075  sizeof (Elf32_External_Shdr),
8076  sizeof (Elf32_External_Rel),
8077  sizeof (Elf32_External_Rela),
8078  sizeof (Elf32_External_Sym),
8079  sizeof (Elf32_External_Dyn),
8080  sizeof (Elf_External_Note),
8081  4,
8082  1,
8083  32, 2,
8084  ELFCLASS32, EV_CURRENT,
8085  bfd_elf32_write_out_phdrs,
8086  bfd_elf32_write_shdrs_and_ehdr,
8087  bfd_elf32_write_relocs,
8088  elf32_arm_swap_symbol_in,
8089  elf32_arm_swap_symbol_out,
8090  bfd_elf32_slurp_reloc_table,
8091  bfd_elf32_slurp_symbol_table,
8092  bfd_elf32_swap_dyn_in,
8093  bfd_elf32_swap_dyn_out,
8094  bfd_elf32_swap_reloc_in,
8095  bfd_elf32_swap_reloc_out,
8096  bfd_elf32_swap_reloca_in,
8097  bfd_elf32_swap_reloca_out
8098};
8099
8100#define ELF_ARCH			bfd_arch_arm
8101#define ELF_MACHINE_CODE		EM_ARM
8102#ifdef __QNXTARGET__
8103#define ELF_MAXPAGESIZE			0x1000
8104#else
8105#define ELF_MAXPAGESIZE			0x8000
8106#endif
8107#define ELF_MINPAGESIZE			0x1000
8108
8109#define bfd_elf32_mkobject		        elf32_arm_mkobject
8110
8111#define bfd_elf32_bfd_copy_private_bfd_data	elf32_arm_copy_private_bfd_data
8112#define bfd_elf32_bfd_merge_private_bfd_data	elf32_arm_merge_private_bfd_data
8113#define bfd_elf32_bfd_set_private_flags		elf32_arm_set_private_flags
8114#define bfd_elf32_bfd_print_private_bfd_data	elf32_arm_print_private_bfd_data
8115#define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
8116#define bfd_elf32_bfd_reloc_type_lookup		elf32_arm_reloc_type_lookup
8117#define bfd_elf32_find_nearest_line	        elf32_arm_find_nearest_line
8118#define bfd_elf32_find_inliner_info	        elf32_arm_find_inliner_info
8119#define bfd_elf32_new_section_hook		elf32_arm_new_section_hook
8120#define bfd_elf32_bfd_is_target_special_symbol	elf32_arm_is_target_special_symbol
8121#define bfd_elf32_close_and_cleanup             elf32_arm_close_and_cleanup
8122#define bfd_elf32_bfd_final_link		elf32_arm_bfd_final_link
8123
8124#define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
8125#define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
8126#define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
8127#define elf_backend_check_relocs                elf32_arm_check_relocs
8128#define elf_backend_relocate_section		elf32_arm_relocate_section
8129#define elf_backend_write_section		elf32_arm_write_section
8130#define elf_backend_adjust_dynamic_symbol	elf32_arm_adjust_dynamic_symbol
8131#define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
8132#define elf_backend_finish_dynamic_symbol	elf32_arm_finish_dynamic_symbol
8133#define elf_backend_finish_dynamic_sections	elf32_arm_finish_dynamic_sections
8134#define elf_backend_link_output_symbol_hook	elf32_arm_output_symbol_hook
8135#define elf_backend_size_dynamic_sections	elf32_arm_size_dynamic_sections
8136#define elf_backend_post_process_headers	elf32_arm_post_process_headers
8137#define elf_backend_reloc_type_class		elf32_arm_reloc_type_class
8138#define elf_backend_object_p			elf32_arm_object_p
8139#define elf_backend_section_flags		elf32_arm_section_flags
8140#define elf_backend_fake_sections  		elf32_arm_fake_sections
8141#define elf_backend_section_from_shdr  		elf32_arm_section_from_shdr
8142#define elf_backend_final_write_processing      elf32_arm_final_write_processing
8143#define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
8144#define elf_backend_symbol_processing		elf32_arm_symbol_processing
8145#define elf_backend_size_info			elf32_arm_size_info
8146#define elf_backend_modify_segment_map		elf32_arm_modify_segment_map
8147#define elf_backend_additional_program_headers \
8148  elf32_arm_additional_program_headers
8149
8150#define elf_backend_can_refcount    1
8151#define elf_backend_can_gc_sections 1
8152#define elf_backend_plt_readonly    1
8153#define elf_backend_want_got_plt    1
8154#define elf_backend_want_plt_sym    0
8155#define elf_backend_may_use_rel_p   1
8156#define elf_backend_may_use_rela_p  0
8157#define elf_backend_default_use_rela_p 0
8158#define elf_backend_rela_normal     0
8159
8160#define elf_backend_got_header_size	12
8161
8162#include "elf32-target.h"
8163
8164/* VxWorks Targets */
8165
8166#undef TARGET_LITTLE_SYM
8167#define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vxworks_vec
8168#undef TARGET_LITTLE_NAME
8169#define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
8170#undef TARGET_BIG_SYM
8171#define TARGET_BIG_SYM                  bfd_elf32_bigarm_vxworks_vec
8172#undef TARGET_BIG_NAME
8173#define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
8174
8175/* Like elf32_arm_link_hash_table_create -- but overrides
8176   appropriately for VxWorks.  */
8177static struct bfd_link_hash_table *
8178elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
8179{
8180  struct bfd_link_hash_table *ret;
8181
8182  ret = elf32_arm_link_hash_table_create (abfd);
8183  if (ret)
8184    {
8185      struct elf32_arm_link_hash_table *htab
8186	= (struct elf32_arm_link_hash_table *) ret;
8187      htab->use_rel = 0;
8188      htab->vxworks_p = 1;
8189    }
8190  return ret;
8191}
8192
8193static void
8194elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
8195{
8196  elf32_arm_final_write_processing (abfd, linker);
8197  elf_vxworks_final_write_processing (abfd, linker);
8198}
8199
8200#undef elf32_bed
8201#define elf32_bed elf32_arm_vxworks_bed
8202
8203#undef bfd_elf32_bfd_link_hash_table_create
8204#define bfd_elf32_bfd_link_hash_table_create \
8205  elf32_arm_vxworks_link_hash_table_create
8206#undef elf_backend_add_symbol_hook
8207#define elf_backend_add_symbol_hook \
8208  elf_vxworks_add_symbol_hook
8209#undef elf_backend_final_write_processing
8210#define elf_backend_final_write_processing \
8211  elf32_arm_vxworks_final_write_processing
8212#undef elf_backend_emit_relocs
8213#define elf_backend_emit_relocs \
8214  elf_vxworks_emit_relocs
8215
8216#undef elf_backend_may_use_rel_p
8217#define elf_backend_may_use_rel_p	0
8218#undef elf_backend_may_use_rela_p
8219#define elf_backend_may_use_rela_p	1
8220#undef elf_backend_default_use_rela_p
8221#define elf_backend_default_use_rela_p	1
8222#undef elf_backend_rela_normal
8223#define elf_backend_rela_normal		1
8224#undef elf_backend_want_plt_sym
8225#define elf_backend_want_plt_sym	1
8226#undef ELF_MAXPAGESIZE
8227#define ELF_MAXPAGESIZE			0x1000
8228
8229#include "elf32-target.h"
8230
8231
8232/* Symbian OS Targets */
8233
8234#undef TARGET_LITTLE_SYM
8235#define TARGET_LITTLE_SYM               bfd_elf32_littlearm_symbian_vec
8236#undef TARGET_LITTLE_NAME
8237#define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
8238#undef TARGET_BIG_SYM
8239#define TARGET_BIG_SYM                  bfd_elf32_bigarm_symbian_vec
8240#undef TARGET_BIG_NAME
8241#define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
8242
8243/* Like elf32_arm_link_hash_table_create -- but overrides
8244   appropriately for Symbian OS.  */
8245static struct bfd_link_hash_table *
8246elf32_arm_symbian_link_hash_table_create (bfd *abfd)
8247{
8248  struct bfd_link_hash_table *ret;
8249
8250  ret = elf32_arm_link_hash_table_create (abfd);
8251  if (ret)
8252    {
8253      struct elf32_arm_link_hash_table *htab
8254	= (struct elf32_arm_link_hash_table *)ret;
8255      /* There is no PLT header for Symbian OS.  */
8256      htab->plt_header_size = 0;
8257      /* The PLT entries are each three instructions.  */
8258      htab->plt_entry_size = 4 * NUM_ELEM (elf32_arm_symbian_plt_entry);
8259      htab->symbian_p = 1;
8260      /* Symbian uses armv5t or above, so use_blx is always true.  */
8261      htab->use_blx = 1;
8262      htab->root.is_relocatable_executable = 1;
8263    }
8264  return ret;
8265}
8266
8267static const struct bfd_elf_special_section
8268elf32_arm_symbian_special_sections[] =
8269{
8270  /* In a BPABI executable, the dynamic linking sections do not go in
8271     the loadable read-only segment.  The post-linker may wish to
8272     refer to these sections, but they are not part of the final
8273     program image.  */
8274  { ".dynamic",        8,  0, SHT_DYNAMIC,  0 },
8275  { ".dynstr",         7,  0, SHT_STRTAB,   0 },
8276  { ".dynsym",         7,  0, SHT_DYNSYM,   0 },
8277  { ".got",            4,  0, SHT_PROGBITS, 0 },
8278  { ".hash",           5,  0, SHT_HASH,     0 },
8279  /* These sections do not need to be writable as the SymbianOS
8280     postlinker will arrange things so that no dynamic relocation is
8281     required.  */
8282  { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC },
8283  { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC },
8284  { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC },
8285  { NULL,              0,  0, 0,            0 }
8286};
8287
8288static void
8289elf32_arm_symbian_begin_write_processing (bfd *abfd,
8290					  struct bfd_link_info *link_info
8291					    ATTRIBUTE_UNUSED)
8292{
8293  /* BPABI objects are never loaded directly by an OS kernel; they are
8294     processed by a postlinker first, into an OS-specific format.  If
8295     the D_PAGED bit is set on the file, BFD will align segments on
8296     page boundaries, so that an OS can directly map the file.  With
8297     BPABI objects, that just results in wasted space.  In addition,
8298     because we clear the D_PAGED bit, map_sections_to_segments will
8299     recognize that the program headers should not be mapped into any
8300     loadable segment.  */
8301  abfd->flags &= ~D_PAGED;
8302}
8303
8304static bfd_boolean
8305elf32_arm_symbian_modify_segment_map (bfd *abfd,
8306				      struct bfd_link_info *info)
8307{
8308  struct elf_segment_map *m;
8309  asection *dynsec;
8310
8311  /* BPABI shared libraries and executables should have a PT_DYNAMIC
8312     segment.  However, because the .dynamic section is not marked
8313     with SEC_LOAD, the generic ELF code will not create such a
8314     segment.  */
8315  dynsec = bfd_get_section_by_name (abfd, ".dynamic");
8316  if (dynsec)
8317    {
8318      m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
8319      m->next = elf_tdata (abfd)->segment_map;
8320      elf_tdata (abfd)->segment_map = m;
8321    }
8322
8323  /* Also call the generic arm routine.  */
8324  return elf32_arm_modify_segment_map (abfd, info);
8325}
8326
8327#undef elf32_bed
8328#define elf32_bed elf32_arm_symbian_bed
8329
8330/* The dynamic sections are not allocated on SymbianOS; the postlinker
8331   will process them and then discard them.  */
8332#undef ELF_DYNAMIC_SEC_FLAGS
8333#define ELF_DYNAMIC_SEC_FLAGS \
8334  (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
8335
8336#undef bfd_elf32_bfd_link_hash_table_create
8337#define bfd_elf32_bfd_link_hash_table_create \
8338  elf32_arm_symbian_link_hash_table_create
8339#undef elf_backend_add_symbol_hook
8340
8341#undef elf_backend_special_sections
8342#define elf_backend_special_sections elf32_arm_symbian_special_sections
8343
8344#undef elf_backend_begin_write_processing
8345#define elf_backend_begin_write_processing \
8346    elf32_arm_symbian_begin_write_processing
8347#undef elf_backend_final_write_processing
8348#define elf_backend_final_write_processing \
8349  elf32_arm_final_write_processing
8350#undef elf_backend_emit_relocs
8351
8352#undef elf_backend_modify_segment_map
8353#define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
8354
8355/* There is no .got section for BPABI objects, and hence no header.  */
8356#undef elf_backend_got_header_size
8357#define elf_backend_got_header_size 0
8358
8359/* Similarly, there is no .got.plt section.  */
8360#undef elf_backend_want_got_plt
8361#define elf_backend_want_got_plt 0
8362
8363#undef elf_backend_may_use_rel_p
8364#define elf_backend_may_use_rel_p	1
8365#undef elf_backend_may_use_rela_p
8366#define elf_backend_may_use_rela_p	0
8367#undef elf_backend_default_use_rela_p
8368#define elf_backend_default_use_rela_p	0
8369#undef elf_backend_rela_normal
8370#define elf_backend_rela_normal		0
8371#undef elf_backend_want_plt_sym
8372#define elf_backend_want_plt_sym	0
8373#undef ELF_MAXPAGESIZE
8374#define ELF_MAXPAGESIZE			0x8000
8375
8376#include "elf32-target.h"
8377