1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 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 3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
28 /* Forward declarations. */
29 static reloc_howto_type * m32c_reloc_type_lookup
30 (bfd *, bfd_reloc_code_real_type);
31 static void m32c_info_to_howto_rela
32 (bfd *, arelent *, Elf_Internal_Rela *);
33 static bfd_boolean m32c_elf_relocate_section
34 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35 static bfd_boolean m32c_elf_check_relocs
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
39 char * m32c_get_reloc (long reloc);
40 void dump_symtab (bfd *, void *, void *);
42 static bfd_boolean m32c_elf_relax_section
43 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
46 static reloc_howto_type m32c_elf_howto_table [] =
48 /* This reloc does nothing. */
49 HOWTO (R_M32C_NONE, /* type */
51 0, /* size (0 = byte, 1 = short, 2 = long) */
53 FALSE, /* pc_relative */
55 complain_overflow_bitfield, /* complain_on_overflow */
56 bfd_elf_generic_reloc, /* special_function */
57 "R_M32C_NONE", /* name */
58 FALSE, /* partial_inplace */
61 FALSE), /* pcrel_offset */
63 /* GCC intentionally overflows these next two in order to work
64 around limitations in the addressing modes, so don't complain
66 HOWTO (R_M32C_16, /* type */
68 1, /* size (0 = byte, 1 = short, 2 = long) */
70 FALSE, /* pc_relative */
72 complain_overflow_dont, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_M32C_16", /* name */
75 FALSE, /* partial_inplace */
77 0xffff, /* dst_mask */
78 FALSE), /* pcrel_offset */
80 HOWTO (R_M32C_24, /* type */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
84 FALSE, /* pc_relative */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_M32C_24", /* name */
89 FALSE, /* partial_inplace */
91 0xffffff, /* dst_mask */
92 FALSE), /* pcrel_offset */
94 HOWTO (R_M32C_32, /* type */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
98 FALSE, /* pc_relative */
100 complain_overflow_bitfield, /* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
102 "R_M32C_32", /* name */
103 FALSE, /* partial_inplace */
105 0xffffffff, /* dst_mask */
106 FALSE), /* pcrel_offset */
108 HOWTO (R_M32C_8_PCREL, /* type */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
112 TRUE, /* pc_relative */
114 complain_overflow_signed, /* complain_on_overflow */
115 bfd_elf_generic_reloc, /* special_function */
116 "R_M32C_8_PCREL", /* name */
117 FALSE, /* partial_inplace */
120 TRUE), /* pcrel_offset */
122 HOWTO (R_M32C_16_PCREL, /* type */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
126 TRUE, /* pc_relative */
128 complain_overflow_signed, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_M32C_16_PCREL", /* name */
131 FALSE, /* partial_inplace */
133 0xffff, /* dst_mask */
134 TRUE), /* pcrel_offset */
136 HOWTO (R_M32C_8, /* type */
138 0, /* size (0 = byte, 1 = short, 2 = long) */
140 FALSE, /* pc_relative */
142 complain_overflow_unsigned, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_M32C_8", /* name */
145 FALSE, /* partial_inplace */
148 FALSE), /* pcrel_offset */
150 HOWTO (R_M32C_LO16, /* type */
152 1, /* size (0 = byte, 1 = short, 2 = long) */
154 FALSE, /* pc_relative */
156 complain_overflow_dont, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_M32C_LO16", /* name */
159 FALSE, /* partial_inplace */
161 0xffff, /* dst_mask */
162 FALSE), /* pcrel_offset */
164 HOWTO (R_M32C_HI8, /* type */
166 0, /* size (0 = byte, 1 = short, 2 = long) */
168 FALSE, /* pc_relative */
170 complain_overflow_dont, /* complain_on_overflow */
171 bfd_elf_generic_reloc, /* special_function */
172 "R_M32C_HI8", /* name */
173 FALSE, /* partial_inplace */
176 FALSE), /* pcrel_offset */
178 HOWTO (R_M32C_HI16, /* type */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
182 FALSE, /* pc_relative */
184 complain_overflow_dont, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_M32C_HI16", /* name */
187 FALSE, /* partial_inplace */
189 0xffff, /* dst_mask */
190 FALSE), /* pcrel_offset */
192 HOWTO (R_M32C_RL_JUMP, /* type */
194 0, /* size (0 = byte, 1 = short, 2 = long) */
196 FALSE, /* pc_relative */
198 complain_overflow_signed, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_M32C_RL_JUMP", /* name */
201 FALSE, /* partial_inplace */
204 FALSE), /* pcrel_offset */
206 HOWTO (R_M32C_RL_1ADDR, /* type */
208 0, /* size (0 = byte, 1 = short, 2 = long) */
210 FALSE, /* pc_relative */
212 complain_overflow_signed, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_M32C_RL_1ADDR", /* name */
215 FALSE, /* partial_inplace */
218 FALSE), /* pcrel_offset */
220 HOWTO (R_M32C_RL_2ADDR, /* type */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
224 FALSE, /* pc_relative */
226 complain_overflow_signed, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_M32C_RL_2ADDR", /* name */
229 FALSE, /* partial_inplace */
232 FALSE), /* pcrel_offset */
236 /* Map BFD reloc types to M32C ELF reloc types. */
238 struct m32c_reloc_map
240 bfd_reloc_code_real_type bfd_reloc_val;
241 unsigned int m32c_reloc_val;
244 static const struct m32c_reloc_map m32c_reloc_map [] =
246 { BFD_RELOC_NONE, R_M32C_NONE },
247 { BFD_RELOC_16, R_M32C_16 },
248 { BFD_RELOC_24, R_M32C_24 },
249 { BFD_RELOC_32, R_M32C_32 },
250 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
251 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
252 { BFD_RELOC_8, R_M32C_8 },
253 { BFD_RELOC_LO16, R_M32C_LO16 },
254 { BFD_RELOC_HI16, R_M32C_HI16 },
255 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
256 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
257 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
258 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
261 static reloc_howto_type *
262 m32c_reloc_type_lookup
263 (bfd * abfd ATTRIBUTE_UNUSED,
264 bfd_reloc_code_real_type code)
268 for (i = ARRAY_SIZE (m32c_reloc_map); --i;)
269 if (m32c_reloc_map [i].bfd_reloc_val == code)
270 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
275 static reloc_howto_type *
276 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
281 i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
283 if (m32c_elf_howto_table[i].name != NULL
284 && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
285 return &m32c_elf_howto_table[i];
290 /* Set the howto pointer for an M32C ELF reloc. */
293 m32c_info_to_howto_rela
294 (bfd * abfd ATTRIBUTE_UNUSED,
296 Elf_Internal_Rela * dst)
300 r_type = ELF32_R_TYPE (dst->r_info);
301 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
302 cache_ptr->howto = & m32c_elf_howto_table [r_type];
307 /* Relocate an M32C ELF section.
308 There is some attempt to make this function usable for many architectures,
309 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
310 if only to serve as a learning tool.
312 The RELOCATE_SECTION function is called by the new ELF backend linker
313 to handle the relocations for a section.
315 The relocs are always passed as Rela structures; if the section
316 actually uses Rel structures, the r_addend field will always be
319 This function is responsible for adjusting the section contents as
320 necessary, and (if using Rela relocs and generating a relocatable
321 output file) adjusting the reloc addend as necessary.
323 This function does not have to worry about setting the reloc
324 address or the reloc symbol index.
326 LOCAL_SYMS is a pointer to the swapped in local symbols.
328 LOCAL_SECTIONS is an array giving the section in the input file
329 corresponding to the st_shndx field of each local symbol.
331 The global hash table entry for the global symbols can be found
332 via elf_sym_hashes (input_bfd).
334 When generating relocatable output, this function must handle
335 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
336 going to be the section symbol corresponding to the output
337 section, which means that the addend must be adjusted
341 m32c_elf_relocate_section
342 (bfd * output_bfd ATTRIBUTE_UNUSED,
343 struct bfd_link_info * info,
345 asection * input_section,
347 Elf_Internal_Rela * relocs,
348 Elf_Internal_Sym * local_syms,
349 asection ** local_sections)
351 Elf_Internal_Shdr * symtab_hdr;
352 struct elf_link_hash_entry ** sym_hashes;
353 Elf_Internal_Rela * rel;
354 Elf_Internal_Rela * relend;
358 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
359 sym_hashes = elf_sym_hashes (input_bfd);
360 relend = relocs + input_section->reloc_count;
362 dynobj = elf_hash_table (info)->dynobj;
365 splt = bfd_get_section_by_name (dynobj, ".plt");
367 for (rel = relocs; rel < relend; rel ++)
369 reloc_howto_type * howto;
370 unsigned long r_symndx;
371 Elf_Internal_Sym * sym;
373 struct elf_link_hash_entry * h;
375 bfd_reloc_status_type r;
376 const char * name = NULL;
379 r_type = ELF32_R_TYPE (rel->r_info);
381 /* These are only used for relaxing; we don't actually relocate
382 anything with them, so skip them. */
383 if (r_type == R_M32C_RL_JUMP
384 || r_type == R_M32C_RL_1ADDR
385 || r_type == R_M32C_RL_2ADDR)
388 r_symndx = ELF32_R_SYM (rel->r_info);
390 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
396 if (r_symndx < symtab_hdr->sh_info)
398 sym = local_syms + r_symndx;
399 sec = local_sections [r_symndx];
400 relocation = (sec->output_section->vma
404 name = bfd_elf_string_from_elf_section
405 (input_bfd, symtab_hdr->sh_link, sym->st_name);
406 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
410 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
412 while (h->root.type == bfd_link_hash_indirect
413 || h->root.type == bfd_link_hash_warning)
414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
416 name = h->root.root.string;
418 if (h->root.type == bfd_link_hash_defined
419 || h->root.type == bfd_link_hash_defweak)
421 sec = h->root.u.def.section;
422 relocation = (h->root.u.def.value
423 + sec->output_section->vma
424 + sec->output_offset);
426 else if (h->root.type == bfd_link_hash_undefweak)
428 else if (!info->relocatable)
430 if (! ((*info->callbacks->undefined_symbol)
431 (info, h->root.root.string, input_bfd,
432 input_section, rel->r_offset, TRUE)))
437 if (sec != NULL && discarded_section (sec))
438 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
439 rel, 1, relend, howto, 0, contents);
441 if (info->relocatable)
443 /* This is a relocatable link. We don't have to change
444 anything, unless the reloc is against a section symbol,
445 in which case we have to adjust according to where the
446 section symbol winds up in the output section. */
447 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
448 rel->r_addend += sec->output_offset;
452 switch (ELF32_R_TYPE (rel->r_info))
459 plt_offset = &h->plt.offset;
461 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
463 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
464 relocation, *plt_offset);*/
465 if (relocation <= 0xffff)
467 /* If the symbol is in range for a 16-bit address, we should
468 have deallocated the plt entry in relax_section. */
469 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
473 /* If the symbol is out of range for a 16-bit address,
474 we must have allocated a plt entry. */
475 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
477 /* If this is the first time we've processed this symbol,
478 fill in the plt entry with the correct symbol address. */
479 if ((*plt_offset & 1) == 0)
483 x = 0x000000fc; /* jmpf */
484 x |= (relocation << 8) & 0xffffff00;
485 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
489 relocation = (splt->output_section->vma
490 + splt->output_offset
491 + (*plt_offset & -2));
494 char *newname = bfd_malloc (strlen(name)+5);
495 strcpy (newname, name);
496 strcat(newname, ".plt");
497 _bfd_generic_link_add_one_symbol (info,
500 BSF_FUNCTION | BSF_WEAK,
519 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
520 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
521 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
522 relocation, rel->r_addend);
526 printf (" %02x", contents[rel->r_offset+i]);
530 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
531 contents, rel->r_offset, relocation,
534 if (r != bfd_reloc_ok)
536 const char * msg = (const char *) NULL;
540 case bfd_reloc_overflow:
541 r = info->callbacks->reloc_overflow
542 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
543 input_bfd, input_section, rel->r_offset);
546 case bfd_reloc_undefined:
547 r = info->callbacks->undefined_symbol
548 (info, name, input_bfd, input_section, rel->r_offset,
552 case bfd_reloc_outofrange:
553 msg = _("internal error: out of range error");
556 case bfd_reloc_notsupported:
557 msg = _("internal error: unsupported relocation error");
560 case bfd_reloc_dangerous:
561 msg = _("internal error: dangerous relocation");
565 msg = _("internal error: unknown error");
570 r = info->callbacks->warning
571 (info, msg, name, input_bfd, input_section, rel->r_offset);
581 /* We support 16-bit pointers to code above 64k by generating a thunk
582 below 64k containing a JMP instruction to the final address. */
585 m32c_elf_check_relocs
587 struct bfd_link_info * info,
589 const Elf_Internal_Rela * relocs)
591 Elf_Internal_Shdr * symtab_hdr;
592 struct elf_link_hash_entry ** sym_hashes;
593 const Elf_Internal_Rela * rel;
594 const Elf_Internal_Rela * rel_end;
595 bfd_vma *local_plt_offsets;
599 if (info->relocatable)
602 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
603 sym_hashes = elf_sym_hashes (abfd);
604 local_plt_offsets = elf_local_got_offsets (abfd);
606 dynobj = elf_hash_table(info)->dynobj;
608 rel_end = relocs + sec->reloc_count;
609 for (rel = relocs; rel < rel_end; rel++)
611 struct elf_link_hash_entry *h;
612 unsigned long r_symndx;
615 r_symndx = ELF32_R_SYM (rel->r_info);
616 if (r_symndx < symtab_hdr->sh_info)
620 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
621 while (h->root.type == bfd_link_hash_indirect
622 || h->root.type == bfd_link_hash_warning)
623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
626 switch (ELF32_R_TYPE (rel->r_info))
628 /* This relocation describes a 16-bit pointer to a function.
629 We may need to allocate a thunk in low memory; reserve memory
633 elf_hash_table (info)->dynobj = dynobj = abfd;
636 splt = bfd_get_section_by_name (dynobj, ".plt");
639 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
640 | SEC_IN_MEMORY | SEC_LINKER_CREATED
641 | SEC_READONLY | SEC_CODE);
642 splt = bfd_make_section_with_flags (dynobj, ".plt", flags);
644 || ! bfd_set_section_alignment (dynobj, splt, 1))
650 offset = &h->plt.offset;
653 if (local_plt_offsets == NULL)
658 size = symtab_hdr->sh_info * sizeof (bfd_vma);
659 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
660 if (local_plt_offsets == NULL)
662 elf_local_got_offsets (abfd) = local_plt_offsets;
664 for (i = 0; i < symtab_hdr->sh_info; i++)
665 local_plt_offsets[i] = (bfd_vma) -1;
667 offset = &local_plt_offsets[r_symndx];
670 if (*offset == (bfd_vma) -1)
672 *offset = splt->size;
682 /* This must exist if dynobj is ever set. */
685 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
686 struct bfd_link_info *info)
691 /* As an extra sanity check, verify that all plt entries have
694 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
695 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
697 bfd_byte *contents = splt->contents;
698 unsigned int i, size = splt->size;
699 for (i = 0; i < size; i += 4)
701 unsigned int x = bfd_get_32 (dynobj, contents + i);
710 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
711 struct bfd_link_info *info)
716 if (info->relocatable)
719 dynobj = elf_hash_table (info)->dynobj;
723 splt = bfd_get_section_by_name (dynobj, ".plt");
724 BFD_ASSERT (splt != NULL);
726 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
727 if (splt->contents == NULL)
733 /* Function to set the ELF flag bits. */
736 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
738 elf_elfheader (abfd)->e_flags = flags;
739 elf_flags_init (abfd) = TRUE;
743 /* Merge backend specific data from an object file to the output
744 object file when linking. */
747 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
749 flagword old_flags, old_partial;
750 flagword new_flags, new_partial;
751 bfd_boolean error = FALSE;
755 new_opt[0] = old_opt[0] = '\0';
756 new_flags = elf_elfheader (ibfd)->e_flags;
757 old_flags = elf_elfheader (obfd)->e_flags;
760 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
761 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
762 bfd_get_filename (ibfd));
765 if (!elf_flags_init (obfd))
767 /* First call, no flags set. */
768 elf_flags_init (obfd) = TRUE;
769 elf_elfheader (obfd)->e_flags = new_flags;
772 else if (new_flags == old_flags)
773 /* Compatible flags are ok. */
776 else /* Possibly incompatible flags. */
778 /* Warn if different cpu is used (allow a specific cpu to override
780 new_partial = (new_flags & EF_M32C_CPU_MASK);
781 old_partial = (old_flags & EF_M32C_CPU_MASK);
782 if (new_partial == old_partial)
789 default: strcat (new_opt, " -m16c"); break;
790 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
791 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
796 default: strcat (old_opt, " -m16c"); break;
797 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
798 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
802 /* Print out any mismatches from above. */
806 (*_bfd_error_handler)
807 (_("%s: compiled with %s and linked with modules compiled with %s"),
808 bfd_get_filename (ibfd), new_opt, old_opt);
811 new_flags &= ~ EF_M32C_ALL_FLAGS;
812 old_flags &= ~ EF_M32C_ALL_FLAGS;
814 /* Warn about any other mismatches. */
815 if (new_flags != old_flags)
818 (*_bfd_error_handler)
819 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
820 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
825 bfd_set_error (bfd_error_bad_value);
832 m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
834 FILE *file = (FILE *) ptr;
837 BFD_ASSERT (abfd != NULL && ptr != NULL);
839 /* Print normal ELF private data. */
840 _bfd_elf_print_private_bfd_data (abfd, ptr);
842 flags = elf_elfheader (abfd)->e_flags;
843 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
845 switch (flags & EF_M32C_CPU_MASK)
848 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
849 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
856 /* Return the MACH for an e_flags value. */
859 elf32_m32c_machine (bfd *abfd)
861 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
863 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
864 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
867 return bfd_mach_m16c;
871 m32c_elf_object_p (bfd *abfd)
873 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
874 elf32_m32c_machine (abfd));
881 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
884 Elf_Internal_Sym *isymbuf;
885 Elf_Internal_Sym *isymend;
886 Elf_Internal_Sym *isym;
887 Elf_Internal_Shdr *symtab_hdr;
888 bfd_boolean free_internal = 0, free_external = 0;
890 char * st_info_stb_str;
896 internal_syms = bfd_malloc (1000);
901 external_syms = bfd_malloc (1000);
905 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
906 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
908 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
909 symtab_hdr->sh_info, 0,
910 internal_syms, external_syms, NULL);
912 isymbuf = internal_syms;
913 isymend = isymbuf + locsymcount;
915 for (isym = isymbuf ; isym < isymend ; isym++)
917 switch (ELF_ST_TYPE (isym->st_info))
919 case STT_FUNC: st_info_str = "STT_FUNC";
920 case STT_SECTION: st_info_str = "STT_SECTION";
921 case STT_FILE: st_info_str = "STT_FILE";
922 case STT_OBJECT: st_info_str = "STT_OBJECT";
923 case STT_TLS: st_info_str = "STT_TLS";
924 default: st_info_str = "";
926 switch (ELF_ST_BIND (isym->st_info))
928 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
929 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
930 default: st_info_stb_str = "";
932 switch (ELF_ST_VISIBILITY (isym->st_other))
934 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
935 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
936 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
937 default: st_other_str = "";
939 switch (isym->st_shndx)
941 case SHN_ABS: st_shndx_str = "SHN_ABS";
942 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
943 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
944 default: st_shndx_str = "";
947 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
948 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
950 (unsigned long) isym->st_value,
951 (unsigned long) isym->st_size,
953 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
955 isym->st_info, st_info_str, st_info_stb_str,
956 isym->st_other, st_other_str,
957 isym->st_shndx, st_shndx_str);
960 free (internal_syms);
962 free (external_syms);
966 m32c_get_reloc (long reloc)
968 if (0 <= reloc && reloc < R_M32C_max)
969 return m32c_elf_howto_table[reloc].name;
975 /* Handle relaxing. */
977 /* A subroutine of m32c_elf_relax_section. If the global symbol H
978 is within the low 64k, remove any entry for it in the plt. */
980 struct relax_plt_data
987 m32c_relax_plt_check (struct elf_link_hash_entry *h,
990 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
992 if (h->plt.offset != (bfd_vma) -1)
996 if (h->root.type == bfd_link_hash_undefined
997 || h->root.type == bfd_link_hash_undefweak)
1000 address = (h->root.u.def.section->output_section->vma
1001 + h->root.u.def.section->output_offset
1002 + h->root.u.def.value);
1004 if (address <= 0xffff)
1007 data->splt->size -= 4;
1008 *data->again = TRUE;
1015 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1016 previously had a plt entry, give it a new entry offset. */
1019 m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1022 bfd_vma *entry = (bfd_vma *) xdata;
1024 if (h->plt.offset != (bfd_vma) -1)
1026 h->plt.offset = *entry;
1034 m32c_elf_relax_plt_section (bfd *dynobj,
1036 struct bfd_link_info *info,
1039 struct relax_plt_data relax_plt_data;
1042 /* Assume nothing changes. */
1045 if (info->relocatable)
1048 /* We only relax the .plt section at the moment. */
1049 if (dynobj != elf_hash_table (info)->dynobj
1050 || strcmp (splt->name, ".plt") != 0)
1053 /* Quick check for an empty plt. */
1054 if (splt->size == 0)
1057 /* Map across all global symbols; see which ones happen to
1058 fall in the low 64k. */
1059 relax_plt_data.splt = splt;
1060 relax_plt_data.again = again;
1061 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1064 /* Likewise for local symbols, though that's somewhat less convenient
1065 as we have to walk the list of input bfds and swap in symbol data. */
1066 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1068 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1069 Elf_Internal_Shdr *symtab_hdr;
1070 Elf_Internal_Sym *isymbuf = NULL;
1073 if (! local_plt_offsets)
1076 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1077 if (symtab_hdr->sh_info != 0)
1079 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1080 if (isymbuf == NULL)
1081 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1082 symtab_hdr->sh_info, 0,
1084 if (isymbuf == NULL)
1088 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1090 Elf_Internal_Sym *isym;
1094 if (local_plt_offsets[idx] == (bfd_vma) -1)
1097 isym = &isymbuf[idx];
1098 if (isym->st_shndx == SHN_UNDEF)
1100 else if (isym->st_shndx == SHN_ABS)
1101 tsec = bfd_abs_section_ptr;
1102 else if (isym->st_shndx == SHN_COMMON)
1103 tsec = bfd_com_section_ptr;
1105 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1107 address = (tsec->output_section->vma
1108 + tsec->output_offset
1110 if (address <= 0xffff)
1112 local_plt_offsets[idx] = -1;
1119 && symtab_hdr->contents != (unsigned char *) isymbuf)
1121 if (! info->keep_memory)
1125 /* Cache the symbols for elf_link_input_bfd. */
1126 symtab_hdr->contents = (unsigned char *) isymbuf;
1131 /* If we changed anything, walk the symbols again to reallocate
1132 .plt entry addresses. */
1133 if (*again && splt->size > 0)
1137 elf_link_hash_traverse (elf_hash_table (info),
1138 m32c_relax_plt_realloc, &entry);
1140 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1142 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1143 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1146 if (! local_plt_offsets)
1149 for (idx = 0; idx < nlocals; ++idx)
1150 if (local_plt_offsets[idx] != (bfd_vma) -1)
1152 local_plt_offsets[idx] = entry;
1162 compare_reloc (const void *e1, const void *e2)
1164 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1165 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1167 if (i1->r_offset == i2->r_offset)
1170 return i1->r_offset < i2->r_offset ? -1 : 1;
1173 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1175 m32c_offset_for_reloc (bfd *abfd,
1176 Elf_Internal_Rela *rel,
1177 Elf_Internal_Shdr *symtab_hdr,
1178 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1179 Elf_Internal_Sym *intsyms)
1183 /* Get the value of the symbol referred to by the reloc. */
1184 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1186 /* A local symbol. */
1187 Elf_Internal_Sym *isym;
1190 isym = intsyms + ELF32_R_SYM (rel->r_info);
1191 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1192 symval = isym->st_value;
1194 symval += ssec->output_section->vma
1195 + ssec->output_offset;
1200 struct elf_link_hash_entry *h;
1202 /* An external symbol. */
1203 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1204 h = elf_sym_hashes (abfd)[indx];
1205 BFD_ASSERT (h != NULL);
1207 if (h->root.type != bfd_link_hash_defined
1208 && h->root.type != bfd_link_hash_defweak)
1209 /* This appears to be a reference to an undefined
1210 symbol. Just ignore it--it will be caught by the
1211 regular reloc processing. */
1214 symval = (h->root.u.def.value
1215 + h->root.u.def.section->output_section->vma
1216 + h->root.u.def.section->output_offset);
1221 static int bytes_saved = 0;
1223 static int bytes_to_reloc[] = {
1231 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1233 /* Mask for the number of relocs associated with this insn. */
1234 #define RLA_RELOCS 0x0000000f
1235 /* Number of bytes gas emitted (before gas's relaxing) */
1236 #define RLA_NBYTES 0x00000ff0
1238 /* If the displacement is within the given range and the new encoding
1239 differs from the old encoding (the index), then the insn can be
1240 relaxed to the new encoding. */
1243 unsigned int max_disp;
1244 unsigned char new_encoding;
1247 static EncodingTable m16c_addr_encodings[] = {
1248 { 0, 0, 0 }, /* R0 */
1249 { 0, 0, 1 }, /* R1 */
1250 { 0, 0, 2 }, /* R2 */
1251 { 0, 0, 3 }, /* R3 */
1252 { 0, 0, 4 }, /* A0 */
1253 { 0, 0, 5 }, /* A1 */
1254 { 0, 0, 6 }, /* [A0] */
1255 { 0, 0, 7 }, /* [A1] */
1256 { 1, 0, 6 }, /* udsp:8[A0] */
1257 { 1, 0, 7 }, /* udsp:8[A1] */
1258 { 1, 0, 10 }, /* udsp:8[SB] */
1259 { 1, 0, 11 }, /* sdsp:8[FB] */
1260 { 2, 255, 8 }, /* udsp:16[A0] */
1261 { 2, 255, 9 }, /* udsp:16[A1] */
1262 { 2, 255, 10 }, /* udsp:16[SB] */
1263 { 2, 0, 15 }, /* abs:16 */
1266 static EncodingTable m16c_jmpaddr_encodings[] = {
1267 { 0, 0, 0 }, /* R0 */
1268 { 0, 0, 1 }, /* R1 */
1269 { 0, 0, 2 }, /* R2 */
1270 { 0, 0, 3 }, /* R3 */
1271 { 0, 0, 4 }, /* A0 */
1272 { 0, 0, 5 }, /* A1 */
1273 { 0, 0, 6 }, /* [A0] */
1274 { 0, 0, 7 }, /* [A1] */
1275 { 1, 0, 6 }, /* udsp:8[A0] */
1276 { 1, 0, 7 }, /* udsp:8[A1] */
1277 { 1, 0, 10 }, /* udsp:8[SB] */
1278 { 1, 0, 11 }, /* sdsp:8[FB] */
1279 { 3, 255, 8 }, /* udsp:20[A0] */
1280 { 3, 255, 9 }, /* udsp:20[A1] */
1281 { 2, 255, 10 }, /* udsp:16[SB] */
1282 { 2, 0, 15 }, /* abs:16 */
1285 static EncodingTable m32c_addr_encodings[] = {
1286 { 0, 0, 0 }, /* [A0] */
1287 { 0, 0, 1 }, /* [A1] */
1288 { 0, 0, 2 }, /* A0 */
1289 { 0, 0, 3 }, /* A1 */
1290 { 1, 0, 0 }, /* udsp:8[A0] */
1291 { 1, 0, 1 }, /* udsp:8[A1] */
1292 { 1, 0, 6 }, /* udsp:8[SB] */
1293 { 1, 0, 7 }, /* sdsp:8[FB] */
1294 { 2, 255, 4 }, /* udsp:16[A0] */
1295 { 2, 255, 5 }, /* udsp:16[A1] */
1296 { 2, 255, 6 }, /* udsp:16[SB] */
1297 { 2, 127, 7 }, /* sdsp:16[FB] */
1298 { 3, 65535, 8 }, /* udsp:24[A0] */
1299 { 3, 65535, 9 }, /* udsp:24[A1] */
1300 { 3, 65535, 15 }, /* abs24 */
1301 { 2, 0, 15 }, /* abs16 */
1302 { 0, 0, 16 }, /* R2 */
1303 { 0, 0, 17 }, /* R3 */
1304 { 0, 0, 18 }, /* R0 */
1305 { 0, 0, 19 }, /* R1 */
1321 m32c_elf_relax_section
1324 struct bfd_link_info * link_info,
1325 bfd_boolean * again)
1327 Elf_Internal_Shdr *symtab_hdr;
1328 Elf_Internal_Shdr *shndx_hdr;
1329 Elf_Internal_Rela *internal_relocs;
1330 Elf_Internal_Rela *free_relocs = NULL;
1331 Elf_Internal_Rela *irel, *irelend, *srel;
1332 bfd_byte * contents = NULL;
1333 bfd_byte * free_contents = NULL;
1334 Elf_Internal_Sym *intsyms = NULL;
1335 Elf_Internal_Sym *free_intsyms = NULL;
1336 Elf_External_Sym_Shndx *shndx_buf = NULL;
1339 if (abfd == elf_hash_table (link_info)->dynobj
1340 && strcmp (sec->name, ".plt") == 0)
1341 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1343 /* Assume nothing changes. */
1346 machine = elf32_m32c_machine (abfd);
1348 /* We don't have to do anything for a relocatable link, if
1349 this section does not have relocs, or if this is not a
1351 if (link_info->relocatable
1352 || (sec->flags & SEC_RELOC) == 0
1353 || sec->reloc_count == 0
1354 || (sec->flags & SEC_CODE) == 0)
1357 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1358 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1360 /* Get the section contents. */
1361 if (elf_section_data (sec)->this_hdr.contents != NULL)
1362 contents = elf_section_data (sec)->this_hdr.contents;
1363 /* Go get them off disk. */
1364 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1367 /* Read this BFD's symbols. */
1368 /* Get cached copy if it exists. */
1369 if (symtab_hdr->contents != NULL)
1371 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1375 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1376 symtab_hdr->contents = (bfd_byte *) intsyms;
1379 if (shndx_hdr->sh_size != 0)
1383 amt = symtab_hdr->sh_info;
1384 amt *= sizeof (Elf_External_Sym_Shndx);
1385 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1386 if (shndx_buf == NULL)
1388 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1389 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1391 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1394 /* Get a copy of the native relocations. */
1395 internal_relocs = (_bfd_elf_link_read_relocs
1396 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1397 link_info->keep_memory));
1398 if (internal_relocs == NULL)
1400 if (! link_info->keep_memory)
1401 free_relocs = internal_relocs;
1403 /* The RL_ relocs must be just before the operand relocs they go
1404 with, so we must sort them to guarantee this. */
1405 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1408 /* Walk through them looking for relaxing opportunities. */
1409 irelend = internal_relocs + sec->reloc_count;
1411 for (irel = internal_relocs; irel < irelend; irel++)
1414 unsigned char *insn, *gap, *einsn;
1416 bfd_signed_vma pcrel;
1422 EncodingTable *enctbl;
1425 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1426 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1427 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1432 /* There will always be room for the relaxed insn, since it is smaller
1433 than the one it would replace. */
1434 BFD_ASSERT (irel->r_offset < sec->size);
1436 insn = contents + irel->r_offset;
1437 relax_relocs = irel->r_addend % 16;
1439 /* Ok, we only have three relocs we care about, and they're all
1440 fake. The lower four bits of the addend is always the number
1441 of following relocs (hence the qsort above) that are assigned
1442 to this opcode. The next 8 bits of the addend indicates the
1443 number of bytes in the insn. We use the rest of them
1444 ourselves as flags for the more expensive operations (defines
1445 above). The three relocs are:
1447 RL_JUMP: This marks all direct jump insns. We check the
1448 displacement and replace them with shorter jumps if
1449 they're in range. We also use this to find JMP.S
1450 insns and manually shorten them when we delete bytes.
1451 We have to decode these insns to figure out what to
1454 RL_1ADDR: This is a :G or :Q insn, which has a single
1455 "standard" operand. We have to extract the type
1456 field, see if it's a wide displacement, then figure
1457 out if we can replace it with a narrow displacement.
1458 We don't have to decode these insns.
1460 RL_2ADDR: Similarly, but two "standard" operands. Note that
1461 r_addend may still be 1, as standard operands don't
1462 always have displacements. Gas shouldn't give us one
1463 with zero operands, but since we don't know which one
1464 has the displacement, we check them both anyway.
1466 These all point to the beginning of the insn itself, not the
1469 Note that we only relax one step at a time, relying on the
1470 linker to call us repeatedly. Thus, there is no code for
1471 JMP.A->JMP.B although that will happen in two steps.
1472 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1475 /* Get the value of the symbol referred to by the reloc. Just
1476 in case this is the last reloc in the list, use the RL's
1477 addend to choose between this reloc (no addend) or the next
1478 (yes addend, which means at least one following reloc). */
1479 srel = irel + (relax_relocs ? 1 : 0);
1480 symval = OFFSET_FOR_RELOC (srel);
1482 /* Setting gap_size nonzero is the flag which means "something
1486 new_type = ELF32_R_TYPE(srel->r_info);
1488 pc = sec->output_section->vma + sec->output_offset
1490 pcrel = symval - pc + srel->r_addend;
1492 if (machine == bfd_mach_m16c)
1496 switch (ELF32_R_TYPE(irel->r_info))
1499 case R_M32C_RL_JUMP:
1502 case 0xfe: /* jmp.b */
1503 if (pcrel >= 2 && pcrel <= 9)
1505 /* Relax JMP.B -> JMP.S. We need to get rid of
1506 the following reloc though. */
1507 insn[0] = 0x60 | (pcrel - 2);
1508 new_type = R_M32C_NONE;
1509 irel->r_addend = 0x10;
1515 case 0xf4: /* jmp.w */
1516 /* 128 is allowed because it will be one byte closer
1517 after relaxing. Likewise for all other pc-rel
1519 if (pcrel <= 128 && pcrel >= -128)
1521 /* Relax JMP.W -> JMP.B */
1524 new_type = R_M32C_8_PCREL;
1530 case 0xfc: /* jmp.a */
1531 if (pcrel <= 32768 && pcrel >= -32768)
1533 /* Relax JMP.A -> JMP.W */
1537 new_type = R_M32C_16_PCREL;
1543 case 0xfd: /* jsr.a */
1544 if (pcrel <= 32768 && pcrel >= -32768)
1546 /* Relax JSR.A -> JSR.W */
1550 new_type = R_M32C_16_PCREL;
1558 case R_M32C_RL_2ADDR:
1559 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1561 enctbl = m16c_addr_encodings;
1563 enc = (insn[1] >> 4) & 0x0f;
1566 if (srel->r_offset == irel->r_offset + posn
1567 && e->new_encoding != enc
1568 && symval <= e->max_disp)
1571 insn[1] |= e->new_encoding << 4;
1572 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1573 gap = insn + posn + enctbl[e->new_encoding].bytes;
1574 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1577 if (relax_relocs == 2)
1583 case R_M32C_RL_1ADDR:
1584 /* xxxx xxxx xxxx dest [disp] */
1586 enctbl = m16c_addr_encodings;
1589 /* Check the opcode for jumps. We know it's safe to
1590 do this because all 2ADDR insns are at least two
1592 enc = insn[0] * 256 + insn[1];
1599 enctbl = m16c_jmpaddr_encodings;
1603 /* srel, posn, and enc must be set here. */
1605 symval = OFFSET_FOR_RELOC (srel);
1606 enc = insn[1] & 0x0f;
1609 if (srel->r_offset == irel->r_offset + posn
1610 && e->new_encoding != enc
1611 && symval <= e->max_disp)
1614 insn[1] |= e->new_encoding;
1615 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1616 gap = insn + posn + enctbl[e->new_encoding].bytes;
1617 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1623 } /* Ends switch (reloc type) for m16c. */
1625 else /* machine == bfd_mach_m32c */
1629 switch (ELF32_R_TYPE(irel->r_info))
1632 case R_M32C_RL_JUMP:
1635 case 0xbb: /* jmp.b */
1636 if (pcrel >= 2 && pcrel <= 9)
1639 /* Relax JMP.B -> JMP.S. We need to get rid of
1640 the following reloc though. */
1641 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1642 new_type = R_M32C_NONE;
1643 irel->r_addend = 0x10;
1649 case 0xce: /* jmp.w */
1650 if (pcrel <= 128 && pcrel >= -128)
1652 /* Relax JMP.W -> JMP.B */
1655 new_type = R_M32C_8_PCREL;
1661 case 0xcc: /* jmp.a */
1662 if (pcrel <= 32768 && pcrel >= -32768)
1664 /* Relax JMP.A -> JMP.W */
1668 new_type = R_M32C_16_PCREL;
1674 case 0xcd: /* jsr.a */
1675 if (pcrel <= 32768 && pcrel >= -32768)
1677 /* Relax JSR.A -> JSR.W */
1681 new_type = R_M32C_16_PCREL;
1689 case R_M32C_RL_2ADDR:
1690 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1696 /* prefix; remove it as far as the RL reloc is concerned. */
1701 enctbl = m32c_addr_encodings;
1702 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1705 if (srel->r_offset == irel->r_offset + posn
1706 && e->new_encoding != enc
1707 && symval <= e->max_disp)
1710 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1712 einsn[1] |= (e->new_encoding & 0x03) << 4;
1713 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1714 gap = insn + posn + enctbl[e->new_encoding].bytes;
1715 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1718 if (relax_relocs == 2)
1724 case R_M32C_RL_1ADDR:
1725 /* xxxx DDDx DDxx xxxx [disp] */
1731 /* prefix; remove it as far as the RL reloc is concerned. */
1736 enctbl = m32c_addr_encodings;
1739 /* srel, posn, and enc must be set here. */
1741 symval = OFFSET_FOR_RELOC (srel);
1742 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1745 if (srel->r_offset == irel->r_offset + posn
1746 && e->new_encoding != enc
1747 && symval <= e->max_disp)
1750 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1752 einsn[1] |= (e->new_encoding & 0x03) << 6;
1753 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1754 gap = insn + posn + enctbl[e->new_encoding].bytes;
1755 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1761 } /* Ends switch (reloc type) for m32c. */
1769 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1771 /* Note that we've changed the relocs, section contents, etc. */
1772 elf_section_data (sec)->relocs = internal_relocs;
1775 elf_section_data (sec)->this_hdr.contents = contents;
1776 free_contents = NULL;
1778 symtab_hdr->contents = (bfd_byte *) intsyms;
1779 free_intsyms = NULL;
1781 bytes_saved += gap_size;
1783 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1786 } /* next relocation */
1788 if (free_relocs != NULL)
1794 if (free_contents != NULL)
1796 if (! link_info->keep_memory)
1797 free (free_contents);
1798 /* Cache the section contents for elf_link_input_bfd. */
1800 elf_section_data (sec)->this_hdr.contents = contents;
1802 free_contents = NULL;
1805 if (shndx_buf != NULL)
1807 shndx_hdr->contents = NULL;
1811 if (free_intsyms != NULL)
1813 if (! link_info->keep_memory)
1814 free (free_intsyms);
1815 /* Cache the symbols for elf_link_input_bfd. */
1818 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1821 free_intsyms = NULL;
1827 if (free_relocs != NULL)
1829 if (free_contents != NULL)
1830 free (free_contents);
1831 if (shndx_buf != NULL)
1833 shndx_hdr->contents = NULL;
1836 if (free_intsyms != NULL)
1837 free (free_intsyms);
1841 /* Delete some bytes from a section while relaxing. */
1844 m32c_elf_relax_delete_bytes
1850 Elf_Internal_Shdr *symtab_hdr;
1851 Elf_Internal_Shdr *shndx_hdr;
1854 Elf_Internal_Rela *irel;
1855 Elf_Internal_Rela *irelend;
1857 Elf_Internal_Sym *isym;
1858 Elf_Internal_Sym *isymend;
1859 Elf_Internal_Sym *intsyms;
1860 Elf_External_Sym_Shndx *shndx_buf;
1861 Elf_External_Sym_Shndx *shndx;
1862 struct elf_link_hash_entry ** sym_hashes;
1863 struct elf_link_hash_entry ** end_hashes;
1864 unsigned int symcount;
1866 contents = elf_section_data (sec)->this_hdr.contents;
1870 irel = elf_section_data (sec)->relocs;
1871 irelend = irel + sec->reloc_count;
1873 /* Actually delete the bytes. */
1874 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1877 /* Adjust all the relocs. */
1878 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1880 /* Get the new reloc address. */
1881 if (irel->r_offset > addr && irel->r_offset < toaddr)
1882 irel->r_offset -= count;
1884 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1885 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1886 && irel->r_offset + 1 < addr
1887 && irel->r_offset + 7 > addr)
1890 unsigned char *insn = &contents[irel->r_offset];
1892 /* This is a JMP.S, which we have to manually update. */
1893 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1895 if ((*insn & 0xf8) != 0x60)
1901 if ((*insn & 0xce) != 0x4a)
1903 disp = ((disp & 0x30) >> 3) | (disp & 1);
1905 if (irel->r_offset + disp + 2 >= addr+count)
1908 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1910 *insn = (*insn & 0xf8) | disp;
1914 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1920 /* Adjust the local symbols defined in this section. */
1921 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1922 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1924 isymend = isym + symtab_hdr->sh_info;
1926 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1927 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1928 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1931 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1933 /* If the symbol is in the range of memory we just moved, we
1934 have to adjust its value. */
1935 if ((int) isym->st_shndx == sec_shndx
1936 && isym->st_value > addr
1937 && isym->st_value < toaddr)
1939 isym->st_value -= count;
1941 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1942 *end* is in the moved bytes but it's *start* isn't), then we
1943 must adjust its size. */
1944 if ((int) isym->st_shndx == sec_shndx
1945 && isym->st_value < addr
1946 && isym->st_value + isym->st_size > addr
1947 && isym->st_value + isym->st_size < toaddr)
1949 isym->st_size -= count;
1953 /* Now adjust the global symbols defined in this section. */
1954 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1955 - symtab_hdr->sh_info);
1956 sym_hashes = elf_sym_hashes (abfd);
1957 // sym_hashes += symtab_hdr->sh_info;
1958 end_hashes = sym_hashes + symcount;
1960 for (; sym_hashes < end_hashes; sym_hashes ++)
1962 struct elf_link_hash_entry * sym_hash = * sym_hashes;
1965 (sym_hash->root.type == bfd_link_hash_defined
1966 || sym_hash->root.type == bfd_link_hash_defweak)
1967 && sym_hash->root.u.def.section == sec)
1969 if (sym_hash->root.u.def.value > addr
1970 && sym_hash->root.u.def.value < toaddr)
1972 sym_hash->root.u.def.value -= count;
1974 if (sym_hash->root.u.def.value < addr
1975 && sym_hash->root.u.def.value + sym_hash->size > addr
1976 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1978 sym_hash->size -= count;
1986 /* This is for versions of gcc prior to 4.3. */
1988 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
1990 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
1997 #define ELF_ARCH bfd_arch_m32c
1998 #define ELF_MACHINE_CODE EM_M32C
1999 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2000 #define ELF_MAXPAGESIZE 0x100
2003 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
2004 #define TARGET_BIG_NAME "elf32-m32c"
2006 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
2007 #define TARGET_LITTLE_NAME "elf32-m32c"
2010 #define elf_info_to_howto_rel NULL
2011 #define elf_info_to_howto m32c_info_to_howto_rela
2012 #define elf_backend_object_p m32c_elf_object_p
2013 #define elf_backend_relocate_section m32c_elf_relocate_section
2014 #define elf_backend_check_relocs m32c_elf_check_relocs
2015 #define elf_backend_object_p m32c_elf_object_p
2016 #define elf_symbol_leading_char ('_')
2017 #define elf_backend_always_size_sections \
2018 m32c_elf_always_size_sections
2019 #define elf_backend_finish_dynamic_sections \
2020 m32c_elf_finish_dynamic_sections
2022 #define elf_backend_can_gc_sections 1
2023 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2025 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2026 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2027 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2028 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2029 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2030 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2032 #include "elf32-target.h"