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