2001-12-18 H.J. Lu <hjl@gnu.org>
[platform/upstream/binutils.git] / bfd / elf32-mcore.c
1 /* Motorola MCore specific support for 32-bit ELF
2    Copyright 1994, 1995, 1999, 2000, 2001 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 /* This file is based on a preliminary RCE ELF ABI.  The
21    information may not match the final RCE ELF ABI.   */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/mcore.h"
29 #include <assert.h>
30
31 #define USE_RELA        /* Only USE_REL is actually significant, but this is
32                            here are a reminder...  */
33
34 static void mcore_elf_howto_init
35   PARAMS ((void));
36 static reloc_howto_type * mcore_elf_reloc_type_lookup
37   PARAMS ((bfd *, bfd_reloc_code_real_type));
38 static void mcore_elf_info_to_howto
39   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
40 static boolean mcore_elf_set_private_flags
41   PARAMS ((bfd *, flagword));
42 static boolean mcore_elf_merge_private_bfd_data
43   PARAMS ((bfd *, bfd *));
44 static bfd_reloc_status_type mcore_elf_unsupported_reloc
45   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static boolean mcore_elf_relocate_section
47   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
49 static asection * mcore_elf_gc_mark_hook
50   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
51            struct elf_link_hash_entry *, Elf_Internal_Sym *));
52 static boolean mcore_elf_gc_sweep_hook
53   PARAMS ((bfd *, struct bfd_link_info *, asection *,
54            const Elf_Internal_Rela *));
55 static boolean mcore_elf_check_relocs
56   PARAMS ((bfd *, struct bfd_link_info *, asection *,
57            const Elf_Internal_Rela *));
58
59 static reloc_howto_type * mcore_elf_howto_table [(int) R_MCORE_max];
60
61 static reloc_howto_type mcore_elf_howto_raw[] =
62 {
63   /* This reloc does nothing.  */
64   HOWTO (R_MCORE_NONE,          /* type */
65          0,                     /* rightshift */
66          2,                     /* size (0 = byte, 1 = short, 2 = long) */
67          32,                    /* bitsize */
68          false,                 /* pc_relative */
69          0,                     /* bitpos */
70          complain_overflow_bitfield,  /* complain_on_overflow */
71          NULL,                  /* special_function */
72          "R_MCORE_NONE",        /* name */
73          false,                 /* partial_inplace */
74          0,                     /* src_mask */
75          0,                     /* dst_mask */
76          false),                /* pcrel_offset */
77
78   /* A standard 32 bit relocation.  */
79   HOWTO (R_MCORE_ADDR32,        /* type */
80          0,                     /* rightshift */
81          2,                     /* size (0 = byte, 1 = short, 2 = long) */
82          32,                    /* bitsize */
83          false,                 /* pc_relative */
84          0,                     /* bitpos */
85          complain_overflow_bitfield, /* complain_on_overflow */
86          bfd_elf_generic_reloc, /* special_function */
87          "ADDR32",              /* name *//* For compatability with coff/pe port.  */
88          false,                 /* partial_inplace */
89          0x0,                   /* src_mask */
90          0xffffffff,            /* dst_mask */
91          false),                /* pcrel_offset */
92
93   /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
94      Should not appear in object files.  */
95   HOWTO (R_MCORE_PCRELIMM8BY4,  /* type */
96          2,                     /* rightshift */
97          1,                     /* size (0 = byte, 1 = short, 2 = long) */
98          8,                     /* bitsize */
99          true,                  /* pc_relative */
100          0,                     /* bitpos */
101          complain_overflow_bitfield, /* complain_on_overflow */
102          mcore_elf_unsupported_reloc,   /* special_function */
103          "R_MCORE_PCRELIMM8BY4",/* name */
104          false,                 /* partial_inplace */
105          0,                     /* src_mask */
106          0,                     /* dst_mask */
107          true),                 /* pcrel_offset */
108
109   /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
110      Span 2k instructions == 4k bytes.
111      Only useful pieces at the relocated address are the opcode (5 bits) */
112   HOWTO (R_MCORE_PCRELIMM11BY2,/* type */
113          1,                     /* rightshift */
114          1,                     /* size (0 = byte, 1 = short, 2 = long) */
115          11,                    /* bitsize */
116          true,                  /* pc_relative */
117          0,                     /* bitpos */
118          complain_overflow_signed, /* complain_on_overflow */
119          bfd_elf_generic_reloc, /* special_function */
120          "R_MCORE_PCRELIMM11BY2",/* name */
121          false,                 /* partial_inplace */
122          0x0,                   /* src_mask */
123          0x7ff,                 /* dst_mask */
124          true),                 /* pcrel_offset */
125
126   /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported.  */
127   HOWTO (R_MCORE_PCRELIMM4BY2,  /* type */
128          1,                     /* rightshift */
129          1,                     /* size (0 = byte, 1 = short, 2 = long) */
130          4,                     /* bitsize */
131          true,                  /* pc_relative */
132          0,                     /* bitpos */
133          complain_overflow_bitfield, /* complain_on_overflow */
134          mcore_elf_unsupported_reloc,/* special_function */
135          "R_MCORE_PCRELIMM4BY2",/* name */
136          false,                 /* partial_inplace */
137          0,                     /* src_mask */
138          0,                     /* dst_mask */
139          true),                 /* pcrel_offset */
140
141   /* 32-bit pc-relative. Eventually this will help support PIC code.  */
142   HOWTO (R_MCORE_PCREL32,       /* type */
143          0,                     /* rightshift */
144          2,                     /* size (0 = byte, 1 = short, 2 = long) */
145          32,                    /* bitsize */
146          true,                  /* pc_relative */
147          0,                     /* bitpos */
148          complain_overflow_bitfield, /* complain_on_overflow */
149          bfd_elf_generic_reloc, /* special_function */
150          "R_MCORE_PCREL32",     /* name */
151          false,                 /* partial_inplace */
152          0x0,                   /* src_mask */
153          0xffffffff,            /* dst_mask */
154          true),                 /* pcrel_offset */
155
156   /* Like PCRELIMM11BY2, this relocation indicates that there is a
157      'jsri' at the specified address. There is a separate relocation
158      entry for the literal pool entry that it references, but we
159      might be able to change the jsri to a bsr if the target turns out
160      to be close enough [even though we won't reclaim the literal pool
161      entry, we'll get some runtime efficiency back]. Note that this
162      is a relocation that we are allowed to safely ignore.  */
163   HOWTO (R_MCORE_PCRELJSR_IMM11BY2,/* type */
164          1,                     /* rightshift */
165          1,                     /* size (0 = byte, 1 = short, 2 = long) */
166          11,                    /* bitsize */
167          true,                  /* pc_relative */
168          0,                     /* bitpos */
169          complain_overflow_signed, /* complain_on_overflow */
170          bfd_elf_generic_reloc, /* special_function */
171          "R_MCORE_PCRELJSR_IMM11BY2", /* name */
172          false,                 /* partial_inplace */
173          0x0,                   /* src_mask */
174          0x7ff,                 /* dst_mask */
175          true),                 /* pcrel_offset */
176
177   /* GNU extension to record C++ vtable hierarchy */
178   HOWTO (R_MCORE_GNU_VTINHERIT, /* type */
179          0,                     /* rightshift */
180          2,                     /* size (0 = byte, 1 = short, 2 = long) */
181          0,                     /* bitsize */
182          false,                 /* pc_relative */
183          0,                     /* bitpos */
184          complain_overflow_dont, /* complain_on_overflow */
185          NULL,                  /* special_function */
186          "R_MCORE_GNU_VTINHERIT", /* name */
187          false,                 /* partial_inplace */
188          0,                     /* src_mask */
189          0,                     /* dst_mask */
190          false),                /* pcrel_offset */
191
192   /* GNU extension to record C++ vtable member usage */
193   HOWTO (R_MCORE_GNU_VTENTRY,   /* type */
194          0,                     /* rightshift */
195          2,                     /* size (0 = byte, 1 = short, 2 = long) */
196          0,                     /* bitsize */
197          false,                 /* pc_relative */
198          0,                     /* bitpos */
199          complain_overflow_dont,/* complain_on_overflow */
200          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
201          "R_MCORE_GNU_VTENTRY", /* name */
202          false,                 /* partial_inplace */
203          0,                     /* src_mask */
204          0,                     /* dst_mask */
205          false),                /* pcrel_offset */
206
207   HOWTO (R_MCORE_RELATIVE,      /* type */
208          0,                     /* rightshift */
209          2,                     /* size (0 = byte, 1 = short, 2 = long) */
210          32,                    /* bitsize */
211          false,                 /* pc_relative */
212          0,                     /* bitpos */
213          complain_overflow_signed, /* complain_on_overflow */
214          NULL,                  /* special_function */
215          "R_MCORE_RELATIVE",    /* name */
216          true,                  /* partial_inplace */
217          0xffffffff,            /* src_mask */
218          0xffffffff,            /* dst_mask */
219          false)                 /* pcrel_offset */
220 };
221
222 #ifndef NUM_ELEM
223 #define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
224 #endif
225 \f
226 /* Initialize the mcore_elf_howto_table, so that linear accesses can be done.  */
227 static void
228 mcore_elf_howto_init ()
229 {
230   unsigned int i;
231
232   for (i = NUM_ELEM (mcore_elf_howto_raw); i--;)
233     {
234       unsigned int type;
235
236       type = mcore_elf_howto_raw[i].type;
237
238       BFD_ASSERT (type < NUM_ELEM (mcore_elf_howto_table));
239
240       mcore_elf_howto_table [type] = & mcore_elf_howto_raw [i];
241     }
242 }
243 \f
244 static reloc_howto_type *
245 mcore_elf_reloc_type_lookup (abfd, code)
246      bfd * abfd ATTRIBUTE_UNUSED;
247      bfd_reloc_code_real_type code;
248 {
249   enum elf_mcore_reloc_type mcore_reloc = R_MCORE_NONE;
250
251   switch (code)
252     {
253     case BFD_RELOC_NONE:                     mcore_reloc = R_MCORE_NONE; break;
254     case BFD_RELOC_32:                       mcore_reloc = R_MCORE_ADDR32; break;
255     case BFD_RELOC_MCORE_PCREL_IMM8BY4:      mcore_reloc = R_MCORE_PCRELIMM8BY4; break;
256     case BFD_RELOC_MCORE_PCREL_IMM11BY2:     mcore_reloc = R_MCORE_PCRELIMM11BY2; break;
257     case BFD_RELOC_MCORE_PCREL_IMM4BY2:      mcore_reloc = R_MCORE_PCRELIMM4BY2; break;
258     case BFD_RELOC_32_PCREL:                 mcore_reloc = R_MCORE_PCREL32; break;
259     case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2: mcore_reloc = R_MCORE_PCRELJSR_IMM11BY2; break;
260     case BFD_RELOC_VTABLE_INHERIT:           mcore_reloc = R_MCORE_GNU_VTINHERIT; break;
261     case BFD_RELOC_VTABLE_ENTRY:             mcore_reloc = R_MCORE_GNU_VTENTRY; break;
262     case BFD_RELOC_RVA:                      mcore_reloc = R_MCORE_RELATIVE; break;
263     default:
264       return (reloc_howto_type *)NULL;
265     }
266
267   if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])   /* Initialize howto table if needed */
268     mcore_elf_howto_init ();
269
270   return mcore_elf_howto_table [(int) mcore_reloc];
271 };
272
273 /* Set the howto pointer for a RCE ELF reloc.  */
274 static void
275 mcore_elf_info_to_howto (abfd, cache_ptr, dst)
276      bfd * abfd ATTRIBUTE_UNUSED;
277      arelent * cache_ptr;
278      Elf32_Internal_Rela * dst;
279 {
280   if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])   /* Initialize howto table if needed */
281     mcore_elf_howto_init ();
282
283   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_MCORE_max);
284
285   cache_ptr->howto = mcore_elf_howto_table [ELF32_R_TYPE (dst->r_info)];
286 }
287
288 /* Function to set whether a module needs the -mrelocatable bit set.  */
289 static boolean
290 mcore_elf_set_private_flags (abfd, flags)
291      bfd * abfd;
292      flagword flags;
293 {
294   BFD_ASSERT (! elf_flags_init (abfd)
295               || elf_elfheader (abfd)->e_flags == flags);
296
297   elf_elfheader (abfd)->e_flags = flags;
298   elf_flags_init (abfd) = true;
299   return true;
300 }
301
302 /* Merge backend specific data from an object file to the output
303    object file when linking.  */
304 static boolean
305 mcore_elf_merge_private_bfd_data (ibfd, obfd)
306      bfd * ibfd;
307      bfd * obfd;
308 {
309   flagword old_flags;
310   flagword new_flags;
311
312   /* Check if we have the same endianess */
313   if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
314     return false;
315
316   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
317       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
318     return true;
319
320   new_flags = elf_elfheader (ibfd)->e_flags;
321   old_flags = elf_elfheader (obfd)->e_flags;
322
323   if (! elf_flags_init (obfd))  /* First call, no flags set */
324     {
325       elf_flags_init (obfd) = true;
326       elf_elfheader (obfd)->e_flags = new_flags;
327     }
328   else if (new_flags == old_flags)      /* Compatible flags are ok */
329     ;
330   else
331     {
332       /* FIXME */
333     }
334
335   return true;
336 }
337 \f
338 /* Don't pretend we can deal with unsupported relocs.  */
339
340 static bfd_reloc_status_type
341 mcore_elf_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
342                            output_bfd, error_message)
343      bfd * abfd;
344      arelent * reloc_entry;
345      asymbol * symbol ATTRIBUTE_UNUSED;
346      PTR data ATTRIBUTE_UNUSED;
347      asection * input_section ATTRIBUTE_UNUSED;
348      bfd * output_bfd ATTRIBUTE_UNUSED;
349      char ** error_message ATTRIBUTE_UNUSED;
350 {
351   BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
352
353   _bfd_error_handler (_("%s: Relocation %s (%d) is not currently supported.\n"),
354                       bfd_archive_filename (abfd),
355                       reloc_entry->howto->name,
356                       reloc_entry->howto->type);
357
358   return bfd_reloc_notsupported;
359 }
360 \f
361 /* The RELOCATE_SECTION function is called by the ELF backend linker
362    to handle the relocations for a section.
363
364    The relocs are always passed as Rela structures; if the section
365    actually uses Rel structures, the r_addend field will always be
366    zero.
367
368    This function is responsible for adjust the section contents as
369    necessary, and (if using Rela relocs and generating a
370    relocateable output file) adjusting the reloc addend as
371    necessary.
372
373    This function does not have to worry about setting the reloc
374    address or the reloc symbol index.
375
376    LOCAL_SYMS is a pointer to the swapped in local symbols.
377
378    LOCAL_SECTIONS is an array giving the section in the input file
379    corresponding to the st_shndx field of each local symbol.
380
381    The global hash table entry for the global symbols can be found
382    via elf_sym_hashes (input_bfd).
383
384    When generating relocateable output, this function must handle
385    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
386    going to be the section symbol corresponding to the output
387    section, which means that the addend must be adjusted
388    accordingly.  */
389
390 static boolean
391 mcore_elf_relocate_section (output_bfd, info, input_bfd, input_section,
392                           contents, relocs, local_syms, local_sections)
393      bfd * output_bfd;
394      struct bfd_link_info * info;
395      bfd * input_bfd;
396      asection * input_section;
397      bfd_byte * contents;
398      Elf_Internal_Rela * relocs;
399      Elf_Internal_Sym * local_syms;
400      asection ** local_sections;
401 {
402   Elf_Internal_Shdr *           symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
403   struct elf_link_hash_entry ** sym_hashes = elf_sym_hashes (input_bfd);
404   Elf_Internal_Rela *           rel = relocs;
405   Elf_Internal_Rela *           relend = relocs + input_section->reloc_count;
406   boolean ret = true;
407
408 #ifdef DEBUG
409   fprintf (stderr,
410            "mcore_elf_relocate_section called for %s section %s, %ld relocations%s\n",
411            bfd_archive_filename (input_bfd),
412            bfd_section_name(input_bfd, input_section),
413            (long) input_section->reloc_count,
414            (info->relocateable) ? " (relocatable)" : "");
415 #endif
416
417   if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])   /* Initialize howto table if needed */
418     mcore_elf_howto_init ();
419
420   for (; rel < relend; rel++)
421     {
422       enum elf_mcore_reloc_type    r_type = (enum elf_mcore_reloc_type) ELF32_R_TYPE (rel->r_info);
423       bfd_vma                      offset = rel->r_offset;
424       bfd_vma                      addend = rel->r_addend;
425       bfd_reloc_status_type        r = bfd_reloc_other;
426       asection *                   sec = (asection *) 0;
427       reloc_howto_type *           howto;
428       bfd_vma                      relocation;
429       Elf_Internal_Sym *           sym = (Elf_Internal_Sym *) 0;
430       unsigned long                r_symndx;
431       struct elf_link_hash_entry * h = (struct elf_link_hash_entry *) 0;
432       unsigned short               oldinst = 0;
433
434       /* Unknown relocation handling */
435       if ((unsigned) r_type >= (unsigned) R_MCORE_max
436           || ! mcore_elf_howto_table [(int)r_type])
437         {
438           _bfd_error_handler (_("%s: Unknown relocation type %d\n"),
439                               bfd_archive_filename (input_bfd),
440                               (int) r_type);
441
442           bfd_set_error (bfd_error_bad_value);
443           ret = false;
444           continue;
445         }
446
447       howto = mcore_elf_howto_table [(int) r_type];
448       r_symndx = ELF32_R_SYM (rel->r_info);
449
450       if (info->relocateable)
451         {
452           /* This is a relocateable link.  We don't have to change
453              anything, unless the reloc is against a section symbol,
454              in which case we have to adjust according to where the
455              section symbol winds up in the output section.  */
456           if (r_symndx < symtab_hdr->sh_info)
457             {
458               sym = local_syms + r_symndx;
459
460               if ((unsigned)ELF_ST_TYPE (sym->st_info) == STT_SECTION)
461                 {
462                   sec = local_sections[r_symndx];
463                   addend = rel->r_addend += sec->output_offset + sym->st_value;
464                 }
465             }
466
467 #ifdef DEBUG
468           fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
469                    howto->name, (int) r_type, r_symndx, (long) offset, (long) addend);
470 #endif
471           continue;
472         }
473
474       /* This is a final link.  */
475
476       /* Complain about known relocation that are not yet supported.  */
477       if (howto->special_function == mcore_elf_unsupported_reloc)
478         {
479           _bfd_error_handler (_("%s: Relocation %s (%d) is not currently supported.\n"),
480                               bfd_archive_filename (input_bfd),
481                               howto->name,
482                               (int)r_type);
483
484           bfd_set_error (bfd_error_bad_value);
485           ret = false;
486           continue;
487         }
488
489       if (r_symndx < symtab_hdr->sh_info)
490         {
491           sym = local_syms + r_symndx;
492           sec = local_sections [r_symndx];
493           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
494           addend = rel->r_addend;
495         }
496       else
497         {
498           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
499           if (   h->root.type == bfd_link_hash_defined
500               || h->root.type == bfd_link_hash_defweak)
501             {
502               sec = h->root.u.def.section;
503               relocation = (h->root.u.def.value
504                             + sec->output_section->vma
505                             + sec->output_offset);
506             }
507           else if (h->root.type == bfd_link_hash_undefweak)
508             relocation = 0;
509           else if (info->shared
510                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
511             relocation = 0;
512           else
513             {
514               if (! ((*info->callbacks->undefined_symbol)
515                         (info, h->root.root.string, input_bfd,
516                          input_section, rel->r_offset, true)))
517                 return false;
518
519               ret = false;
520               continue;
521             }
522         }
523
524       switch (r_type)
525         {
526         default:
527           break;
528
529         case R_MCORE_PCRELJSR_IMM11BY2:
530           oldinst = bfd_get_16 (input_bfd, contents + offset);
531 #define MCORE_INST_BSR  0xF800
532           bfd_put_16 (input_bfd, (bfd_vma) MCORE_INST_BSR, contents + offset);
533           break;
534         }
535
536 #ifdef DEBUG
537       fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
538                howto->name, r_type, r_symndx, (long) offset, (long) addend);
539 #endif
540
541       r = _bfd_final_link_relocate
542         (howto, input_bfd, input_section, contents, offset, relocation, addend);
543
544       if (r != bfd_reloc_ok && r_type == R_MCORE_PCRELJSR_IMM11BY2)
545         {
546           /* Wasn't ok, back it out and give up.  */
547           bfd_put_16 (input_bfd, (bfd_vma) oldinst, contents + offset);
548           r = bfd_reloc_ok;
549         }
550
551       if (r != bfd_reloc_ok)
552         {
553           ret = false;
554
555           switch (r)
556             {
557             default:
558               break;
559
560             case bfd_reloc_overflow:
561               {
562                 const char * name;
563
564                 if (h != NULL)
565                   name = h->root.root.string;
566                 else
567                   {
568                     name = bfd_elf_string_from_elf_section
569                       (input_bfd, symtab_hdr->sh_link, sym->st_name);
570
571                     if (name == NULL)
572                       break;
573
574                     if (* name == '\0')
575                       name = bfd_section_name (input_bfd, sec);
576                   }
577
578                 (*info->callbacks->reloc_overflow)
579                   (info, name, howto->name, (bfd_vma) 0, input_bfd, input_section,
580                    offset);
581               }
582               break;
583             }
584         }
585     }
586
587 #ifdef DEBUG
588   fprintf (stderr, "\n");
589 #endif
590
591   return ret;
592 }
593 \f
594 /* Return the section that should be marked against GC for a given
595    relocation.  */
596
597 static asection *
598 mcore_elf_gc_mark_hook (abfd, info, rel, h, sym)
599      bfd *                        abfd;
600      struct bfd_link_info *       info ATTRIBUTE_UNUSED;
601      Elf_Internal_Rela *          rel;
602      struct elf_link_hash_entry * h;
603      Elf_Internal_Sym *           sym;
604 {
605   if (h != NULL)
606     {
607       switch (ELF32_R_TYPE (rel->r_info))
608         {
609         case R_MCORE_GNU_VTINHERIT:
610         case R_MCORE_GNU_VTENTRY:
611           break;
612
613         default:
614           switch (h->root.type)
615             {
616             case bfd_link_hash_defined:
617             case bfd_link_hash_defweak:
618               return h->root.u.def.section;
619
620             case bfd_link_hash_common:
621               return h->root.u.c.p->section;
622
623             default:
624               break;
625             }
626         }
627     }
628   else
629     {
630       return bfd_section_from_elf_index (abfd, sym->st_shndx);
631     }
632
633   return NULL;
634 }
635
636 /* Update the got entry reference counts for the section being removed.  */
637
638 static boolean
639 mcore_elf_gc_sweep_hook (abfd, info, sec, relocs)
640      bfd *                     abfd ATTRIBUTE_UNUSED;
641      struct bfd_link_info *    info ATTRIBUTE_UNUSED;
642      asection *                sec ATTRIBUTE_UNUSED;
643      const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
644 {
645   return true;
646 }
647
648 /* Look through the relocs for a section during the first phase.
649    Since we don't do .gots or .plts, we just need to consider the
650    virtual table relocs for gc.  */
651
652 static boolean
653 mcore_elf_check_relocs (abfd, info, sec, relocs)
654      bfd * abfd;
655      struct bfd_link_info * info;
656      asection * sec;
657      const Elf_Internal_Rela * relocs;
658 {
659   Elf_Internal_Shdr *           symtab_hdr;
660   struct elf_link_hash_entry ** sym_hashes;
661   struct elf_link_hash_entry ** sym_hashes_end;
662   const Elf_Internal_Rela *     rel;
663   const Elf_Internal_Rela *     rel_end;
664
665   if (info->relocateable)
666     return true;
667
668   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
669   sym_hashes = elf_sym_hashes (abfd);
670   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
671   if (!elf_bad_symtab (abfd))
672     sym_hashes_end -= symtab_hdr->sh_info;
673
674   rel_end = relocs + sec->reloc_count;
675
676   for (rel = relocs; rel < rel_end; rel++)
677     {
678       struct elf_link_hash_entry * h;
679       unsigned long r_symndx;
680
681       r_symndx = ELF32_R_SYM (rel->r_info);
682
683       if (r_symndx < symtab_hdr->sh_info)
684         h = NULL;
685       else
686         h = sym_hashes [r_symndx - symtab_hdr->sh_info];
687
688       switch (ELF32_R_TYPE (rel->r_info))
689         {
690         /* This relocation describes the C++ object vtable hierarchy.
691            Reconstruct it for later use during GC.  */
692         case R_MCORE_GNU_VTINHERIT:
693           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
694             return false;
695           break;
696
697         /* This relocation describes which C++ vtable entries are actually
698            used.  Record for later use during GC.  */
699         case R_MCORE_GNU_VTENTRY:
700           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
701             return false;
702           break;
703         }
704     }
705
706   return true;
707 }
708
709 #define TARGET_BIG_SYM          bfd_elf32_mcore_big_vec
710 #define TARGET_BIG_NAME         "elf32-mcore-big"
711 #define TARGET_LITTLE_SYM       bfd_elf32_mcore_little_vec
712 #define TARGET_LITTLE_NAME      "elf32-mcore-little"
713
714 #define ELF_ARCH                bfd_arch_mcore
715 #define ELF_MACHINE_CODE        EM_MCORE
716 #define ELF_MAXPAGESIZE         0x1000          /* 4k, if we ever have 'em */
717 #define elf_info_to_howto       mcore_elf_info_to_howto
718 #define elf_info_to_howto_rel   NULL
719
720 #define bfd_elf32_bfd_merge_private_bfd_data    mcore_elf_merge_private_bfd_data
721 #define bfd_elf32_bfd_set_private_flags         mcore_elf_set_private_flags
722 #define bfd_elf32_bfd_reloc_type_lookup         mcore_elf_reloc_type_lookup
723 #define elf_backend_relocate_section            mcore_elf_relocate_section
724 #define elf_backend_gc_mark_hook                mcore_elf_gc_mark_hook
725 #define elf_backend_gc_sweep_hook               mcore_elf_gc_sweep_hook
726 #define elf_backend_check_relocs                mcore_elf_check_relocs
727
728 #define elf_backend_can_gc_sections             1
729
730 #include "elf32-target.h"