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