1 /* Motorola MCore specific support for 32-bit ELF
2 Copyright 1994, 1995, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
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, bfd * obfd)
57 /* Check if we have the same endianess. */
58 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
61 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
62 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
65 new_flags = elf_elfheader (ibfd)->e_flags;
66 old_flags = elf_elfheader (obfd)->e_flags;
68 if (! elf_flags_init (obfd))
70 /* First call, no flags set. */
71 elf_flags_init (obfd) = TRUE;
72 elf_elfheader (obfd)->e_flags = new_flags;
74 else if (new_flags == old_flags)
75 /* Compatible flags are OK. */
85 /* Don't pretend we can deal with unsupported relocs. */
87 static bfd_reloc_status_type
88 mcore_elf_unsupported_reloc (bfd * abfd,
89 arelent * reloc_entry,
90 asymbol * symbol ATTRIBUTE_UNUSED,
91 PTR data ATTRIBUTE_UNUSED,
92 asection * input_section ATTRIBUTE_UNUSED,
93 bfd * output_bfd ATTRIBUTE_UNUSED,
94 char ** error_message ATTRIBUTE_UNUSED)
96 BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
98 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
100 reloc_entry->howto->name,
101 reloc_entry->howto->type);
103 return bfd_reloc_notsupported;
106 static reloc_howto_type * mcore_elf_howto_table [(int) R_MCORE_max];
108 static reloc_howto_type mcore_elf_howto_raw[] =
110 /* This reloc does nothing. */
111 HOWTO (R_MCORE_NONE, /* type */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
115 FALSE, /* pc_relative */
117 complain_overflow_bitfield, /* complain_on_overflow */
118 NULL, /* special_function */
119 "R_MCORE_NONE", /* name */
120 FALSE, /* partial_inplace */
123 FALSE), /* pcrel_offset */
125 /* A standard 32 bit relocation. */
126 HOWTO (R_MCORE_ADDR32, /* type */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
130 FALSE, /* pc_relative */
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 */
137 0xffffffff, /* dst_mask */
138 FALSE), /* pcrel_offset */
140 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
141 Should not appear in object files. */
142 HOWTO (R_MCORE_PCRELIMM8BY4, /* type */
144 1, /* size (0 = byte, 1 = short, 2 = long) */
146 TRUE, /* pc_relative */
148 complain_overflow_bitfield, /* complain_on_overflow */
149 mcore_elf_unsupported_reloc, /* special_function */
150 "R_MCORE_PCRELIMM8BY4",/* name */
151 FALSE, /* partial_inplace */
154 TRUE), /* pcrel_offset */
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 */
161 1, /* size (0 = byte, 1 = short, 2 = long) */
163 TRUE, /* pc_relative */
165 complain_overflow_signed, /* complain_on_overflow */
166 bfd_elf_generic_reloc, /* special_function */
167 "R_MCORE_PCRELIMM11BY2",/* name */
168 FALSE, /* partial_inplace */
170 0x7ff, /* dst_mask */
171 TRUE), /* pcrel_offset */
173 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
174 HOWTO (R_MCORE_PCRELIMM4BY2, /* type */
176 1, /* size (0 = byte, 1 = short, 2 = long) */
178 TRUE, /* pc_relative */
180 complain_overflow_bitfield, /* complain_on_overflow */
181 mcore_elf_unsupported_reloc,/* special_function */
182 "R_MCORE_PCRELIMM4BY2",/* name */
183 FALSE, /* partial_inplace */
186 TRUE), /* pcrel_offset */
188 /* 32-bit pc-relative. Eventually this will help support PIC code. */
189 HOWTO (R_MCORE_PCREL32, /* type */
191 2, /* size (0 = byte, 1 = short, 2 = long) */
193 TRUE, /* pc_relative */
195 complain_overflow_bitfield, /* complain_on_overflow */
196 bfd_elf_generic_reloc, /* special_function */
197 "R_MCORE_PCREL32", /* name */
198 FALSE, /* partial_inplace */
200 0xffffffff, /* dst_mask */
201 TRUE), /* pcrel_offset */
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 */
212 1, /* size (0 = byte, 1 = short, 2 = long) */
214 TRUE, /* pc_relative */
216 complain_overflow_signed, /* complain_on_overflow */
217 bfd_elf_generic_reloc, /* special_function */
218 "R_MCORE_PCRELJSR_IMM11BY2", /* name */
219 FALSE, /* partial_inplace */
221 0x7ff, /* dst_mask */
222 TRUE), /* pcrel_offset */
224 /* GNU extension to record C++ vtable hierarchy. */
225 HOWTO (R_MCORE_GNU_VTINHERIT, /* type */
227 2, /* size (0 = byte, 1 = short, 2 = long) */
229 FALSE, /* pc_relative */
231 complain_overflow_dont, /* complain_on_overflow */
232 NULL, /* special_function */
233 "R_MCORE_GNU_VTINHERIT", /* name */
234 FALSE, /* partial_inplace */
237 FALSE), /* pcrel_offset */
239 /* GNU extension to record C++ vtable member usage. */
240 HOWTO (R_MCORE_GNU_VTENTRY, /* type */
242 2, /* size (0 = byte, 1 = short, 2 = long) */
244 FALSE, /* pc_relative */
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 */
252 FALSE), /* pcrel_offset */
254 HOWTO (R_MCORE_RELATIVE, /* type */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
258 FALSE, /* pc_relative */
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 */
270 #define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
273 /* Initialize the mcore_elf_howto_table, so that linear accesses can be done. */
275 mcore_elf_howto_init (void)
279 for (i = NUM_ELEM (mcore_elf_howto_raw); i--;)
283 type = mcore_elf_howto_raw[i].type;
285 BFD_ASSERT (type < NUM_ELEM (mcore_elf_howto_table));
287 mcore_elf_howto_table [type] = & mcore_elf_howto_raw [i];
291 static reloc_howto_type *
292 mcore_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
293 bfd_reloc_code_real_type code)
295 enum elf_mcore_reloc_type mcore_reloc = R_MCORE_NONE;
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;
313 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
314 /* Initialize howto table if needed. */
315 mcore_elf_howto_init ();
317 return mcore_elf_howto_table [(int) mcore_reloc];
320 /* Set the howto pointer for a RCE ELF reloc. */
323 mcore_elf_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
325 Elf_Internal_Rela * dst)
327 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4])
328 /* Initialize howto table if needed. */
329 mcore_elf_howto_init ();
331 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_MCORE_max);
333 cache_ptr->howto = mcore_elf_howto_table [ELF32_R_TYPE (dst->r_info)];
336 /* The RELOCATE_SECTION function is called by the ELF backend linker
337 to handle the relocations for a section.
339 The relocs are always passed as Rela structures; if the section
340 actually uses Rel structures, the r_addend field will always be
343 This function is responsible for adjust the section contents as
344 necessary, and (if using Rela relocs and generating a
345 relocatable output file) adjusting the reloc addend as
348 This function does not have to worry about setting the reloc
349 address or the reloc symbol index.
351 LOCAL_SYMS is a pointer to the swapped in local symbols.
353 LOCAL_SECTIONS is an array giving the section in the input file
354 corresponding to the st_shndx field of each local symbol.
356 The global hash table entry for the global symbols can be found
357 via elf_sym_hashes (input_bfd).
359 When generating relocatable output, this function must handle
360 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
361 going to be the section symbol corresponding to the output
362 section, which means that the addend must be adjusted
366 mcore_elf_relocate_section (bfd * output_bfd,
367 struct bfd_link_info * info,
369 asection * input_section,
371 Elf_Internal_Rela * relocs,
372 Elf_Internal_Sym * local_syms,
373 asection ** local_sections)
375 Elf_Internal_Shdr * symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
376 struct elf_link_hash_entry ** sym_hashes = elf_sym_hashes (input_bfd);
377 Elf_Internal_Rela * rel = relocs;
378 Elf_Internal_Rela * relend = relocs + input_section->reloc_count;
379 bfd_boolean ret = TRUE;
383 ("mcore_elf_relocate_section called for %B section %A, %ld relocations%s",
386 (long) input_section->reloc_count,
387 (info->relocatable) ? " (relocatable)" : "");
390 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
391 mcore_elf_howto_init ();
393 for (; rel < relend; rel++)
395 enum elf_mcore_reloc_type r_type = (enum elf_mcore_reloc_type) ELF32_R_TYPE (rel->r_info);
396 bfd_vma offset = rel->r_offset;
397 bfd_vma addend = rel->r_addend;
398 bfd_reloc_status_type r = bfd_reloc_other;
399 asection * sec = NULL;
400 reloc_howto_type * howto;
402 Elf_Internal_Sym * sym = NULL;
403 unsigned long r_symndx;
404 struct elf_link_hash_entry * h = NULL;
405 unsigned short oldinst = 0;
407 /* Unknown relocation handling. */
408 if ((unsigned) r_type >= (unsigned) R_MCORE_max
409 || ! mcore_elf_howto_table [(int)r_type])
411 _bfd_error_handler (_("%B: Unknown relocation type %d\n"),
412 input_bfd, (int) r_type);
414 bfd_set_error (bfd_error_bad_value);
419 howto = mcore_elf_howto_table [(int) r_type];
420 r_symndx = ELF32_R_SYM (rel->r_info);
422 /* Complain about known relocation that are not yet supported. */
423 if (howto->special_function == mcore_elf_unsupported_reloc)
425 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
430 bfd_set_error (bfd_error_bad_value);
435 if (r_symndx < symtab_hdr->sh_info)
437 sym = local_syms + r_symndx;
438 sec = local_sections [r_symndx];
439 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
440 addend = rel->r_addend;
444 bfd_boolean unresolved_reloc, warned;
446 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
447 r_symndx, symtab_hdr, sym_hashes,
449 unresolved_reloc, warned);
452 if (sec != NULL && elf_discarded_section (sec))
454 /* For relocs against symbols from removed linkonce sections,
455 or sections discarded by a linker script, we just want the
456 section contents zeroed. Avoid any special processing. */
457 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
463 if (info->relocatable)
471 case R_MCORE_PCRELJSR_IMM11BY2:
472 oldinst = bfd_get_16 (input_bfd, contents + offset);
473 #define MCORE_INST_BSR 0xF800
474 bfd_put_16 (input_bfd, (bfd_vma) MCORE_INST_BSR, contents + offset);
479 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
480 howto->name, r_type, r_symndx, (long) offset, (long) addend);
483 r = _bfd_final_link_relocate
484 (howto, input_bfd, input_section, contents, offset, relocation, addend);
486 if (r != bfd_reloc_ok && r_type == R_MCORE_PCRELJSR_IMM11BY2)
488 /* Wasn't ok, back it out and give up. */
489 bfd_put_16 (input_bfd, (bfd_vma) oldinst, contents + offset);
493 if (r != bfd_reloc_ok)
502 case bfd_reloc_overflow:
510 name = bfd_elf_string_from_elf_section
511 (input_bfd, symtab_hdr->sh_link, sym->st_name);
517 name = bfd_section_name (input_bfd, sec);
520 (*info->callbacks->reloc_overflow)
521 (info, (h ? &h->root : NULL), name, howto->name,
522 (bfd_vma) 0, input_bfd, input_section, offset);
530 fprintf (stderr, "\n");
536 /* Return the section that should be marked against GC for a given
540 mcore_elf_gc_mark_hook (asection *sec,
541 struct bfd_link_info *info,
542 Elf_Internal_Rela *rel,
543 struct elf_link_hash_entry *h,
544 Elf_Internal_Sym *sym)
547 switch (ELF32_R_TYPE (rel->r_info))
549 case R_MCORE_GNU_VTINHERIT:
550 case R_MCORE_GNU_VTENTRY:
554 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
557 /* Update the got entry reference counts for the section being removed. */
560 mcore_elf_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
561 struct bfd_link_info * info ATTRIBUTE_UNUSED,
562 asection * sec ATTRIBUTE_UNUSED,
563 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
568 /* Look through the relocs for a section during the first phase.
569 Since we don't do .gots or .plts, we just need to consider the
570 virtual table relocs for gc. */
573 mcore_elf_check_relocs (bfd * abfd,
574 struct bfd_link_info * info,
576 const Elf_Internal_Rela * relocs)
578 Elf_Internal_Shdr * symtab_hdr;
579 struct elf_link_hash_entry ** sym_hashes;
580 struct elf_link_hash_entry ** sym_hashes_end;
581 const Elf_Internal_Rela * rel;
582 const Elf_Internal_Rela * rel_end;
584 if (info->relocatable)
587 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
588 sym_hashes = elf_sym_hashes (abfd);
589 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
590 if (!elf_bad_symtab (abfd))
591 sym_hashes_end -= symtab_hdr->sh_info;
593 rel_end = relocs + sec->reloc_count;
595 for (rel = relocs; rel < rel_end; rel++)
597 struct elf_link_hash_entry * h;
598 unsigned long r_symndx;
600 r_symndx = ELF32_R_SYM (rel->r_info);
602 if (r_symndx < symtab_hdr->sh_info)
606 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
607 while (h->root.type == bfd_link_hash_indirect
608 || h->root.type == bfd_link_hash_warning)
609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
612 switch (ELF32_R_TYPE (rel->r_info))
614 /* This relocation describes the C++ object vtable hierarchy.
615 Reconstruct it for later use during GC. */
616 case R_MCORE_GNU_VTINHERIT:
617 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
621 /* This relocation describes which C++ vtable entries are actually
622 used. Record for later use during GC. */
623 case R_MCORE_GNU_VTENTRY:
624 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
633 static const struct bfd_elf_special_section mcore_elf_special_sections[]=
635 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
636 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
640 #define TARGET_BIG_SYM bfd_elf32_mcore_big_vec
641 #define TARGET_BIG_NAME "elf32-mcore-big"
642 #define TARGET_LITTLE_SYM bfd_elf32_mcore_little_vec
643 #define TARGET_LITTLE_NAME "elf32-mcore-little"
645 #define ELF_ARCH bfd_arch_mcore
646 #define ELF_MACHINE_CODE EM_MCORE
647 #define ELF_MAXPAGESIZE 0x1000 /* 4k, if we ever have 'em */
648 #define elf_info_to_howto mcore_elf_info_to_howto
649 #define elf_info_to_howto_rel NULL
651 #define bfd_elf32_bfd_merge_private_bfd_data mcore_elf_merge_private_bfd_data
652 #define bfd_elf32_bfd_set_private_flags mcore_elf_set_private_flags
653 #define bfd_elf32_bfd_reloc_type_lookup mcore_elf_reloc_type_lookup
654 #define elf_backend_relocate_section mcore_elf_relocate_section
655 #define elf_backend_gc_mark_hook mcore_elf_gc_mark_hook
656 #define elf_backend_gc_sweep_hook mcore_elf_gc_sweep_hook
657 #define elf_backend_check_relocs mcore_elf_check_relocs
658 #define elf_backend_special_sections mcore_elf_special_sections
660 #define elf_backend_can_gc_sections 1
661 #define elf_backend_rela_normal 1
663 #include "elf32-target.h"