1 /* Motorola MCore specific support for 32-bit ELF
2 Copyright (C) 1994-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
22 /* This file is based on a preliminary RCE ELF ABI. The
23 information may not match the final RCE ELF ABI. */
30 #include "elf/mcore.h"
33 /* RELA relocs are used here... */
35 /* Function to set whether a module needs the -mrelocatable bit set. */
38 mcore_elf_set_private_flags (bfd * abfd, flagword flags)
40 BFD_ASSERT (! elf_flags_init (abfd)
41 || elf_elfheader (abfd)->e_flags == flags);
43 elf_elfheader (abfd)->e_flags = flags;
44 elf_flags_init (abfd) = TRUE;
48 /* Merge backend specific data from an object file to the output
49 object file when linking. */
52 mcore_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
54 bfd *obfd = info->output_bfd;
58 /* Check if we have the same endianness. */
59 if (! _bfd_generic_verify_endian_match (ibfd, info))
62 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
63 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
66 new_flags = elf_elfheader (ibfd)->e_flags;
67 old_flags = elf_elfheader (obfd)->e_flags;
69 if (! elf_flags_init (obfd))
71 /* First call, no flags set. */
72 elf_flags_init (obfd) = TRUE;
73 elf_elfheader (obfd)->e_flags = new_flags;
75 else if (new_flags == old_flags)
76 /* Compatible flags are OK. */
86 /* Don't pretend we can deal with unsupported relocs. */
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)
97 BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
99 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
101 reloc_entry->howto->name,
102 reloc_entry->howto->type);
104 return bfd_reloc_notsupported;
107 static reloc_howto_type * mcore_elf_howto_table [(int) R_MCORE_max];
109 static reloc_howto_type mcore_elf_howto_raw[] =
111 /* This reloc does nothing. */
112 HOWTO (R_MCORE_NONE, /* type */
114 3, /* size (0 = byte, 1 = short, 2 = long) */
116 FALSE, /* pc_relative */
118 complain_overflow_dont, /* complain_on_overflow */
119 NULL, /* special_function */
120 "R_MCORE_NONE", /* name */
121 FALSE, /* partial_inplace */
124 FALSE), /* pcrel_offset */
126 /* A standard 32 bit relocation. */
127 HOWTO (R_MCORE_ADDR32, /* type */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
131 FALSE, /* pc_relative */
133 complain_overflow_bitfield, /* complain_on_overflow */
134 bfd_elf_generic_reloc, /* special_function */
135 "ADDR32", /* name *//* For compatibility with coff/pe port. */
136 FALSE, /* partial_inplace */
138 0xffffffff, /* dst_mask */
139 FALSE), /* pcrel_offset */
141 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
142 Should not appear in object files. */
143 HOWTO (R_MCORE_PCRELIMM8BY4, /* type */
145 1, /* size (0 = byte, 1 = short, 2 = long) */
147 TRUE, /* pc_relative */
149 complain_overflow_bitfield, /* complain_on_overflow */
150 mcore_elf_unsupported_reloc, /* special_function */
151 "R_MCORE_PCRELIMM8BY4",/* name */
152 FALSE, /* partial_inplace */
155 TRUE), /* pcrel_offset */
157 /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
158 Span 2k instructions == 4k bytes.
159 Only useful pieces at the relocated address are the opcode (5 bits) */
160 HOWTO (R_MCORE_PCRELIMM11BY2,/* type */
162 1, /* size (0 = byte, 1 = short, 2 = long) */
164 TRUE, /* pc_relative */
166 complain_overflow_signed, /* complain_on_overflow */
167 bfd_elf_generic_reloc, /* special_function */
168 "R_MCORE_PCRELIMM11BY2",/* name */
169 FALSE, /* partial_inplace */
171 0x7ff, /* dst_mask */
172 TRUE), /* pcrel_offset */
174 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
175 HOWTO (R_MCORE_PCRELIMM4BY2, /* type */
177 1, /* size (0 = byte, 1 = short, 2 = long) */
179 TRUE, /* pc_relative */
181 complain_overflow_bitfield, /* complain_on_overflow */
182 mcore_elf_unsupported_reloc,/* special_function */
183 "R_MCORE_PCRELIMM4BY2",/* name */
184 FALSE, /* partial_inplace */
187 TRUE), /* pcrel_offset */
189 /* 32-bit pc-relative. Eventually this will help support PIC code. */
190 HOWTO (R_MCORE_PCREL32, /* type */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 TRUE, /* pc_relative */
196 complain_overflow_bitfield, /* complain_on_overflow */
197 bfd_elf_generic_reloc, /* special_function */
198 "R_MCORE_PCREL32", /* name */
199 FALSE, /* partial_inplace */
201 0xffffffff, /* dst_mask */
202 TRUE), /* pcrel_offset */
204 /* Like PCRELIMM11BY2, this relocation indicates that there is a
205 'jsri' at the specified address. There is a separate relocation
206 entry for the literal pool entry that it references, but we
207 might be able to change the jsri to a bsr if the target turns out
208 to be close enough [even though we won't reclaim the literal pool
209 entry, we'll get some runtime efficiency back]. Note that this
210 is a relocation that we are allowed to safely ignore. */
211 HOWTO (R_MCORE_PCRELJSR_IMM11BY2,/* type */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
215 TRUE, /* pc_relative */
217 complain_overflow_signed, /* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_MCORE_PCRELJSR_IMM11BY2", /* name */
220 FALSE, /* partial_inplace */
222 0x7ff, /* dst_mask */
223 TRUE), /* pcrel_offset */
225 /* GNU extension to record C++ vtable hierarchy. */
226 HOWTO (R_MCORE_GNU_VTINHERIT, /* type */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
230 FALSE, /* pc_relative */
232 complain_overflow_dont, /* complain_on_overflow */
233 NULL, /* special_function */
234 "R_MCORE_GNU_VTINHERIT", /* name */
235 FALSE, /* partial_inplace */
238 FALSE), /* pcrel_offset */
240 /* GNU extension to record C++ vtable member usage. */
241 HOWTO (R_MCORE_GNU_VTENTRY, /* type */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
245 FALSE, /* pc_relative */
247 complain_overflow_dont,/* complain_on_overflow */
248 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
249 "R_MCORE_GNU_VTENTRY", /* name */
250 FALSE, /* partial_inplace */
253 FALSE), /* pcrel_offset */
255 HOWTO (R_MCORE_RELATIVE, /* type */
257 2, /* size (0 = byte, 1 = short, 2 = long) */
259 FALSE, /* pc_relative */
261 complain_overflow_signed, /* complain_on_overflow */
262 NULL, /* special_function */
263 "R_MCORE_RELATIVE", /* name */
264 TRUE, /* partial_inplace */
265 0xffffffff, /* src_mask */
266 0xffffffff, /* dst_mask */
267 FALSE) /* pcrel_offset */
271 #define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
274 /* Initialize the mcore_elf_howto_table, so that linear accesses can be done. */
276 mcore_elf_howto_init (void)
280 for (i = NUM_ELEM (mcore_elf_howto_raw); i--;)
284 type = mcore_elf_howto_raw[i].type;
286 BFD_ASSERT (type < NUM_ELEM (mcore_elf_howto_table));
288 mcore_elf_howto_table [type] = & mcore_elf_howto_raw [i];
292 static reloc_howto_type *
293 mcore_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
294 bfd_reloc_code_real_type code)
296 enum elf_mcore_reloc_type mcore_reloc = R_MCORE_NONE;
300 case BFD_RELOC_NONE: mcore_reloc = R_MCORE_NONE; break;
301 case BFD_RELOC_32: mcore_reloc = R_MCORE_ADDR32; break;
302 case BFD_RELOC_MCORE_PCREL_IMM8BY4: mcore_reloc = R_MCORE_PCRELIMM8BY4; break;
303 case BFD_RELOC_MCORE_PCREL_IMM11BY2: mcore_reloc = R_MCORE_PCRELIMM11BY2; break;
304 case BFD_RELOC_MCORE_PCREL_IMM4BY2: mcore_reloc = R_MCORE_PCRELIMM4BY2; break;
305 case BFD_RELOC_32_PCREL: mcore_reloc = R_MCORE_PCREL32; break;
306 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2: mcore_reloc = R_MCORE_PCRELJSR_IMM11BY2; break;
307 case BFD_RELOC_VTABLE_INHERIT: mcore_reloc = R_MCORE_GNU_VTINHERIT; break;
308 case BFD_RELOC_VTABLE_ENTRY: mcore_reloc = R_MCORE_GNU_VTENTRY; break;
309 case BFD_RELOC_RVA: mcore_reloc = R_MCORE_RELATIVE; break;
314 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
315 /* Initialize howto table if needed. */
316 mcore_elf_howto_init ();
318 return mcore_elf_howto_table [(int) mcore_reloc];
321 static reloc_howto_type *
322 mcore_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
328 i < sizeof (mcore_elf_howto_raw) / sizeof (mcore_elf_howto_raw[0]);
330 if (mcore_elf_howto_raw[i].name != NULL
331 && strcasecmp (mcore_elf_howto_raw[i].name, r_name) == 0)
332 return &mcore_elf_howto_raw[i];
337 /* Set the howto pointer for a RCE ELF reloc. */
340 mcore_elf_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
342 Elf_Internal_Rela * dst)
346 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
347 /* Initialize howto table if needed. */
348 mcore_elf_howto_init ();
350 r_type = ELF32_R_TYPE (dst->r_info);
351 if (r_type >= R_MCORE_max)
353 _bfd_error_handler (_("%B: unrecognised MCore reloc number: %d"),
355 bfd_set_error (bfd_error_bad_value);
356 r_type = R_MCORE_NONE;
359 cache_ptr->howto = mcore_elf_howto_table [r_type];
362 /* The RELOCATE_SECTION function is called by the ELF backend linker
363 to handle the relocations for a section.
365 The relocs are always passed as Rela structures; if the section
366 actually uses Rel structures, the r_addend field will always be
369 This function is responsible for adjust the section contents as
370 necessary, and (if using Rela relocs and generating a
371 relocatable output file) adjusting the reloc addend as
374 This function does not have to worry about setting the reloc
375 address or the reloc symbol index.
377 LOCAL_SYMS is a pointer to the swapped in local symbols.
379 LOCAL_SECTIONS is an array giving the section in the input file
380 corresponding to the st_shndx field of each local symbol.
382 The global hash table entry for the global symbols can be found
383 via elf_sym_hashes (input_bfd).
385 When generating relocatable output, this function must handle
386 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
387 going to be the section symbol corresponding to the output
388 section, which means that the addend must be adjusted
392 mcore_elf_relocate_section (bfd * output_bfd,
393 struct bfd_link_info * info,
395 asection * input_section,
397 Elf_Internal_Rela * relocs,
398 Elf_Internal_Sym * local_syms,
399 asection ** local_sections)
401 Elf_Internal_Shdr * symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
402 struct elf_link_hash_entry ** sym_hashes = elf_sym_hashes (input_bfd);
403 Elf_Internal_Rela * rel = relocs;
404 Elf_Internal_Rela * relend = relocs + input_section->reloc_count;
405 bfd_boolean ret = TRUE;
409 ("mcore_elf_relocate_section called for %B section %A, %ld relocations%s",
412 (long) input_section->reloc_count,
413 (bfd_link_relocatable (info)) ? " (relocatable)" : "");
416 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
417 mcore_elf_howto_init ();
419 for (; rel < relend; rel++)
421 enum elf_mcore_reloc_type r_type = (enum elf_mcore_reloc_type) ELF32_R_TYPE (rel->r_info);
422 bfd_vma offset = rel->r_offset;
423 bfd_vma addend = rel->r_addend;
424 bfd_reloc_status_type r = bfd_reloc_other;
425 asection * sec = NULL;
426 reloc_howto_type * howto;
428 Elf_Internal_Sym * sym = NULL;
429 unsigned long r_symndx;
430 struct elf_link_hash_entry * h = NULL;
431 unsigned short oldinst = 0;
433 /* Unknown relocation handling. */
434 if ((unsigned) r_type >= (unsigned) R_MCORE_max
435 || ! mcore_elf_howto_table [(int)r_type])
437 _bfd_error_handler (_("%B: Unknown relocation type %d\n"),
438 input_bfd, (int) r_type);
440 bfd_set_error (bfd_error_bad_value);
445 howto = mcore_elf_howto_table [(int) r_type];
446 r_symndx = ELF32_R_SYM (rel->r_info);
448 /* Complain about known relocation that are not yet supported. */
449 if (howto->special_function == mcore_elf_unsupported_reloc)
451 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
456 bfd_set_error (bfd_error_bad_value);
461 if (r_symndx < symtab_hdr->sh_info)
463 sym = local_syms + r_symndx;
464 sec = local_sections [r_symndx];
465 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
466 addend = rel->r_addend;
470 bfd_boolean unresolved_reloc, warned, ignored;
472 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
473 r_symndx, symtab_hdr, sym_hashes,
475 unresolved_reloc, warned, ignored);
478 if (sec != NULL && discarded_section (sec))
479 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
480 rel, 1, relend, howto, 0, contents);
482 if (bfd_link_relocatable (info))
490 case R_MCORE_PCRELJSR_IMM11BY2:
491 oldinst = bfd_get_16 (input_bfd, contents + offset);
492 #define MCORE_INST_BSR 0xF800
493 bfd_put_16 (input_bfd, (bfd_vma) MCORE_INST_BSR, contents + offset);
498 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
499 howto->name, r_type, r_symndx, (long) offset, (long) addend);
502 r = _bfd_final_link_relocate
503 (howto, input_bfd, input_section, contents, offset, relocation, addend);
505 if (r != bfd_reloc_ok && r_type == R_MCORE_PCRELJSR_IMM11BY2)
507 /* Wasn't ok, back it out and give up. */
508 bfd_put_16 (input_bfd, (bfd_vma) oldinst, contents + offset);
512 if (r != bfd_reloc_ok)
521 case bfd_reloc_overflow:
529 name = bfd_elf_string_from_elf_section
530 (input_bfd, symtab_hdr->sh_link, sym->st_name);
536 name = bfd_section_name (input_bfd, sec);
539 (*info->callbacks->reloc_overflow)
540 (info, (h ? &h->root : NULL), name, howto->name,
541 (bfd_vma) 0, input_bfd, input_section, offset);
549 fprintf (stderr, "\n");
555 /* Return the section that should be marked against GC for a given
559 mcore_elf_gc_mark_hook (asection *sec,
560 struct bfd_link_info *info,
561 Elf_Internal_Rela *rel,
562 struct elf_link_hash_entry *h,
563 Elf_Internal_Sym *sym)
566 switch (ELF32_R_TYPE (rel->r_info))
568 case R_MCORE_GNU_VTINHERIT:
569 case R_MCORE_GNU_VTENTRY:
573 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
576 /* Update the got entry reference counts for the section being removed. */
579 mcore_elf_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
580 struct bfd_link_info * info ATTRIBUTE_UNUSED,
581 asection * sec ATTRIBUTE_UNUSED,
582 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
587 /* Look through the relocs for a section during the first phase.
588 Since we don't do .gots or .plts, we just need to consider the
589 virtual table relocs for gc. */
592 mcore_elf_check_relocs (bfd * abfd,
593 struct bfd_link_info * info,
595 const Elf_Internal_Rela * relocs)
597 Elf_Internal_Shdr * symtab_hdr;
598 struct elf_link_hash_entry ** sym_hashes;
599 const Elf_Internal_Rela * rel;
600 const Elf_Internal_Rela * rel_end;
602 if (bfd_link_relocatable (info))
605 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
606 sym_hashes = elf_sym_hashes (abfd);
608 rel_end = relocs + sec->reloc_count;
610 for (rel = relocs; rel < rel_end; rel++)
612 struct elf_link_hash_entry * h;
613 unsigned long r_symndx;
615 r_symndx = ELF32_R_SYM (rel->r_info);
617 if (r_symndx < symtab_hdr->sh_info)
621 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
622 while (h->root.type == bfd_link_hash_indirect
623 || h->root.type == bfd_link_hash_warning)
624 h = (struct elf_link_hash_entry *) h->root.u.i.link;
626 /* PR15323, ref flags aren't set for references in the same
628 h->root.non_ir_ref = 1;
631 switch (ELF32_R_TYPE (rel->r_info))
633 /* This relocation describes the C++ object vtable hierarchy.
634 Reconstruct it for later use during GC. */
635 case R_MCORE_GNU_VTINHERIT:
636 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
640 /* This relocation describes which C++ vtable entries are actually
641 used. Record for later use during GC. */
642 case R_MCORE_GNU_VTENTRY:
643 BFD_ASSERT (h != NULL);
645 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
654 static const struct bfd_elf_special_section mcore_elf_special_sections[]=
656 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
657 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
661 #define TARGET_BIG_SYM mcore_elf32_be_vec
662 #define TARGET_BIG_NAME "elf32-mcore-big"
663 #define TARGET_LITTLE_SYM mcore_elf32_le_vec
664 #define TARGET_LITTLE_NAME "elf32-mcore-little"
666 #define ELF_ARCH bfd_arch_mcore
667 #define ELF_MACHINE_CODE EM_MCORE
668 #define ELF_MAXPAGESIZE 0x1000 /* 4k, if we ever have 'em */
669 #define elf_info_to_howto mcore_elf_info_to_howto
670 #define elf_info_to_howto_rel NULL
672 #define bfd_elf32_bfd_merge_private_bfd_data mcore_elf_merge_private_bfd_data
673 #define bfd_elf32_bfd_set_private_flags mcore_elf_set_private_flags
674 #define bfd_elf32_bfd_reloc_type_lookup mcore_elf_reloc_type_lookup
675 #define bfd_elf32_bfd_reloc_name_lookup mcore_elf_reloc_name_lookup
676 #define elf_backend_relocate_section mcore_elf_relocate_section
677 #define elf_backend_gc_mark_hook mcore_elf_gc_mark_hook
678 #define elf_backend_gc_sweep_hook mcore_elf_gc_sweep_hook
679 #define elf_backend_check_relocs mcore_elf_check_relocs
680 #define elf_backend_special_sections mcore_elf_special_sections
682 #define elf_backend_can_gc_sections 1
683 #define elf_backend_rela_normal 1
685 #include "elf32-target.h"