]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/binutils/bfd/elf32-arm.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / binutils / bfd / elf32-arm.h
1 /* 32-bit ELF support for ARM
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #ifndef USE_REL
22 #define USE_REL 0
23 #endif
24
25 typedef unsigned long int insn32;
26 typedef unsigned short int insn16;
27
28 static bfd_boolean elf32_arm_set_private_flags
29   PARAMS ((bfd *, flagword));
30 static bfd_boolean elf32_arm_copy_private_bfd_data
31   PARAMS ((bfd *, bfd *));
32 static bfd_boolean elf32_arm_merge_private_bfd_data
33   PARAMS ((bfd *, bfd *));
34 static bfd_boolean elf32_arm_print_private_bfd_data
35   PARAMS ((bfd *, PTR));
36 static int elf32_arm_get_symbol_type
37   PARAMS (( Elf_Internal_Sym *, int));
38 static struct bfd_link_hash_table *elf32_arm_link_hash_table_create
39   PARAMS ((bfd *));
40 static bfd_reloc_status_type elf32_arm_final_link_relocate
41   PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
42            Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
43            const char *, int, struct elf_link_hash_entry *));
44 static insn32 insert_thumb_branch
45   PARAMS ((insn32, int));
46 static struct elf_link_hash_entry *find_thumb_glue
47   PARAMS ((struct bfd_link_info *, const char *, bfd *));
48 static struct elf_link_hash_entry *find_arm_glue
49   PARAMS ((struct bfd_link_info *, const char *, bfd *));
50 static void elf32_arm_post_process_headers
51   PARAMS ((bfd *, struct bfd_link_info *));
52 static int elf32_arm_to_thumb_stub
53   PARAMS ((struct bfd_link_info *, const char *, bfd *, bfd *, asection *,
54            bfd_byte *, asection *, bfd_vma, bfd_signed_vma, bfd_vma));
55 static int elf32_thumb_to_arm_stub
56   PARAMS ((struct bfd_link_info *, const char *, bfd *, bfd *, asection *,
57            bfd_byte *, asection *, bfd_vma, bfd_signed_vma, bfd_vma));
58 static bfd_boolean elf32_arm_relocate_section
59   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
60            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
61 static asection * elf32_arm_gc_mark_hook
62   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
63            struct elf_link_hash_entry *, Elf_Internal_Sym *));
64 static bfd_boolean elf32_arm_gc_sweep_hook
65   PARAMS ((bfd *, struct bfd_link_info *, asection *,
66            const Elf_Internal_Rela *));
67 static bfd_boolean elf32_arm_check_relocs
68   PARAMS ((bfd *, struct bfd_link_info *, asection *,
69            const Elf_Internal_Rela *));
70 static bfd_boolean elf32_arm_find_nearest_line
71   PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
72            const char **, unsigned int *));
73 static bfd_boolean elf32_arm_adjust_dynamic_symbol
74   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static bfd_boolean elf32_arm_size_dynamic_sections
76   PARAMS ((bfd *, struct bfd_link_info *));
77 static bfd_boolean elf32_arm_finish_dynamic_symbol
78   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
79            Elf_Internal_Sym *));
80 static bfd_boolean elf32_arm_finish_dynamic_sections
81   PARAMS ((bfd *, struct bfd_link_info *));
82 static struct bfd_hash_entry * elf32_arm_link_hash_newfunc
83   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
84 #if USE_REL
85 static void arm_add_to_rel
86   PARAMS ((bfd *, bfd_byte *, reloc_howto_type *, bfd_signed_vma));
87 #endif
88 static bfd_boolean allocate_dynrelocs 
89   PARAMS ((struct elf_link_hash_entry *h, PTR inf));
90 static bfd_boolean create_got_section 
91   PARAMS ((bfd * dynobj, struct bfd_link_info * info));
92 static bfd_boolean elf32_arm_create_dynamic_sections 
93   PARAMS ((bfd * dynobj, struct bfd_link_info * info));
94 static enum elf_reloc_type_class elf32_arm_reloc_type_class
95   PARAMS ((const Elf_Internal_Rela *));
96 static bfd_boolean elf32_arm_object_p
97   PARAMS ((bfd *));
98
99 #ifndef ELFARM_NABI_C_INCLUDED
100 static void record_arm_to_thumb_glue
101   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
102 static void record_thumb_to_arm_glue
103   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
104 bfd_boolean bfd_elf32_arm_allocate_interworking_sections
105   PARAMS ((struct bfd_link_info *));
106 bfd_boolean bfd_elf32_arm_get_bfd_for_interworking
107   PARAMS ((bfd *, struct bfd_link_info *));
108 bfd_boolean bfd_elf32_arm_process_before_allocation
109   PARAMS ((bfd *, struct bfd_link_info *, int));
110 #endif
111
112
113 #define INTERWORK_FLAG(abfd)   (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK)
114
115 /* The linker script knows the section names for placement.
116    The entry_names are used to do simple name mangling on the stubs.
117    Given a function name, and its type, the stub can be found. The
118    name can be changed. The only requirement is the %s be present.  */
119 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
120 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
121
122 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
123 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
124
125 /* The name of the dynamic interpreter.  This is put in the .interp
126    section.  */
127 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
128
129 #ifdef FOUR_WORD_PLT
130
131 /* The size in bytes of the special first entry in the procedure
132    linkage table.  */
133 #define PLT_HEADER_SIZE 16
134
135 /* The size in bytes of an entry in the procedure linkage table.  */
136 #define PLT_ENTRY_SIZE 16
137
138 /* The first entry in a procedure linkage table looks like
139    this.  It is set up so that any shared library function that is
140    called before the relocation has been set up calls the dynamic
141    linker first.  */
142 static const bfd_vma elf32_arm_plt0_entry [PLT_HEADER_SIZE / 4] =
143   {
144     0xe52de004,         /* str   lr, [sp, #-4]! */
145     0xe59fe010,         /* ldr   lr, [pc, #16]  */
146     0xe08fe00e,         /* add   lr, pc, lr     */
147     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
148   };
149
150 /* Subsequent entries in a procedure linkage table look like
151    this.  */
152 static const bfd_vma elf32_arm_plt_entry [PLT_ENTRY_SIZE / 4] =
153   {
154     0xe28fc600,         /* add   ip, pc, #NN    */
155     0xe28cca00,         /* add   ip, ip, #NN    */
156     0xe5bcf000,         /* ldr   pc, [ip, #NN]! */
157     0x00000000,         /* unused               */
158   };
159
160 #else
161
162 /* The size in bytes of the special first entry in the procedure
163    linkage table.  */
164 #define PLT_HEADER_SIZE 20
165
166 /* The size in bytes of an entry in the procedure linkage table.  */
167 #define PLT_ENTRY_SIZE 12
168
169 /* The first entry in a procedure linkage table looks like
170    this.  It is set up so that any shared library function that is
171    called before the relocation has been set up calls the dynamic
172    linker first.  */
173 static const bfd_vma elf32_arm_plt0_entry [PLT_HEADER_SIZE / 4] =
174   {
175     0xe52de004,         /* str   lr, [sp, #-4]! */
176     0xe59fe004,         /* ldr   lr, [pc, #4]   */
177     0xe08fe00e,         /* add   lr, pc, lr     */
178     0xe5bef008,         /* ldr   pc, [lr, #8]!  */
179     0x00000000,         /* &GOT[0] - .          */
180   };
181
182 /* Subsequent entries in a procedure linkage table look like
183    this.  */
184 static const bfd_vma elf32_arm_plt_entry [PLT_ENTRY_SIZE / 4] =
185   {
186     0xe28fc600,         /* add   ip, pc, #0xNN00000 */
187     0xe28cca00,         /* add   ip, ip, #0xNN000   */
188     0xe5bcf000,         /* ldr   pc, [ip, #0xNNN]!  */
189   };
190
191 #endif
192
193 /* The ARM linker needs to keep track of the number of relocs that it
194    decides to copy in check_relocs for each symbol.  This is so that
195    it can discard PC relative relocs if it doesn't need them when
196    linking with -Bsymbolic.  We store the information in a field
197    extending the regular ELF linker hash table.  */
198
199 /* This structure keeps track of the number of PC relative relocs we
200    have copied for a given symbol.  */
201 struct elf32_arm_relocs_copied
202   {
203     /* Next section.  */
204     struct elf32_arm_relocs_copied * next;
205     /* A section in dynobj.  */
206     asection * section;
207     /* Number of relocs copied in this section.  */
208     bfd_size_type count;
209   };
210
211 /* Arm ELF linker hash entry.  */
212 struct elf32_arm_link_hash_entry
213   {
214     struct elf_link_hash_entry root;
215
216     /* Number of PC relative relocs copied for this symbol.  */
217     struct elf32_arm_relocs_copied * relocs_copied;
218   };
219
220 /* Traverse an arm ELF linker hash table.  */
221 #define elf32_arm_link_hash_traverse(table, func, info)                 \
222   (elf_link_hash_traverse                                               \
223    (&(table)->root,                                                     \
224     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
225     (info)))
226
227 /* Get the ARM elf linker hash table from a link_info structure.  */
228 #define elf32_arm_hash_table(info) \
229   ((struct elf32_arm_link_hash_table *) ((info)->hash))
230
231 /* ARM ELF linker hash table.  */
232 struct elf32_arm_link_hash_table
233   {
234     /* The main hash table.  */
235     struct elf_link_hash_table root;
236
237     /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
238     bfd_size_type thumb_glue_size;
239
240     /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
241     bfd_size_type arm_glue_size;
242
243     /* An arbitrary input BFD chosen to hold the glue sections.  */
244     bfd * bfd_of_glue_owner;
245
246     /* A boolean indicating whether knowledge of the ARM's pipeline
247        length should be applied by the linker.  */
248     int no_pipeline_knowledge;
249
250     /* Short-cuts to get to dynamic linker sections.  */
251     asection *sgot;
252     asection *sgotplt;
253     asection *srelgot;
254     asection *splt;
255     asection *srelplt;
256     asection *sdynbss;
257     asection *srelbss;
258
259     /* Small local sym to section mapping cache.  */
260     struct sym_sec_cache sym_sec;
261   };
262
263 /* Create an entry in an ARM ELF linker hash table.  */
264
265 static struct bfd_hash_entry *
266 elf32_arm_link_hash_newfunc (entry, table, string)
267      struct bfd_hash_entry * entry;
268      struct bfd_hash_table * table;
269      const char * string;
270 {
271   struct elf32_arm_link_hash_entry * ret =
272     (struct elf32_arm_link_hash_entry *) entry;
273
274   /* Allocate the structure if it has not already been allocated by a
275      subclass.  */
276   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
277     ret = ((struct elf32_arm_link_hash_entry *)
278            bfd_hash_allocate (table,
279                               sizeof (struct elf32_arm_link_hash_entry)));
280   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
281     return (struct bfd_hash_entry *) ret;
282
283   /* Call the allocation method of the superclass.  */
284   ret = ((struct elf32_arm_link_hash_entry *)
285          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
286                                      table, string));
287   if (ret != (struct elf32_arm_link_hash_entry *) NULL)
288     ret->relocs_copied = NULL;
289
290   return (struct bfd_hash_entry *) ret;
291 }
292
293 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
294    shortcuts to them in our hash table.  */
295
296 static bfd_boolean
297 create_got_section (dynobj, info)
298      bfd *dynobj;
299      struct bfd_link_info *info;
300 {
301   struct elf32_arm_link_hash_table *htab;
302
303   if (! _bfd_elf_create_got_section (dynobj, info))
304     return FALSE;
305
306   htab = elf32_arm_hash_table (info);
307   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
308   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
309   if (!htab->sgot || !htab->sgotplt)
310     abort ();
311
312   htab->srelgot = bfd_make_section (dynobj, ".rel.got");
313   if (htab->srelgot == NULL
314       || ! bfd_set_section_flags (dynobj, htab->srelgot,
315                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
316                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
317                                    | SEC_READONLY))
318       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
319     return FALSE;
320   return TRUE;
321 }
322
323 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
324    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
325    hash table.  */
326
327 static bfd_boolean
328 elf32_arm_create_dynamic_sections (dynobj, info)
329      bfd *dynobj;
330      struct bfd_link_info *info;
331 {
332   struct elf32_arm_link_hash_table *htab;
333
334   htab = elf32_arm_hash_table (info);
335   if (!htab->sgot && !create_got_section (dynobj, info))
336     return FALSE;
337
338   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
339     return FALSE;
340
341   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
342   htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
343   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
344   if (!info->shared)
345     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
346
347   if (!htab->splt || !htab->srelplt || !htab->sdynbss
348       || (!info->shared && !htab->srelbss))
349     abort ();
350
351   return TRUE;
352 }
353
354 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
355
356 static void
357 elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
358                                 struct elf_link_hash_entry *dir,
359                                 struct elf_link_hash_entry *ind)
360 {
361   struct elf32_arm_link_hash_entry *edir, *eind;
362
363   edir = (struct elf32_arm_link_hash_entry *) dir;
364   eind = (struct elf32_arm_link_hash_entry *) ind;
365
366   if (eind->relocs_copied != NULL)
367     {
368       if (edir->relocs_copied != NULL)
369         {
370           struct elf32_arm_relocs_copied **pp;
371           struct elf32_arm_relocs_copied *p;
372
373           if (ind->root.type == bfd_link_hash_indirect)
374             abort ();
375
376           /* Add reloc counts against the weak sym to the strong sym
377              list.  Merge any entries against the same section.  */
378           for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
379             {
380               struct elf32_arm_relocs_copied *q;
381
382               for (q = edir->relocs_copied; q != NULL; q = q->next)
383                 if (q->section == p->section)
384                   {
385                     q->count += p->count;
386                     *pp = p->next;
387                     break;
388                   }
389               if (q == NULL)
390                 pp = &p->next;
391             }
392           *pp = edir->relocs_copied;
393         }
394
395       edir->relocs_copied = eind->relocs_copied;
396       eind->relocs_copied = NULL;
397     }
398
399   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
400 }
401
402 /* Create an ARM elf linker hash table.  */
403
404 static struct bfd_link_hash_table *
405 elf32_arm_link_hash_table_create (abfd)
406      bfd *abfd;
407 {
408   struct elf32_arm_link_hash_table *ret;
409   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
410
411   ret = (struct elf32_arm_link_hash_table *) bfd_malloc (amt);
412   if (ret == (struct elf32_arm_link_hash_table *) NULL)
413     return NULL;
414
415   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
416                                       elf32_arm_link_hash_newfunc))
417     {
418       free (ret);
419       return NULL;
420     }
421
422   ret->sgot = NULL;
423   ret->sgotplt = NULL;
424   ret->srelgot = NULL;
425   ret->splt = NULL;
426   ret->srelplt = NULL;
427   ret->sdynbss = NULL;
428   ret->srelbss = NULL;
429   ret->thumb_glue_size = 0;
430   ret->arm_glue_size = 0;
431   ret->bfd_of_glue_owner = NULL;
432   ret->no_pipeline_knowledge = 0;
433   ret->sym_sec.abfd = NULL;
434
435   return &ret->root.root;
436 }
437
438 /* Locate the Thumb encoded calling stub for NAME.  */
439
440 static struct elf_link_hash_entry *
441 find_thumb_glue (link_info, name, input_bfd)
442      struct bfd_link_info *link_info;
443      const char *name;
444      bfd *input_bfd;
445 {
446   char *tmp_name;
447   struct elf_link_hash_entry *hash;
448   struct elf32_arm_link_hash_table *hash_table;
449
450   /* We need a pointer to the armelf specific hash table.  */
451   hash_table = elf32_arm_hash_table (link_info);
452
453   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
454                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
455
456   BFD_ASSERT (tmp_name);
457
458   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
459
460   hash = elf_link_hash_lookup
461     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
462
463   if (hash == NULL)
464     /* xgettext:c-format */
465     (*_bfd_error_handler) (_("%s: unable to find THUMB glue '%s' for `%s'"),
466                            bfd_archive_filename (input_bfd), tmp_name, name);
467
468   free (tmp_name);
469
470   return hash;
471 }
472
473 /* Locate the ARM encoded calling stub for NAME.  */
474
475 static struct elf_link_hash_entry *
476 find_arm_glue (link_info, name, input_bfd)
477      struct bfd_link_info *link_info;
478      const char *name;
479      bfd *input_bfd;
480 {
481   char *tmp_name;
482   struct elf_link_hash_entry *myh;
483   struct elf32_arm_link_hash_table *hash_table;
484
485   /* We need a pointer to the elfarm specific hash table.  */
486   hash_table = elf32_arm_hash_table (link_info);
487
488   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
489                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
490
491   BFD_ASSERT (tmp_name);
492
493   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
494
495   myh = elf_link_hash_lookup
496     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
497
498   if (myh == NULL)
499     /* xgettext:c-format */
500     (*_bfd_error_handler) (_("%s: unable to find ARM glue '%s' for `%s'"),
501                            bfd_archive_filename (input_bfd), tmp_name, name);
502
503   free (tmp_name);
504
505   return myh;
506 }
507
508 /* ARM->Thumb glue:
509
510    .arm
511    __func_from_arm:
512    ldr r12, __func_addr
513    bx  r12
514    __func_addr:
515    .word func    @ behave as if you saw a ARM_32 reloc.  */
516
517 #define ARM2THUMB_GLUE_SIZE 12
518 static const insn32 a2t1_ldr_insn = 0xe59fc000;
519 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
520 static const insn32 a2t3_func_addr_insn = 0x00000001;
521
522 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
523
524    .thumb                               .thumb
525    .align 2                             .align 2
526    __func_from_thumb:              __func_from_thumb:
527    bx pc                                push {r6, lr}
528    nop                                  ldr  r6, __func_addr
529    .arm                                         mov  lr, pc
530    __func_change_to_arm:                        bx   r6
531    b func                       .arm
532    __func_back_to_thumb:
533    ldmia r13! {r6, lr}
534    bx    lr
535    __func_addr:
536    .word        func  */
537
538 #define THUMB2ARM_GLUE_SIZE 8
539 static const insn16 t2a1_bx_pc_insn = 0x4778;
540 static const insn16 t2a2_noop_insn = 0x46c0;
541 static const insn32 t2a3_b_insn = 0xea000000;
542
543 #ifndef ELFARM_NABI_C_INCLUDED
544 bfd_boolean
545 bfd_elf32_arm_allocate_interworking_sections (info)
546      struct bfd_link_info * info;
547 {
548   asection * s;
549   bfd_byte * foo;
550   struct elf32_arm_link_hash_table * globals;
551
552   globals = elf32_arm_hash_table (info);
553
554   BFD_ASSERT (globals != NULL);
555
556   if (globals->arm_glue_size != 0)
557     {
558       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
559
560       s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
561                                    ARM2THUMB_GLUE_SECTION_NAME);
562
563       BFD_ASSERT (s != NULL);
564
565       foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
566                                     globals->arm_glue_size);
567
568       s->_raw_size = s->_cooked_size = globals->arm_glue_size;
569       s->contents = foo;
570     }
571
572   if (globals->thumb_glue_size != 0)
573     {
574       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
575
576       s = bfd_get_section_by_name
577         (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
578
579       BFD_ASSERT (s != NULL);
580
581       foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
582                                     globals->thumb_glue_size);
583
584       s->_raw_size = s->_cooked_size = globals->thumb_glue_size;
585       s->contents = foo;
586     }
587
588   return TRUE;
589 }
590
591 static void
592 record_arm_to_thumb_glue (link_info, h)
593      struct bfd_link_info * link_info;
594      struct elf_link_hash_entry * h;
595 {
596   const char * name = h->root.root.string;
597   asection * s;
598   char * tmp_name;
599   struct elf_link_hash_entry * myh;
600   struct bfd_link_hash_entry * bh;
601   struct elf32_arm_link_hash_table * globals;
602   bfd_vma val;
603
604   globals = elf32_arm_hash_table (link_info);
605
606   BFD_ASSERT (globals != NULL);
607   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
608
609   s = bfd_get_section_by_name
610     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
611
612   BFD_ASSERT (s != NULL);
613
614   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
615                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
616
617   BFD_ASSERT (tmp_name);
618
619   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
620
621   myh = elf_link_hash_lookup
622     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
623
624   if (myh != NULL)
625     {
626       /* We've already seen this guy.  */
627       free (tmp_name);
628       return;
629     }
630
631   /* The only trick here is using hash_table->arm_glue_size as the value. Even
632      though the section isn't allocated yet, this is where we will be putting
633      it.  */
634   bh = NULL;
635   val = globals->arm_glue_size + 1;
636   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
637                                     tmp_name, BSF_GLOBAL, s, val,
638                                     NULL, TRUE, FALSE, &bh);
639
640   free (tmp_name);
641
642   globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
643
644   return;
645 }
646
647 static void
648 record_thumb_to_arm_glue (link_info, h)
649      struct bfd_link_info *link_info;
650      struct elf_link_hash_entry *h;
651 {
652   const char *name = h->root.root.string;
653   asection *s;
654   char *tmp_name;
655   struct elf_link_hash_entry *myh;
656   struct bfd_link_hash_entry *bh;
657   struct elf32_arm_link_hash_table *hash_table;
658   char bind;
659   bfd_vma val;
660
661   hash_table = elf32_arm_hash_table (link_info);
662
663   BFD_ASSERT (hash_table != NULL);
664   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
665
666   s = bfd_get_section_by_name
667     (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
668
669   BFD_ASSERT (s != NULL);
670
671   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
672                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
673
674   BFD_ASSERT (tmp_name);
675
676   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
677
678   myh = elf_link_hash_lookup
679     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
680
681   if (myh != NULL)
682     {
683       /* We've already seen this guy.  */
684       free (tmp_name);
685       return;
686     }
687
688   bh = NULL;
689   val = hash_table->thumb_glue_size + 1;
690   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
691                                     tmp_name, BSF_GLOBAL, s, val,
692                                     NULL, TRUE, FALSE, &bh);
693
694   /* If we mark it 'Thumb', the disassembler will do a better job.  */
695   myh = (struct elf_link_hash_entry *) bh;
696   bind = ELF_ST_BIND (myh->type);
697   myh->type = ELF_ST_INFO (bind, STT_ARM_TFUNC);
698
699   free (tmp_name);
700
701 #define CHANGE_TO_ARM "__%s_change_to_arm"
702 #define BACK_FROM_ARM "__%s_back_from_arm"
703
704   /* Allocate another symbol to mark where we switch to Arm mode.  */
705   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
706                                   + strlen (CHANGE_TO_ARM) + 1);
707
708   BFD_ASSERT (tmp_name);
709
710   sprintf (tmp_name, CHANGE_TO_ARM, name);
711
712   bh = NULL;
713   val = hash_table->thumb_glue_size + 4,
714   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
715                                     tmp_name, BSF_LOCAL, s, val,
716                                     NULL, TRUE, FALSE, &bh);
717
718   free (tmp_name);
719
720   hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
721
722   return;
723 }
724
725 /* Add the glue sections to ABFD.  This function is called from the
726    linker scripts in ld/emultempl/{armelf}.em.  */
727
728 bfd_boolean
729 bfd_elf32_arm_add_glue_sections_to_bfd (abfd, info)
730      bfd *abfd;
731      struct bfd_link_info *info;
732 {
733   flagword flags;
734   asection *sec;
735
736   /* If we are only performing a partial
737      link do not bother adding the glue.  */
738   if (info->relocatable)
739     return TRUE;
740
741   sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
742
743   if (sec == NULL)
744     {
745       /* Note: we do not include the flag SEC_LINKER_CREATED, as this
746          will prevent elf_link_input_bfd() from processing the contents
747          of this section.  */
748       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
749
750       sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
751
752       if (sec == NULL
753           || !bfd_set_section_flags (abfd, sec, flags)
754           || !bfd_set_section_alignment (abfd, sec, 2))
755         return FALSE;
756
757       /* Set the gc mark to prevent the section from being removed by garbage
758          collection, despite the fact that no relocs refer to this section.  */
759       sec->gc_mark = 1;
760     }
761
762   sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
763
764   if (sec == NULL)
765     {
766       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
767
768       sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
769
770       if (sec == NULL
771           || !bfd_set_section_flags (abfd, sec, flags)
772           || !bfd_set_section_alignment (abfd, sec, 2))
773         return FALSE;
774
775       sec->gc_mark = 1;
776     }
777
778   return TRUE;
779 }
780
781 /* Select a BFD to be used to hold the sections used by the glue code.
782    This function is called from the linker scripts in ld/emultempl/
783    {armelf/pe}.em  */
784
785 bfd_boolean
786 bfd_elf32_arm_get_bfd_for_interworking (abfd, info)
787      bfd *abfd;
788      struct bfd_link_info *info;
789 {
790   struct elf32_arm_link_hash_table *globals;
791
792   /* If we are only performing a partial link
793      do not bother getting a bfd to hold the glue.  */
794   if (info->relocatable)
795     return TRUE;
796
797   globals = elf32_arm_hash_table (info);
798
799   BFD_ASSERT (globals != NULL);
800
801   if (globals->bfd_of_glue_owner != NULL)
802     return TRUE;
803
804   /* Save the bfd for later use.  */
805   globals->bfd_of_glue_owner = abfd;
806
807   return TRUE;
808 }
809
810 bfd_boolean
811 bfd_elf32_arm_process_before_allocation (abfd, link_info, no_pipeline_knowledge)
812      bfd *abfd;
813      struct bfd_link_info *link_info;
814      int no_pipeline_knowledge;
815 {
816   Elf_Internal_Shdr *symtab_hdr;
817   Elf_Internal_Rela *internal_relocs = NULL;
818   Elf_Internal_Rela *irel, *irelend;
819   bfd_byte *contents = NULL;
820
821   asection *sec;
822   struct elf32_arm_link_hash_table *globals;
823
824   /* If we are only performing a partial link do not bother
825      to construct any glue.  */
826   if (link_info->relocatable)
827     return TRUE;
828
829   /* Here we have a bfd that is to be included on the link.  We have a hook
830      to do reloc rummaging, before section sizes are nailed down.  */
831   globals = elf32_arm_hash_table (link_info);
832
833   BFD_ASSERT (globals != NULL);
834   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
835
836   globals->no_pipeline_knowledge = no_pipeline_knowledge;
837
838   /* Rummage around all the relocs and map the glue vectors.  */
839   sec = abfd->sections;
840
841   if (sec == NULL)
842     return TRUE;
843
844   for (; sec != NULL; sec = sec->next)
845     {
846       if (sec->reloc_count == 0)
847         continue;
848
849       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
850
851       /* Load the relocs.  */
852       internal_relocs
853         = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
854                                      (Elf_Internal_Rela *) NULL, FALSE);
855
856       if (internal_relocs == NULL)
857         goto error_return;
858
859       irelend = internal_relocs + sec->reloc_count;
860       for (irel = internal_relocs; irel < irelend; irel++)
861         {
862           long r_type;
863           unsigned long r_index;
864
865           struct elf_link_hash_entry *h;
866
867           r_type = ELF32_R_TYPE (irel->r_info);
868           r_index = ELF32_R_SYM (irel->r_info);
869
870           /* These are the only relocation types we care about.  */
871           if (   r_type != R_ARM_PC24
872               && r_type != R_ARM_THM_PC22)
873             continue;
874
875           /* Get the section contents if we haven't done so already.  */
876           if (contents == NULL)
877             {
878               /* Get cached copy if it exists.  */
879               if (elf_section_data (sec)->this_hdr.contents != NULL)
880                 contents = elf_section_data (sec)->this_hdr.contents;
881               else
882                 {
883                   /* Go get them off disk.  */
884                   contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
885                   if (contents == NULL)
886                     goto error_return;
887
888                   if (!bfd_get_section_contents (abfd, sec, contents,
889                                                  (file_ptr) 0, sec->_raw_size))
890                     goto error_return;
891                 }
892             }
893
894           /* If the relocation is not against a symbol it cannot concern us.  */
895           h = NULL;
896
897           /* We don't care about local symbols.  */
898           if (r_index < symtab_hdr->sh_info)
899             continue;
900
901           /* This is an external symbol.  */
902           r_index -= symtab_hdr->sh_info;
903           h = (struct elf_link_hash_entry *)
904             elf_sym_hashes (abfd)[r_index];
905
906           /* If the relocation is against a static symbol it must be within
907              the current section and so cannot be a cross ARM/Thumb relocation.  */
908           if (h == NULL)
909             continue;
910
911           switch (r_type)
912             {
913             case R_ARM_PC24:
914               /* This one is a call from arm code.  We need to look up
915                  the target of the call.  If it is a thumb target, we
916                  insert glue.  */
917               if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
918                 record_arm_to_thumb_glue (link_info, h);
919               break;
920
921             case R_ARM_THM_PC22:
922               /* This one is a call from thumb code.  We look
923                  up the target of the call.  If it is not a thumb
924                  target, we insert glue.  */
925               if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
926                 record_thumb_to_arm_glue (link_info, h);
927               break;
928
929             default:
930               break;
931             }
932         }
933
934       if (contents != NULL
935           && elf_section_data (sec)->this_hdr.contents != contents)
936         free (contents);
937       contents = NULL;
938
939       if (internal_relocs != NULL
940           && elf_section_data (sec)->relocs != internal_relocs)
941         free (internal_relocs);
942       internal_relocs = NULL;
943     }
944
945   return TRUE;
946
947 error_return:
948   if (contents != NULL
949       && elf_section_data (sec)->this_hdr.contents != contents)
950     free (contents);
951   if (internal_relocs != NULL
952       && elf_section_data (sec)->relocs != internal_relocs)
953     free (internal_relocs);
954
955   return FALSE;
956 }
957 #endif
958
959 /* The thumb form of a long branch is a bit finicky, because the offset
960    encoding is split over two fields, each in it's own instruction. They
961    can occur in any order. So given a thumb form of long branch, and an
962    offset, insert the offset into the thumb branch and return finished
963    instruction.
964
965    It takes two thumb instructions to encode the target address. Each has
966    11 bits to invest. The upper 11 bits are stored in one (identified by
967    H-0.. see below), the lower 11 bits are stored in the other (identified
968    by H-1).
969
970    Combine together and shifted left by 1 (it's a half word address) and
971    there you have it.
972
973    Op: 1111 = F,
974    H-0, upper address-0 = 000
975    Op: 1111 = F,
976    H-1, lower address-0 = 800
977
978    They can be ordered either way, but the arm tools I've seen always put
979    the lower one first. It probably doesn't matter. krk@cygnus.com
980
981    XXX:  Actually the order does matter.  The second instruction (H-1)
982    moves the computed address into the PC, so it must be the second one
983    in the sequence.  The problem, however is that whilst little endian code
984    stores the instructions in HI then LOW order, big endian code does the
985    reverse.  nickc@cygnus.com.  */
986
987 #define LOW_HI_ORDER      0xF800F000
988 #define HI_LOW_ORDER      0xF000F800
989
990 static insn32
991 insert_thumb_branch (br_insn, rel_off)
992      insn32 br_insn;
993      int rel_off;
994 {
995   unsigned int low_bits;
996   unsigned int high_bits;
997
998   BFD_ASSERT ((rel_off & 1) != 1);
999
1000   rel_off >>= 1;                                /* Half word aligned address.  */
1001   low_bits = rel_off & 0x000007FF;              /* The bottom 11 bits.  */
1002   high_bits = (rel_off >> 11) & 0x000007FF;     /* The top 11 bits.  */
1003
1004   if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
1005     br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
1006   else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
1007     br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
1008   else
1009     /* FIXME: abort is probably not the right call. krk@cygnus.com  */
1010     abort ();                   /* error - not a valid branch instruction form.  */
1011
1012   return br_insn;
1013 }
1014
1015 /* Thumb code calling an ARM function.  */
1016
1017 static int
1018 elf32_thumb_to_arm_stub (info, name, input_bfd, output_bfd, input_section,
1019                          hit_data, sym_sec, offset, addend, val)
1020      struct bfd_link_info * info;
1021      const char *           name;
1022      bfd *                  input_bfd;
1023      bfd *                  output_bfd;
1024      asection *             input_section;
1025      bfd_byte *             hit_data;
1026      asection *             sym_sec;
1027      bfd_vma                offset;
1028      bfd_signed_vma         addend;
1029      bfd_vma                val;
1030 {
1031   asection * s = 0;
1032   bfd_vma my_offset;
1033   unsigned long int tmp;
1034   long int ret_offset;
1035   struct elf_link_hash_entry * myh;
1036   struct elf32_arm_link_hash_table * globals;
1037
1038   myh = find_thumb_glue (info, name, input_bfd);
1039   if (myh == NULL)
1040     return FALSE;
1041
1042   globals = elf32_arm_hash_table (info);
1043
1044   BFD_ASSERT (globals != NULL);
1045   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1046
1047   my_offset = myh->root.u.def.value;
1048
1049   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1050                                THUMB2ARM_GLUE_SECTION_NAME);
1051
1052   BFD_ASSERT (s != NULL);
1053   BFD_ASSERT (s->contents != NULL);
1054   BFD_ASSERT (s->output_section != NULL);
1055
1056   if ((my_offset & 0x01) == 0x01)
1057     {
1058       if (sym_sec != NULL
1059           && sym_sec->owner != NULL
1060           && !INTERWORK_FLAG (sym_sec->owner))
1061         {
1062           (*_bfd_error_handler)
1063             (_("%s(%s): warning: interworking not enabled."),
1064              bfd_archive_filename (sym_sec->owner), name);
1065           (*_bfd_error_handler)
1066             (_("  first occurrence: %s: thumb call to arm"),
1067              bfd_archive_filename (input_bfd));
1068
1069           return FALSE;
1070         }
1071
1072       --my_offset;
1073       myh->root.u.def.value = my_offset;
1074
1075       bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
1076                   s->contents + my_offset);
1077
1078       bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
1079                   s->contents + my_offset + 2);
1080
1081       ret_offset =
1082         /* Address of destination of the stub.  */
1083         ((bfd_signed_vma) val)
1084         - ((bfd_signed_vma)
1085            /* Offset from the start of the current section to the start of the stubs.  */
1086            (s->output_offset
1087             /* Offset of the start of this stub from the start of the stubs.  */
1088             + my_offset
1089             /* Address of the start of the current section.  */
1090             + s->output_section->vma)
1091            /* The branch instruction is 4 bytes into the stub.  */
1092            + 4
1093            /* ARM branches work from the pc of the instruction + 8.  */
1094            + 8);
1095
1096       bfd_put_32 (output_bfd,
1097                   (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
1098                   s->contents + my_offset + 4);
1099     }
1100
1101   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
1102
1103   /* Now go back and fix up the original BL insn to point to here.  */
1104   ret_offset =
1105     /* Address of where the stub is located.  */
1106     (s->output_section->vma + s->output_offset + my_offset)
1107      /* Address of where the BL is located.  */
1108     - (input_section->output_section->vma + input_section->output_offset + offset)
1109     /* Addend in the relocation.  */
1110     - addend
1111     /* Biassing for PC-relative addressing.  */
1112     - 8;
1113
1114   tmp = bfd_get_32 (input_bfd, hit_data
1115                     - input_section->vma);
1116
1117   bfd_put_32 (output_bfd,
1118               (bfd_vma) insert_thumb_branch (tmp, ret_offset),
1119               hit_data - input_section->vma);
1120
1121   return TRUE;
1122 }
1123
1124 /* Arm code calling a Thumb function.  */
1125
1126 static int
1127 elf32_arm_to_thumb_stub (info, name, input_bfd, output_bfd, input_section,
1128                          hit_data, sym_sec, offset, addend, val)
1129      struct bfd_link_info * info;
1130      const char *           name;
1131      bfd *                  input_bfd;
1132      bfd *                  output_bfd;
1133      asection *             input_section;
1134      bfd_byte *             hit_data;
1135      asection *             sym_sec;
1136      bfd_vma                offset;
1137      bfd_signed_vma         addend;
1138      bfd_vma                val;
1139 {
1140   unsigned long int tmp;
1141   bfd_vma my_offset;
1142   asection * s;
1143   long int ret_offset;
1144   struct elf_link_hash_entry * myh;
1145   struct elf32_arm_link_hash_table * globals;
1146
1147   myh = find_arm_glue (info, name, input_bfd);
1148   if (myh == NULL)
1149     return FALSE;
1150
1151   globals = elf32_arm_hash_table (info);
1152
1153   BFD_ASSERT (globals != NULL);
1154   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1155
1156   my_offset = myh->root.u.def.value;
1157   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1158                                ARM2THUMB_GLUE_SECTION_NAME);
1159   BFD_ASSERT (s != NULL);
1160   BFD_ASSERT (s->contents != NULL);
1161   BFD_ASSERT (s->output_section != NULL);
1162
1163   if ((my_offset & 0x01) == 0x01)
1164     {
1165       if (sym_sec != NULL
1166           && sym_sec->owner != NULL
1167           && !INTERWORK_FLAG (sym_sec->owner))
1168         {
1169           (*_bfd_error_handler)
1170             (_("%s(%s): warning: interworking not enabled."),
1171              bfd_archive_filename (sym_sec->owner), name);
1172           (*_bfd_error_handler)
1173             (_("  first occurrence: %s: arm call to thumb"),
1174              bfd_archive_filename (input_bfd));
1175         }
1176
1177       --my_offset;
1178       myh->root.u.def.value = my_offset;
1179
1180       bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
1181                   s->contents + my_offset);
1182
1183       bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
1184                   s->contents + my_offset + 4);
1185
1186       /* It's a thumb address.  Add the low order bit.  */
1187       bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
1188                   s->contents + my_offset + 8);
1189     }
1190
1191   BFD_ASSERT (my_offset <= globals->arm_glue_size);
1192
1193   tmp = bfd_get_32 (input_bfd, hit_data);
1194   tmp = tmp & 0xFF000000;
1195
1196   /* Somehow these are both 4 too far, so subtract 8.  */
1197   ret_offset = (s->output_offset
1198                 + my_offset
1199                 + s->output_section->vma
1200                 - (input_section->output_offset
1201                    + input_section->output_section->vma
1202                    + offset + addend)
1203                 - 8);
1204
1205   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
1206
1207   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
1208
1209   return TRUE;
1210 }
1211
1212 /* Perform a relocation as part of a final link.  */
1213
1214 static bfd_reloc_status_type
1215 elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
1216                                input_section, contents, rel, value,
1217                                info, sym_sec, sym_name, sym_flags, h)
1218      reloc_howto_type *     howto;
1219      bfd *                  input_bfd;
1220      bfd *                  output_bfd;
1221      asection *             input_section;
1222      bfd_byte *             contents;
1223      Elf_Internal_Rela *    rel;
1224      bfd_vma                value;
1225      struct bfd_link_info * info;
1226      asection *             sym_sec;
1227      const char *           sym_name;
1228      int                    sym_flags;
1229      struct elf_link_hash_entry * h;
1230 {
1231   unsigned long                 r_type = howto->type;
1232   unsigned long                 r_symndx;
1233   bfd_byte *                    hit_data = contents + rel->r_offset;
1234   bfd *                         dynobj = NULL;
1235   Elf_Internal_Shdr *           symtab_hdr;
1236   struct elf_link_hash_entry ** sym_hashes;
1237   bfd_vma *                     local_got_offsets;
1238   asection *                    sgot = NULL;
1239   asection *                    splt = NULL;
1240   asection *                    sreloc = NULL;
1241   bfd_vma                       addend;
1242   bfd_signed_vma                signed_addend;
1243   struct elf32_arm_link_hash_table * globals;
1244
1245   /* If the start address has been set, then set the EF_ARM_HASENTRY
1246      flag.  Setting this more than once is redundant, but the cost is
1247      not too high, and it keeps the code simple.
1248
1249      The test is done  here, rather than somewhere else, because the
1250      start address is only set just before the final link commences.
1251
1252      Note - if the user deliberately sets a start address of 0, the
1253      flag will not be set.  */
1254   if (bfd_get_start_address (output_bfd) != 0)
1255     elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
1256
1257   globals = elf32_arm_hash_table (info);
1258
1259   dynobj = elf_hash_table (info)->dynobj;
1260   if (dynobj)
1261     {
1262       sgot = bfd_get_section_by_name (dynobj, ".got");
1263       splt = bfd_get_section_by_name (dynobj, ".plt");
1264     }
1265   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1266   sym_hashes = elf_sym_hashes (input_bfd);
1267   local_got_offsets = elf_local_got_offsets (input_bfd);
1268   r_symndx = ELF32_R_SYM (rel->r_info);
1269
1270 #if USE_REL
1271   addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
1272
1273   if (addend & ((howto->src_mask + 1) >> 1))
1274     {
1275       signed_addend = -1;
1276       signed_addend &= ~ howto->src_mask;
1277       signed_addend |= addend;
1278     }
1279   else
1280     signed_addend = addend;
1281 #else
1282   addend = signed_addend = rel->r_addend;
1283 #endif
1284
1285   switch (r_type)
1286     {
1287     case R_ARM_NONE:
1288       return bfd_reloc_ok;
1289
1290     case R_ARM_PC24:
1291     case R_ARM_ABS32:
1292     case R_ARM_REL32:
1293 #ifndef OLD_ARM_ABI
1294     case R_ARM_XPC25:
1295 #endif
1296     case R_ARM_PLT32:
1297       /* r_symndx will be zero only for relocs against symbols
1298          from removed linkonce sections, or sections discarded by
1299          a linker script.  */
1300       if (r_symndx == 0)
1301         return bfd_reloc_ok;
1302
1303       /* Handle relocations which should use the PLT entry.  ABS32/REL32
1304          will use the symbol's value, which may point to a PLT entry, but we
1305          don't need to handle that here.  If we created a PLT entry, all
1306          branches in this object should go to it.  */
1307       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
1308           && h != NULL
1309           && splt != NULL
1310           && h->plt.offset != (bfd_vma) -1)
1311         {
1312           /* If we've created a .plt section, and assigned a PLT entry to
1313              this function, it should not be known to bind locally.  If
1314              it were, we would have cleared the PLT entry.  */
1315           BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1316
1317           value = (splt->output_section->vma
1318                    + splt->output_offset
1319                    + h->plt.offset);
1320           return _bfd_final_link_relocate (howto, input_bfd, input_section,
1321                                            contents, rel->r_offset, value,
1322                                            (bfd_vma) 0);
1323         }
1324
1325       /* When generating a shared object, these relocations are copied
1326          into the output file to be resolved at run time.  */
1327       if (info->shared
1328           && (input_section->flags & SEC_ALLOC)
1329           && (r_type != R_ARM_REL32
1330               || !SYMBOL_CALLS_LOCAL (info, h))
1331           && (h == NULL
1332               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1333               || h->root.type != bfd_link_hash_undefweak)
1334           && r_type != R_ARM_PC24
1335           && r_type != R_ARM_PLT32)
1336         {
1337           Elf_Internal_Rela outrel;
1338           bfd_byte *loc;
1339           bfd_boolean skip, relocate;
1340
1341           if (sreloc == NULL)
1342             {
1343               const char * name;
1344
1345               name = (bfd_elf_string_from_elf_section
1346                       (input_bfd,
1347                        elf_elfheader (input_bfd)->e_shstrndx,
1348                        elf_section_data (input_section)->rel_hdr.sh_name));
1349               if (name == NULL)
1350                 return bfd_reloc_notsupported;
1351
1352               BFD_ASSERT (strncmp (name, ".rel", 4) == 0
1353                           && strcmp (bfd_get_section_name (input_bfd,
1354                                                            input_section),
1355                                      name + 4) == 0);
1356
1357               sreloc = bfd_get_section_by_name (dynobj, name);
1358               BFD_ASSERT (sreloc != NULL);
1359             }
1360
1361           skip = FALSE;
1362           relocate = FALSE;
1363
1364           outrel.r_offset =
1365             _bfd_elf_section_offset (output_bfd, info, input_section,
1366                                      rel->r_offset);
1367           if (outrel.r_offset == (bfd_vma) -1)
1368             skip = TRUE;
1369           else if (outrel.r_offset == (bfd_vma) -2)
1370             skip = TRUE, relocate = TRUE;
1371           outrel.r_offset += (input_section->output_section->vma
1372                               + input_section->output_offset);
1373
1374           if (skip)
1375             memset (&outrel, 0, sizeof outrel);
1376           else if (h != NULL
1377                    && h->dynindx != -1
1378                    && (!info->shared
1379                        || !info->symbolic
1380                        || (h->elf_link_hash_flags
1381                            & ELF_LINK_HASH_DEF_REGULAR) == 0))
1382             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1383           else
1384             {
1385               /* This symbol is local, or marked to become local.  */
1386               relocate = TRUE;
1387               outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1388             }
1389
1390           loc = sreloc->contents;
1391           loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
1392           bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1393
1394           /* If this reloc is against an external symbol, we do not want to
1395              fiddle with the addend.  Otherwise, we need to include the symbol
1396              value so that it becomes an addend for the dynamic reloc.  */
1397           if (! relocate)
1398             return bfd_reloc_ok;
1399
1400           return _bfd_final_link_relocate (howto, input_bfd, input_section,
1401                                            contents, rel->r_offset, value,
1402                                            (bfd_vma) 0);
1403         }
1404       else switch (r_type)
1405         {
1406 #ifndef OLD_ARM_ABI
1407         case R_ARM_XPC25:         /* Arm BLX instruction.  */
1408 #endif
1409         case R_ARM_PC24:          /* Arm B/BL instruction */
1410         case R_ARM_PLT32:
1411 #ifndef OLD_ARM_ABI
1412           if (r_type == R_ARM_XPC25)
1413             {
1414               /* Check for Arm calling Arm function.  */
1415               /* FIXME: Should we translate the instruction into a BL
1416                  instruction instead ?  */
1417               if (sym_flags != STT_ARM_TFUNC)
1418                 (*_bfd_error_handler) (_("\
1419 %s: Warning: Arm BLX instruction targets Arm function '%s'."),
1420                                        bfd_archive_filename (input_bfd),
1421                                        h ? h->root.root.string : "(local)");
1422             }
1423           else
1424 #endif
1425             {
1426               /* Check for Arm calling Thumb function.  */
1427               if (sym_flags == STT_ARM_TFUNC)
1428                 {
1429                   elf32_arm_to_thumb_stub (info, sym_name, input_bfd, output_bfd,
1430                                            input_section, hit_data, sym_sec, rel->r_offset,
1431                                            signed_addend, value);
1432                   return bfd_reloc_ok;
1433                 }
1434             }
1435
1436           if (   strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1437               || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0)
1438             {
1439               /* The old way of doing things.  Trearing the addend as a
1440                  byte sized field and adding in the pipeline offset.  */
1441               value -= (input_section->output_section->vma
1442                         + input_section->output_offset);
1443               value -= rel->r_offset;
1444               value += addend;
1445
1446               if (! globals->no_pipeline_knowledge)
1447                 value -= 8;
1448             }
1449           else
1450             {
1451               /* The ARM ELF ABI says that this reloc is computed as: S - P + A
1452                  where:
1453                   S is the address of the symbol in the relocation.
1454                   P is address of the instruction being relocated.
1455                   A is the addend (extracted from the instruction) in bytes.
1456
1457                  S is held in 'value'.
1458                  P is the base address of the section containing the instruction
1459                    plus the offset of the reloc into that section, ie:
1460                      (input_section->output_section->vma +
1461                       input_section->output_offset +
1462                       rel->r_offset).
1463                  A is the addend, converted into bytes, ie:
1464                      (signed_addend * 4)
1465
1466                  Note: None of these operations have knowledge of the pipeline
1467                  size of the processor, thus it is up to the assembler to encode
1468                  this information into the addend.  */
1469               value -= (input_section->output_section->vma
1470                         + input_section->output_offset);
1471               value -= rel->r_offset;
1472               value += (signed_addend << howto->size);
1473
1474               /* Previous versions of this code also used to add in the pipeline
1475                  offset here.  This is wrong because the linker is not supposed
1476                  to know about such things, and one day it might change.  In order
1477                  to support old binaries that need the old behaviour however, so
1478                  we attempt to detect which ABI was used to create the reloc.  */
1479               if (! globals->no_pipeline_knowledge)
1480                 {
1481                   Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
1482
1483                   i_ehdrp = elf_elfheader (input_bfd);
1484
1485                   if (i_ehdrp->e_ident[EI_OSABI] == 0)
1486                     value -= 8;
1487                 }
1488             }
1489
1490           signed_addend = value;
1491           signed_addend >>= howto->rightshift;
1492
1493           /* It is not an error for an undefined weak reference to be
1494              out of range.  Any program that branches to such a symbol
1495              is going to crash anyway, so there is no point worrying
1496              about getting the destination exactly right.  */
1497           if (! h || h->root.type != bfd_link_hash_undefweak)
1498             {
1499               /* Perform a signed range check.  */
1500               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
1501                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
1502                 return bfd_reloc_overflow;
1503             }
1504
1505 #ifndef OLD_ARM_ABI
1506           /* If necessary set the H bit in the BLX instruction.  */
1507           if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
1508             value = (signed_addend & howto->dst_mask)
1509               | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
1510               | (1 << 24);
1511           else
1512 #endif
1513             value = (signed_addend & howto->dst_mask)
1514               | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
1515           break;
1516
1517         case R_ARM_ABS32:
1518           value += addend;
1519           if (sym_flags == STT_ARM_TFUNC)
1520             value |= 1;
1521           break;
1522
1523         case R_ARM_REL32:
1524           value -= (input_section->output_section->vma
1525                     + input_section->output_offset + rel->r_offset);
1526           value += addend;
1527           break;
1528         }
1529
1530       bfd_put_32 (input_bfd, value, hit_data);
1531       return bfd_reloc_ok;
1532
1533     case R_ARM_ABS8:
1534       value += addend;
1535       if ((long) value > 0x7f || (long) value < -0x80)
1536         return bfd_reloc_overflow;
1537
1538       bfd_put_8 (input_bfd, value, hit_data);
1539       return bfd_reloc_ok;
1540
1541     case R_ARM_ABS16:
1542       value += addend;
1543
1544       if ((long) value > 0x7fff || (long) value < -0x8000)
1545         return bfd_reloc_overflow;
1546
1547       bfd_put_16 (input_bfd, value, hit_data);
1548       return bfd_reloc_ok;
1549
1550     case R_ARM_ABS12:
1551       /* Support ldr and str instruction for the arm */
1552       /* Also thumb b (unconditional branch).  ??? Really?  */
1553       value += addend;
1554
1555       if ((long) value > 0x7ff || (long) value < -0x800)
1556         return bfd_reloc_overflow;
1557
1558       value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
1559       bfd_put_32 (input_bfd, value, hit_data);
1560       return bfd_reloc_ok;
1561
1562     case R_ARM_THM_ABS5:
1563       /* Support ldr and str instructions for the thumb.  */
1564 #if USE_REL
1565       /* Need to refetch addend.  */
1566       addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1567       /* ??? Need to determine shift amount from operand size.  */
1568       addend >>= howto->rightshift;
1569 #endif
1570       value += addend;
1571
1572       /* ??? Isn't value unsigned?  */
1573       if ((long) value > 0x1f || (long) value < -0x10)
1574         return bfd_reloc_overflow;
1575
1576       /* ??? Value needs to be properly shifted into place first.  */
1577       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
1578       bfd_put_16 (input_bfd, value, hit_data);
1579       return bfd_reloc_ok;
1580
1581 #ifndef OLD_ARM_ABI
1582     case R_ARM_THM_XPC22:
1583 #endif
1584     case R_ARM_THM_PC22:
1585       /* Thumb BL (branch long instruction).  */
1586       {
1587         bfd_vma relocation;
1588         bfd_boolean overflow = FALSE;
1589         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
1590         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
1591         bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
1592         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1593         bfd_vma check;
1594         bfd_signed_vma signed_check;
1595
1596 #if USE_REL
1597         /* Need to refetch the addend and squish the two 11 bit pieces
1598            together.  */
1599         {
1600           bfd_vma upper = upper_insn & 0x7ff;
1601           bfd_vma lower = lower_insn & 0x7ff;
1602           upper = (upper ^ 0x400) - 0x400; /* Sign extend.  */
1603           addend = (upper << 12) | (lower << 1);
1604           signed_addend = addend;
1605         }
1606 #endif
1607 #ifndef OLD_ARM_ABI
1608         if (r_type == R_ARM_THM_XPC22)
1609           {
1610             /* Check for Thumb to Thumb call.  */
1611             /* FIXME: Should we translate the instruction into a BL
1612                instruction instead ?  */
1613             if (sym_flags == STT_ARM_TFUNC)
1614               (*_bfd_error_handler) (_("\
1615 %s: Warning: Thumb BLX instruction targets thumb function '%s'."),
1616                                      bfd_archive_filename (input_bfd),
1617                                      h ? h->root.root.string : "(local)");
1618           }
1619         else
1620 #endif
1621           {
1622             /* If it is not a call to Thumb, assume call to Arm.
1623                If it is a call relative to a section name, then it is not a
1624                function call at all, but rather a long jump.  */
1625             if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION)
1626               {
1627                 if (elf32_thumb_to_arm_stub
1628                     (info, sym_name, input_bfd, output_bfd, input_section,
1629                      hit_data, sym_sec, rel->r_offset, signed_addend, value))
1630                   return bfd_reloc_ok;
1631                 else
1632                   return bfd_reloc_dangerous;
1633               }
1634           }
1635
1636         relocation = value + signed_addend;
1637
1638         relocation -= (input_section->output_section->vma
1639                        + input_section->output_offset
1640                        + rel->r_offset);
1641
1642         if (! globals->no_pipeline_knowledge)
1643           {
1644             Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form.  */
1645
1646             i_ehdrp = elf_elfheader (input_bfd);
1647
1648             /* Previous versions of this code also used to add in the pipline
1649                offset here.  This is wrong because the linker is not supposed
1650                to know about such things, and one day it might change.  In order
1651                to support old binaries that need the old behaviour however, so
1652                we attempt to detect which ABI was used to create the reloc.  */
1653             if (   strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1654                 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0
1655                 || i_ehdrp->e_ident[EI_OSABI] == 0)
1656               relocation += 4;
1657           }
1658
1659         check = relocation >> howto->rightshift;
1660
1661         /* If this is a signed value, the rightshift just dropped
1662            leading 1 bits (assuming twos complement).  */
1663         if ((bfd_signed_vma) relocation >= 0)
1664           signed_check = check;
1665         else
1666           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
1667
1668         /* Assumes two's complement.  */
1669         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1670           overflow = TRUE;
1671
1672 #ifndef OLD_ARM_ABI
1673         if (r_type == R_ARM_THM_XPC22
1674             && ((lower_insn & 0x1800) == 0x0800))
1675           /* For a BLX instruction, make sure that the relocation is rounded up
1676              to a word boundary.  This follows the semantics of the instruction
1677              which specifies that bit 1 of the target address will come from bit
1678              1 of the base address.  */
1679           relocation = (relocation + 2) & ~ 3;
1680 #endif
1681         /* Put RELOCATION back into the insn.  */
1682         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
1683         lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
1684
1685         /* Put the relocated value back in the object file:  */
1686         bfd_put_16 (input_bfd, upper_insn, hit_data);
1687         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
1688
1689         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
1690       }
1691       break;
1692
1693     case R_ARM_THM_PC11:
1694       /* Thumb B (branch) instruction).  */
1695       {
1696         bfd_signed_vma relocation;
1697         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1698         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1699         bfd_signed_vma signed_check;
1700
1701 #if USE_REL
1702         /* Need to refetch addend.  */
1703         addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1704         if (addend & ((howto->src_mask + 1) >> 1))
1705           {
1706             signed_addend = -1;
1707             signed_addend &= ~ howto->src_mask;
1708             signed_addend |= addend;
1709           }
1710         else
1711           signed_addend = addend;
1712         /* The value in the insn has been right shifted.  We need to
1713            undo this, so that we can perform the address calculation
1714            in terms of bytes.  */
1715         signed_addend <<= howto->rightshift;
1716 #endif
1717         relocation = value + signed_addend;
1718
1719         relocation -= (input_section->output_section->vma
1720                        + input_section->output_offset
1721                        + rel->r_offset);
1722
1723         relocation >>= howto->rightshift;
1724         signed_check = relocation;
1725         relocation &= howto->dst_mask;
1726         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
1727
1728         bfd_put_16 (input_bfd, relocation, hit_data);
1729
1730         /* Assumes two's complement.  */
1731         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1732           return bfd_reloc_overflow;
1733
1734         return bfd_reloc_ok;
1735       }
1736
1737     case R_ARM_GNU_VTINHERIT:
1738     case R_ARM_GNU_VTENTRY:
1739       return bfd_reloc_ok;
1740
1741     case R_ARM_COPY:
1742       return bfd_reloc_notsupported;
1743
1744     case R_ARM_GLOB_DAT:
1745       return bfd_reloc_notsupported;
1746
1747     case R_ARM_JUMP_SLOT:
1748       return bfd_reloc_notsupported;
1749
1750     case R_ARM_RELATIVE:
1751       return bfd_reloc_notsupported;
1752
1753     case R_ARM_GOTOFF:
1754       /* Relocation is relative to the start of the
1755          global offset table.  */
1756
1757       BFD_ASSERT (sgot != NULL);
1758       if (sgot == NULL)
1759         return bfd_reloc_notsupported;
1760
1761       /* If we are addressing a Thumb function, we need to adjust the
1762          address by one, so that attempts to call the function pointer will
1763          correctly interpret it as Thumb code.  */
1764       if (sym_flags == STT_ARM_TFUNC)
1765         value += 1;
1766
1767       /* Note that sgot->output_offset is not involved in this
1768          calculation.  We always want the start of .got.  If we
1769          define _GLOBAL_OFFSET_TABLE in a different way, as is
1770          permitted by the ABI, we might have to change this
1771          calculation.  */
1772       value -= sgot->output_section->vma;
1773       return _bfd_final_link_relocate (howto, input_bfd, input_section,
1774                                        contents, rel->r_offset, value,
1775                                        (bfd_vma) 0);
1776
1777     case R_ARM_GOTPC:
1778       /* Use global offset table as symbol value.  */
1779       BFD_ASSERT (sgot != NULL);
1780
1781       if (sgot == NULL)
1782         return bfd_reloc_notsupported;
1783
1784       value = sgot->output_section->vma;
1785       return _bfd_final_link_relocate (howto, input_bfd, input_section,
1786                                        contents, rel->r_offset, value,
1787                                        (bfd_vma) 0);
1788
1789     case R_ARM_GOT32:
1790       /* Relocation is to the entry for this symbol in the
1791          global offset table.  */
1792       if (sgot == NULL)
1793         return bfd_reloc_notsupported;
1794
1795       if (h != NULL)
1796         {
1797           bfd_vma off;
1798           bfd_boolean dyn;
1799
1800           off = h->got.offset;
1801           BFD_ASSERT (off != (bfd_vma) -1);
1802           dyn = globals->root.dynamic_sections_created;
1803
1804           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1805               || (info->shared
1806                   && SYMBOL_REFERENCES_LOCAL (info, h))
1807               || (ELF_ST_VISIBILITY (h->other)
1808                   && h->root.type == bfd_link_hash_undefweak))
1809             {
1810               /* This is actually a static link, or it is a -Bsymbolic link
1811                  and the symbol is defined locally.  We must initialize this
1812                  entry in the global offset table.  Since the offset must
1813                  always be a multiple of 4, we use the least significant bit
1814                  to record whether we have initialized it already.
1815
1816                  When doing a dynamic link, we create a .rel.got relocation
1817                  entry to initialize the value.  This is done in the
1818                  finish_dynamic_symbol routine.  */
1819               if ((off & 1) != 0)
1820                 off &= ~1;
1821               else
1822                 {
1823                   /* If we are addressing a Thumb function, we need to
1824                      adjust the address by one, so that attempts to
1825                      call the function pointer will correctly
1826                      interpret it as Thumb code.  */
1827                   if (sym_flags == STT_ARM_TFUNC)
1828                     value |= 1;
1829
1830                   bfd_put_32 (output_bfd, value, sgot->contents + off);
1831                   h->got.offset |= 1;
1832                 }
1833             }
1834
1835           value = sgot->output_offset + off;
1836         }
1837       else
1838         {
1839           bfd_vma off;
1840
1841           BFD_ASSERT (local_got_offsets != NULL &&
1842                       local_got_offsets[r_symndx] != (bfd_vma) -1);
1843
1844           off = local_got_offsets[r_symndx];
1845
1846           /* The offset must always be a multiple of 4.  We use the
1847              least significant bit to record whether we have already
1848              generated the necessary reloc.  */
1849           if ((off & 1) != 0)
1850             off &= ~1;
1851           else
1852             {
1853               bfd_put_32 (output_bfd, value, sgot->contents + off);
1854
1855               if (info->shared)
1856                 {
1857                   asection * srelgot;
1858                   Elf_Internal_Rela outrel;
1859                   bfd_byte *loc;
1860
1861                   srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1862                   BFD_ASSERT (srelgot != NULL);
1863
1864                   outrel.r_offset = (sgot->output_section->vma
1865                                      + sgot->output_offset
1866                                      + off);
1867                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1868                   loc = srelgot->contents;
1869                   loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
1870                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1871                 }
1872
1873               local_got_offsets[r_symndx] |= 1;
1874             }
1875
1876           value = sgot->output_offset + off;
1877         }
1878
1879       return _bfd_final_link_relocate (howto, input_bfd, input_section,
1880                                        contents, rel->r_offset, value,
1881                                        (bfd_vma) 0);
1882
1883     case R_ARM_SBREL32:
1884       return bfd_reloc_notsupported;
1885
1886     case R_ARM_AMP_VCALL9:
1887       return bfd_reloc_notsupported;
1888
1889     case R_ARM_RSBREL32:
1890       return bfd_reloc_notsupported;
1891
1892     case R_ARM_THM_RPC22:
1893       return bfd_reloc_notsupported;
1894
1895     case R_ARM_RREL32:
1896       return bfd_reloc_notsupported;
1897
1898     case R_ARM_RABS32:
1899       return bfd_reloc_notsupported;
1900
1901     case R_ARM_RPC24:
1902       return bfd_reloc_notsupported;
1903
1904     case R_ARM_RBASE:
1905       return bfd_reloc_notsupported;
1906
1907     default:
1908       return bfd_reloc_notsupported;
1909     }
1910 }
1911
1912 #if USE_REL
1913 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
1914 static void
1915 arm_add_to_rel (abfd, address, howto, increment)
1916      bfd *              abfd;
1917      bfd_byte *         address;
1918      reloc_howto_type * howto;
1919      bfd_signed_vma     increment;
1920 {
1921   bfd_signed_vma addend;
1922
1923   if (howto->type == R_ARM_THM_PC22)
1924     {
1925       int upper_insn, lower_insn;
1926       int upper, lower;
1927
1928       upper_insn = bfd_get_16 (abfd, address);
1929       lower_insn = bfd_get_16 (abfd, address + 2);
1930       upper = upper_insn & 0x7ff;
1931       lower = lower_insn & 0x7ff;
1932
1933       addend = (upper << 12) | (lower << 1);
1934       addend += increment;
1935       addend >>= 1;
1936
1937       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
1938       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
1939
1940       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
1941       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
1942     }
1943   else
1944     {
1945       bfd_vma        contents;
1946
1947       contents = bfd_get_32 (abfd, address);
1948
1949       /* Get the (signed) value from the instruction.  */
1950       addend = contents & howto->src_mask;
1951       if (addend & ((howto->src_mask + 1) >> 1))
1952         {
1953           bfd_signed_vma mask;
1954
1955           mask = -1;
1956           mask &= ~ howto->src_mask;
1957           addend |= mask;
1958         }
1959
1960       /* Add in the increment, (which is a byte value).  */
1961       switch (howto->type)
1962         {
1963         default:
1964           addend += increment;
1965           break;
1966
1967         case R_ARM_PC24:
1968           addend <<= howto->size;
1969           addend += increment;
1970
1971           /* Should we check for overflow here ?  */
1972
1973           /* Drop any undesired bits.  */
1974           addend >>= howto->rightshift;
1975           break;
1976         }
1977
1978       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
1979
1980       bfd_put_32 (abfd, contents, address);
1981     }
1982 }
1983 #endif /* USE_REL */
1984
1985 /* Relocate an ARM ELF section.  */
1986 static bfd_boolean
1987 elf32_arm_relocate_section (output_bfd, info, input_bfd, input_section,
1988                             contents, relocs, local_syms, local_sections)
1989      bfd *output_bfd;
1990      struct bfd_link_info *info;
1991      bfd *input_bfd;
1992      asection *input_section;
1993      bfd_byte *contents;
1994      Elf_Internal_Rela *relocs;
1995      Elf_Internal_Sym *local_syms;
1996      asection **local_sections;
1997 {
1998   Elf_Internal_Shdr *symtab_hdr;
1999   struct elf_link_hash_entry **sym_hashes;
2000   Elf_Internal_Rela *rel;
2001   Elf_Internal_Rela *relend;
2002   const char *name;
2003
2004 #if !USE_REL
2005   if (info->relocatable)
2006     return TRUE;
2007 #endif
2008
2009   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2010   sym_hashes = elf_sym_hashes (input_bfd);
2011
2012   rel = relocs;
2013   relend = relocs + input_section->reloc_count;
2014   for (; rel < relend; rel++)
2015     {
2016       int                          r_type;
2017       reloc_howto_type *           howto;
2018       unsigned long                r_symndx;
2019       Elf_Internal_Sym *           sym;
2020       asection *                   sec;
2021       struct elf_link_hash_entry * h;
2022       bfd_vma                      relocation;
2023       bfd_reloc_status_type        r;
2024       arelent                      bfd_reloc;
2025
2026       r_symndx = ELF32_R_SYM (rel->r_info);
2027       r_type   = ELF32_R_TYPE (rel->r_info);
2028
2029       if (   r_type == R_ARM_GNU_VTENTRY
2030           || r_type == R_ARM_GNU_VTINHERIT)
2031         continue;
2032
2033       elf32_arm_info_to_howto (input_bfd, & bfd_reloc, rel);
2034       howto = bfd_reloc.howto;
2035
2036 #if USE_REL
2037       if (info->relocatable)
2038         {
2039           /* This is a relocatable link.  We don't have to change
2040              anything, unless the reloc is against a section symbol,
2041              in which case we have to adjust according to where the
2042              section symbol winds up in the output section.  */
2043           if (r_symndx < symtab_hdr->sh_info)
2044             {
2045               sym = local_syms + r_symndx;
2046               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2047                 {
2048                   sec = local_sections[r_symndx];
2049                   arm_add_to_rel (input_bfd, contents + rel->r_offset,
2050                                   howto,
2051                                   (bfd_signed_vma) (sec->output_offset
2052                                                     + sym->st_value));
2053                 }
2054             }
2055
2056           continue;
2057         }
2058 #endif
2059
2060       /* This is a final link.  */
2061       h = NULL;
2062       sym = NULL;
2063       sec = NULL;
2064
2065       if (r_symndx < symtab_hdr->sh_info)
2066         {
2067           sym = local_syms + r_symndx;
2068           sec = local_sections[r_symndx];
2069 #if USE_REL
2070           relocation = (sec->output_section->vma
2071                         + sec->output_offset
2072                         + sym->st_value);
2073           if ((sec->flags & SEC_MERGE)
2074                    && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2075             {
2076               asection *msec;
2077               bfd_vma addend, value;
2078
2079               if (howto->rightshift)
2080                 {
2081                   (*_bfd_error_handler)
2082                     (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
2083                      bfd_archive_filename (input_bfd),
2084                      bfd_get_section_name (input_bfd, input_section),
2085                      (long) rel->r_offset, howto->name);
2086                   return FALSE;
2087                 }
2088
2089               value = bfd_get_32 (input_bfd, contents + rel->r_offset);
2090
2091               /* Get the (signed) value from the instruction.  */
2092               addend = value & howto->src_mask;
2093               if (addend & ((howto->src_mask + 1) >> 1))
2094                 {
2095                   bfd_signed_vma mask;
2096
2097                   mask = -1;
2098                   mask &= ~ howto->src_mask;
2099                   addend |= mask;
2100                 }
2101               msec = sec;
2102               addend =
2103                 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
2104                 - relocation;
2105               addend += msec->output_section->vma + msec->output_offset;
2106               value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
2107               bfd_put_32 (input_bfd, value, contents + rel->r_offset);
2108             }
2109 #else
2110           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2111 #endif
2112         }
2113       else
2114         {
2115           bfd_boolean warned;
2116           bfd_boolean unresolved_reloc;
2117
2118           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2119                                    r_symndx, symtab_hdr, sym_hashes,
2120                                    h, sec, relocation,
2121                                    unresolved_reloc, warned);
2122           
2123           if (unresolved_reloc || relocation != 0)
2124             {
2125               /* In these cases, we don't need the relocation value.
2126                  We check specially because in some obscure cases
2127                  sec->output_section will be NULL.  */
2128               switch (r_type)
2129                 {
2130                 case R_ARM_PC24:
2131                 case R_ARM_ABS32:
2132                 case R_ARM_THM_PC22:
2133                 case R_ARM_PLT32:
2134
2135                   if (info->shared
2136                       && (
2137                           (!info->symbolic && h->dynindx != -1)
2138                           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2139                           )
2140                       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2141                       && ((input_section->flags & SEC_ALLOC) != 0
2142                           /* DWARF will emit R_ARM_ABS32 relocations in its
2143                              sections against symbols defined externally
2144                              in shared libraries.  We can't do anything
2145                              with them here.  */
2146                           || ((input_section->flags & SEC_DEBUGGING) != 0
2147                               && (h->elf_link_hash_flags
2148                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2149                       )
2150                     relocation = 0;
2151                   break;
2152
2153                 case R_ARM_GOTPC:
2154                   relocation = 0;
2155                   break;
2156
2157                 case R_ARM_GOT32:
2158                   if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL
2159                        (elf_hash_table (info)->dynamic_sections_created,
2160                         info->shared, h))
2161                       && (!info->shared
2162                           || (!info->symbolic && h->dynindx != -1)
2163                           || (h->elf_link_hash_flags
2164                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
2165                     relocation = 0;
2166                   break;
2167
2168                 default:
2169                   if (unresolved_reloc)
2170                     _bfd_error_handler
2171                       (_("%s: warning: unresolvable relocation %d against symbol `%s' from %s section"),
2172                        bfd_archive_filename (input_bfd),
2173                        r_type,
2174                        h->root.root.string,
2175                        bfd_get_section_name (input_bfd, input_section));
2176                   break;
2177                 }
2178             }
2179         }
2180
2181       if (h != NULL)
2182         name = h->root.root.string;
2183       else
2184         {
2185           name = (bfd_elf_string_from_elf_section
2186                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
2187           if (name == NULL || *name == '\0')
2188             name = bfd_section_name (input_bfd, sec);
2189         }
2190
2191       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
2192                                          input_section, contents, rel,
2193                                          relocation, info, sec, name,
2194                                          (h ? ELF_ST_TYPE (h->type) :
2195                                           ELF_ST_TYPE (sym->st_info)), h);
2196
2197       if (r != bfd_reloc_ok)
2198         {
2199           const char * msg = (const char *) 0;
2200
2201           switch (r)
2202             {
2203             case bfd_reloc_overflow:
2204               /* If the overflowing reloc was to an undefined symbol,
2205                  we have already printed one error message and there
2206                  is no point complaining again.  */
2207               if ((! h ||
2208                    h->root.type != bfd_link_hash_undefined)
2209                   && (!((*info->callbacks->reloc_overflow)
2210                         (info, name, howto->name, (bfd_vma) 0,
2211                          input_bfd, input_section, rel->r_offset))))
2212                   return FALSE;
2213               break;
2214
2215             case bfd_reloc_undefined:
2216               if (!((*info->callbacks->undefined_symbol)
2217                     (info, name, input_bfd, input_section,
2218                      rel->r_offset, TRUE)))
2219                 return FALSE;
2220               break;
2221
2222             case bfd_reloc_outofrange:
2223               msg = _("internal error: out of range error");
2224               goto common_error;
2225
2226             case bfd_reloc_notsupported:
2227               msg = _("internal error: unsupported relocation error");
2228               goto common_error;
2229
2230             case bfd_reloc_dangerous:
2231               msg = _("internal error: dangerous error");
2232               goto common_error;
2233
2234             default:
2235               msg = _("internal error: unknown error");
2236               /* fall through */
2237
2238             common_error:
2239               if (!((*info->callbacks->warning)
2240                     (info, msg, name, input_bfd, input_section,
2241                      rel->r_offset)))
2242                 return FALSE;
2243               break;
2244             }
2245         }
2246     }
2247
2248   return TRUE;
2249 }
2250
2251 /* Set the right machine number.  */
2252
2253 static bfd_boolean
2254 elf32_arm_object_p (abfd)
2255      bfd *abfd;
2256 {
2257   unsigned int mach;
2258   
2259   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
2260
2261   if (mach != bfd_mach_arm_unknown)
2262     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2263
2264   else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
2265     bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
2266
2267   else
2268     bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2269
2270   return TRUE;
2271 }
2272
2273 /* Function to keep ARM specific flags in the ELF header.  */
2274 static bfd_boolean
2275 elf32_arm_set_private_flags (abfd, flags)
2276      bfd *abfd;
2277      flagword flags;
2278 {
2279   if (elf_flags_init (abfd)
2280       && elf_elfheader (abfd)->e_flags != flags)
2281     {
2282       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
2283         {
2284           if (flags & EF_ARM_INTERWORK)
2285             (*_bfd_error_handler) (_("\
2286 Warning: Not setting interworking flag of %s since it has already been specified as non-interworking"),
2287                                    bfd_archive_filename (abfd));
2288           else
2289             _bfd_error_handler (_("\
2290 Warning: Clearing the interworking flag of %s due to outside request"),
2291                                 bfd_archive_filename (abfd));
2292         }
2293     }
2294   else
2295     {
2296       elf_elfheader (abfd)->e_flags = flags;
2297       elf_flags_init (abfd) = TRUE;
2298     }
2299
2300   return TRUE;
2301 }
2302
2303 /* Copy backend specific data from one object module to another.  */
2304
2305 static bfd_boolean
2306 elf32_arm_copy_private_bfd_data (ibfd, obfd)
2307      bfd *ibfd;
2308      bfd *obfd;
2309 {
2310   flagword in_flags;
2311   flagword out_flags;
2312
2313   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2314       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2315     return TRUE;
2316
2317   in_flags  = elf_elfheader (ibfd)->e_flags;
2318   out_flags = elf_elfheader (obfd)->e_flags;
2319
2320   if (elf_flags_init (obfd)
2321       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
2322       && in_flags != out_flags)
2323     {
2324       /* Cannot mix APCS26 and APCS32 code.  */
2325       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2326         return FALSE;
2327
2328       /* Cannot mix float APCS and non-float APCS code.  */
2329       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2330         return FALSE;
2331
2332       /* If the src and dest have different interworking flags
2333          then turn off the interworking bit.  */
2334       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2335         {
2336           if (out_flags & EF_ARM_INTERWORK)
2337             _bfd_error_handler (_("\
2338 Warning: Clearing the interworking flag of %s because non-interworking code in %s has been linked with it"),
2339                                 bfd_get_filename (obfd),
2340                                 bfd_archive_filename (ibfd));
2341
2342           in_flags &= ~EF_ARM_INTERWORK;
2343         }
2344
2345       /* Likewise for PIC, though don't warn for this case.  */
2346       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
2347         in_flags &= ~EF_ARM_PIC;
2348     }
2349
2350   elf_elfheader (obfd)->e_flags = in_flags;
2351   elf_flags_init (obfd) = TRUE;
2352
2353   return TRUE;
2354 }
2355
2356 /* Merge backend specific data from an object file to the output
2357    object file when linking.  */
2358
2359 static bfd_boolean
2360 elf32_arm_merge_private_bfd_data (ibfd, obfd)
2361      bfd * ibfd;
2362      bfd * obfd;
2363 {
2364   flagword out_flags;
2365   flagword in_flags;
2366   bfd_boolean flags_compatible = TRUE;
2367   asection *sec;
2368
2369   /* Check if we have the same endianess.  */
2370   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2371     return FALSE;
2372
2373   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2374       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2375     return TRUE;
2376
2377   /* The input BFD must have had its flags initialised.  */
2378   /* The following seems bogus to me -- The flags are initialized in
2379      the assembler but I don't think an elf_flags_init field is
2380      written into the object.  */
2381   /* BFD_ASSERT (elf_flags_init (ibfd)); */
2382
2383   in_flags  = elf_elfheader (ibfd)->e_flags;
2384   out_flags = elf_elfheader (obfd)->e_flags;
2385
2386   if (!elf_flags_init (obfd))
2387     {
2388       /* If the input is the default architecture and had the default
2389          flags then do not bother setting the flags for the output
2390          architecture, instead allow future merges to do this.  If no
2391          future merges ever set these flags then they will retain their
2392          uninitialised values, which surprise surprise, correspond
2393          to the default values.  */
2394       if (bfd_get_arch_info (ibfd)->the_default
2395           && elf_elfheader (ibfd)->e_flags == 0)
2396         return TRUE;
2397
2398       elf_flags_init (obfd) = TRUE;
2399       elf_elfheader (obfd)->e_flags = in_flags;
2400
2401       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2402           && bfd_get_arch_info (obfd)->the_default)
2403         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2404
2405       return TRUE;
2406     }
2407
2408   /* Determine what should happen if the input ARM architecture
2409      does not match the output ARM architecture.  */
2410   if (! bfd_arm_merge_machines (ibfd, obfd))
2411     return FALSE;
2412
2413   /* Identical flags must be compatible.  */
2414   if (in_flags == out_flags)
2415     return TRUE;
2416
2417   /* Check to see if the input BFD actually contains any sections.  If
2418      not, its flags may not have been initialised either, but it
2419      cannot actually cause any incompatibility.  Do not short-circuit
2420      dynamic objects; their section list may be emptied by
2421     elf_link_add_object_symbols.
2422
2423     Also check to see if there are no code sections in the input.
2424     In this case there is no need to check for code specific flags.
2425     XXX - do we need to worry about floating-point format compatability
2426     in data sections ?  */
2427   if (!(ibfd->flags & DYNAMIC))
2428     {
2429       bfd_boolean null_input_bfd = TRUE;
2430       bfd_boolean only_data_sections = TRUE;
2431
2432       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2433         {
2434           /* Ignore synthetic glue sections.  */
2435           if (strcmp (sec->name, ".glue_7")
2436               && strcmp (sec->name, ".glue_7t"))
2437             {
2438               if ((bfd_get_section_flags (ibfd, sec)
2439                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2440                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2441                 only_data_sections = FALSE;
2442
2443               null_input_bfd = FALSE;
2444               break;
2445             }
2446         }
2447
2448       if (null_input_bfd || only_data_sections)
2449         return TRUE;
2450     }
2451
2452   /* Complain about various flag mismatches.  */
2453   if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
2454     {
2455       _bfd_error_handler (_("\
2456 ERROR: %s is compiled for EABI version %d, whereas %s is compiled for version %d"),
2457                           bfd_archive_filename (ibfd),
2458                           (in_flags & EF_ARM_EABIMASK) >> 24,
2459                           bfd_get_filename (obfd),
2460                           (out_flags & EF_ARM_EABIMASK) >> 24);
2461       return FALSE;
2462     }
2463
2464   /* Not sure what needs to be checked for EABI versions >= 1.  */
2465   if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
2466     {
2467       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2468         {
2469           _bfd_error_handler (_("\
2470 ERROR: %s is compiled for APCS-%d, whereas target %s uses APCS-%d"),
2471                               bfd_archive_filename (ibfd),
2472                               in_flags & EF_ARM_APCS_26 ? 26 : 32,
2473                               bfd_get_filename (obfd),
2474                               out_flags & EF_ARM_APCS_26 ? 26 : 32);
2475           flags_compatible = FALSE;
2476         }
2477
2478       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2479         {
2480           if (in_flags & EF_ARM_APCS_FLOAT)
2481             _bfd_error_handler (_("\
2482 ERROR: %s passes floats in float registers, whereas %s passes them in integer registers"),
2483                                 bfd_archive_filename (ibfd),
2484                                 bfd_get_filename (obfd));
2485           else
2486             _bfd_error_handler (_("\
2487 ERROR: %s passes floats in integer registers, whereas %s passes them in float registers"),
2488                                 bfd_archive_filename (ibfd),
2489                                 bfd_get_filename (obfd));
2490
2491           flags_compatible = FALSE;
2492         }
2493
2494       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
2495         {
2496           if (in_flags & EF_ARM_VFP_FLOAT)
2497             _bfd_error_handler (_("\
2498 ERROR: %s uses VFP instructions, whereas %s does not"),
2499                                 bfd_archive_filename (ibfd),
2500                                 bfd_get_filename (obfd));
2501           else
2502             _bfd_error_handler (_("\
2503 ERROR: %s uses FPA instructions, whereas %s does not"),
2504                                 bfd_archive_filename (ibfd),
2505                                 bfd_get_filename (obfd));
2506
2507           flags_compatible = FALSE;
2508         }
2509
2510       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
2511         {
2512           if (in_flags & EF_ARM_MAVERICK_FLOAT)
2513             _bfd_error_handler (_("\
2514 ERROR: %s uses Maverick instructions, whereas %s does not"),
2515                                 bfd_archive_filename (ibfd),
2516                                 bfd_get_filename (obfd));
2517           else
2518             _bfd_error_handler (_("\
2519 ERROR: %s does not use Maverick instructions, whereas %s does"),
2520                                 bfd_archive_filename (ibfd),
2521                                 bfd_get_filename (obfd));
2522
2523           flags_compatible = FALSE;
2524         }
2525
2526 #ifdef EF_ARM_SOFT_FLOAT
2527       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
2528         {
2529           /* We can allow interworking between code that is VFP format
2530              layout, and uses either soft float or integer regs for
2531              passing floating point arguments and results.  We already
2532              know that the APCS_FLOAT flags match; similarly for VFP
2533              flags.  */
2534           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
2535               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
2536             {
2537               if (in_flags & EF_ARM_SOFT_FLOAT)
2538                 _bfd_error_handler (_("\
2539 ERROR: %s uses software FP, whereas %s uses hardware FP"),
2540                                     bfd_archive_filename (ibfd),
2541                                     bfd_get_filename (obfd));
2542               else
2543                 _bfd_error_handler (_("\
2544 ERROR: %s uses hardware FP, whereas %s uses software FP"),
2545                                     bfd_archive_filename (ibfd),
2546                                     bfd_get_filename (obfd));
2547
2548               flags_compatible = FALSE;
2549             }
2550         }
2551 #endif
2552
2553       /* Interworking mismatch is only a warning.  */
2554       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2555         {
2556           if (in_flags & EF_ARM_INTERWORK)
2557             {
2558               _bfd_error_handler (_("\
2559 Warning: %s supports interworking, whereas %s does not"),
2560                                   bfd_archive_filename (ibfd),
2561                                   bfd_get_filename (obfd));
2562             }
2563           else
2564             {
2565               _bfd_error_handler (_("\
2566 Warning: %s does not support interworking, whereas %s does"),
2567                                   bfd_archive_filename (ibfd),
2568                                   bfd_get_filename (obfd));
2569             }
2570         }
2571     }
2572
2573   return flags_compatible;
2574 }
2575
2576 /* Display the flags field.  */
2577
2578 static bfd_boolean
2579 elf32_arm_print_private_bfd_data (abfd, ptr)
2580      bfd *abfd;
2581      PTR ptr;
2582 {
2583   FILE * file = (FILE *) ptr;
2584   unsigned long flags;
2585
2586   BFD_ASSERT (abfd != NULL && ptr != NULL);
2587
2588   /* Print normal ELF private data.  */
2589   _bfd_elf_print_private_bfd_data (abfd, ptr);
2590
2591   flags = elf_elfheader (abfd)->e_flags;
2592   /* Ignore init flag - it may not be set, despite the flags field
2593      containing valid data.  */
2594
2595   /* xgettext:c-format */
2596   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2597
2598   switch (EF_ARM_EABI_VERSION (flags))
2599     {
2600     case EF_ARM_EABI_UNKNOWN:
2601       /* The following flag bits are GNU extensions and not part of the
2602          official ARM ELF extended ABI.  Hence they are only decoded if
2603          the EABI version is not set.  */
2604       if (flags & EF_ARM_INTERWORK)
2605         fprintf (file, _(" [interworking enabled]"));
2606
2607       if (flags & EF_ARM_APCS_26)
2608         fprintf (file, " [APCS-26]");
2609       else
2610         fprintf (file, " [APCS-32]");
2611
2612       if (flags & EF_ARM_VFP_FLOAT)
2613         fprintf (file, _(" [VFP float format]"));
2614       else if (flags & EF_ARM_MAVERICK_FLOAT)
2615         fprintf (file, _(" [Maverick float format]"));
2616       else
2617         fprintf (file, _(" [FPA float format]"));
2618
2619       if (flags & EF_ARM_APCS_FLOAT)
2620         fprintf (file, _(" [floats passed in float registers]"));
2621
2622       if (flags & EF_ARM_PIC)
2623         fprintf (file, _(" [position independent]"));
2624
2625       if (flags & EF_ARM_NEW_ABI)
2626         fprintf (file, _(" [new ABI]"));
2627
2628       if (flags & EF_ARM_OLD_ABI)
2629         fprintf (file, _(" [old ABI]"));
2630
2631       if (flags & EF_ARM_SOFT_FLOAT)
2632         fprintf (file, _(" [software FP]"));
2633
2634       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
2635                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
2636                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
2637                  | EF_ARM_MAVERICK_FLOAT);
2638       break;
2639
2640     case EF_ARM_EABI_VER1:
2641       fprintf (file, _(" [Version1 EABI]"));
2642
2643       if (flags & EF_ARM_SYMSARESORTED)
2644         fprintf (file, _(" [sorted symbol table]"));
2645       else
2646         fprintf (file, _(" [unsorted symbol table]"));
2647
2648       flags &= ~ EF_ARM_SYMSARESORTED;
2649       break;
2650
2651     case EF_ARM_EABI_VER2:
2652       fprintf (file, _(" [Version2 EABI]"));
2653
2654       if (flags & EF_ARM_SYMSARESORTED)
2655         fprintf (file, _(" [sorted symbol table]"));
2656       else
2657         fprintf (file, _(" [unsorted symbol table]"));
2658
2659       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
2660         fprintf (file, _(" [dynamic symbols use segment index]"));
2661
2662       if (flags & EF_ARM_MAPSYMSFIRST)
2663         fprintf (file, _(" [mapping symbols precede others]"));
2664
2665       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
2666                  | EF_ARM_MAPSYMSFIRST);
2667       break;
2668
2669     default:
2670       fprintf (file, _(" <EABI version unrecognised>"));
2671       break;
2672     }
2673
2674   flags &= ~ EF_ARM_EABIMASK;
2675
2676   if (flags & EF_ARM_RELEXEC)
2677     fprintf (file, _(" [relocatable executable]"));
2678
2679   if (flags & EF_ARM_HASENTRY)
2680     fprintf (file, _(" [has entry point]"));
2681
2682   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
2683
2684   if (flags)
2685     fprintf (file, _("<Unrecognised flag bits set>"));
2686
2687   fputc ('\n', file);
2688
2689   return TRUE;
2690 }
2691
2692 static int
2693 elf32_arm_get_symbol_type (elf_sym, type)
2694      Elf_Internal_Sym * elf_sym;
2695      int type;
2696 {
2697   switch (ELF_ST_TYPE (elf_sym->st_info))
2698     {
2699     case STT_ARM_TFUNC:
2700       return ELF_ST_TYPE (elf_sym->st_info);
2701
2702     case STT_ARM_16BIT:
2703       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
2704          This allows us to distinguish between data used by Thumb instructions
2705          and non-data (which is probably code) inside Thumb regions of an
2706          executable.  */
2707       if (type != STT_OBJECT)
2708         return ELF_ST_TYPE (elf_sym->st_info);
2709       break;
2710
2711     default:
2712       break;
2713     }
2714
2715   return type;
2716 }
2717
2718 static asection *
2719 elf32_arm_gc_mark_hook (sec, info, rel, h, sym)
2720        asection *sec;
2721        struct bfd_link_info *info ATTRIBUTE_UNUSED;
2722        Elf_Internal_Rela *rel;
2723        struct elf_link_hash_entry *h;
2724        Elf_Internal_Sym *sym;
2725 {
2726   if (h != NULL)
2727     {
2728       switch (ELF32_R_TYPE (rel->r_info))
2729       {
2730       case R_ARM_GNU_VTINHERIT:
2731       case R_ARM_GNU_VTENTRY:
2732         break;
2733
2734       default:
2735         switch (h->root.type)
2736           {
2737           case bfd_link_hash_defined:
2738           case bfd_link_hash_defweak:
2739             return h->root.u.def.section;
2740
2741           case bfd_link_hash_common:
2742             return h->root.u.c.p->section;
2743
2744           default:
2745             break;
2746           }
2747        }
2748      }
2749    else
2750      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2751
2752   return NULL;
2753 }
2754
2755 /* Update the got entry reference counts for the section being removed.  */
2756
2757 static bfd_boolean
2758 elf32_arm_gc_sweep_hook (abfd, info, sec, relocs)
2759      bfd *abfd ATTRIBUTE_UNUSED;
2760      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2761      asection *sec ATTRIBUTE_UNUSED;
2762      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2763 {
2764   Elf_Internal_Shdr *symtab_hdr;
2765   struct elf_link_hash_entry **sym_hashes;
2766   bfd_signed_vma *local_got_refcounts;
2767   const Elf_Internal_Rela *rel, *relend;
2768   unsigned long r_symndx;
2769   struct elf_link_hash_entry *h;
2770
2771   elf_section_data (sec)->local_dynrel = NULL;
2772
2773   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2774   sym_hashes = elf_sym_hashes (abfd);
2775   local_got_refcounts = elf_local_got_refcounts (abfd);
2776
2777   relend = relocs + sec->reloc_count;
2778   for (rel = relocs; rel < relend; rel++)
2779     switch (ELF32_R_TYPE (rel->r_info))
2780       {
2781       case R_ARM_GOT32:
2782         r_symndx = ELF32_R_SYM (rel->r_info);
2783         if (r_symndx >= symtab_hdr->sh_info)
2784           {
2785             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2786             if (h->got.refcount > 0)
2787               h->got.refcount -= 1;
2788           }
2789         else if (local_got_refcounts != NULL)
2790           {
2791             if (local_got_refcounts[r_symndx] > 0)
2792               local_got_refcounts[r_symndx] -= 1;
2793           }
2794         break;
2795
2796       case R_ARM_ABS32:
2797       case R_ARM_REL32:
2798       case R_ARM_PC24:
2799       case R_ARM_PLT32:
2800         r_symndx = ELF32_R_SYM (rel->r_info);
2801         if (r_symndx >= symtab_hdr->sh_info)
2802           {
2803             struct elf32_arm_link_hash_entry *eh;
2804             struct elf32_arm_relocs_copied **pp;
2805             struct elf32_arm_relocs_copied *p;
2806
2807             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2808
2809             if (h->plt.refcount > 0)
2810               h->plt.refcount -= 1;
2811
2812             if (ELF32_R_TYPE (rel->r_info) == R_ARM_ABS32
2813                 || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
2814               {
2815                 eh = (struct elf32_arm_link_hash_entry *) h;
2816
2817                 for (pp = &eh->relocs_copied; (p = *pp) != NULL;
2818                      pp = &p->next)
2819                 if (p->section == sec)
2820                   {
2821                     p->count -= 1;
2822                     if (p->count == 0)
2823                       *pp = p->next;
2824                     break;
2825                   }
2826               }
2827           }
2828         break;
2829
2830       default:
2831         break;
2832       }
2833
2834   return TRUE;
2835 }
2836
2837 /* Look through the relocs for a section during the first phase.  */
2838
2839 static bfd_boolean
2840 elf32_arm_check_relocs (abfd, info, sec, relocs)
2841      bfd *abfd;
2842      struct bfd_link_info *info;
2843      asection *sec;
2844      const Elf_Internal_Rela *relocs;
2845 {
2846   Elf_Internal_Shdr *symtab_hdr;
2847   struct elf_link_hash_entry **sym_hashes;
2848   struct elf_link_hash_entry **sym_hashes_end;
2849   const Elf_Internal_Rela *rel;
2850   const Elf_Internal_Rela *rel_end;
2851   bfd *dynobj;
2852   asection *sreloc;
2853   bfd_vma *local_got_offsets;
2854   struct elf32_arm_link_hash_table *htab;
2855
2856   if (info->relocatable)
2857     return TRUE;
2858
2859   htab = elf32_arm_hash_table (info);
2860   sreloc = NULL;
2861
2862   dynobj = elf_hash_table (info)->dynobj;
2863   local_got_offsets = elf_local_got_offsets (abfd);
2864
2865   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2866   sym_hashes = elf_sym_hashes (abfd);
2867   sym_hashes_end = sym_hashes
2868     + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2869
2870   if (!elf_bad_symtab (abfd))
2871     sym_hashes_end -= symtab_hdr->sh_info;
2872
2873   rel_end = relocs + sec->reloc_count;
2874   for (rel = relocs; rel < rel_end; rel++)
2875     {
2876       struct elf_link_hash_entry *h;
2877       unsigned long r_symndx;
2878
2879       r_symndx = ELF32_R_SYM (rel->r_info);
2880       if (r_symndx < symtab_hdr->sh_info)
2881         h = NULL;
2882       else
2883         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2884
2885       switch (ELF32_R_TYPE (rel->r_info))
2886         {
2887           case R_ARM_GOT32:
2888             /* This symbol requires a global offset table entry.  */
2889             if (h != NULL)
2890               {
2891                 h->got.refcount++;
2892               }
2893             else
2894               {
2895                 bfd_signed_vma *local_got_refcounts;
2896
2897                 /* This is a global offset table entry for a local symbol.  */
2898                 local_got_refcounts = elf_local_got_refcounts (abfd);
2899                 if (local_got_refcounts == NULL)
2900                   {
2901                     bfd_size_type size;
2902
2903                     size = symtab_hdr->sh_info;
2904                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
2905                     local_got_refcounts = ((bfd_signed_vma *)
2906                                            bfd_zalloc (abfd, size));
2907                     if (local_got_refcounts == NULL)
2908                       return FALSE;
2909                     elf_local_got_refcounts (abfd) = local_got_refcounts;
2910                   }
2911                 local_got_refcounts[r_symndx] += 1;
2912               }
2913             break;
2914
2915           case R_ARM_GOTOFF:
2916           case R_ARM_GOTPC:
2917             if (htab->sgot == NULL)
2918               {
2919                 if (htab->root.dynobj == NULL)
2920                   htab->root.dynobj = abfd;
2921                 if (!create_got_section (htab->root.dynobj, info))
2922                   return FALSE;
2923               }
2924             break;
2925
2926           case R_ARM_ABS32:
2927           case R_ARM_REL32:
2928           case R_ARM_PC24:
2929           case R_ARM_PLT32:
2930             if (h != NULL)
2931               {
2932                 /* If this reloc is in a read-only section, we might
2933                    need a copy reloc.  We can't check reliably at this
2934                    stage whether the section is read-only, as input
2935                    sections have not yet been mapped to output sections.
2936                    Tentatively set the flag for now, and correct in
2937                    adjust_dynamic_symbol.  */
2938                 if (!info->shared)
2939                   h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2940
2941                 /* We may need a .plt entry if the function this reloc
2942                    refers to is in a different object.  We can't tell for
2943                    sure yet, because something later might force the
2944                    symbol local.  */
2945                 if (ELF32_R_TYPE (rel->r_info) == R_ARM_PC24
2946                     || ELF32_R_TYPE (rel->r_info) == R_ARM_PLT32)
2947                   h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2948
2949                 /* If we create a PLT entry, this relocation will reference
2950                    it, even if it's an ABS32 relocation.  */
2951                 h->plt.refcount += 1;
2952               }
2953
2954             /* If we are creating a shared library, and this is a reloc
2955                against a global symbol, or a non PC relative reloc
2956                against a local symbol, then we need to copy the reloc
2957                into the shared library.  However, if we are linking with
2958                -Bsymbolic, we do not need to copy a reloc against a
2959                global symbol which is defined in an object we are
2960                including in the link (i.e., DEF_REGULAR is set).  At
2961                this point we have not seen all the input files, so it is
2962                possible that DEF_REGULAR is not set now but will be set
2963                later (it is never cleared).  We account for that
2964                possibility below by storing information in the
2965                relocs_copied field of the hash table entry.  */
2966             if (info->shared
2967                 && (sec->flags & SEC_ALLOC) != 0
2968                 && ((ELF32_R_TYPE (rel->r_info) != R_ARM_PC24
2969                      && ELF32_R_TYPE (rel->r_info) != R_ARM_PLT32
2970                      && ELF32_R_TYPE (rel->r_info) != R_ARM_REL32)
2971                     || (h != NULL
2972                         && (! info->symbolic
2973                             || (h->elf_link_hash_flags
2974                                 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2975               {
2976                 struct elf32_arm_relocs_copied *p, **head;
2977
2978                 /* When creating a shared object, we must copy these
2979                    reloc types into the output file.  We create a reloc
2980                    section in dynobj and make room for this reloc.  */
2981                 if (sreloc == NULL)
2982                   {
2983                     const char * name;
2984
2985                     name = (bfd_elf_string_from_elf_section
2986                             (abfd,
2987                              elf_elfheader (abfd)->e_shstrndx,
2988                              elf_section_data (sec)->rel_hdr.sh_name));
2989                     if (name == NULL)
2990                       return FALSE;
2991
2992                     BFD_ASSERT (strncmp (name, ".rel", 4) == 0
2993                                 && strcmp (bfd_get_section_name (abfd, sec),
2994                                            name + 4) == 0);
2995
2996                     sreloc = bfd_get_section_by_name (dynobj, name);
2997                     if (sreloc == NULL)
2998                       {
2999                         flagword flags;
3000
3001                         sreloc = bfd_make_section (dynobj, name);
3002                         flags = (SEC_HAS_CONTENTS | SEC_READONLY
3003                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3004                         if ((sec->flags & SEC_ALLOC) != 0)
3005                           flags |= SEC_ALLOC | SEC_LOAD;
3006                         if (sreloc == NULL
3007                             || ! bfd_set_section_flags (dynobj, sreloc, flags)
3008                             || ! bfd_set_section_alignment (dynobj, sreloc, 2))
3009                           return FALSE;
3010                       }
3011
3012                     elf_section_data (sec)->sreloc = sreloc;
3013                   }
3014
3015                 /* If this is a global symbol, we count the number of
3016                    relocations we need for this symbol.  */
3017                 if (h != NULL)
3018                   {
3019                     head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
3020                   }
3021                 else
3022                   {
3023                     /* Track dynamic relocs needed for local syms too.
3024                        We really need local syms available to do this
3025                        easily.  Oh well.  */
3026                     
3027                     asection *s;
3028                     s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3029                                                    sec, r_symndx);
3030                     if (s == NULL)
3031                       return FALSE;
3032                     
3033                     head = ((struct elf32_arm_relocs_copied **)
3034                             &elf_section_data (s)->local_dynrel);
3035                   }
3036                 
3037                 p = *head;
3038                 if (p == NULL || p->section != sec)
3039                   {
3040                     bfd_size_type amt = sizeof *p;
3041                     p = bfd_alloc (htab->root.dynobj, amt);
3042                     if (p == NULL)
3043                       return FALSE;
3044                     p->next = *head;
3045                     *head = p;
3046                     p->section = sec;
3047                     p->count = 0;
3048                   }
3049                 
3050                 if (ELF32_R_TYPE (rel->r_info) == R_ARM_ABS32
3051                     || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
3052                   p->count += 1;
3053               }
3054             break;
3055
3056         /* This relocation describes the C++ object vtable hierarchy.
3057            Reconstruct it for later use during GC.  */
3058         case R_ARM_GNU_VTINHERIT:
3059           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3060             return FALSE;
3061           break;
3062
3063         /* This relocation describes which C++ vtable entries are actually
3064            used.  Record for later use during GC.  */
3065         case R_ARM_GNU_VTENTRY:
3066           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
3067             return FALSE;
3068           break;
3069         }
3070     }
3071
3072   return TRUE;
3073 }
3074
3075 /* Find the nearest line to a particular section and offset, for error
3076    reporting.   This code is a duplicate of the code in elf.c, except
3077    that it also accepts STT_ARM_TFUNC as a symbol that names a function.  */
3078
3079 static bfd_boolean
3080 elf32_arm_find_nearest_line
3081   (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
3082      bfd *abfd;
3083      asection *section;
3084      asymbol **symbols;
3085      bfd_vma offset;
3086      const char **filename_ptr;
3087      const char **functionname_ptr;
3088      unsigned int *line_ptr;
3089 {
3090   bfd_boolean found;
3091   const char *filename;
3092   asymbol *func;
3093   bfd_vma low_func;
3094   asymbol **p;
3095
3096   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3097                                      filename_ptr, functionname_ptr,
3098                                      line_ptr, 0,
3099                                      &elf_tdata (abfd)->dwarf2_find_line_info))
3100     return TRUE;
3101
3102   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3103                                              &found, filename_ptr,
3104                                              functionname_ptr, line_ptr,
3105                                              &elf_tdata (abfd)->line_info))
3106     return FALSE;
3107
3108   if (found)
3109     return TRUE;
3110
3111   if (symbols == NULL)
3112     return FALSE;
3113
3114   filename = NULL;
3115   func = NULL;
3116   low_func = 0;
3117
3118   for (p = symbols; *p != NULL; p++)
3119     {
3120       elf_symbol_type *q;
3121
3122       q = (elf_symbol_type *) *p;
3123
3124       if (bfd_get_section (&q->symbol) != section)
3125         continue;
3126
3127       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3128         {
3129         default:
3130           break;
3131         case STT_FILE:
3132           filename = bfd_asymbol_name (&q->symbol);
3133           break;
3134         case STT_NOTYPE:
3135         case STT_FUNC:
3136         case STT_ARM_TFUNC:
3137           if (q->symbol.section == section
3138               && q->symbol.value >= low_func
3139               && q->symbol.value <= offset)
3140             {
3141               func = (asymbol *) q;
3142               low_func = q->symbol.value;
3143             }
3144           break;
3145         }
3146     }
3147
3148   if (func == NULL)
3149     return FALSE;
3150
3151   *filename_ptr = filename;
3152   *functionname_ptr = bfd_asymbol_name (func);
3153   *line_ptr = 0;
3154
3155   return TRUE;
3156 }
3157
3158 /* Adjust a symbol defined by a dynamic object and referenced by a
3159    regular object.  The current definition is in some section of the
3160    dynamic object, but we're not including those sections.  We have to
3161    change the definition to something the rest of the link can
3162    understand.  */
3163
3164 static bfd_boolean
3165 elf32_arm_adjust_dynamic_symbol (info, h)
3166      struct bfd_link_info * info;
3167      struct elf_link_hash_entry * h;
3168 {
3169   bfd * dynobj;
3170   asection * s;
3171   unsigned int power_of_two;
3172
3173   dynobj = elf_hash_table (info)->dynobj;
3174
3175   /* Make sure we know what is going on here.  */
3176   BFD_ASSERT (dynobj != NULL
3177               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3178                   || h->weakdef != NULL
3179                   || ((h->elf_link_hash_flags
3180                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3181                       && (h->elf_link_hash_flags
3182                           & ELF_LINK_HASH_REF_REGULAR) != 0
3183                       && (h->elf_link_hash_flags
3184                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3185
3186   /* If this is a function, put it in the procedure linkage table.  We
3187      will fill in the contents of the procedure linkage table later,
3188      when we know the address of the .got section.  */
3189   if (h->type == STT_FUNC
3190       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3191     {
3192       if (h->plt.refcount <= 0
3193           || SYMBOL_CALLS_LOCAL (info, h)
3194           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3195               && h->root.type == bfd_link_hash_undefweak))
3196         {
3197           /* This case can occur if we saw a PLT32 reloc in an input
3198              file, but the symbol was never referred to by a dynamic
3199              object, or if all references were garbage collected.  In
3200              such a case, we don't actually need to build a procedure
3201              linkage table, and we can just do a PC24 reloc instead.  */
3202           h->plt.offset = (bfd_vma) -1;
3203           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3204         }
3205
3206       return TRUE;
3207     }
3208   else
3209     /* It's possible that we incorrectly decided a .plt reloc was
3210        needed for an R_ARM_PC24 reloc to a non-function sym in
3211        check_relocs.  We can't decide accurately between function and
3212        non-function syms in check-relocs;  Objects loaded later in
3213        the link may change h->type.  So fix it now.  */
3214     h->plt.offset = (bfd_vma) -1;
3215
3216   /* If this is a weak symbol, and there is a real definition, the
3217      processor independent code will have arranged for us to see the
3218      real definition first, and we can just use the same value.  */
3219   if (h->weakdef != NULL)
3220     {
3221       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3222                   || h->weakdef->root.type == bfd_link_hash_defweak);
3223       h->root.u.def.section = h->weakdef->root.u.def.section;
3224       h->root.u.def.value = h->weakdef->root.u.def.value;
3225       return TRUE;
3226     }
3227
3228   /* This is a reference to a symbol defined by a dynamic object which
3229      is not a function.  */
3230
3231   /* If we are creating a shared library, we must presume that the
3232      only references to the symbol are via the global offset table.
3233      For such cases we need not do anything here; the relocations will
3234      be handled correctly by relocate_section.  */
3235   if (info->shared)
3236     return TRUE;
3237
3238   /* We must allocate the symbol in our .dynbss section, which will
3239      become part of the .bss section of the executable.  There will be
3240      an entry for this symbol in the .dynsym section.  The dynamic
3241      object will contain position independent code, so all references
3242      from the dynamic object to this symbol will go through the global
3243      offset table.  The dynamic linker will use the .dynsym entry to
3244      determine the address it must put in the global offset table, so
3245      both the dynamic object and the regular object will refer to the
3246      same memory location for the variable.  */
3247   s = bfd_get_section_by_name (dynobj, ".dynbss");
3248   BFD_ASSERT (s != NULL);
3249
3250   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
3251      copy the initial value out of the dynamic object and into the
3252      runtime process image.  We need to remember the offset into the
3253      .rel.bss section we are going to use.  */
3254   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3255     {
3256       asection *srel;
3257
3258       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
3259       BFD_ASSERT (srel != NULL);
3260       srel->_raw_size += sizeof (Elf32_External_Rel);
3261       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3262     }
3263
3264   /* We need to figure out the alignment required for this symbol.  I
3265      have no idea how ELF linkers handle this.  */
3266   power_of_two = bfd_log2 (h->size);
3267   if (power_of_two > 3)
3268     power_of_two = 3;
3269
3270   /* Apply the required alignment.  */
3271   s->_raw_size = BFD_ALIGN (s->_raw_size,
3272                             (bfd_size_type) (1 << power_of_two));
3273   if (power_of_two > bfd_get_section_alignment (dynobj, s))
3274     {
3275       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3276         return FALSE;
3277     }
3278
3279   /* Define the symbol as being at this point in the section.  */
3280   h->root.u.def.section = s;
3281   h->root.u.def.value = s->_raw_size;
3282
3283   /* Increment the section size to make room for the symbol.  */
3284   s->_raw_size += h->size;
3285
3286   return TRUE;
3287 }
3288
3289 /* Allocate space in .plt, .got and associated reloc sections for
3290    dynamic relocs.  */
3291
3292 static bfd_boolean
3293 allocate_dynrelocs (h, inf)
3294      struct elf_link_hash_entry *h;
3295      PTR inf;
3296 {
3297   struct bfd_link_info *info;
3298   struct elf32_arm_link_hash_table *htab;
3299   struct elf32_arm_link_hash_entry *eh;
3300   struct elf32_arm_relocs_copied *p;
3301
3302   if (h->root.type == bfd_link_hash_indirect)
3303     return TRUE;
3304
3305   if (h->root.type == bfd_link_hash_warning)
3306     /* When warning symbols are created, they **replace** the "real"
3307        entry in the hash table, thus we never get to see the real
3308        symbol in a hash traversal.  So look at it now.  */
3309     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3310
3311   info = (struct bfd_link_info *) inf;
3312   htab = elf32_arm_hash_table (info);
3313
3314   if (htab->root.dynamic_sections_created
3315       && h->plt.refcount > 0)
3316     {
3317       /* Make sure this symbol is output as a dynamic symbol.
3318          Undefined weak syms won't yet be marked as dynamic.  */
3319       if (h->dynindx == -1
3320           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3321         {
3322           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3323             return FALSE;
3324         }
3325
3326       if (info->shared
3327           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3328         {
3329           asection *s = htab->splt;
3330
3331           /* If this is the first .plt entry, make room for the special
3332              first entry.  */
3333           if (s->_raw_size == 0)
3334             s->_raw_size += PLT_HEADER_SIZE;
3335
3336           h->plt.offset = s->_raw_size;
3337
3338           /* If this symbol is not defined in a regular file, and we are
3339              not generating a shared library, then set the symbol to this
3340              location in the .plt.  This is required to make function
3341              pointers compare as equal between the normal executable and
3342              the shared library.  */
3343           if (! info->shared
3344               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3345             {
3346               h->root.u.def.section = s;
3347               h->root.u.def.value = h->plt.offset;
3348             }
3349
3350           /* Make room for this entry.  */
3351           s->_raw_size += PLT_ENTRY_SIZE;
3352
3353           /* We also need to make an entry in the .got.plt section, which
3354              will be placed in the .got section by the linker script.  */
3355           htab->sgotplt->_raw_size += 4;
3356
3357           /* We also need to make an entry in the .rel.plt section.  */
3358           htab->srelplt->_raw_size += sizeof (Elf32_External_Rel);
3359         }
3360       else
3361         {
3362           h->plt.offset = (bfd_vma) -1;
3363           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3364         }
3365     }
3366   else
3367     {
3368       h->plt.offset = (bfd_vma) -1;
3369       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3370     }
3371
3372   if (h->got.refcount > 0)
3373     {
3374       asection *s;
3375       bfd_boolean dyn;
3376
3377       /* Make sure this symbol is output as a dynamic symbol.
3378          Undefined weak syms won't yet be marked as dynamic.  */
3379       if (h->dynindx == -1
3380           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3381         {
3382           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3383             return FALSE;
3384         }
3385
3386       s = htab->sgot;
3387       h->got.offset = s->_raw_size;
3388       s->_raw_size += 4;
3389       dyn = htab->root.dynamic_sections_created;
3390       if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3391            || h->root.type != bfd_link_hash_undefweak)
3392           && (info->shared
3393               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3394         htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
3395     }
3396   else
3397     h->got.offset = (bfd_vma) -1;
3398
3399   eh = (struct elf32_arm_link_hash_entry *) h;
3400   if (eh->relocs_copied == NULL)
3401     return TRUE;
3402
3403   /* In the shared -Bsymbolic case, discard space allocated for
3404      dynamic pc-relative relocs against symbols which turn out to be
3405      defined in regular objects.  For the normal shared case, discard
3406      space for pc-relative relocs that have become local due to symbol
3407      visibility changes.  */
3408
3409   if (info->shared)
3410     {
3411       /* Discard relocs on undefined weak syms with non-default
3412          visibility.  */
3413       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3414           && h->root.type == bfd_link_hash_undefweak)
3415         eh->relocs_copied = NULL;
3416     }
3417   else
3418     {
3419       /* For the non-shared case, discard space for relocs against
3420          symbols which turn out to need copy relocs or are not
3421          dynamic.  */
3422
3423       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3424           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3425                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3426               || (htab->root.dynamic_sections_created
3427                   && (h->root.type == bfd_link_hash_undefweak
3428                       || h->root.type == bfd_link_hash_undefined))))
3429         {
3430           /* Make sure this symbol is output as a dynamic symbol.
3431              Undefined weak syms won't yet be marked as dynamic.  */
3432           if (h->dynindx == -1
3433               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3434             {
3435               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3436                 return FALSE;
3437             }
3438
3439           /* If that succeeded, we know we'll be keeping all the
3440              relocs.  */
3441           if (h->dynindx != -1)
3442             goto keep;
3443         }
3444
3445       eh->relocs_copied = NULL;
3446
3447     keep: ;
3448     }
3449
3450   /* Finally, allocate space.  */
3451   for (p = eh->relocs_copied; p != NULL; p = p->next)
3452     {
3453       asection *sreloc = elf_section_data (p->section)->sreloc;
3454       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rel);
3455     }
3456
3457   return TRUE;
3458 }
3459
3460 /* Set the sizes of the dynamic sections.  */
3461
3462 static bfd_boolean
3463 elf32_arm_size_dynamic_sections (output_bfd, info)
3464      bfd * output_bfd ATTRIBUTE_UNUSED;
3465      struct bfd_link_info * info;
3466 {
3467   bfd * dynobj;
3468   asection * s;
3469   bfd_boolean plt;
3470   bfd_boolean relocs;
3471   bfd *ibfd;
3472   struct elf32_arm_link_hash_table *htab;
3473
3474   htab = elf32_arm_hash_table (info);
3475   dynobj = elf_hash_table (info)->dynobj;
3476   BFD_ASSERT (dynobj != NULL);
3477
3478   if (elf_hash_table (info)->dynamic_sections_created)
3479     {
3480       /* Set the contents of the .interp section to the interpreter.  */
3481       if (info->executable)
3482         {
3483           s = bfd_get_section_by_name (dynobj, ".interp");
3484           BFD_ASSERT (s != NULL);
3485           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3486           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3487         }
3488     }
3489
3490   /* Set up .got offsets for local syms, and space for local dynamic
3491      relocs.  */
3492   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3493     {
3494       bfd_signed_vma *local_got;
3495       bfd_signed_vma *end_local_got;
3496       char *local_tls_type;
3497       bfd_size_type locsymcount;
3498       Elf_Internal_Shdr *symtab_hdr;
3499       asection *srel;
3500
3501       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3502         continue;
3503
3504       for (s = ibfd->sections; s != NULL; s = s->next)
3505         {
3506           struct elf32_arm_relocs_copied *p;
3507
3508           for (p = *((struct elf32_arm_relocs_copied **)
3509                      &elf_section_data (s)->local_dynrel);
3510                p != NULL;
3511                p = p->next)
3512             {
3513               if (!bfd_is_abs_section (p->section)
3514                   && bfd_is_abs_section (p->section->output_section))
3515                 {
3516                   /* Input section has been discarded, either because
3517                      it is a copy of a linkonce section or due to
3518                      linker script /DISCARD/, so we'll be discarding
3519                      the relocs too.  */
3520                 }
3521               else if (p->count != 0)
3522                 {
3523                   srel = elf_section_data (p->section)->sreloc;
3524                   srel->_raw_size += p->count * sizeof (Elf32_External_Rel);
3525                   if ((p->section->output_section->flags & SEC_READONLY) != 0)
3526                     info->flags |= DF_TEXTREL;
3527                 }
3528             }
3529         }
3530
3531       local_got = elf_local_got_refcounts (ibfd);
3532       if (!local_got)
3533         continue;
3534
3535       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3536       locsymcount = symtab_hdr->sh_info;
3537       end_local_got = local_got + locsymcount;
3538       s = htab->sgot;
3539       srel = htab->srelgot;
3540       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3541         {
3542           if (*local_got > 0)
3543             {
3544               *local_got = s->_raw_size;
3545               s->_raw_size += 4;
3546               if (info->shared)
3547                 srel->_raw_size += sizeof (Elf32_External_Rel);
3548             }
3549           else
3550             *local_got = (bfd_vma) -1;
3551         }
3552     }
3553
3554   /* Allocate global sym .plt and .got entries, and space for global
3555      sym dynamic relocs.  */
3556   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
3557
3558   /* The check_relocs and adjust_dynamic_symbol entry points have
3559      determined the sizes of the various dynamic sections.  Allocate
3560      memory for them.  */
3561   plt = FALSE;
3562   relocs = FALSE;
3563   for (s = dynobj->sections; s != NULL; s = s->next)
3564     {
3565       const char * name;
3566       bfd_boolean strip;
3567
3568       if ((s->flags & SEC_LINKER_CREATED) == 0)
3569         continue;
3570
3571       /* It's OK to base decisions on the section name, because none
3572          of the dynobj section names depend upon the input files.  */
3573       name = bfd_get_section_name (dynobj, s);
3574
3575       strip = FALSE;
3576
3577       if (strcmp (name, ".plt") == 0)
3578         {
3579           if (s->_raw_size == 0)
3580             {
3581               /* Strip this section if we don't need it; see the
3582                  comment below.  */
3583               strip = TRUE;
3584             }
3585           else
3586             {
3587               /* Remember whether there is a PLT.  */
3588               plt = TRUE;
3589             }
3590         }
3591       else if (strncmp (name, ".rel", 4) == 0)
3592         {
3593           if (s->_raw_size == 0)
3594             {
3595               /* If we don't need this section, strip it from the
3596                  output file.  This is mostly to handle .rel.bss and
3597                  .rel.plt.  We must create both sections in
3598                  create_dynamic_sections, because they must be created
3599                  before the linker maps input sections to output
3600                  sections.  The linker does that before
3601                  adjust_dynamic_symbol is called, and it is that
3602                  function which decides whether anything needs to go
3603                  into these sections.  */
3604               strip = TRUE;
3605             }
3606           else
3607             {
3608               /* Remember whether there are any reloc sections other
3609                  than .rel.plt.  */
3610               if (strcmp (name, ".rel.plt") != 0)
3611                 relocs = TRUE;
3612
3613               /* We use the reloc_count field as a counter if we need
3614                  to copy relocs into the output file.  */
3615               s->reloc_count = 0;
3616             }
3617         }
3618       else if (strncmp (name, ".got", 4) != 0)
3619         {
3620           /* It's not one of our sections, so don't allocate space.  */
3621           continue;
3622         }
3623
3624       if (strip)
3625         {
3626           _bfd_strip_section_from_output (info, s);
3627           continue;
3628         }
3629
3630       /* Allocate memory for the section contents.  */
3631       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
3632       if (s->contents == NULL && s->_raw_size != 0)
3633         return FALSE;
3634     }
3635
3636   if (elf_hash_table (info)->dynamic_sections_created)
3637     {
3638       /* Add some entries to the .dynamic section.  We fill in the
3639          values later, in elf32_arm_finish_dynamic_sections, but we
3640          must add the entries now so that we get the correct size for
3641          the .dynamic section.  The DT_DEBUG entry is filled in by the
3642          dynamic linker and used by the debugger.  */
3643 #define add_dynamic_entry(TAG, VAL) \
3644   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3645
3646       if (!info->shared)
3647         {
3648           if (!add_dynamic_entry (DT_DEBUG, 0))
3649             return FALSE;
3650         }
3651
3652       if (plt)
3653         {
3654           if (   !add_dynamic_entry (DT_PLTGOT, 0)
3655               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3656               || !add_dynamic_entry (DT_PLTREL, DT_REL)
3657               || !add_dynamic_entry (DT_JMPREL, 0))
3658             return FALSE;
3659         }
3660
3661       if (relocs)
3662         {
3663           if (   !add_dynamic_entry (DT_REL, 0)
3664               || !add_dynamic_entry (DT_RELSZ, 0)
3665               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3666             return FALSE;
3667         }
3668
3669       if ((info->flags & DF_TEXTREL) != 0)
3670         {
3671           if (!add_dynamic_entry (DT_TEXTREL, 0))
3672             return FALSE;
3673           info->flags |= DF_TEXTREL;
3674         }
3675     }
3676 #undef add_synamic_entry
3677
3678   return TRUE;
3679 }
3680
3681 /* Finish up dynamic symbol handling.  We set the contents of various
3682    dynamic sections here.  */
3683
3684 static bfd_boolean
3685 elf32_arm_finish_dynamic_symbol (output_bfd, info, h, sym)
3686      bfd * output_bfd;
3687      struct bfd_link_info * info;
3688      struct elf_link_hash_entry * h;
3689      Elf_Internal_Sym * sym;
3690 {
3691   bfd * dynobj;
3692
3693   dynobj = elf_hash_table (info)->dynobj;
3694
3695   if (h->plt.offset != (bfd_vma) -1)
3696     {
3697       asection * splt;
3698       asection * sgot;
3699       asection * srel;
3700       bfd_vma plt_index;
3701       bfd_vma got_offset;
3702       Elf_Internal_Rela rel;
3703       bfd_byte *loc;
3704       bfd_vma got_displacement;
3705
3706       /* This symbol has an entry in the procedure linkage table.  Set
3707          it up.  */
3708
3709       BFD_ASSERT (h->dynindx != -1);
3710
3711       splt = bfd_get_section_by_name (dynobj, ".plt");
3712       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3713       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
3714       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
3715
3716       /* Get the index in the procedure linkage table which
3717          corresponds to this symbol.  This is the index of this symbol
3718          in all the symbols for which we are making plt entries.  The
3719          first entry in the procedure linkage table is reserved.  */
3720       plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3721
3722       /* Get the offset into the .got table of the entry that
3723          corresponds to this function.  Each .got entry is 4 bytes.
3724          The first three are reserved.  */
3725       got_offset = (plt_index + 3) * 4;
3726
3727       /* Calculate the displacement between the PLT slot and the
3728          entry in the GOT.  */
3729       got_displacement = (sgot->output_section->vma
3730                           + sgot->output_offset
3731                           + got_offset
3732                           - splt->output_section->vma
3733                           - splt->output_offset
3734                           - h->plt.offset
3735                           - 8);
3736
3737       BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
3738
3739       /* Fill in the entry in the procedure linkage table.  */
3740       bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
3741                   splt->contents + h->plt.offset + 0);
3742       bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
3743                   splt->contents + h->plt.offset + 4);
3744       bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
3745                   splt->contents + h->plt.offset + 8);
3746 #ifdef FOUR_WORD_PLT
3747       bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
3748                   splt->contents + h->plt.offset + 12);
3749 #endif
3750
3751       /* Fill in the entry in the global offset table.  */
3752       bfd_put_32 (output_bfd,
3753                   (splt->output_section->vma
3754                    + splt->output_offset),
3755                   sgot->contents + got_offset);
3756       
3757       /* Fill in the entry in the .rel.plt section.  */
3758       rel.r_offset = (sgot->output_section->vma
3759                       + sgot->output_offset
3760                       + got_offset);
3761       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
3762       loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
3763       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3764
3765       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3766         {
3767           /* Mark the symbol as undefined, rather than as defined in
3768              the .plt section.  Leave the value alone.  */
3769           sym->st_shndx = SHN_UNDEF;
3770           /* If the symbol is weak, we do need to clear the value.
3771              Otherwise, the PLT entry would provide a definition for
3772              the symbol even if the symbol wasn't defined anywhere,
3773              and so the symbol would never be NULL.  */
3774           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
3775               == 0)
3776             sym->st_value = 0;
3777         }
3778     }
3779
3780   if (h->got.offset != (bfd_vma) -1)
3781     {
3782       asection * sgot;
3783       asection * srel;
3784       Elf_Internal_Rela rel;
3785       bfd_byte *loc;
3786
3787       /* This symbol has an entry in the global offset table.  Set it
3788          up.  */
3789       sgot = bfd_get_section_by_name (dynobj, ".got");
3790       srel = bfd_get_section_by_name (dynobj, ".rel.got");
3791       BFD_ASSERT (sgot != NULL && srel != NULL);
3792
3793       rel.r_offset = (sgot->output_section->vma
3794                       + sgot->output_offset
3795                       + (h->got.offset &~ (bfd_vma) 1));
3796
3797       /* If this is a static link, or it is a -Bsymbolic link and the
3798          symbol is defined locally or was forced to be local because
3799          of a version file, we just want to emit a RELATIVE reloc.
3800          The entry in the global offset table will already have been
3801          initialized in the relocate_section function.  */
3802       if (info->shared
3803           && SYMBOL_REFERENCES_LOCAL (info, h))
3804         {
3805           BFD_ASSERT((h->got.offset & 1) != 0);
3806           rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
3807         }
3808       else
3809         {
3810           BFD_ASSERT((h->got.offset & 1) == 0);
3811           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3812           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
3813         }
3814
3815       loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
3816       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3817     }
3818
3819   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3820     {
3821       asection * s;
3822       Elf_Internal_Rela rel;
3823       bfd_byte *loc;
3824
3825       /* This symbol needs a copy reloc.  Set it up.  */
3826       BFD_ASSERT (h->dynindx != -1
3827                   && (h->root.type == bfd_link_hash_defined
3828                       || h->root.type == bfd_link_hash_defweak));
3829
3830       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3831                                    ".rel.bss");
3832       BFD_ASSERT (s != NULL);
3833
3834       rel.r_offset = (h->root.u.def.value
3835                       + h->root.u.def.section->output_section->vma
3836                       + h->root.u.def.section->output_offset);
3837       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
3838       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
3839       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3840     }
3841
3842   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3843   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3844       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3845     sym->st_shndx = SHN_ABS;
3846
3847   return TRUE;
3848 }
3849
3850 /* Finish up the dynamic sections.  */
3851
3852 static bfd_boolean
3853 elf32_arm_finish_dynamic_sections (output_bfd, info)
3854      bfd * output_bfd;
3855      struct bfd_link_info * info;
3856 {
3857   bfd * dynobj;
3858   asection * sgot;
3859   asection * sdyn;
3860
3861   dynobj = elf_hash_table (info)->dynobj;
3862
3863   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3864   BFD_ASSERT (sgot != NULL);
3865   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3866
3867   if (elf_hash_table (info)->dynamic_sections_created)
3868     {
3869       asection *splt;
3870       Elf32_External_Dyn *dyncon, *dynconend;
3871
3872       splt = bfd_get_section_by_name (dynobj, ".plt");
3873       BFD_ASSERT (splt != NULL && sdyn != NULL);
3874
3875       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3876       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3877
3878       for (; dyncon < dynconend; dyncon++)
3879         {
3880           Elf_Internal_Dyn dyn;
3881           const char * name;
3882           asection * s;
3883
3884           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3885
3886           switch (dyn.d_tag)
3887             {
3888             default:
3889               break;
3890
3891             case DT_PLTGOT:
3892               name = ".got";
3893               goto get_vma;
3894             case DT_JMPREL:
3895               name = ".rel.plt";
3896             get_vma:
3897               s = bfd_get_section_by_name (output_bfd, name);
3898               BFD_ASSERT (s != NULL);
3899               dyn.d_un.d_ptr = s->vma;
3900               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3901               break;
3902
3903             case DT_PLTRELSZ:
3904               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
3905               BFD_ASSERT (s != NULL);
3906               if (s->_cooked_size != 0)
3907                 dyn.d_un.d_val = s->_cooked_size;
3908               else
3909                 dyn.d_un.d_val = s->_raw_size;
3910               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3911               break;
3912
3913             case DT_RELSZ:
3914               /* My reading of the SVR4 ABI indicates that the
3915                  procedure linkage table relocs (DT_JMPREL) should be
3916                  included in the overall relocs (DT_REL).  This is
3917                  what Solaris does.  However, UnixWare can not handle
3918                  that case.  Therefore, we override the DT_RELSZ entry
3919                  here to make it not include the JMPREL relocs.  Since
3920                  the linker script arranges for .rel.plt to follow all
3921                  other relocation sections, we don't have to worry
3922                  about changing the DT_REL entry.  */
3923               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
3924               if (s != NULL)
3925                 {
3926                   if (s->_cooked_size != 0)
3927                     dyn.d_un.d_val -= s->_cooked_size;
3928                   else
3929                     dyn.d_un.d_val -= s->_raw_size;
3930                 }
3931               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3932               break;
3933
3934               /* Set the bottom bit of DT_INIT/FINI if the
3935                  corresponding function is Thumb.  */
3936             case DT_INIT:
3937               name = info->init_function;
3938               goto get_sym;
3939             case DT_FINI:
3940               name = info->fini_function;
3941             get_sym:
3942               /* If it wasn't set by elf_bfd_final_link
3943                  then there is nothing to adjust.  */
3944               if (dyn.d_un.d_val != 0)
3945                 {
3946                   struct elf_link_hash_entry * eh;
3947
3948                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
3949                                              FALSE, FALSE, TRUE);
3950                   if (eh != (struct elf_link_hash_entry *) NULL
3951                       && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
3952                     {
3953                       dyn.d_un.d_val |= 1;
3954                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3955                     }
3956                 }
3957               break;
3958             }
3959         }
3960
3961       /* Fill in the first entry in the procedure linkage table.  */
3962       if (splt->_raw_size > 0)
3963         {
3964           bfd_vma got_displacement;
3965
3966           /* Calculate the displacement between the PLT slot and &GOT[0].  */
3967           got_displacement = (sgot->output_section->vma
3968                               + sgot->output_offset
3969                               - splt->output_section->vma
3970                               - splt->output_offset
3971                               - 16);
3972
3973           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents +  0);
3974           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents +  4);
3975           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents +  8);
3976           bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
3977 #ifdef FOUR_WORD_PLT
3978           /* The displacement value goes in the otherwise-unused last word of
3979              the second entry.  */
3980           bfd_put_32 (output_bfd, got_displacement,        splt->contents + 28);
3981 #else
3982           bfd_put_32 (output_bfd, got_displacement,        splt->contents + 16);
3983 #endif
3984         }
3985
3986       /* UnixWare sets the entsize of .plt to 4, although that doesn't
3987          really seem like the right value.  */
3988       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
3989     }
3990
3991   /* Fill in the first three entries in the global offset table.  */
3992   if (sgot->_raw_size > 0)
3993     {
3994       if (sdyn == NULL)
3995         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3996       else
3997         bfd_put_32 (output_bfd,
3998                     sdyn->output_section->vma + sdyn->output_offset,
3999                     sgot->contents);
4000       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4001       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4002     }
4003
4004   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4005
4006   return TRUE;
4007 }
4008
4009 static void
4010 elf32_arm_post_process_headers (abfd, link_info)
4011      bfd * abfd;
4012      struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
4013 {
4014   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
4015
4016   i_ehdrp = elf_elfheader (abfd);
4017
4018   i_ehdrp->e_ident[EI_OSABI]      = ARM_ELF_OS_ABI_VERSION;
4019   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
4020 }
4021
4022 static enum elf_reloc_type_class
4023 elf32_arm_reloc_type_class (rela)
4024      const Elf_Internal_Rela *rela;
4025 {
4026   switch ((int) ELF32_R_TYPE (rela->r_info))
4027     {
4028     case R_ARM_RELATIVE:
4029       return reloc_class_relative;
4030     case R_ARM_JUMP_SLOT:
4031       return reloc_class_plt;
4032     case R_ARM_COPY:
4033       return reloc_class_copy;
4034     default:
4035       return reloc_class_normal;
4036     }
4037 }
4038
4039 static bfd_boolean elf32_arm_section_flags           PARAMS ((flagword *, Elf_Internal_Shdr *));
4040 static void        elf32_arm_final_write_processing  PARAMS ((bfd *, bfd_boolean));
4041
4042 /* Set the right machine number for an Arm ELF file.  */
4043
4044 static bfd_boolean
4045 elf32_arm_section_flags (flags, hdr)
4046      flagword *flags;
4047      Elf_Internal_Shdr *hdr;
4048 {
4049   if (hdr->sh_type == SHT_NOTE)
4050     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
4051
4052   return TRUE;
4053 }
4054
4055 void
4056 elf32_arm_final_write_processing (abfd, linker)
4057      bfd *abfd;
4058      bfd_boolean linker ATTRIBUTE_UNUSED;
4059 {
4060   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
4061 }
4062
4063 #define ELF_ARCH                        bfd_arch_arm
4064 #define ELF_MACHINE_CODE                EM_ARM
4065 #ifdef __QNXTARGET__
4066 #define ELF_MAXPAGESIZE                 0x1000
4067 #else
4068 #define ELF_MAXPAGESIZE                 0x8000
4069 #endif
4070
4071 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
4072 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
4073 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
4074 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
4075 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
4076 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
4077 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
4078
4079 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
4080 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
4081 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
4082 #define elf_backend_check_relocs                elf32_arm_check_relocs
4083 #define elf_backend_relocate_section            elf32_arm_relocate_section
4084 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
4085 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
4086 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
4087 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
4088 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
4089 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
4090 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
4091 #define elf_backend_object_p                    elf32_arm_object_p
4092 #define elf_backend_section_flags               elf32_arm_section_flags
4093 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
4094 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
4095
4096 #define elf_backend_can_refcount    1
4097 #define elf_backend_can_gc_sections 1
4098 #define elf_backend_plt_readonly    1
4099 #define elf_backend_want_got_plt    1
4100 #define elf_backend_want_plt_sym    0
4101 #if !USE_REL
4102 #define elf_backend_rela_normal     1
4103 #endif
4104
4105 #define elf_backend_got_header_size     12
4106
4107 #include "elf32-target.h"
4108