1 /* Motorola MCore specific support for 32-bit ELF
2 Copyright (C) 1994-2018 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 /* xgettext:c-format */
100 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
102 reloc_entry->howto->name,
103 reloc_entry->howto->type);
105 return bfd_reloc_notsupported;
108 static reloc_howto_type * mcore_elf_howto_table [(int) R_MCORE_max];
110 static reloc_howto_type mcore_elf_howto_raw[] =
112 /* This reloc does nothing. */
113 HOWTO (R_MCORE_NONE, /* type */
115 3, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE, /* pc_relative */
119 complain_overflow_dont, /* complain_on_overflow */
120 NULL, /* special_function */
121 "R_MCORE_NONE", /* name */
122 FALSE, /* partial_inplace */
125 FALSE), /* pcrel_offset */
127 /* A standard 32 bit relocation. */
128 HOWTO (R_MCORE_ADDR32, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 FALSE, /* pc_relative */
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 */
139 0xffffffff, /* dst_mask */
140 FALSE), /* pcrel_offset */
142 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
143 Should not appear in object files. */
144 HOWTO (R_MCORE_PCRELIMM8BY4, /* type */
146 1, /* size (0 = byte, 1 = short, 2 = long) */
148 TRUE, /* pc_relative */
150 complain_overflow_bitfield, /* complain_on_overflow */
151 mcore_elf_unsupported_reloc, /* special_function */
152 "R_MCORE_PCRELIMM8BY4",/* name */
153 FALSE, /* partial_inplace */
156 TRUE), /* pcrel_offset */
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 */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
165 TRUE, /* pc_relative */
167 complain_overflow_signed, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_MCORE_PCRELIMM11BY2",/* name */
170 FALSE, /* partial_inplace */
172 0x7ff, /* dst_mask */
173 TRUE), /* pcrel_offset */
175 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
176 HOWTO (R_MCORE_PCRELIMM4BY2, /* type */
178 1, /* size (0 = byte, 1 = short, 2 = long) */
180 TRUE, /* pc_relative */
182 complain_overflow_bitfield, /* complain_on_overflow */
183 mcore_elf_unsupported_reloc,/* special_function */
184 "R_MCORE_PCRELIMM4BY2",/* name */
185 FALSE, /* partial_inplace */
188 TRUE), /* pcrel_offset */
190 /* 32-bit pc-relative. Eventually this will help support PIC code. */
191 HOWTO (R_MCORE_PCREL32, /* type */
193 2, /* size (0 = byte, 1 = short, 2 = long) */
195 TRUE, /* pc_relative */
197 complain_overflow_bitfield, /* complain_on_overflow */
198 bfd_elf_generic_reloc, /* special_function */
199 "R_MCORE_PCREL32", /* name */
200 FALSE, /* partial_inplace */
202 0xffffffff, /* dst_mask */
203 TRUE), /* pcrel_offset */
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 */
214 1, /* size (0 = byte, 1 = short, 2 = long) */
216 TRUE, /* pc_relative */
218 complain_overflow_signed, /* complain_on_overflow */
219 bfd_elf_generic_reloc, /* special_function */
220 "R_MCORE_PCRELJSR_IMM11BY2", /* name */
221 FALSE, /* partial_inplace */
223 0x7ff, /* dst_mask */
224 TRUE), /* pcrel_offset */
226 /* GNU extension to record C++ vtable hierarchy. */
227 HOWTO (R_MCORE_GNU_VTINHERIT, /* type */
229 2, /* size (0 = byte, 1 = short, 2 = long) */
231 FALSE, /* pc_relative */
233 complain_overflow_dont, /* complain_on_overflow */
234 NULL, /* special_function */
235 "R_MCORE_GNU_VTINHERIT", /* name */
236 FALSE, /* partial_inplace */
239 FALSE), /* pcrel_offset */
241 /* GNU extension to record C++ vtable member usage. */
242 HOWTO (R_MCORE_GNU_VTENTRY, /* type */
244 2, /* size (0 = byte, 1 = short, 2 = long) */
246 FALSE, /* pc_relative */
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 */
254 FALSE), /* pcrel_offset */
256 HOWTO (R_MCORE_RELATIVE, /* type */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
260 FALSE, /* pc_relative */
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 */
272 #define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
275 /* Initialize the mcore_elf_howto_table, so that linear accesses can be done. */
277 mcore_elf_howto_init (void)
281 for (i = NUM_ELEM (mcore_elf_howto_raw); i--;)
285 type = mcore_elf_howto_raw[i].type;
287 BFD_ASSERT (type < NUM_ELEM (mcore_elf_howto_table));
289 mcore_elf_howto_table [type] = & mcore_elf_howto_raw [i];
293 static reloc_howto_type *
294 mcore_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
295 bfd_reloc_code_real_type code)
297 enum elf_mcore_reloc_type mcore_reloc = R_MCORE_NONE;
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;
315 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
316 /* Initialize howto table if needed. */
317 mcore_elf_howto_init ();
319 return mcore_elf_howto_table [(int) mcore_reloc];
322 static reloc_howto_type *
323 mcore_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
329 i < sizeof (mcore_elf_howto_raw) / sizeof (mcore_elf_howto_raw[0]);
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];
338 /* Set the howto pointer for a RCE ELF reloc. */
341 mcore_elf_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
343 Elf_Internal_Rela * dst)
347 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
348 /* Initialize howto table if needed. */
349 mcore_elf_howto_init ();
351 r_type = ELF32_R_TYPE (dst->r_info);
352 if (r_type >= R_MCORE_max)
354 /* xgettext:c-format */
355 _bfd_error_handler (_("%B: unrecognised MCore reloc number: %d"),
357 bfd_set_error (bfd_error_bad_value);
358 r_type = R_MCORE_NONE;
361 cache_ptr->howto = mcore_elf_howto_table [r_type];
364 /* The RELOCATE_SECTION function is called by the ELF backend linker
365 to handle the relocations for a section.
367 The relocs are always passed as Rela structures; if the section
368 actually uses Rel structures, the r_addend field will always be
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
376 This function does not have to worry about setting the reloc
377 address or the reloc symbol index.
379 LOCAL_SYMS is a pointer to the swapped in local symbols.
381 LOCAL_SECTIONS is an array giving the section in the input file
382 corresponding to the st_shndx field of each local symbol.
384 The global hash table entry for the global symbols can be found
385 via elf_sym_hashes (input_bfd).
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
394 mcore_elf_relocate_section (bfd * output_bfd,
395 struct bfd_link_info * info,
397 asection * input_section,
399 Elf_Internal_Rela * relocs,
400 Elf_Internal_Sym * local_syms,
401 asection ** local_sections)
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;
411 ("mcore_elf_relocate_section called for %B section %A, %u relocations%s",
414 input_section->reloc_count,
415 (bfd_link_relocatable (info)) ? " (relocatable)" : "");
418 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
419 mcore_elf_howto_init ();
421 for (; rel < relend; rel++)
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;
430 Elf_Internal_Sym * sym = NULL;
431 unsigned long r_symndx;
432 struct elf_link_hash_entry * h = NULL;
433 unsigned short oldinst = 0;
435 /* Unknown relocation handling. */
436 if ((unsigned) r_type >= (unsigned) R_MCORE_max
437 || ! mcore_elf_howto_table [(int)r_type])
439 /* xgettext:c-format */
440 _bfd_error_handler (_("%B: Unknown relocation type %d\n"),
441 input_bfd, (int) r_type);
443 bfd_set_error (bfd_error_bad_value);
448 howto = mcore_elf_howto_table [(int) r_type];
449 r_symndx = ELF32_R_SYM (rel->r_info);
451 /* Complain about known relocation that are not yet supported. */
452 if (howto->special_function == mcore_elf_unsupported_reloc)
454 /* xgettext:c-format */
455 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
460 bfd_set_error (bfd_error_bad_value);
465 if (r_symndx < symtab_hdr->sh_info)
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;
474 bfd_boolean unresolved_reloc, warned, ignored;
476 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
477 r_symndx, symtab_hdr, sym_hashes,
479 unresolved_reloc, warned, ignored);
482 if (sec != NULL && discarded_section (sec))
483 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
484 rel, 1, relend, howto, 0, contents);
486 if (bfd_link_relocatable (info))
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);
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);
506 r = _bfd_final_link_relocate
507 (howto, input_bfd, input_section, contents, offset, relocation, addend);
509 if (r != bfd_reloc_ok && r_type == R_MCORE_PCRELJSR_IMM11BY2)
511 /* Wasn't ok, back it out and give up. */
512 bfd_put_16 (input_bfd, (bfd_vma) oldinst, contents + offset);
516 if (r != bfd_reloc_ok)
525 case bfd_reloc_overflow:
533 name = bfd_elf_string_from_elf_section
534 (input_bfd, symtab_hdr->sh_link, sym->st_name);
540 name = bfd_section_name (input_bfd, sec);
543 (*info->callbacks->reloc_overflow)
544 (info, (h ? &h->root : NULL), name, howto->name,
545 (bfd_vma) 0, input_bfd, input_section, offset);
553 fprintf (stderr, "\n");
559 /* Return the section that should be marked against GC for a given
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)
570 switch (ELF32_R_TYPE (rel->r_info))
572 case R_MCORE_GNU_VTINHERIT:
573 case R_MCORE_GNU_VTENTRY:
577 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
580 /* Look through the relocs for a section during the first phase.
581 Since we don't do .gots or .plts, we just need to consider the
582 virtual table relocs for gc. */
585 mcore_elf_check_relocs (bfd * abfd,
586 struct bfd_link_info * info,
588 const Elf_Internal_Rela * relocs)
590 Elf_Internal_Shdr * symtab_hdr;
591 struct elf_link_hash_entry ** sym_hashes;
592 const Elf_Internal_Rela * rel;
593 const Elf_Internal_Rela * rel_end;
595 if (bfd_link_relocatable (info))
598 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
599 sym_hashes = elf_sym_hashes (abfd);
601 rel_end = relocs + sec->reloc_count;
603 for (rel = relocs; rel < rel_end; rel++)
605 struct elf_link_hash_entry * h;
606 unsigned long r_symndx;
608 r_symndx = ELF32_R_SYM (rel->r_info);
610 if (r_symndx < symtab_hdr->sh_info)
614 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
615 while (h->root.type == bfd_link_hash_indirect
616 || h->root.type == bfd_link_hash_warning)
617 h = (struct elf_link_hash_entry *) h->root.u.i.link;
620 switch (ELF32_R_TYPE (rel->r_info))
622 /* This relocation describes the C++ object vtable hierarchy.
623 Reconstruct it for later use during GC. */
624 case R_MCORE_GNU_VTINHERIT:
625 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
629 /* This relocation describes which C++ vtable entries are actually
630 used. Record for later use during GC. */
631 case R_MCORE_GNU_VTENTRY:
632 BFD_ASSERT (h != NULL);
634 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
643 static const struct bfd_elf_special_section mcore_elf_special_sections[]=
645 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
646 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
650 #define TARGET_BIG_SYM mcore_elf32_be_vec
651 #define TARGET_BIG_NAME "elf32-mcore-big"
652 #define TARGET_LITTLE_SYM mcore_elf32_le_vec
653 #define TARGET_LITTLE_NAME "elf32-mcore-little"
655 #define ELF_ARCH bfd_arch_mcore
656 #define ELF_MACHINE_CODE EM_MCORE
657 #define ELF_MAXPAGESIZE 0x1000 /* 4k, if we ever have 'em */
658 #define elf_info_to_howto mcore_elf_info_to_howto
659 #define elf_info_to_howto_rel NULL
661 #define bfd_elf32_bfd_merge_private_bfd_data mcore_elf_merge_private_bfd_data
662 #define bfd_elf32_bfd_set_private_flags mcore_elf_set_private_flags
663 #define bfd_elf32_bfd_reloc_type_lookup mcore_elf_reloc_type_lookup
664 #define bfd_elf32_bfd_reloc_name_lookup mcore_elf_reloc_name_lookup
665 #define elf_backend_relocate_section mcore_elf_relocate_section
666 #define elf_backend_gc_mark_hook mcore_elf_gc_mark_hook
667 #define elf_backend_check_relocs mcore_elf_check_relocs
668 #define elf_backend_special_sections mcore_elf_special_sections
670 #define elf_backend_can_gc_sections 1
671 #define elf_backend_rela_normal 1
673 #include "elf32-target.h"