Applied patches from Philip Blundell <pb@nexus.co.uk>, to improve PIC support.
[external/binutils.git] / bfd / elf32-arm.h
1 /* 32-bit ELF support for ARM
2    Copyright 1998, 1999 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20
21 typedef unsigned long int insn32;
22 typedef unsigned short int insn16;
23
24 static boolean elf32_arm_set_private_flags
25   PARAMS ((bfd *, flagword));
26 static boolean elf32_arm_copy_private_bfd_data
27   PARAMS ((bfd *, bfd *));
28 static boolean elf32_arm_merge_private_bfd_data
29   PARAMS ((bfd *, bfd *));
30 static boolean elf32_arm_print_private_bfd_data
31   PARAMS ((bfd *, PTR));
32 static int elf32_arm_get_symbol_type 
33   PARAMS (( Elf_Internal_Sym *, int));
34 static struct bfd_link_hash_table *elf32_arm_link_hash_table_create
35   PARAMS ((bfd *));
36 static bfd_reloc_status_type elf32_arm_final_link_relocate
37   PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
38            Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
39            const char *, unsigned char, struct elf_link_hash_entry *));
40
41 static insn32 insert_thumb_branch
42   PARAMS ((insn32, int));
43 static struct elf_link_hash_entry *find_thumb_glue
44   PARAMS ((struct bfd_link_info *, CONST char *, bfd *));
45 static struct elf_link_hash_entry *find_arm_glue
46   PARAMS ((struct bfd_link_info *, CONST char *, bfd *));
47 static void record_arm_to_thumb_glue
48   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
49 static void record_thumb_to_arm_glue
50   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
51
52 /* The linker script knows the section names for placement.
53    The entry_names are used to do simple name mangling on the stubs.
54    Given a function name, and its type, the stub can be found. The
55    name can be changed. The only requirement is the %s be present.
56  */
57
58 #define INTERWORK_FLAG( abfd )   (elf_elfheader (abfd)->e_flags & EF_INTERWORK)
59
60 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
61 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
62
63 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
64 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
65
66 /* The name of the dynamic interpreter.  This is put in the .interp
67    section.  */
68 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
69
70 /* The size in bytes of an entry in the procedure linkage table.  */
71
72 #define PLT_ENTRY_SIZE 16
73
74 /* The first entry in a procedure linkage table looks like
75    this.  It is set up so that any shared library function that is
76    called before the relocation has been set up calles the dynamic
77    linker first */
78
79 static const bfd_byte elf32_arm_plt0_entry [PLT_ENTRY_SIZE] =
80 {
81   0x04, 0xe0, 0x2d, 0xe5,       /* str   lr, [sp, #-4]!     */
82   0x10, 0xe0, 0x9f, 0xe5,       /* ldr   lr, [pc, #16]      */
83   0x0e, 0xe0, 0x8f, 0xe0,       /* adr   lr, pc, lr         */
84   0x08, 0xf0, 0xbe, 0xe5        /* ldr   pc, [lr, #-4]      */
85 };
86
87 /* Subsequent entries in a procedure linkage table look like
88    this.  */
89
90 static const bfd_byte elf32_arm_plt_entry [PLT_ENTRY_SIZE] =
91 {
92   0x04, 0xc0, 0x9f, 0xe5,       /* ldr   ip, [pc, #4]       */
93   0x0c, 0xc0, 0x8f, 0xe0,       /* add   ip, pc, ip         */
94   0x00, 0xf0, 0x9c, 0xe5,       /* ldr   pc, [ip]           */
95   0x00, 0x00, 0x00, 0x00        /* offset to symbol in got  */
96 };
97
98
99 /* The ARM linker needs to keep track of the number of relocs that it
100    decides to copy in check_relocs for each symbol.  This is so that
101    it can discard PC relative relocs if it doesn't need them when
102    linking with -Bsymbolic.  We store the information in a field
103    extending the regular ELF linker hash table.  */
104
105 /* This structure keeps track of the number of PC relative relocs we
106    have copied for a given symbol.  */
107
108 struct elf32_arm_pcrel_relocs_copied
109 {
110   /* Next section.  */
111   struct elf32_arm_pcrel_relocs_copied * next;
112   /* A section in dynobj.  */
113   asection * section;
114   /* Number of relocs copied in this section.  */
115   bfd_size_type count;
116 };
117
118 /* arm ELF linker hash entry.  */
119
120 struct elf32_arm_link_hash_entry
121 {
122   struct elf_link_hash_entry root;
123
124   /* Number of PC relative relocs copied for this symbol.  */
125   struct elf32_arm_pcrel_relocs_copied * pcrel_relocs_copied;
126 };
127
128 /* Declare this now that the above structures are defined.  */
129
130 static boolean elf32_arm_discard_copies
131   PARAMS ((struct elf32_arm_link_hash_entry *, PTR));
132
133 /* Traverse an arm ELF linker hash table.  */
134
135 #define elf32_arm_link_hash_traverse(table, func, info)                 \
136   (elf_link_hash_traverse                                               \
137    (&(table)->root,                                                     \
138     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
139     (info)))
140
141 /* Get the ARM elf linker hash table from a link_info structure.  */
142 #define elf32_arm_hash_table(info) \
143   ((struct elf32_arm_link_hash_table *) ((info)->hash))
144
145 /* ARM ELF linker hash table */
146 struct elf32_arm_link_hash_table
147   {
148     /* The main hash table.  */
149     struct elf_link_hash_table root;
150
151     /* The size in bytes of the section containg the Thumb-to-ARM glue.  */
152     long int thumb_glue_size;
153
154     /* The size in bytes of the section containg the ARM-to-Thumb glue.  */
155     long int arm_glue_size;
156
157     /* An arbitary input BFD chosen to hold the glue sections.  */
158     bfd * bfd_of_glue_owner;
159   };
160
161
162 /* Create an entry in an ARM ELF linker hash table.  */
163
164 static struct bfd_hash_entry *
165 elf32_arm_link_hash_newfunc (entry, table, string)
166      struct bfd_hash_entry * entry;
167      struct bfd_hash_table * table;
168      const char * string;
169 {
170   struct elf32_arm_link_hash_entry * ret =
171     (struct elf32_arm_link_hash_entry *) entry;
172
173   /* Allocate the structure if it has not already been allocated by a
174      subclass.  */
175   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
176     ret = ((struct elf32_arm_link_hash_entry *)
177            bfd_hash_allocate (table,
178                               sizeof (struct elf32_arm_link_hash_entry)));
179   if (ret == (struct elf32_arm_link_hash_entry *) NULL)
180     return (struct bfd_hash_entry *) ret;
181
182   /* Call the allocation method of the superclass.  */
183   ret = ((struct elf32_arm_link_hash_entry *)
184          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
185                                      table, string));
186   if (ret != (struct elf32_arm_link_hash_entry *) NULL)
187     ret->pcrel_relocs_copied = NULL;
188
189   return (struct bfd_hash_entry *) ret;
190 }
191
192 /* Create an ARM elf linker hash table */
193
194 static struct bfd_link_hash_table *
195 elf32_arm_link_hash_table_create (abfd)
196      bfd *abfd;
197 {
198   struct elf32_arm_link_hash_table *ret;
199
200   ret = ((struct elf32_arm_link_hash_table *)
201          bfd_alloc (abfd, sizeof (struct elf32_arm_link_hash_table)));
202   if (ret == (struct elf32_arm_link_hash_table *) NULL)
203     return NULL;
204
205   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
206                                       elf32_arm_link_hash_newfunc))
207     {
208       bfd_release (abfd, ret);
209       return NULL;
210     }
211
212   ret->thumb_glue_size = 0;
213   ret->arm_glue_size = 0;
214   ret->bfd_of_glue_owner = NULL;
215
216   return &ret->root.root;
217 }
218
219 static struct elf_link_hash_entry *
220 find_thumb_glue (link_info, name, input_bfd)
221      struct bfd_link_info *link_info;
222      CONST char *name;
223      bfd *input_bfd;
224 {
225   char *tmp_name;
226   struct elf_link_hash_entry *hash;
227   struct elf32_arm_link_hash_table *hash_table;
228
229   /* We need a pointer to the armelf specific hash table.  */
230   hash_table = elf32_arm_hash_table (link_info);
231
232
233   tmp_name = ((char *)
234        bfd_malloc (strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1));
235
236   BFD_ASSERT (tmp_name);
237
238   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
239
240   hash = elf_link_hash_lookup
241     (&(hash_table)->root, tmp_name, false, false, true);
242
243   if (hash == NULL)
244     /* xgettext:c-format */
245     _bfd_error_handler (_ ("%s: unable to find THUMB glue '%s' for `%s'"),
246                         bfd_get_filename (input_bfd), tmp_name, name);
247
248   free (tmp_name);
249
250   return hash;
251 }
252
253 static struct elf_link_hash_entry *
254 find_arm_glue (link_info, name, input_bfd)
255      struct bfd_link_info *link_info;
256      CONST char *name;
257      bfd *input_bfd;
258 {
259   char *tmp_name;
260   struct elf_link_hash_entry *myh;
261   struct elf32_arm_link_hash_table *hash_table;
262
263   /* We need a pointer to the elfarm specific hash table.  */
264   hash_table = elf32_arm_hash_table (link_info);
265
266   tmp_name = ((char *)
267        bfd_malloc (strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1));
268
269   BFD_ASSERT (tmp_name);
270
271   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
272
273   myh = elf_link_hash_lookup
274     (&(hash_table)->root, tmp_name, false, false, true);
275
276   if (myh == NULL)
277     /* xgettext:c-format */
278     _bfd_error_handler (_ ("%s: unable to find ARM glue '%s' for `%s'"),
279                         bfd_get_filename (input_bfd), tmp_name, name);
280
281   free (tmp_name);
282
283   return myh;
284 }
285
286 /*
287    ARM->Thumb glue:
288
289    .arm
290    __func_from_arm:
291    ldr r12, __func_addr
292    bx  r12
293    __func_addr:
294    .word func    @ behave as if you saw a ARM_32 reloc
295  */
296
297 #define ARM2THUMB_GLUE_SIZE 12
298 static const insn32 a2t1_ldr_insn = 0xe59fc000;
299 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
300 static const insn32 a2t3_func_addr_insn = 0x00000001;
301
302 /*
303    Thumb->ARM:                          Thumb->(non-interworking aware) ARM
304
305    .thumb                               .thumb
306    .align 2                             .align 2
307    __func_from_thumb:              __func_from_thumb:
308    bx pc                                push {r6, lr}
309    nop                                  ldr  r6, __func_addr
310    .arm                                         mov  lr, pc
311    __func_change_to_arm:                        bx   r6
312    b func                       .arm
313    __func_back_to_thumb:
314    ldmia r13! {r6, lr}
315    bx    lr
316    __func_addr:
317    .word        func 
318  */
319
320 #define THUMB2ARM_GLUE_SIZE 8
321 static const insn16 t2a1_bx_pc_insn = 0x4778;
322 static const insn16 t2a2_noop_insn = 0x46c0;
323 static const insn32 t2a3_b_insn = 0xea000000;
324
325 static const insn16 t2a1_push_insn = 0xb540;
326 static const insn16 t2a2_ldr_insn = 0x4e03;
327 static const insn16 t2a3_mov_insn = 0x46fe;
328 static const insn16 t2a4_bx_insn = 0x4730;
329 static const insn32 t2a5_pop_insn = 0xe8bd4040;
330 static const insn32 t2a6_bx_insn = 0xe12fff1e;
331
332 boolean
333 bfd_elf32_arm_allocate_interworking_sections (info)
334      struct bfd_link_info * info;
335 {
336   asection * s;
337   bfd_byte * foo;
338   struct elf32_arm_link_hash_table * globals;
339
340   globals = elf32_arm_hash_table (info);
341
342   BFD_ASSERT (globals != NULL);
343
344   if (globals->arm_glue_size != 0)
345     {
346       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
347
348       s = bfd_get_section_by_name
349         (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
350
351       BFD_ASSERT (s != NULL);
352
353       foo = (bfd_byte *) bfd_alloc
354         (globals->bfd_of_glue_owner, globals->arm_glue_size);
355
356       s->_raw_size = s->_cooked_size = globals->arm_glue_size;
357       s->contents = foo;
358     }
359
360   if (globals->thumb_glue_size != 0)
361     {
362       BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
363
364       s = bfd_get_section_by_name
365         (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
366
367       BFD_ASSERT (s != NULL);
368
369       foo = (bfd_byte *) bfd_alloc
370         (globals->bfd_of_glue_owner, globals->thumb_glue_size);
371
372       s->_raw_size = s->_cooked_size = globals->thumb_glue_size;
373       s->contents = foo;
374     }
375
376   return true;
377 }
378
379 static void
380 record_arm_to_thumb_glue (link_info, h)
381      struct bfd_link_info * link_info;
382      struct elf_link_hash_entry * h;
383 {
384   const char * name = h->root.root.string;
385   register asection * s;
386   char * tmp_name;
387   struct elf_link_hash_entry * myh;
388   struct elf32_arm_link_hash_table * globals;
389
390   globals = elf32_arm_hash_table (link_info);
391
392   BFD_ASSERT (globals != NULL);
393   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
394
395   s = bfd_get_section_by_name
396     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
397
398
399   BFD_ASSERT (s != NULL);
400
401   tmp_name = ((char *)
402        bfd_malloc (strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1));
403
404   BFD_ASSERT (tmp_name);
405
406   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
407
408   myh = elf_link_hash_lookup
409     (&(globals)->root, tmp_name, false, false, true);
410
411   if (myh != NULL)
412     {
413       free (tmp_name);
414       return;                   /* we've already seen this guy */
415     }
416
417   /* The only trick here is using hash_table->arm_glue_size as the value. Even
418      though the section isn't allocated yet, this is where we will be putting
419      it.  */
420
421   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner, tmp_name,
422                                     BSF_GLOBAL,
423                                     s, globals->arm_glue_size + 1,
424                                     NULL, true, false,
425                                     (struct bfd_link_hash_entry **) &myh);
426
427   free (tmp_name);
428
429   globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
430
431   return;
432 }
433
434 static void
435 record_thumb_to_arm_glue (link_info, h)
436      struct bfd_link_info *link_info;
437      struct elf_link_hash_entry *h;
438 {
439   const char *name = h->root.root.string;
440   register asection *s;
441   char *tmp_name;
442   struct elf_link_hash_entry *myh;
443   struct elf32_arm_link_hash_table *hash_table;
444   char bind;
445
446   hash_table = elf32_arm_hash_table (link_info);
447
448   BFD_ASSERT (hash_table != NULL);
449   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
450
451   s = bfd_get_section_by_name
452     (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
453
454   BFD_ASSERT (s != NULL);
455
456   tmp_name = (char *) bfd_malloc (strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
457
458   BFD_ASSERT (tmp_name);
459
460   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
461
462   myh = elf_link_hash_lookup
463     (&(hash_table)->root, tmp_name, false, false, true);
464
465   if (myh != NULL)
466     {
467       free (tmp_name);
468       return;                   /* we've already seen this guy */
469     }
470
471   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner, tmp_name,
472                              BSF_GLOBAL, s, hash_table->thumb_glue_size + 1,
473                                     NULL, true, false,
474                                     (struct bfd_link_hash_entry **) &myh);
475
476   /* If we mark it 'thumb', the disassembler will do a better job.  */
477   bind = ELF_ST_BIND (myh->type);
478   myh->type = ELF_ST_INFO (bind, STT_ARM_TFUNC);
479
480   free (tmp_name);
481
482   /* Allocate another symbol to mark where we switch to arm mode.  */
483
484 #define CHANGE_TO_ARM "__%s_change_to_arm"
485 #define BACK_FROM_ARM "__%s_back_from_arm"
486
487   tmp_name = (char *) bfd_malloc (strlen (name) + strlen (CHANGE_TO_ARM) + 1);
488
489   BFD_ASSERT (tmp_name);
490
491   sprintf (tmp_name, CHANGE_TO_ARM, name);
492
493   myh = NULL;
494
495   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner, tmp_name,
496                               BSF_LOCAL, s, hash_table->thumb_glue_size + 4,
497                                     NULL, true, false,
498                                     (struct bfd_link_hash_entry **) &myh);
499
500   free (tmp_name);
501
502   hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
503
504   return;
505 }
506
507 /* Select a BFD to be used to hold the sections used by the glue code.
508    This function is called from the linker scripts in ld/emultempl/
509    {armelf/pe}.em  */
510 boolean
511 bfd_elf32_arm_get_bfd_for_interworking (abfd, info)
512      bfd *abfd;
513      struct bfd_link_info *info;
514 {
515   struct elf32_arm_link_hash_table *globals;
516   flagword flags;
517   asection *sec;
518
519   /* If we are only performing a partial link do not bother
520      getting a bfd to hold the glue.  */
521   if (info->relocateable)
522     return true;
523
524   globals = elf32_arm_hash_table (info);
525
526   BFD_ASSERT (globals != NULL);
527
528   if (globals->bfd_of_glue_owner != NULL)
529     return true;
530
531   sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
532
533   if (sec == NULL)
534     {
535       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
536
537       sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
538
539       if (sec == NULL
540           || !bfd_set_section_flags (abfd, sec, flags)
541           || !bfd_set_section_alignment (abfd, sec, 2))
542         return false;
543     }
544
545   sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
546
547   if (sec == NULL)
548     {
549       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
550
551       sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
552
553       if (sec == NULL
554           || !bfd_set_section_flags (abfd, sec, flags)
555           || !bfd_set_section_alignment (abfd, sec, 2))
556         return false;
557     }
558
559   /* Save the bfd for later use.  */
560   globals->bfd_of_glue_owner = abfd;
561
562   return true;
563 }
564
565 boolean
566 bfd_elf32_arm_process_before_allocation (abfd, link_info)
567      bfd *abfd;
568      struct bfd_link_info *link_info;
569 {
570   Elf_Internal_Shdr *symtab_hdr;
571   Elf_Internal_Rela *free_relocs = NULL;
572   Elf_Internal_Rela *irel, *irelend;
573   bfd_byte *contents = NULL;
574   bfd_byte *free_contents = NULL;
575   Elf32_External_Sym *extsyms = NULL;
576   Elf32_External_Sym *free_extsyms = NULL;
577
578   asection *sec;
579   struct elf32_arm_link_hash_table *globals;
580
581   /* If we are only performing a partial link do not bother
582      to construct any glue.  */
583   if (link_info->relocateable)
584     return true;
585
586   /* Here we have a bfd that is to be included on the link.  We have a hook
587      to do reloc rummaging, before section sizes are nailed down.  */
588
589   globals = elf32_arm_hash_table (link_info);
590
591   BFD_ASSERT (globals != NULL);
592   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
593
594   /* Rummage around all the relocs and map the glue vectors.  */
595   sec = abfd->sections;
596
597   if (sec == NULL)
598     return true;
599
600   for (; sec != NULL; sec = sec->next)
601     {
602       if (sec->reloc_count == 0)
603         continue;
604
605       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
606       /* Load the relocs.  */
607
608       irel = (_bfd_elf32_link_read_relocs (abfd, sec, (PTR) NULL,
609                                         (Elf_Internal_Rela *) NULL, false));
610
611       BFD_ASSERT (irel != 0);
612
613       irelend = irel + sec->reloc_count;
614       for (; irel < irelend; irel++)
615         {
616           long r_type;
617           unsigned long r_index;
618           unsigned char code;
619
620           struct elf_link_hash_entry *h;
621
622           r_type = ELF32_R_TYPE (irel->r_info);
623           r_index = ELF32_R_SYM (irel->r_info);
624
625           /* These are the only relocation types we care about */
626           if (r_type != R_ARM_PC24
627               && r_type != R_ARM_THM_PC22)
628             continue;
629
630           /* Get the section contents if we haven't done so already.  */
631           if (contents == NULL)
632             {
633               /* Get cached copy if it exists.  */
634               if (elf_section_data (sec)->this_hdr.contents != NULL)
635                 contents = elf_section_data (sec)->this_hdr.contents;
636               else
637                 {
638                   /* Go get them off disk.  */
639                   contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
640                   if (contents == NULL)
641                     goto error_return;
642                   free_contents = contents;
643
644                   if (!bfd_get_section_contents (abfd, sec, contents,
645                                               (file_ptr) 0, sec->_raw_size))
646                     goto error_return;
647                 }
648             }
649
650           /* Read this BFD's symbols if we haven't done so already.  */
651           if (extsyms == NULL)
652             {
653               /* Get cached copy if it exists.  */
654               if (symtab_hdr->contents != NULL)
655                 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
656               else
657                 {
658                   /* Go get them off disk.  */
659                   extsyms = ((Elf32_External_Sym *)
660                              bfd_malloc (symtab_hdr->sh_size));
661                   if (extsyms == NULL)
662                     goto error_return;
663                   free_extsyms = extsyms;
664                   if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
665                       || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
666                           != symtab_hdr->sh_size))
667                     goto error_return;
668                 }
669             }
670
671           /* If the relocation is not against a symbol it cannot concern us. */
672
673           h = NULL;
674
675           /* We don't care about local symbols */
676           if (r_index < symtab_hdr->sh_info)
677             continue;
678
679           /* This is an external symbol */
680           r_index -= symtab_hdr->sh_info;
681           h = (struct elf_link_hash_entry *)
682             elf_sym_hashes (abfd)[r_index];
683
684           /* If the relocation is against a static symbol it must be within
685              the current section and so cannot be a cross ARM/Thumb relocation.  */
686           if (h == NULL)
687             continue;
688
689           switch (r_type)
690             {
691             case R_ARM_PC24:
692               /* This one is a call from arm code.  We need to look up
693                  the target of the call. If it is a thumb target, we
694                  insert glue.  */
695
696               if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
697                 record_arm_to_thumb_glue (link_info, h);
698               break;
699
700             case R_ARM_THM_PC22:
701               /* This one is a call from thumb code.  We look 
702                  up the target of the call. If it is not a thumb
703                  target, we insert glue. */ 
704
705               if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
706                 record_thumb_to_arm_glue (link_info, h);
707               break;
708
709             default:
710               break;
711             }
712         }
713     }
714
715   return true;
716 error_return:
717   if (free_relocs != NULL)
718     free (free_relocs);
719   if (free_contents != NULL)
720     free (free_contents);
721   if (free_extsyms != NULL)
722     free (free_extsyms);
723   return false;
724
725 }
726
727 /* The thumb form of a long branch is a bit finicky, because the offset
728    encoding is split over two fields, each in it's own instruction. They
729    can occur in any order. So given a thumb form of long branch, and an 
730    offset, insert the offset into the thumb branch and return finished
731    instruction. 
732
733    It takes two thumb instructions to encode the target address. Each has 
734    11 bits to invest. The upper 11 bits are stored in one (identifed by
735    H-0.. see below), the lower 11 bits are stored in the other (identified 
736    by H-1). 
737
738    Combine together and shifted left by 1 (it's a half word address) and 
739    there you have it.
740
741    Op: 1111 = F,
742    H-0, upper address-0 = 000
743    Op: 1111 = F,
744    H-1, lower address-0 = 800
745
746    They can be ordered either way, but the arm tools I've seen always put 
747    the lower one first. It probably doesn't matter. krk@cygnus.com
748
749    XXX:  Actually the order does matter.  The second instruction (H-1)
750    moves the computed address into the PC, so it must be the second one
751    in the sequence.  The problem, however is that whilst little endian code
752    stores the instructions in HI then LOW order, big endian code does the
753    reverse.  nickc@cygnus.com  */
754
755 #define LOW_HI_ORDER 0xF800F000
756 #define HI_LOW_ORDER 0xF000F800
757
758 static insn32
759 insert_thumb_branch (br_insn, rel_off)
760      insn32 br_insn;
761      int rel_off;
762 {
763   unsigned int low_bits;
764   unsigned int high_bits;
765
766
767   BFD_ASSERT ((rel_off & 1) != 1);
768
769   rel_off >>= 1;                /* half word aligned address */
770   low_bits = rel_off & 0x000007FF;      /* the bottom 11 bits */
771   high_bits = (rel_off >> 11) & 0x000007FF;     /* the top 11 bits */
772
773   if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
774     br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
775   else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
776     br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
777   else
778     abort ();                   /* error - not a valid branch instruction form */
779
780   /* FIXME: abort is probably not the right call. krk@cygnus.com */
781
782   return br_insn;
783 }
784
785 /* Thumb code calling an ARM function */
786 static int
787 elf32_thumb_to_arm_stub (info, name, input_bfd, output_bfd, input_section,
788                          hit_data, sym_sec, offset, addend, val)
789      struct bfd_link_info *info;
790      char *name;
791      bfd *input_bfd;
792      bfd *output_bfd;
793      asection *input_section;
794      bfd_byte *hit_data;
795      asection *sym_sec;
796      int offset;
797      int addend;
798      bfd_vma val;
799 {
800   asection *s = 0;
801   long int my_offset;
802   unsigned long int tmp;
803   long int ret_offset;
804   struct elf_link_hash_entry *myh;
805   struct elf32_arm_link_hash_table *globals;
806
807   myh = find_thumb_glue (info, name, input_bfd);
808   if (myh == NULL)
809     return false;
810
811   globals = elf32_arm_hash_table (info);
812
813   BFD_ASSERT (globals != NULL);
814   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
815
816   my_offset = myh->root.u.def.value;
817
818   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
819                                THUMB2ARM_GLUE_SECTION_NAME);
820
821   BFD_ASSERT (s != NULL);
822   BFD_ASSERT (s->contents != NULL);
823   BFD_ASSERT (s->output_section != NULL);
824
825   if ((my_offset & 0x01) == 0x01)
826     {
827       if (sym_sec != NULL
828           && sym_sec->owner != NULL
829           && !INTERWORK_FLAG (sym_sec->owner))
830         {
831           _bfd_error_handler
832             (_ ("%s(%s): warning: interworking not enabled."),
833              bfd_get_filename (sym_sec->owner), name);
834           _bfd_error_handler
835             (_ ("  first occurrence: %s: thumb call to arm"),
836              bfd_get_filename (input_bfd));
837
838           return false;
839         }
840
841       --my_offset;
842       myh->root.u.def.value = my_offset;
843
844       bfd_put_16 (output_bfd, t2a1_bx_pc_insn,
845                   s->contents + my_offset);
846
847       bfd_put_16 (output_bfd, t2a2_noop_insn,
848                   s->contents + my_offset + 2);
849
850       ret_offset =
851         ((bfd_signed_vma) val)  /* Address of destination of the stub */
852         - ((bfd_signed_vma)
853            (s->output_offset    /* Offset from the start of the current section to the start of the stubs.  */
854             + my_offset         /* Offset of the start of this stub from the start of the stubs.  */
855             + s->output_section->vma)   /* Address of the start of the current section.  */
856            + 4                  /* The branch instruction is 4 bytes into the stub.  */
857            + 8);                /* ARM branches work from the pc of the instruction + 8.  */
858
859       bfd_put_32 (output_bfd,
860                   t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
861                   s->contents + my_offset + 4);
862     }
863
864   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
865
866   /* Now go back and fix up the original BL insn to point
867      to here.  */
868   ret_offset =
869     s->output_offset
870     + my_offset
871     - (input_section->output_offset
872        + offset + addend)
873     - 4;
874
875   tmp = bfd_get_32 (input_bfd, hit_data
876                     - input_section->vma);
877
878   bfd_put_32 (output_bfd,
879               insert_thumb_branch (tmp, ret_offset),
880               hit_data - input_section->vma);
881
882   return true;
883 }
884
885 /* Arm code calling a Thumb function */
886 static int
887 elf32_arm_to_thumb_stub (info, name, input_bfd, output_bfd, input_section,
888                          hit_data, sym_sec, offset, addend, val)
889
890      struct bfd_link_info *info;
891      char *name;
892      bfd *input_bfd;
893      bfd *output_bfd;
894      asection *input_section;
895      bfd_byte *hit_data;
896      asection *sym_sec;
897      int offset;
898      int addend;
899      bfd_vma val;
900 {
901   unsigned long int tmp;
902   long int my_offset;
903   asection *s;
904   long int ret_offset;
905   struct elf_link_hash_entry *myh;
906   struct elf32_arm_link_hash_table *globals;
907
908   myh = find_arm_glue (info, name, input_bfd);
909   if (myh == NULL)
910     return false;
911
912   globals = elf32_arm_hash_table (info);
913
914   BFD_ASSERT (globals != NULL);
915   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
916
917   my_offset = myh->root.u.def.value;
918   s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
919                                ARM2THUMB_GLUE_SECTION_NAME);
920   BFD_ASSERT (s != NULL);
921   BFD_ASSERT (s->contents != NULL);
922   BFD_ASSERT (s->output_section != NULL);
923
924   if ((my_offset & 0x01) == 0x01)
925     {
926       if (sym_sec != NULL
927           && sym_sec->owner != NULL
928           && !INTERWORK_FLAG (sym_sec->owner))
929         {
930           _bfd_error_handler
931             (_ ("%s(%s): warning: interworking not enabled."),
932              bfd_get_filename (sym_sec->owner), name);
933           _bfd_error_handler
934             (_ ("  first occurrence: %s: arm call to thumb"),
935              bfd_get_filename (input_bfd));
936         }
937       --my_offset;
938       myh->root.u.def.value = my_offset;
939
940       bfd_put_32 (output_bfd, a2t1_ldr_insn,
941                   s->contents + my_offset);
942
943       bfd_put_32 (output_bfd, a2t2_bx_r12_insn,
944                   s->contents + my_offset + 4);
945
946       /* It's a thumb address.  Add the low order bit.  */
947       bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
948                   s->contents + my_offset + 8);
949     }
950
951   BFD_ASSERT (my_offset <= globals->arm_glue_size);
952
953   tmp = bfd_get_32 (input_bfd, hit_data);
954   tmp = tmp & 0xFF000000;
955
956   /* Somehow these are both 4 too far, so subtract 8. */
957   ret_offset = s->output_offset
958     + my_offset
959     + s->output_section->vma
960     - (input_section->output_offset
961        + input_section->output_section->vma
962        + offset + addend)
963     - 8;
964
965   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
966
967   bfd_put_32 (output_bfd, tmp, hit_data
968               - input_section->vma);
969
970
971   return true;
972 }
973
974 /* Perform a relocation as part of a final link.  */
975 static bfd_reloc_status_type
976 elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
977                                input_section, contents, rel, value,
978                                info, sym_sec, sym_name, sym_flags, h)
979      reloc_howto_type *     howto;
980      bfd *                  input_bfd;
981      bfd *                  output_bfd;
982      asection *             input_section;
983      bfd_byte *             contents;
984      Elf_Internal_Rela *    rel;
985      bfd_vma                value;
986      struct bfd_link_info * info;
987      asection *             sym_sec;
988      const char *           sym_name;
989      unsigned char          sym_flags;
990      struct elf_link_hash_entry * h;
991 {
992   unsigned long                 r_type = howto->type;
993   unsigned long                 r_symndx;
994   bfd_byte *                    hit_data = contents + rel->r_offset;
995   bfd *                         dynobj = NULL;
996   Elf_Internal_Shdr *           symtab_hdr;
997   struct elf_link_hash_entry ** sym_hashes;
998   bfd_vma *                     local_got_offsets;
999   asection *                    sgot = NULL;
1000   asection *                    splt = NULL;
1001   asection *                    sreloc = NULL;
1002   bfd_vma                       addend;
1003     
1004   dynobj = elf_hash_table (info)->dynobj;
1005   if (dynobj)
1006     {
1007       sgot = bfd_get_section_by_name (dynobj, ".got");
1008       splt = bfd_get_section_by_name (dynobj, ".plt");
1009     }
1010   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1011   sym_hashes = elf_sym_hashes (input_bfd);
1012   local_got_offsets = elf_local_got_offsets (input_bfd);
1013   r_symndx = ELF32_R_SYM (rel->r_info);
1014
1015 #ifdef USE_REL
1016   addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask);
1017 #else
1018   addend = rel->r_addend;
1019 #endif
1020   
1021   switch (r_type)
1022     {
1023     case R_ARM_NONE:
1024       return bfd_reloc_ok;
1025
1026     case R_ARM_PC24:
1027     case R_ARM_ABS32:
1028     case R_ARM_REL32:
1029       /* When generating a shared object, these relocations are copied
1030          into the output file to be resolved at run time. */
1031       
1032       if (info->shared
1033           && (r_type != R_ARM_PC24
1034               || (h != NULL
1035                   && h->dynindx != -1
1036                   && (! info->symbolic
1037                       || (h->elf_link_hash_flags
1038                           & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1039         {
1040           Elf_Internal_Rel outrel;
1041           boolean skip, relocate;
1042           
1043           if (sreloc == NULL)
1044             {
1045               const char * name;
1046               
1047               name = (bfd_elf_string_from_elf_section
1048                       (input_bfd,
1049                        elf_elfheader (input_bfd)->e_shstrndx,
1050                        elf_section_data (input_section)->rel_hdr.sh_name));
1051               if (name == NULL)
1052                 return bfd_reloc_notsupported;
1053               
1054               BFD_ASSERT (strncmp (name, ".rel", 4) == 0
1055                           && strcmp (bfd_get_section_name (input_bfd,
1056                                                            input_section),
1057                                      name + 4) == 0);
1058               
1059               sreloc = bfd_get_section_by_name (dynobj, name);
1060               BFD_ASSERT (sreloc != NULL);
1061             }
1062           
1063           skip = false;
1064           
1065           if (elf_section_data (input_section)->stab_info == NULL)
1066             outrel.r_offset = rel->r_offset;
1067           else
1068             {
1069               bfd_vma off;
1070               
1071               off = (_bfd_stab_section_offset
1072                      (output_bfd, &elf_hash_table (info)->stab_info,
1073                       input_section,
1074                       & elf_section_data (input_section)->stab_info,
1075                       rel->r_offset));
1076               if (off == (bfd_vma) -1)
1077                 skip = true;
1078               outrel.r_offset = off;
1079             }
1080           
1081           outrel.r_offset += (input_section->output_section->vma
1082                               + input_section->output_offset);
1083           
1084           if (skip)
1085             {
1086               memset (&outrel, 0, sizeof outrel);
1087               relocate = false;
1088             }
1089           else if (r_type == R_ARM_PC24)
1090             {
1091               BFD_ASSERT (h != NULL && h->dynindx != -1);
1092               if ((input_section->flags & SEC_ALLOC) != 0)
1093                 relocate = false;
1094               else
1095                 relocate = true;
1096               outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_PC24);
1097             }
1098           else
1099             {
1100               if (h == NULL
1101                   || ((info->symbolic || h->dynindx == -1)
1102                       && (h->elf_link_hash_flags
1103                           & ELF_LINK_HASH_DEF_REGULAR) != 0))
1104                 {
1105                   relocate = true;
1106                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1107                 }
1108               else
1109                 {
1110                   BFD_ASSERT (h->dynindx != -1);
1111                   if ((input_section->flags & SEC_ALLOC) != 0)
1112                     relocate = false;
1113                   else
1114                     relocate = true;
1115                   outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_ABS32);
1116                 }
1117             }
1118           
1119           bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1120                                     (((Elf32_External_Rel *)
1121                                       sreloc->contents)
1122                                      + sreloc->reloc_count));
1123           ++sreloc->reloc_count;
1124           
1125           /* If this reloc is against an external symbol, we do not want to 
1126              fiddle with the addend.  Otherwise, we need to include the symbol
1127              value so that it becomes an addend for the dynamic reloc. */
1128           if (! relocate)
1129             return bfd_reloc_ok;
1130           
1131           return _bfd_final_link_relocate (howto, input_bfd, input_section, 
1132                                            contents, rel->r_offset, value,
1133                                            (bfd_vma) 0);
1134         }
1135       else switch (r_type)
1136         {
1137         case R_ARM_PC24:
1138           /* Arm B/BL instruction */
1139           
1140           /* Check for arm calling thumb function.  */
1141           if (sym_flags == STT_ARM_TFUNC)
1142             {
1143               elf32_arm_to_thumb_stub (info, sym_name, input_bfd, output_bfd,
1144                                        input_section, hit_data, sym_sec, rel->r_offset, addend, value);
1145               return bfd_reloc_ok;
1146             }
1147           
1148           value = value + addend;
1149           value -= (input_section->output_section->vma
1150                     + input_section->output_offset + 8);
1151           value -= rel->r_offset;
1152           value = value >> howto->rightshift;
1153           
1154           value &= 0xffffff;
1155           value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
1156           break;
1157           
1158         case R_ARM_ABS32:
1159           value += addend;
1160           if (sym_flags == STT_ARM_TFUNC)
1161             value |= 1;
1162           break;
1163           
1164         case R_ARM_REL32:
1165           value -= (input_section->output_section->vma
1166                     + input_section->output_offset);
1167           value += addend;
1168           break;
1169         }
1170       
1171       bfd_put_32 (input_bfd, value, hit_data);
1172       return bfd_reloc_ok;
1173
1174     case R_ARM_ABS8:
1175       value += addend;
1176       if ((long) value > 0x7f || (long) value < -0x80)
1177         return bfd_reloc_overflow;
1178
1179       bfd_put_8 (input_bfd, value, hit_data);
1180       return bfd_reloc_ok;
1181
1182     case R_ARM_ABS16:
1183       value += addend;
1184
1185       if ((long) value > 0x7fff || (long) value < -0x8000)
1186         return bfd_reloc_overflow;
1187
1188       bfd_put_16 (input_bfd, value, hit_data);
1189       return bfd_reloc_ok;
1190
1191     case R_ARM_ABS12:
1192       /* Support ldr and str instruction for the arm */
1193       /* Also thumb b (unconditional branch).  ??? Really?  */
1194       value += addend;
1195
1196       if ((long) value > 0x7ff || (long) value < -0x800)
1197         return bfd_reloc_overflow;
1198
1199       value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
1200       bfd_put_32 (input_bfd, value, hit_data);
1201       return bfd_reloc_ok;
1202
1203     case R_ARM_THM_ABS5:
1204       /* Support ldr and str instructions for the thumb. */
1205 #ifdef USE_REL
1206       /* Need to refetch addend.  */
1207       addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1208       /* ??? Need to determine shift amount from operand size.  */
1209       addend >>= howto->rightshift;
1210 #endif
1211       value += addend;
1212
1213       /* ??? Isn't value unsigned?  */
1214       if ((long) value > 0x1f || (long) value < -0x10)
1215         return bfd_reloc_overflow;
1216
1217       /* ??? Value needs to be properly shifted into place first.  */
1218       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
1219       bfd_put_16 (input_bfd, value, hit_data);
1220       return bfd_reloc_ok;
1221
1222     case R_ARM_THM_PC22:
1223       /* Thumb BL (branch long instruction). */
1224       {
1225         bfd_vma relocation;
1226         boolean overflow = false;
1227         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
1228         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
1229         bfd_vma src_mask = 0x007FFFFE;
1230         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1231         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1232         bfd_vma check;
1233         bfd_signed_vma signed_check;
1234         bfd_vma add;
1235         bfd_signed_vma signed_add;
1236
1237 #ifdef USE_REL
1238         /* Need to refetch the addend and squish the two 11 bit pieces
1239            together.  */
1240         {
1241           bfd_vma upper = bfd_get_16 (input_bfd, hit_data) & 0x7ff;
1242           bfd_vma lower = bfd_get_16 (input_bfd, hit_data + 2) & 0x7ff;
1243           upper = (upper ^ 0x400) - 0x400; /* sign extend */
1244           addend = (upper << 12) | (lower << 1);
1245         }
1246 #endif
1247
1248         /* If it's not a call to thumb, assume call to arm */
1249         if (sym_flags != STT_ARM_TFUNC)
1250           {
1251             if (elf32_thumb_to_arm_stub
1252                 (info, sym_name, input_bfd, output_bfd, input_section,
1253                  hit_data, sym_sec, rel->r_offset, addend, value))
1254               return bfd_reloc_ok;
1255             else
1256               return bfd_reloc_dangerous;
1257           }
1258
1259         /* +4: pc is offset by 4 */
1260         relocation = value + addend + 4;
1261         relocation -= (input_section->output_section->vma
1262                        + input_section->output_offset);
1263         relocation -= rel->r_offset;
1264
1265         check = relocation >> howto->rightshift;
1266
1267         /* If this is a signed value, the rightshift just dropped
1268            leading 1 bits (assuming twos complement).  */
1269         if ((bfd_signed_vma) relocation >= 0)
1270           signed_check = check;
1271         else
1272           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
1273
1274         add = ((upper_insn & 0x7ff) << 12) | ((lower_insn & 0x7ff) << 1);
1275         /* sign extend */
1276         signed_add = (add ^ 0x400000) - 0x400000;
1277
1278         /* Add the value from the object file.  */
1279         signed_check += signed_add;
1280         relocation += signed_add;
1281
1282         /* Assumes two's complement.  */
1283         if (signed_check > reloc_signed_max
1284             || signed_check < reloc_signed_min)
1285           overflow = true;
1286
1287         /* Put RELOCATION back into the insn.  */
1288         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
1289         lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
1290
1291         /* Put the relocated value back in the object file:  */
1292         bfd_put_16 (input_bfd, upper_insn, hit_data);
1293         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
1294
1295         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
1296       }
1297       break;
1298
1299     case R_ARM_GNU_VTINHERIT:
1300     case R_ARM_GNU_VTENTRY:
1301       return bfd_reloc_ok;
1302
1303     case R_ARM_COPY:
1304       return bfd_reloc_notsupported;
1305
1306     case R_ARM_GLOB_DAT:
1307       return bfd_reloc_notsupported;
1308
1309     case R_ARM_JUMP_SLOT:
1310       return bfd_reloc_notsupported;
1311
1312     case R_ARM_RELATIVE:
1313       return bfd_reloc_notsupported;
1314
1315     case R_ARM_GOTOFF:
1316       /* Relocation is relative to the start of the
1317          global offset table.  */
1318
1319       BFD_ASSERT (sgot != NULL);
1320       if (sgot == NULL)
1321         return bfd_reloc_notsupported;
1322         
1323       /* Note that sgot->output_offset is not involved in this
1324          calculation.  We always want the start of .got.  If we
1325          define _GLOBAL_OFFSET_TABLE in a different way, as is
1326          permitted by the ABI, we might have to change this
1327          calculation. */
1328       
1329       value -= sgot->output_section->vma;
1330       return _bfd_final_link_relocate (howto, input_bfd, input_section, 
1331                                        contents, rel->r_offset, value,
1332                                        (bfd_vma) 0);
1333
1334     case R_ARM_GOTPC:
1335       /* Use global offset table as symbol value. */
1336
1337       BFD_ASSERT (sgot != NULL);
1338       
1339       if (sgot == NULL)
1340         return bfd_reloc_notsupported;
1341
1342       value = sgot->output_section->vma;
1343       return _bfd_final_link_relocate (howto, input_bfd, input_section, 
1344                                        contents, rel->r_offset, value,
1345                                        (bfd_vma) 0);
1346       
1347     case R_ARM_GOT32:
1348       /* Relocation is to the entry for this symbol in the
1349          global offset table. */
1350       if (sgot == NULL)
1351         return bfd_reloc_notsupported;
1352       
1353       if (h != NULL)
1354         {
1355           bfd_vma off;
1356           
1357           off = h->got.offset;
1358           BFD_ASSERT (off != (bfd_vma) -1);
1359           
1360           if (!elf_hash_table (info)->dynamic_sections_created ||
1361               (info->shared && (info->symbolic || h->dynindx == -1)
1362                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1363             {
1364               /* This is actually a static link, or it is a -Bsymbolic link
1365                  and the symbol is defined locally.  We must initialize this
1366                  entry in the global offset table.  Since the offset must
1367                  always be a multiple of 4, we use the least significant bit
1368                  to record whether we have initialized it already.
1369                  
1370                  When doing a dynamic link, we create a .rel.got relocation
1371                  entry to initialize the value.  This is done in the 
1372                  finish_dynamic_symbol routine. */
1373               
1374               if ((off & 1) != 0)
1375                 off &= ~1;
1376               else
1377                 {
1378                   bfd_put_32 (output_bfd, value, sgot->contents + off);
1379                   h->got.offset |= 1;
1380                 }
1381             }
1382           
1383           value = sgot->output_offset + off;
1384         }
1385       else
1386         {
1387           bfd_vma off;
1388           
1389           BFD_ASSERT (local_got_offsets != NULL &&
1390                       local_got_offsets[r_symndx] != (bfd_vma) -1);
1391           
1392           off = local_got_offsets[r_symndx];
1393           
1394           /* The offset must always be a multiple of 4.  We use the
1395              least significant bit to record whether we have already
1396              generated the necessary reloc. */
1397           if ((off & 1) != 0)
1398             off &= ~1;
1399           else
1400             {
1401               bfd_put_32 (output_bfd, value, sgot->contents + off);
1402               
1403               if (info->shared)
1404                 {
1405                   asection * srelgot;
1406                   Elf_Internal_Rel outrel;
1407                   
1408                   srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1409                   BFD_ASSERT (srelgot != NULL);
1410                   
1411                   outrel.r_offset = (sgot->output_section->vma
1412                                      + sgot->output_offset 
1413                                      + off);
1414                   outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1415                   bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1416                                             (((Elf32_External_Rel *)
1417                                               srelgot->contents)
1418                                              + srelgot->reloc_count));
1419                   ++srelgot->reloc_count;
1420                 }
1421               
1422               local_got_offsets[r_symndx] |= 1;
1423             }
1424           
1425           value = sgot->output_offset + off;
1426         }
1427       
1428       return _bfd_final_link_relocate (howto, input_bfd, input_section, 
1429                                        contents, rel->r_offset, value,
1430                                        (bfd_vma) 0);
1431       
1432     case R_ARM_PLT32:
1433       /* Relocation is to the entry for this symbol in the
1434          procedure linkage table.  */
1435
1436       /* Resolve a PLT32 reloc against a local symbol directly,
1437          without using the procedure linkage table. */
1438       if (h == NULL)
1439         return _bfd_final_link_relocate (howto, input_bfd, input_section,
1440                                          contents, rel->r_offset, value,
1441                                          (bfd_vma) 0);
1442
1443       if (h->plt.offset == (bfd_vma) -1)
1444         /* We didn't make a PLT entry for this symbol.  This
1445            happens when statically linking PIC code, or when
1446            using -Bsymbolic.  */
1447         return _bfd_final_link_relocate (howto, input_bfd, input_section,
1448                                          contents, rel->r_offset, value,
1449                                          (bfd_vma) 0);
1450
1451       BFD_ASSERT(splt != NULL);
1452       if (splt == NULL)
1453         return bfd_reloc_notsupported;
1454
1455       value = (splt->output_section->vma
1456                + splt->output_offset
1457                + h->plt.offset);
1458       return _bfd_final_link_relocate (howto, input_bfd, input_section,
1459                                        contents, rel->r_offset, value,
1460                                        (bfd_vma) 0);
1461       
1462     case R_ARM_SBREL32:
1463       return bfd_reloc_notsupported;
1464
1465     case R_ARM_AMP_VCALL9:
1466       return bfd_reloc_notsupported;
1467
1468     case R_ARM_RSBREL32:
1469       return bfd_reloc_notsupported;
1470
1471     case R_ARM_THM_RPC22:
1472       return bfd_reloc_notsupported;
1473
1474     case R_ARM_RREL32:
1475       return bfd_reloc_notsupported;
1476
1477     case R_ARM_RABS32:
1478       return bfd_reloc_notsupported;
1479
1480     case R_ARM_RPC24:
1481       return bfd_reloc_notsupported;
1482
1483     case R_ARM_RBASE:
1484       return bfd_reloc_notsupported;
1485
1486     default:
1487       return bfd_reloc_notsupported;
1488     }
1489 }
1490
1491
1492 /* Relocate an ARM ELF section.  */
1493 static boolean
1494 elf32_arm_relocate_section (output_bfd, info, input_bfd, input_section,
1495                             contents, relocs, local_syms, local_sections)
1496      bfd *                  output_bfd;
1497      struct bfd_link_info * info;
1498      bfd *                  input_bfd;
1499      asection *             input_section;
1500      bfd_byte *             contents;
1501      Elf_Internal_Rela *    relocs;
1502      Elf_Internal_Sym *     local_syms;
1503      asection **            local_sections;
1504 {
1505   Elf_Internal_Shdr *           symtab_hdr;
1506   struct elf_link_hash_entry ** sym_hashes;
1507   Elf_Internal_Rela *           rel;
1508   Elf_Internal_Rela *           relend;
1509   const char *                  name;
1510
1511   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1512   sym_hashes = elf_sym_hashes (input_bfd);
1513
1514   rel = relocs;
1515   relend = relocs + input_section->reloc_count;
1516   for (; rel < relend; rel++)
1517     {
1518       int r_type;
1519       reloc_howto_type * howto;
1520       unsigned long r_symndx;
1521       Elf_Internal_Sym * sym;
1522       asection * sec;
1523       struct elf_link_hash_entry * h;
1524       bfd_vma relocation;
1525       bfd_reloc_status_type r;
1526
1527       r_symndx = ELF32_R_SYM (rel->r_info);
1528       r_type = ELF32_R_TYPE (rel->r_info);
1529
1530       if (r_type == R_ARM_GNU_VTENTRY
1531           || r_type == R_ARM_GNU_VTINHERIT )
1532         continue;
1533
1534       /* ScottB: range check r_type here. */
1535       
1536       howto = elf32_arm_howto_table + r_type;
1537
1538       if (info->relocateable)
1539         {
1540           /* This is a relocateable link.  We don't have to change
1541              anything, unless the reloc is against a section symbol,
1542              in which case we have to adjust according to where the
1543              section symbol winds up in the output section.  */
1544           if (r_symndx < symtab_hdr->sh_info)
1545             {
1546               sym = local_syms + r_symndx;
1547               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1548                 {
1549                   sec = local_sections[r_symndx];
1550 #ifdef USE_REL
1551                   {
1552                     bfd_vma val;
1553                     val = bfd_get_32 (input_bfd, contents + rel->r_offset);
1554                     val += (sec->output_offset + sym->st_value);
1555                     bfd_put_32 (input_bfd, val, contents + rel->r_offset);
1556                   }
1557 #else
1558                   rel->r_addend += (sec->output_offset + sym->st_value)
1559                     >> howto->rightshift;
1560 #endif
1561                 }
1562             }
1563
1564           continue;
1565         }
1566
1567       /* This is a final link.  */
1568       h = NULL;
1569       sym = NULL;
1570       sec = NULL;
1571       if (r_symndx < symtab_hdr->sh_info)
1572         {
1573           sym = local_syms + r_symndx;
1574           sec = local_sections[r_symndx];
1575           relocation = (sec->output_section->vma
1576                         + sec->output_offset
1577                         + sym->st_value);
1578         }
1579       else
1580         {
1581           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1582           while (h->root.type == bfd_link_hash_indirect
1583                  || h->root.type == bfd_link_hash_warning)
1584             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1585           if (h->root.type == bfd_link_hash_defined
1586               || h->root.type == bfd_link_hash_defweak)
1587             {
1588               int relocation_needed = 1;
1589               
1590               sec = h->root.u.def.section;
1591               
1592               /* In these cases, we don't need the relocation value.
1593                  We check specially because in some obscure cases 
1594                  sec->output_section will be NULL. */
1595               switch (r_type)
1596                 {
1597                 case R_ARM_PC24:
1598                 case R_ARM_ABS32:
1599                   if (info->shared
1600                       && (
1601                           (!info->symbolic && h->dynindx != -1)
1602                           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1603                           )
1604                       && ((input_section->flags & SEC_ALLOC) != 0)
1605                       )
1606                     relocation_needed = 0;
1607                   break;
1608                   
1609                 case R_ARM_GOTPC:
1610                   relocation_needed = 0;
1611                   break;
1612                   
1613                 case R_ARM_GOT32:
1614                   if (elf_hash_table(info)->dynamic_sections_created
1615                       && (!info->shared
1616                           || (!info->symbolic && h->dynindx != -1)
1617                           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1618                           )
1619                       )
1620                     relocation_needed = 0;
1621                   break;
1622                   
1623                 case R_ARM_PLT32:
1624                   if (h->plt.offset != (bfd_vma)-1)
1625                     relocation_needed = 0;
1626                   break;
1627                   
1628                 default:
1629                   if (sec->output_section == NULL)
1630                     {
1631                       (*_bfd_error_handler)
1632                         (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1633                          bfd_get_filename (input_bfd), h->root.root.string,
1634                          bfd_get_section_name (input_bfd, input_section));
1635                       relocation_needed = 0;
1636                     }
1637                 }
1638
1639               if (relocation_needed)
1640                 relocation = h->root.u.def.value
1641                   + sec->output_section->vma
1642                   + sec->output_offset;
1643               else
1644                 relocation = 0;
1645             }
1646           else if (h->root.type == bfd_link_hash_undefweak)
1647             relocation = 0;
1648           else
1649             {
1650               if (!((*info->callbacks->undefined_symbol)
1651                     (info, h->root.root.string, input_bfd,
1652                      input_section, rel->r_offset)))
1653                 return false;
1654               relocation = 0;
1655             }
1656         }
1657
1658       if (h != NULL)
1659         name = h->root.root.string;
1660       else
1661         {
1662           name = (bfd_elf_string_from_elf_section
1663                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
1664           if (name == NULL || *name == '\0')
1665             name = bfd_section_name (input_bfd, sec);
1666         }
1667       
1668       r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
1669                                          input_section, contents, rel,
1670                                          relocation, info, sec, name,
1671                                          (h ? ELF_ST_TYPE (h->type) :
1672                                           ELF_ST_TYPE (sym->st_info)), h);
1673
1674       if (r != bfd_reloc_ok)
1675         {
1676           const char * msg = (const char *) 0;
1677
1678           switch (r)
1679             {
1680             case bfd_reloc_overflow:
1681               if (!((*info->callbacks->reloc_overflow)
1682                     (info, name, howto->name, (bfd_vma) 0,
1683                      input_bfd, input_section, rel->r_offset)))
1684                 return false;
1685               break;
1686
1687             case bfd_reloc_undefined:
1688               if (!((*info->callbacks->undefined_symbol)
1689                     (info, name, input_bfd, input_section,
1690                      rel->r_offset)))
1691                 return false;
1692               break;
1693
1694             case bfd_reloc_outofrange:
1695               msg = _ ("internal error: out of range error");
1696               goto common_error;
1697
1698             case bfd_reloc_notsupported:
1699               msg = _ ("internal error: unsupported relocation error");
1700               goto common_error;
1701
1702             case bfd_reloc_dangerous:
1703               msg = _ ("internal error: dangerous error");
1704               goto common_error;
1705
1706             default:
1707               msg = _ ("internal error: unknown error");
1708               /* fall through */
1709
1710             common_error:
1711               if (!((*info->callbacks->warning)
1712                     (info, msg, name, input_bfd, input_section,
1713                      rel->r_offset)))
1714                 return false;
1715               break;
1716             }
1717         }
1718     }
1719
1720   return true;
1721 }
1722
1723 /* Function to keep ARM specific flags in the ELF header. */
1724 static boolean
1725 elf32_arm_set_private_flags (abfd, flags)
1726      bfd *abfd;
1727      flagword flags;
1728 {
1729   if (elf_flags_init (abfd)
1730       && elf_elfheader (abfd)->e_flags != flags)
1731     {
1732       if (flags & EF_INTERWORK)
1733         _bfd_error_handler (_ ("\
1734 Warning: Not setting interwork flag of %s since it has already been specified as non-interworking"),
1735                             bfd_get_filename (abfd));
1736       else
1737         _bfd_error_handler (_ ("\
1738 Warning: Clearing the interwork flag of %s due to outside request"),
1739                             bfd_get_filename (abfd));
1740     }
1741   else
1742     {
1743       elf_elfheader (abfd)->e_flags = flags;
1744       elf_flags_init (abfd) = true;
1745     }
1746
1747   return true;
1748 }
1749
1750 /* Copy backend specific data from one object module to another */
1751 static boolean
1752 elf32_arm_copy_private_bfd_data (ibfd, obfd)
1753      bfd *ibfd;
1754      bfd *obfd;
1755 {
1756   flagword in_flags;
1757   flagword out_flags;
1758
1759   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1760       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1761     return true;
1762
1763   in_flags = elf_elfheader (ibfd)->e_flags;
1764   out_flags = elf_elfheader (obfd)->e_flags;
1765
1766   if (elf_flags_init (obfd) && in_flags != out_flags)
1767     {
1768       /* Cannot mix PIC and non-PIC code.  */
1769       if ((in_flags & EF_PIC) != (out_flags & EF_PIC))
1770         return false;
1771
1772       /* Cannot mix APCS26 and APCS32 code.  */
1773       if ((in_flags & EF_APCS_26) != (out_flags & EF_APCS_26))
1774         return false;
1775
1776       /* Cannot mix float APCS and non-float APCS code.  */
1777       if ((in_flags & EF_APCS_FLOAT) != (out_flags & EF_APCS_FLOAT))
1778         return false;
1779
1780       /* If the src and dest have different interworking flags
1781          then turn off the interworking bit.  */
1782       if ((in_flags & EF_INTERWORK) != (out_flags & EF_INTERWORK))
1783         {
1784           if (out_flags & EF_INTERWORK)
1785             _bfd_error_handler (_ ("\
1786 Warning: Clearing the interwork flag in %s because non-interworking code in %s has been linked with it"),
1787                           bfd_get_filename (obfd), bfd_get_filename (ibfd));
1788
1789           in_flags &= ~EF_INTERWORK;
1790         }
1791     }
1792
1793   elf_elfheader (obfd)->e_flags = in_flags;
1794   elf_flags_init (obfd) = true;
1795
1796   return true;
1797 }
1798
1799 /* Merge backend specific data from an object file to the output
1800    object file when linking.  */
1801 static boolean
1802 elf32_arm_merge_private_bfd_data (ibfd, obfd)
1803      bfd *ibfd;
1804      bfd *obfd;
1805 {
1806   flagword out_flags;
1807   flagword in_flags;
1808
1809   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1810       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1811     return true;
1812
1813   /* Check if we have the same endianess */
1814   if (   ibfd->xvec->byteorder != obfd->xvec->byteorder
1815       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1816       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1817     {
1818       (*_bfd_error_handler)
1819         (_("%s: compiled for a %s endian system and target is %s endian"),
1820          bfd_get_filename (ibfd),
1821          bfd_big_endian (ibfd) ? "big" : "little",
1822          bfd_big_endian (obfd) ? "big" : "little");
1823
1824       bfd_set_error (bfd_error_wrong_format);
1825       return false;
1826     }
1827
1828   /* The input BFD must have had its flags initialised.  */
1829   /* The following seems bogus to me -- The flags are initialized in
1830      the assembler but I don't think an elf_flags_init field is
1831      written into the object */
1832   /* BFD_ASSERT (elf_flags_init (ibfd)); */
1833
1834   in_flags  = elf_elfheader (ibfd)->e_flags;
1835   out_flags = elf_elfheader (obfd)->e_flags;
1836
1837   if (!elf_flags_init (obfd))
1838     {
1839       /* If the input is the default architecture then do not
1840          bother setting the flags for the output architecture,
1841          instead allow future merges to do this.  If no future
1842          merges ever set these flags then they will retain their
1843          unitialised values, which surprise surprise, correspond
1844          to the default values.  */
1845       if (bfd_get_arch_info (ibfd)->the_default)
1846         return true;
1847
1848       elf_flags_init (obfd) = true;
1849       elf_elfheader (obfd)->e_flags = in_flags;
1850
1851       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1852           && bfd_get_arch_info (obfd)->the_default)
1853         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1854
1855       return true;
1856     }
1857
1858   /* Check flag compatibility.  */
1859   if (in_flags == out_flags)
1860     return true;
1861
1862   /* Complain about various flag mismatches.  */
1863
1864   if ((in_flags & EF_APCS_26) != (out_flags & EF_APCS_26))
1865     _bfd_error_handler (_ ("\
1866 Error: %s compiled for APCS-%d, whereas %s is compiled for APCS-%d"),
1867                         bfd_get_filename (ibfd),
1868                         in_flags & EF_APCS_26 ? 26 : 32,
1869                         bfd_get_filename (obfd),
1870                         out_flags & EF_APCS_26 ? 26 : 32);
1871
1872   if ((in_flags & EF_APCS_FLOAT) != (out_flags & EF_APCS_FLOAT))
1873     _bfd_error_handler (_ ("\
1874 Error: %s passes floats in %s registers, whereas %s passes them in %s registers"),
1875                         bfd_get_filename (ibfd),
1876                      in_flags & EF_APCS_FLOAT ? _ ("float") : _ ("integer"),
1877                         bfd_get_filename (obfd),
1878                       out_flags & EF_APCS_26 ? _ ("float") : _ ("integer"));
1879
1880   if ((in_flags & EF_PIC) != (out_flags & EF_PIC))
1881     _bfd_error_handler (_ ("\
1882 Error: %s is compiled as position %s code, whereas %s is not"),
1883                         bfd_get_filename (ibfd),
1884                     in_flags & EF_PIC ? _ ("independent") : _ ("dependent"),
1885                         bfd_get_filename (obfd));
1886
1887   /* Interworking mismatch is only a warning. */
1888   if ((in_flags & EF_INTERWORK) != (out_flags & EF_INTERWORK))
1889     {
1890       _bfd_error_handler (_ ("\
1891 Warning: %s %s interworking, whereas %s %s"),
1892                           bfd_get_filename (ibfd),
1893           in_flags & EF_INTERWORK ? _ ("supports") : _ ("does not support"),
1894                           bfd_get_filename (obfd),
1895                     out_flags & EF_INTERWORK ? _ ("does not") : _ ("does"));
1896       return true;
1897     }
1898
1899   return false;
1900 }
1901
1902 /* Display the flags field */
1903 static boolean
1904 elf32_arm_print_private_bfd_data (abfd, ptr)
1905      bfd *abfd;
1906      PTR ptr;
1907 {
1908   FILE *file = (FILE *) ptr;
1909
1910   BFD_ASSERT (abfd != NULL && ptr != NULL);
1911
1912   /* Print normal ELF private data.  */
1913   _bfd_elf_print_private_bfd_data (abfd, ptr);
1914
1915   /* Ignore init flag - it may not be set, despite the flags field containing valid data.  */
1916
1917   /* xgettext:c-format */
1918   fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1919
1920   if (elf_elfheader (abfd)->e_flags & EF_INTERWORK)
1921     fprintf (file, _ (" [interworking enabled]"));
1922   else
1923     fprintf (file, _ (" [interworking not enabled]"));
1924
1925   if (elf_elfheader (abfd)->e_flags & EF_APCS_26)
1926     fprintf (file, _ (" [APCS-26]"));
1927   else
1928     fprintf (file, _ (" [APCS-32]"));
1929
1930   if (elf_elfheader (abfd)->e_flags & EF_APCS_FLOAT)
1931     fprintf (file, _ (" [floats passed in float registers]"));
1932   else
1933     fprintf (file, _ (" [floats passed in integer registers]"));
1934
1935   if (elf_elfheader (abfd)->e_flags & EF_PIC)
1936     fprintf (file, _ (" [position independent]"));
1937   else
1938     fprintf (file, _ (" [absolute position]"));
1939
1940   fputc ('\n', file);
1941
1942   return true;
1943 }
1944
1945 static int
1946 elf32_arm_get_symbol_type (elf_sym, type)
1947      Elf_Internal_Sym * elf_sym;
1948      int type;
1949 {
1950   if (ELF_ST_TYPE (elf_sym->st_info) == STT_ARM_TFUNC)
1951     return ELF_ST_TYPE (elf_sym->st_info);
1952   else
1953     return type;
1954 }
1955     
1956 static asection *
1957 elf32_arm_gc_mark_hook (abfd, info, rel, h, sym)
1958        bfd *abfd;
1959        struct bfd_link_info *info;
1960        Elf_Internal_Rela *rel;
1961        struct elf_link_hash_entry *h;
1962        Elf_Internal_Sym *sym;
1963 {
1964   if (h != NULL)
1965     {
1966       switch (ELF32_R_TYPE (rel->r_info))
1967       {
1968       case R_ARM_GNU_VTINHERIT:
1969       case R_ARM_GNU_VTENTRY:
1970         break;
1971
1972       default:
1973         switch (h->root.type)
1974           {
1975           case bfd_link_hash_defined:
1976           case bfd_link_hash_defweak:
1977             return h->root.u.def.section;
1978
1979           case bfd_link_hash_common:
1980             return h->root.u.c.p->section;
1981           }
1982        }
1983      }
1984    else
1985      {
1986        if (!(elf_bad_symtab (abfd)
1987            && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1988          && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1989                 && sym->st_shndx != SHN_COMMON))
1990           {
1991             return bfd_section_from_elf_index (abfd, sym->st_shndx);
1992           }
1993       }
1994   return NULL;
1995 }
1996
1997 /* Update the got entry reference counts for the section being removed.  */
1998
1999 static boolean
2000 elf32_arm_gc_sweep_hook (abfd, info, sec, relocs)
2001      bfd *abfd;
2002      struct bfd_link_info *info;
2003      asection *sec;
2004      const Elf_Internal_Rela *relocs;
2005 {
2006   /* We don't support garbage collection of GOT and PLT relocs yet.  */
2007   return true;
2008 }
2009
2010 /* Look through the relocs for a section during the first phase.  */
2011
2012 static boolean
2013 elf32_arm_check_relocs (abfd, info, sec, relocs)
2014      bfd *                      abfd;
2015      struct bfd_link_info *     info;
2016      asection *                 sec;
2017      const Elf_Internal_Rela *  relocs;
2018 {
2019   Elf_Internal_Shdr *           symtab_hdr;
2020   struct elf_link_hash_entry ** sym_hashes;
2021   struct elf_link_hash_entry ** sym_hashes_end;
2022   const Elf_Internal_Rela *     rel;
2023   const Elf_Internal_Rela *     rel_end;
2024   bfd *                         dynobj;
2025   asection * sgot, *srelgot, *sreloc;
2026   bfd_vma * local_got_offsets;
2027    
2028   if (info->relocateable)
2029     return true;
2030  
2031   sgot = srelgot = sreloc = NULL;
2032                                    
2033   dynobj = elf_hash_table (info)->dynobj;
2034   local_got_offsets = elf_local_got_offsets (abfd);
2035   
2036   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2037   sym_hashes = elf_sym_hashes (abfd);
2038   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
2039   if (!elf_bad_symtab (abfd))
2040     sym_hashes_end -= symtab_hdr->sh_info;
2041  
2042   rel_end = relocs + sec->reloc_count;
2043   for (rel = relocs; rel < rel_end; rel++)
2044     {
2045       struct elf_link_hash_entry *h;
2046       unsigned long r_symndx;
2047  
2048       r_symndx = ELF32_R_SYM (rel->r_info);
2049       if (r_symndx < symtab_hdr->sh_info)
2050         h = NULL;
2051       else
2052         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2053  
2054       /* Some relocs require a global offset table.  */
2055       if (dynobj == NULL)
2056         {
2057           switch (ELF32_R_TYPE (rel->r_info))
2058             {
2059             case R_ARM_GOT32:
2060             case R_ARM_GOTOFF:
2061             case R_ARM_GOTPC:
2062               elf_hash_table (info)->dynobj = dynobj = abfd;
2063               if (! _bfd_elf_create_got_section (dynobj, info))
2064                 return false;
2065               break;
2066
2067             default:
2068               break;
2069             }
2070         }
2071
2072       switch (ELF32_R_TYPE (rel->r_info))
2073         {
2074           case R_ARM_GOT32:
2075             /* This symbol requires a global offset table entry.  */
2076             if (sgot == NULL)
2077               {
2078                 sgot = bfd_get_section_by_name (dynobj, ".got");
2079                 BFD_ASSERT (sgot != NULL);
2080               }
2081
2082             /* Get the got relocation section if necessary.  */
2083             if (srelgot == NULL
2084                 && (h != NULL || info->shared))
2085               {
2086                 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
2087               
2088                 /* If no got relocation section, make one and initialize.  */
2089                 if (srelgot == NULL)
2090                   {
2091                     srelgot = bfd_make_section (dynobj, ".rel.got");
2092                     if (srelgot == NULL
2093                         || ! bfd_set_section_flags (dynobj, srelgot,
2094                                                     (SEC_ALLOC
2095                                                      | SEC_LOAD
2096                                                      | SEC_HAS_CONTENTS
2097                                                      | SEC_IN_MEMORY
2098                                                      | SEC_LINKER_CREATED
2099                                                      | SEC_READONLY))
2100                         || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2101                       return false;
2102                   }
2103               }
2104
2105             if (h != NULL)
2106               {
2107                 if (h->got.offset != (bfd_vma) -1)
2108                   /* We have already allocated space in the .got.  */
2109                   break;
2110                 
2111                 h->got.offset = sgot->_raw_size;
2112
2113                 /* Make sure this symbol is output as a dynamic symbol.  */
2114                 if (h->dynindx == -1)
2115                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2116                     return false;
2117
2118                 srelgot->_raw_size += sizeof (Elf32_External_Rel);
2119               }
2120             else
2121               {
2122                 /* This is a global offset table entry for a local
2123                    symbol.  */
2124                 if (local_got_offsets == NULL)
2125                   {
2126                     size_t size;
2127                     register unsigned int i;
2128
2129                     size = symtab_hdr->sh_info * sizeof (bfd_vma);
2130                     local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
2131                     if (local_got_offsets == NULL)
2132                       return false;
2133                     elf_local_got_offsets (abfd) = local_got_offsets;
2134                     for (i = 0; i < symtab_hdr->sh_info; i++)
2135                       local_got_offsets[i] = (bfd_vma) -1;
2136                   }
2137                 
2138                 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
2139                   /* We have already allocated space in the .got.  */
2140                   break;
2141
2142                 local_got_offsets[r_symndx] = sgot->_raw_size;
2143
2144                 if (info->shared)
2145                   /* If we are generating a shared object, we need to
2146                      output a R_ARM_RELATIVE reloc so that the dynamic
2147                      linker can adjust this GOT entry.  */
2148                   srelgot->_raw_size += sizeof (Elf32_External_Rel);
2149               }
2150
2151             sgot->_raw_size += 4;
2152             break;
2153
2154           case R_ARM_PLT32:
2155             /* This symbol requires a procedure linkage table entry.  We
2156                actually build the entry in adjust_dynamic_symbol,
2157                because this might be a case of linking PIC code which is
2158                never referenced by a dynamic object, in which case we
2159                don't need to generate a procedure linkage table entry
2160                after all.  */
2161
2162             /* If this is a local symbol, we resolve it directly without
2163                creating a procedure linkage table entry.  */
2164             if (h == NULL)
2165               continue;
2166
2167             h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2168             break;
2169
2170           case R_ARM_ABS32:
2171           case R_ARM_REL32:
2172           case R_ARM_PC24:
2173             /* If we are creating a shared library, and this is a reloc
2174                against a global symbol, or a non PC relative reloc
2175                against a local symbol, then we need to copy the reloc
2176                into the shared library.  However, if we are linking with
2177                -Bsymbolic, we do not need to copy a reloc against a
2178                global symbol which is defined in an object we are
2179                including in the link (i.e., DEF_REGULAR is set).  At
2180                this point we have not seen all the input files, so it is
2181                possible that DEF_REGULAR is not set now but will be set
2182                later (it is never cleared).  We account for that
2183                possibility below by storing information in the
2184                pcrel_relocs_copied field of the hash table entry.  */
2185             if (info->shared
2186               && (ELF32_R_TYPE (rel->r_info) != R_ARM_PC24
2187                 || (h != NULL
2188                   && (! info->symbolic
2189                     || (h->elf_link_hash_flags
2190                       & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2191               {
2192                 /* When creating a shared object, we must copy these
2193                    reloc types into the output file.  We create a reloc
2194                    section in dynobj and make room for this reloc.  */
2195                 if (sreloc == NULL)
2196                   {
2197                     const char * name;
2198
2199                     name = (bfd_elf_string_from_elf_section
2200                             (abfd,
2201                              elf_elfheader (abfd)->e_shstrndx,
2202                              elf_section_data (sec)->rel_hdr.sh_name));
2203                     if (name == NULL)
2204                       return false;
2205
2206                     BFD_ASSERT (strncmp (name, ".rel", 4) == 0
2207                                 && strcmp (bfd_get_section_name (abfd, sec),
2208                                            name + 4) == 0);
2209
2210                     sreloc = bfd_get_section_by_name (dynobj, name);
2211                     if (sreloc == NULL)
2212                       {
2213                         flagword flags;
2214
2215                         sreloc = bfd_make_section (dynobj, name);
2216                         flags = (SEC_HAS_CONTENTS | SEC_READONLY
2217                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2218                         if ((sec->flags & SEC_ALLOC) != 0)
2219                           flags |= SEC_ALLOC | SEC_LOAD;
2220                         if (sreloc == NULL
2221                             || ! bfd_set_section_flags (dynobj, sreloc, flags)
2222                             || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2223                           return false;
2224                       }
2225                   }
2226
2227                 sreloc->_raw_size += sizeof (Elf32_External_Rel);
2228                 /* If we are linking with -Bsymbolic, and this is a
2229                    global symbol, we count the number of PC relative
2230                    relocations we have entered for this symbol, so that
2231                    we can discard them again if the symbol is later
2232                    defined by a regular object.  Note that this function
2233                    is only called if we are using an elf_i386 linker
2234                    hash table, which means that h is really a pointer to
2235                    an elf_i386_link_hash_entry.  */
2236                 if (h != NULL && info->symbolic
2237                     && ELF32_R_TYPE (rel->r_info) == R_ARM_PC24)
2238                   {
2239                     struct elf32_arm_link_hash_entry * eh;
2240                     struct elf32_arm_pcrel_relocs_copied * p;
2241
2242                     eh = (struct elf32_arm_link_hash_entry *) h;
2243
2244                     for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2245                       if (p->section == sreloc)
2246                         break;
2247
2248                     if (p == NULL)
2249                       {
2250                         p = ((struct elf32_arm_pcrel_relocs_copied *)
2251                              bfd_alloc (dynobj, sizeof * p));
2252                         
2253                         if (p == NULL)
2254                           return false;
2255                         p->next = eh->pcrel_relocs_copied;
2256                         eh->pcrel_relocs_copied = p;
2257                         p->section = sreloc;
2258                         p->count = 0;
2259                       }
2260
2261                     ++p->count;
2262                   }
2263               }
2264             break;
2265
2266         /* This relocation describes the C++ object vtable hierarchy.
2267            Reconstruct it for later use during GC.  */
2268         case R_ARM_GNU_VTINHERIT:
2269           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2270             return false;
2271           break;
2272  
2273         /* This relocation describes which C++ vtable entries are actually
2274            used.  Record for later use during GC.  */
2275         case R_ARM_GNU_VTENTRY:
2276           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2277             return false;
2278           break;
2279         }
2280     }
2281  
2282   return true;
2283 }
2284
2285        
2286 /* Find the nearest line to a particular section and offset, for error
2287    reporting.   This code is a duplicate of the code in elf.c, except
2288    that it also accepts STT_ARM_TFUNC as a symbol that names a function. */
2289
2290 static boolean
2291 elf32_arm_find_nearest_line
2292   (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2293      bfd *          abfd;
2294      asection *     section;
2295      asymbol **     symbols;
2296      bfd_vma        offset;
2297      CONST char **  filename_ptr;
2298      CONST char **  functionname_ptr;
2299      unsigned int * line_ptr;
2300 {
2301   boolean      found;
2302   const char * filename;
2303   asymbol *    func;
2304   bfd_vma      low_func;
2305   asymbol **   p;
2306
2307   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2308                                      filename_ptr, functionname_ptr, 
2309                                      line_ptr))
2310     return true;
2311
2312   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2313                                              &found, filename_ptr,
2314                                              functionname_ptr, line_ptr,
2315                                              &elf_tdata (abfd)->line_info))
2316     return false;
2317   
2318   if (found)
2319     return true;
2320
2321   if (symbols == NULL)
2322     return false;
2323
2324   filename = NULL;
2325   func = NULL;
2326   low_func = 0;
2327
2328   for (p = symbols; *p != NULL; p++)
2329     {
2330       elf_symbol_type *q;
2331
2332       q = (elf_symbol_type *) *p;
2333
2334       if (bfd_get_section (&q->symbol) != section)
2335         continue;
2336
2337       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
2338         {
2339         default:
2340           break;
2341         case STT_FILE:
2342           filename = bfd_asymbol_name (&q->symbol);
2343           break;
2344         case STT_NOTYPE:
2345         case STT_FUNC:
2346         case STT_ARM_TFUNC:
2347           if (q->symbol.section == section
2348               && q->symbol.value >= low_func
2349               && q->symbol.value <= offset)
2350             {
2351               func = (asymbol *) q;
2352               low_func = q->symbol.value;
2353             }
2354           break;
2355         }
2356     }
2357
2358   if (func == NULL)
2359     return false;
2360
2361   *filename_ptr = filename;
2362   *functionname_ptr = bfd_asymbol_name (func);
2363   *line_ptr = 0;
2364   
2365   return true;
2366 }
2367
2368 /* Adjust a symbol defined by a dynamic object and referenced by a
2369    regular object.  The current definition is in some section of the
2370    dynamic object, but we're not including those sections.  We have to
2371    change the definition to something the rest of the link can
2372    understand.  */
2373
2374 static boolean
2375 elf32_arm_adjust_dynamic_symbol (info, h)
2376      struct bfd_link_info * info;
2377      struct elf_link_hash_entry * h;
2378 {
2379   bfd * dynobj;
2380   asection * s;
2381   unsigned int power_of_two;
2382
2383   dynobj = elf_hash_table (info)->dynobj;
2384
2385   /* Make sure we know what is going on here.  */
2386   BFD_ASSERT (dynobj != NULL
2387               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2388                   || h->weakdef != NULL
2389                   || ((h->elf_link_hash_flags
2390                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2391                       && (h->elf_link_hash_flags
2392                           & ELF_LINK_HASH_REF_REGULAR) != 0
2393                       && (h->elf_link_hash_flags
2394                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2395
2396   /* If this is a function, put it in the procedure linkage table.  We
2397      will fill in the contents of the procedure linkage table later,
2398      when we know the address of the .got section.  */
2399   if (h->type == STT_FUNC
2400       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2401     {
2402       if (! info->shared
2403           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2404           && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
2405         {
2406           /* This case can occur if we saw a PLT32 reloc in an input
2407              file, but the symbol was never referred to by a dynamic
2408              object.  In such a case, we don't actually need to build
2409              a procedure linkage table, and we can just do a PC32
2410              reloc instead.  */
2411           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2412           return true;
2413         }
2414
2415       /* Make sure this symbol is output as a dynamic symbol.  */
2416       if (h->dynindx == -1)
2417         {
2418           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2419             return false;
2420         }
2421
2422       s = bfd_get_section_by_name (dynobj, ".plt");
2423       BFD_ASSERT (s != NULL);
2424
2425       /* If this is the first .plt entry, make room for the special
2426          first entry.  */
2427       if (s->_raw_size == 0)
2428         s->_raw_size += PLT_ENTRY_SIZE;
2429
2430       /* If this symbol is not defined in a regular file, and we are
2431          not generating a shared library, then set the symbol to this
2432          location in the .plt.  This is required to make function
2433          pointers compare as equal between the normal executable and
2434          the shared library.  */
2435       if (! info->shared
2436           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2437         {
2438           h->root.u.def.section = s;
2439           h->root.u.def.value = s->_raw_size;
2440         }
2441
2442       h->plt.offset = s->_raw_size;
2443
2444       /* Make room for this entry.  */
2445       s->_raw_size += PLT_ENTRY_SIZE;
2446
2447       /* We also need to make an entry in the .got.plt section, which
2448          will be placed in the .got section by the linker script.  */
2449
2450       s = bfd_get_section_by_name (dynobj, ".got.plt");
2451       BFD_ASSERT (s != NULL);
2452       s->_raw_size += 4;
2453
2454       /* We also need to make an entry in the .rel.plt section.  */
2455
2456       s = bfd_get_section_by_name (dynobj, ".rel.plt");
2457       BFD_ASSERT (s != NULL);
2458       s->_raw_size += sizeof (Elf32_External_Rel);
2459
2460       return true;
2461     }
2462
2463   /* If this is a weak symbol, and there is a real definition, the
2464      processor independent code will have arranged for us to see the
2465      real definition first, and we can just use the same value.  */
2466   if (h->weakdef != NULL)
2467     {
2468       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2469                   || h->weakdef->root.type == bfd_link_hash_defweak);
2470       h->root.u.def.section = h->weakdef->root.u.def.section;
2471       h->root.u.def.value = h->weakdef->root.u.def.value;
2472       return true;
2473     }
2474
2475   /* This is a reference to a symbol defined by a dynamic object which
2476      is not a function.  */
2477
2478   /* If we are creating a shared library, we must presume that the
2479      only references to the symbol are via the global offset table.
2480      For such cases we need not do anything here; the relocations will
2481      be handled correctly by relocate_section.  */
2482   if (info->shared)
2483     return true;
2484
2485   /* We must allocate the symbol in our .dynbss section, which will
2486      become part of the .bss section of the executable.  There will be
2487      an entry for this symbol in the .dynsym section.  The dynamic
2488      object will contain position independent code, so all references
2489      from the dynamic object to this symbol will go through the global
2490      offset table.  The dynamic linker will use the .dynsym entry to
2491      determine the address it must put in the global offset table, so
2492      both the dynamic object and the regular object will refer to the
2493      same memory location for the variable.  */
2494
2495   s = bfd_get_section_by_name (dynobj, ".dynbss");
2496   BFD_ASSERT (s != NULL);
2497
2498   /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
2499      copy the initial value out of the dynamic object and into the
2500      runtime process image.  We need to remember the offset into the
2501      .rel.bss section we are going to use.  */
2502   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2503     {
2504       asection *srel;
2505
2506       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
2507       BFD_ASSERT (srel != NULL);
2508       srel->_raw_size += sizeof (Elf32_External_Rel);
2509       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2510     }
2511
2512   /* We need to figure out the alignment required for this symbol.  I
2513      have no idea how ELF linkers handle this.  */
2514   power_of_two = bfd_log2 (h->size);
2515   if (power_of_two > 3)
2516     power_of_two = 3;
2517
2518   /* Apply the required alignment.  */
2519   s->_raw_size = BFD_ALIGN (s->_raw_size,
2520                             (bfd_size_type) (1 << power_of_two));
2521   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2522     {
2523       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2524         return false;
2525     }
2526
2527   /* Define the symbol as being at this point in the section.  */
2528   h->root.u.def.section = s;
2529   h->root.u.def.value = s->_raw_size;
2530
2531   /* Increment the section size to make room for the symbol.  */
2532   s->_raw_size += h->size;
2533
2534   return true;
2535 }
2536
2537 /* Set the sizes of the dynamic sections.  */
2538
2539 static boolean
2540 elf32_arm_size_dynamic_sections (output_bfd, info)
2541      bfd * output_bfd;
2542      struct bfd_link_info * info;
2543 {
2544   bfd * dynobj;
2545   asection * s;
2546   boolean plt;
2547   boolean relocs;
2548   boolean reltext;
2549
2550   dynobj = elf_hash_table (info)->dynobj;
2551   BFD_ASSERT (dynobj != NULL);
2552
2553   if (elf_hash_table (info)->dynamic_sections_created)
2554     {
2555       /* Set the contents of the .interp section to the interpreter.  */
2556       if (! info->shared)
2557         {
2558           s = bfd_get_section_by_name (dynobj, ".interp");
2559           BFD_ASSERT (s != NULL);
2560           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2561           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2562         }
2563     }
2564   else
2565     {
2566       /* We may have created entries in the .rel.got section.
2567          However, if we are not creating the dynamic sections, we will
2568          not actually use these entries.  Reset the size of .rel.got,
2569          which will cause it to get stripped from the output file
2570          below.  */
2571       s = bfd_get_section_by_name (dynobj, ".rel.got");
2572       if (s != NULL)
2573         s->_raw_size = 0;
2574     }
2575
2576   /* If this is a -Bsymbolic shared link, then we need to discard all
2577      PC relative relocs against symbols defined in a regular object.
2578      We allocated space for them in the check_relocs routine, but we
2579      will not fill them in in the relocate_section routine.  */
2580   if (info->shared && info->symbolic)
2581     elf32_arm_link_hash_traverse (elf32_arm_hash_table (info),
2582                                   elf32_arm_discard_copies,
2583                                   (PTR) NULL);
2584
2585   /* The check_relocs and adjust_dynamic_symbol entry points have
2586      determined the sizes of the various dynamic sections.  Allocate
2587      memory for them.  */
2588   plt = false;
2589   relocs = false;
2590   reltext = false;
2591   for (s = dynobj->sections; s != NULL; s = s->next)
2592     {
2593       const char * name;
2594       boolean strip;
2595
2596       if ((s->flags & SEC_LINKER_CREATED) == 0)
2597         continue;
2598
2599       /* It's OK to base decisions on the section name, because none
2600          of the dynobj section names depend upon the input files.  */
2601       name = bfd_get_section_name (dynobj, s);
2602
2603       strip = false;
2604
2605       if (strcmp (name, ".plt") == 0)
2606         {
2607           if (s->_raw_size == 0)
2608             {
2609               /* Strip this section if we don't need it; see the
2610                  comment below.  */
2611               strip = true;
2612             }
2613           else
2614             {
2615               /* Remember whether there is a PLT.  */
2616               plt = true;
2617             }
2618         }
2619       else if (strncmp (name, ".rel", 4) == 0)
2620         {
2621           if (s->_raw_size == 0)
2622             {
2623               /* If we don't need this section, strip it from the
2624                  output file.  This is mostly to handle .rel.bss and
2625                  .rel.plt.  We must create both sections in
2626                  create_dynamic_sections, because they must be created
2627                  before the linker maps input sections to output
2628                  sections.  The linker does that before
2629                  adjust_dynamic_symbol is called, and it is that
2630                  function which decides whether anything needs to go
2631                  into these sections.  */
2632               strip = true;
2633             }
2634           else
2635             {
2636               asection * target;
2637
2638               /* Remember whether there are any reloc sections other
2639                  than .rel.plt.  */
2640               if (strcmp (name, ".rel.plt") != 0)
2641                 {
2642                   const char *outname;
2643
2644                   relocs = true;
2645
2646                   /* If this relocation section applies to a read only
2647                      section, then we probably need a DT_TEXTREL
2648                      entry.  The entries in the .rel.plt section
2649                      really apply to the .got section, which we
2650                      created ourselves and so know is not readonly.  */
2651                   outname = bfd_get_section_name (output_bfd,
2652                                                   s->output_section);
2653                   target = bfd_get_section_by_name (output_bfd, outname + 4);
2654                   if (target != NULL
2655                       && (target->flags & SEC_READONLY) != 0
2656                       && (target->flags & SEC_ALLOC) != 0)
2657                     reltext = true;
2658                 }
2659
2660               /* We use the reloc_count field as a counter if we need
2661                  to copy relocs into the output file.  */
2662               s->reloc_count = 0;
2663             }
2664         }
2665       else if (strncmp (name, ".got", 4) != 0)
2666         {
2667           /* It's not one of our sections, so don't allocate space.  */
2668           continue;
2669         }
2670
2671       if (strip)
2672         {
2673           asection ** spp;
2674
2675           for (spp = &s->output_section->owner->sections;
2676                *spp != s->output_section;
2677                spp = &(*spp)->next)
2678             ;
2679           *spp = s->output_section->next;
2680           --s->output_section->owner->section_count;
2681
2682           continue;
2683         }
2684
2685       /* Allocate memory for the section contents.  */
2686       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2687       if (s->contents == NULL && s->_raw_size != 0)
2688         return false;
2689     }
2690
2691   if (elf_hash_table (info)->dynamic_sections_created)
2692     {
2693       /* Add some entries to the .dynamic section.  We fill in the
2694          values later, in elf32_arm_finish_dynamic_sections, but we
2695          must add the entries now so that we get the correct size for
2696          the .dynamic section.  The DT_DEBUG entry is filled in by the
2697          dynamic linker and used by the debugger.  */
2698       if (! info->shared)
2699         {
2700           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2701             return false;
2702         }
2703
2704       if (plt)
2705         {
2706           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2707               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2708               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
2709               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2710             return false;
2711         }
2712
2713       if (relocs)
2714         {
2715           if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
2716               || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
2717               || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
2718                                                 sizeof (Elf32_External_Rel)))
2719             return false;
2720         }
2721
2722       if (reltext)
2723         {
2724           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2725             return false;
2726         }
2727     }
2728
2729   return true;
2730 }
2731
2732 /* This function is called via elf32_arm_link_hash_traverse if we are
2733    creating a shared object with -Bsymbolic.  It discards the space
2734    allocated to copy PC relative relocs against symbols which are
2735    defined in regular objects.  We allocated space for them in the
2736    check_relocs routine, but we won't fill them in in the
2737    relocate_section routine.  */
2738
2739 static boolean
2740 elf32_arm_discard_copies (h, ignore)
2741      struct elf32_arm_link_hash_entry * h;
2742      PTR ignore;
2743 {
2744   struct elf32_arm_pcrel_relocs_copied * s;
2745
2746   /* We only discard relocs for symbols defined in a regular object.  */
2747   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2748     return true;
2749
2750   for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2751     s->section->_raw_size -= s->count * sizeof (Elf32_External_Rel);
2752
2753   return true;
2754 }
2755
2756 /* Finish up dynamic symbol handling.  We set the contents of various
2757    dynamic sections here.  */
2758
2759 static boolean
2760 elf32_arm_finish_dynamic_symbol (output_bfd, info, h, sym)
2761      bfd * output_bfd;
2762      struct bfd_link_info * info;
2763      struct elf_link_hash_entry * h;
2764      Elf_Internal_Sym * sym;
2765 {
2766   bfd * dynobj;
2767
2768   dynobj = elf_hash_table (info)->dynobj;
2769
2770   if (h->plt.offset != (bfd_vma) -1)
2771     {
2772       asection * splt;
2773       asection * sgot;
2774       asection * srel;
2775       bfd_vma plt_index;
2776       bfd_vma got_offset;
2777       Elf_Internal_Rel rel;
2778
2779       /* This symbol has an entry in the procedure linkage table.  Set
2780          it up.  */
2781
2782       BFD_ASSERT (h->dynindx != -1);
2783
2784       splt = bfd_get_section_by_name (dynobj, ".plt");
2785       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2786       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
2787       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
2788
2789       /* Get the index in the procedure linkage table which
2790          corresponds to this symbol.  This is the index of this symbol
2791          in all the symbols for which we are making plt entries.  The
2792          first entry in the procedure linkage table is reserved.  */
2793       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2794
2795       /* Get the offset into the .got table of the entry that
2796          corresponds to this function.  Each .got entry is 4 bytes.
2797          The first three are reserved.  */
2798       got_offset = (plt_index + 3) * 4;
2799
2800       /* Fill in the entry in the procedure linkage table.  */
2801       memcpy (splt->contents + h->plt.offset,
2802               elf32_arm_plt_entry,
2803               PLT_ENTRY_SIZE);
2804       bfd_put_32 (output_bfd,
2805                       (sgot->output_section->vma
2806                        + sgot->output_offset
2807                        + got_offset 
2808                        - splt->output_section->vma
2809                        - splt->output_offset
2810                        - h->plt.offset - 12),
2811                       splt->contents + h->plt.offset + 12);
2812
2813       /* Fill in the entry in the global offset table.  */
2814       bfd_put_32 (output_bfd,
2815                   (splt->output_section->vma
2816                    + splt->output_offset),
2817                   sgot->contents + got_offset);
2818
2819       /* Fill in the entry in the .rel.plt section.  */
2820       rel.r_offset = (sgot->output_section->vma
2821                       + sgot->output_offset
2822                       + got_offset);
2823       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
2824       bfd_elf32_swap_reloc_out (output_bfd, &rel,
2825                                 ((Elf32_External_Rel *) srel->contents
2826                                  + plt_index));
2827
2828       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2829         {
2830           /* Mark the symbol as undefined, rather than as defined in
2831              the .plt section.  Leave the value alone.  */
2832           sym->st_shndx = SHN_UNDEF;
2833         }
2834     }
2835
2836   if (h->got.offset != (bfd_vma) -1)
2837     {
2838       asection * sgot;
2839       asection * srel;
2840       Elf_Internal_Rel rel;
2841
2842       /* This symbol has an entry in the global offset table.  Set it
2843          up.  */
2844       
2845       sgot = bfd_get_section_by_name (dynobj, ".got");
2846       srel = bfd_get_section_by_name (dynobj, ".rel.got");
2847       BFD_ASSERT (sgot != NULL && srel != NULL);
2848
2849       rel.r_offset = (sgot->output_section->vma
2850                       + sgot->output_offset
2851                       + (h->got.offset &~ 1));
2852
2853       /* If this is a -Bsymbolic link, and the symbol is defined
2854          locally, we just want to emit a RELATIVE reloc.  The entry in
2855          the global offset table will already have been initialized in
2856          the relocate_section function.  */
2857       if (info->shared
2858           && (info->symbolic || h->dynindx == -1)
2859           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2860         rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
2861       else
2862         {
2863           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2864           rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
2865         }
2866
2867       bfd_elf32_swap_reloc_out (output_bfd, &rel,
2868                                 ((Elf32_External_Rel *) srel->contents
2869                                  + srel->reloc_count));
2870       ++srel->reloc_count;
2871     }
2872
2873   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2874     {
2875       asection * s;
2876       Elf_Internal_Rel rel;
2877
2878       /* This symbol needs a copy reloc.  Set it up.  */
2879
2880       BFD_ASSERT (h->dynindx != -1
2881                   && (h->root.type == bfd_link_hash_defined
2882                       || h->root.type == bfd_link_hash_defweak));
2883
2884       s = bfd_get_section_by_name (h->root.u.def.section->owner,
2885                                    ".rel.bss");
2886       BFD_ASSERT (s != NULL);
2887
2888       rel.r_offset = (h->root.u.def.value
2889                       + h->root.u.def.section->output_section->vma
2890                       + h->root.u.def.section->output_offset);
2891       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
2892       bfd_elf32_swap_reloc_out (output_bfd, &rel,
2893                                 ((Elf32_External_Rel *) s->contents
2894                                  + s->reloc_count));
2895       ++s->reloc_count;
2896     }
2897
2898   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2899   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2900       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2901     sym->st_shndx = SHN_ABS;
2902
2903   return true;
2904 }
2905
2906 /* Finish up the dynamic sections.  */
2907
2908 static boolean
2909 elf32_arm_finish_dynamic_sections (output_bfd, info)
2910      bfd * output_bfd;
2911      struct bfd_link_info * info;
2912 {
2913   bfd * dynobj;
2914   asection * sgot;
2915   asection * sdyn;
2916
2917   dynobj = elf_hash_table (info)->dynobj;
2918
2919   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2920   BFD_ASSERT (sgot != NULL);
2921   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2922
2923   if (elf_hash_table (info)->dynamic_sections_created)
2924     {
2925       asection *splt;
2926       Elf32_External_Dyn *dyncon, *dynconend;
2927
2928       splt = bfd_get_section_by_name (dynobj, ".plt");
2929       BFD_ASSERT (splt != NULL && sdyn != NULL);
2930
2931       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2932       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2933       for (; dyncon < dynconend; dyncon++)
2934         {
2935           Elf_Internal_Dyn dyn;
2936           const char * name;
2937           asection * s;
2938
2939           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2940
2941           switch (dyn.d_tag)
2942             {
2943             default:
2944               break;
2945
2946             case DT_PLTGOT:
2947               name = ".got";
2948               goto get_vma;
2949             case DT_JMPREL:
2950               name = ".rel.plt";
2951             get_vma:
2952               s = bfd_get_section_by_name (output_bfd, name);
2953               BFD_ASSERT (s != NULL);
2954               dyn.d_un.d_ptr = s->vma;
2955               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2956               break;
2957
2958             case DT_PLTRELSZ:
2959               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
2960               BFD_ASSERT (s != NULL);
2961               if (s->_cooked_size != 0)
2962                 dyn.d_un.d_val = s->_cooked_size;
2963               else
2964                 dyn.d_un.d_val = s->_raw_size;
2965               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2966               break;
2967
2968             case DT_RELSZ:
2969               /* My reading of the SVR4 ABI indicates that the
2970                  procedure linkage table relocs (DT_JMPREL) should be
2971                  included in the overall relocs (DT_REL).  This is
2972                  what Solaris does.  However, UnixWare can not handle
2973                  that case.  Therefore, we override the DT_RELSZ entry
2974                  here to make it not include the JMPREL relocs.  Since
2975                  the linker script arranges for .rel.plt to follow all
2976                  other relocation sections, we don't have to worry
2977                  about changing the DT_REL entry.  */
2978               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
2979               if (s != NULL)
2980                 {
2981                   if (s->_cooked_size != 0)
2982                     dyn.d_un.d_val -= s->_cooked_size;
2983                   else
2984                     dyn.d_un.d_val -= s->_raw_size;
2985                 }
2986               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2987               break;
2988             }
2989         }
2990
2991       /* Fill in the first entry in the procedure linkage table.  */
2992       if (splt->_raw_size > 0)
2993         memcpy (splt->contents, elf32_arm_plt0_entry, PLT_ENTRY_SIZE);
2994
2995       /* UnixWare sets the entsize of .plt to 4, although that doesn't
2996          really seem like the right value.  */
2997       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
2998     }
2999
3000   /* Fill in the first three entries in the global offset table.  */
3001   if (sgot->_raw_size > 0)
3002     {
3003       if (sdyn == NULL)
3004         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3005       else
3006         bfd_put_32 (output_bfd,
3007                     sdyn->output_section->vma + sdyn->output_offset,
3008                     sgot->contents);
3009       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3010       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3011     }
3012
3013   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3014
3015   return true;
3016 }
3017
3018 #define ELF_ARCH                        bfd_arch_arm
3019 #define ELF_MACHINE_CODE                EM_ARM
3020 #define ELF_MAXPAGE_SIZE                0x8000
3021
3022
3023 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
3024 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
3025 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
3026 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
3027 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
3028 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
3029 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
3030
3031 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
3032 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
3033 #define elf_backend_gc_sweep_hook               elf32_arm_gc_sweep_hook
3034 #define elf_backend_check_relocs                elf32_arm_check_relocs
3035 #define elf_backend_relocate_section            elf32_arm_relocate_section
3036 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
3037 #define elf_backend_create_dynamic_sections     _bfd_elf_create_dynamic_sections
3038 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
3039 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
3040 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
3041
3042 #define elf_backend_can_gc_sections 1
3043 #define elf_backend_plt_readonly    1
3044 #define elf_backend_want_got_plt    1
3045 #define elf_backend_want_plt_sym    0
3046
3047 #include "elf32-target.h"